Authored by wangshusheng

砍价

... ... @@ -7,7 +7,7 @@ import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface CutDownPriceUserHelpMapper {
List<CutDownPriceProductHelpUser> selectHelpInfo(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn);
List<CutDownPriceProductHelpUser> selectHelpInfo(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn, @Param("cutRecordId") Integer cutRecordId);
void addHelpUserInfo(CutDownPriceProductHelpUser helpUser);
... ...
... ... @@ -18,10 +18,10 @@ public interface CutDownPriceUserRecordMapper {
List<CutDownPriceUserRecord> selectCutDownPriceUserRecordList(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn);
int updateCutDownHelpCount(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn);
int updateCutDownHelpCount(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn, @Param("cutRecordId") Integer cutRecordId);
int updateDecreaseUseCount(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn);
int updateDecreaseUseCount(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn, @Param("cutRecordId") Integer cutRecordId);
int updateIncreaseUseCount(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn);
int updateIncreaseUseCount(@Param("userId") Integer userId, @Param("activityId") Integer activityId, @Param("productSkn") Integer productSkn, @Param("cutRecordId") Integer cutRecordId);
}
... ...
... ... @@ -9,6 +9,7 @@ public class CutDownPriceProductHelpUser {
private Integer createTime;
private Integer activityId;
private Integer productSkn;
private Integer cutRecordId;
private BigDecimal cutPrice;
public Integer getId() {
... ... @@ -66,4 +67,12 @@ public class CutDownPriceProductHelpUser {
public void setCutPrice(BigDecimal cutPrice) {
this.cutPrice = cutPrice;
}
public Integer getCutRecordId() {
return cutRecordId;
}
public void setCutRecordId(Integer cutRecordId) {
this.cutRecordId = cutRecordId;
}
}
... ...
... ... @@ -7,11 +7,12 @@
<result column="help_user_id" property="helpUserId" jdbcType="INTEGER" />
<result column="activity_id" property="activityId" jdbcType="INTEGER" />
<result column="product_skn" property="productSkn" jdbcType="INTEGER" />
<result column="cut_record_id" property="cutRecordId" jdbcType="INTEGER" />
<result column="cut_price" property="cutPrice" jdbcType="DECIMAL" />
<result column="create_time" property="createTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, user_id, help_user_id,activity_id,product_skn,cut_price,create_time
id, user_id, help_user_id,activity_id,product_skn,cut_record_id,cut_price,create_time
</sql>
<select id="selectHelpInfo" resultMap="BaseResultMap">
... ... @@ -21,12 +22,13 @@
where user_id = #{userId,jdbcType=INTEGER}
and activity_id = #{activityId,jdbcType=INTEGER}
and product_skn = #{productSkn,jdbcType=INTEGER}
and cut_record_id = #{cutRecordId,jdbcType=INTEGER}
</select>
<insert id="addHelpUserInfo" parameterType="com.yoho.activity.dal.model.CutDownPriceProductHelpUser">
insert into cutdown_price_help_user (user_id, help_user_id,activity_id,product_skn,cut_price,create_time)
insert into cutdown_price_help_user (user_id, help_user_id,activity_id,product_skn,cut_record_id,cut_price,create_time)
values (#{userId,jdbcType=INTEGER}, #{helpUserId,jdbcType=INTEGER}, #{activityId,jdbcType=INTEGER}, #{productSkn,jdbcType=INTEGER},
#{cutPrice,jdbcType=DECIMAL}, #{createTime,jdbcType=INTEGER}
#{cutRecordId,jdbcType=INTEGER}, #{cutPrice,jdbcType=DECIMAL}, #{createTime,jdbcType=INTEGER}
)
</insert>
... ...
... ... @@ -16,7 +16,7 @@
id, user_id, activity_id, product_skn, help_count, use_count, create_time, update_time, cut_time
</sql>
<insert id="addCutDownUserRecord" parameterType="com.yoho.activity.dal.model.CutDownPriceUserRecord">
<insert id="addCutDownUserRecord" parameterType="com.yoho.activity.dal.model.CutDownPriceUserRecord" useGeneratedKeys="true" keyProperty="id">
insert into cutdown_price_user_record (user_id, activity_id, product_skn, create_time)
values (#{userId,jdbcType=INTEGER}, #{activityId,jdbcType=INTEGER}, #{productSkn,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}
)
... ... @@ -66,6 +66,7 @@
where user_id = #{userId,jdbcType=INTEGER}
and activity_id = #{activityId,jdbcType=INTEGER}
and product_skn = #{productSkn,jdbcType=INTEGER}
and id = #{cutRecordId,jdbcType=INTEGER}
</update>
<update id="updateDecreaseUseCount" >
... ... @@ -74,6 +75,7 @@
where user_id = #{userId,jdbcType=INTEGER}
and activity_id = #{activityId,jdbcType=INTEGER}
and product_skn = #{productSkn,jdbcType=INTEGER}
and id = #{cutRecordId,jdbcType=INTEGER}
</update>
<update id="updateIncreaseUseCount" >
... ... @@ -82,5 +84,6 @@
where user_id = #{userId,jdbcType=INTEGER}
and activity_id = #{activityId,jdbcType=INTEGER}
and product_skn = #{productSkn,jdbcType=INTEGER}
and id = #{cutRecordId,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -103,20 +103,25 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
if(userId==null){
return Lists.newArrayList();
}
CutDownPriceUserRecord record = cutDownPriceUserRecordMapper.selectCutDownPriceUserRecord(userId, activityId, productSkn);
if(record==null){
return Lists.newArrayList();
}
Integer recordId = record.getId();
// 取缓存
String redisKeySuffix = userId + ":" + activityId + ":" + productSkn;
String redisKeySuffix = userId + ":" + activityId + ":" + productSkn + ":" + recordId;
CutDownPriceProductHelpUserWrapper helpUserWrapper = redisValueCache.get(CacheKeyEnum.CUTDOWN_PRICE_HELP_USERLIST_INFO, redisKeySuffix, CutDownPriceProductHelpUserWrapper.class);
if (null != helpUserWrapper){
return helpUserWrapper.getHelpUserBoList();
}
List<CutDownPriceProductHelpUser> helpUsers = cutPriceUserHelpMapper.selectHelpInfo(userId, activityId, productSkn);
List<CutDownPriceProductHelpUser> helpUsers = cutPriceUserHelpMapper.selectHelpInfo(userId, activityId, productSkn, recordId);
if (CollectionUtils.isEmpty(helpUsers)){
logger.info("CutDownPriceServiceImpl :: queryHelpInfo result is null");
return Lists.newArrayList();
}
List<CutDownPriceProductHelpUserBo> helpUsersBoList = convert.convertFromList(helpUsers, CutDownPriceProductHelpUserBo.class);
//获取用户头像
getUserInfo(userId, helpUsersBoList);
getUserInfo(helpUsersBoList);
helpUserWrapper = new CutDownPriceProductHelpUserWrapper();
helpUserWrapper.setHelpUserBoList(helpUsersBoList);
redisValueCache.set(CacheKeyEnum.CUTDOWN_PRICE_HELP_USERLIST_INFO, redisKeySuffix, helpUserWrapper, 300, TimeUnit.SECONDS);
... ... @@ -126,7 +131,7 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
/**
* 查询某一个用户的帮砍记录
*/
private List<CutDownPriceProductHelpUserBo> queryHelpInfoList(Integer userId, List<CutPriceHelpUserRequestBO> requestList) {
private List<CutDownPriceProductHelpUserBo> queryHelpInfoList(List<CutPriceHelpUserRequestBO> requestList) {
List<CutDownPriceProductHelpUser> helpUsers = cutPriceUserHelpMapper.selectHelpInfoList(requestList);
if (CollectionUtils.isEmpty(helpUsers)){
logger.info("CutDownPriceServiceImpl :: queryHelpInfoList result is null");
... ... @@ -134,7 +139,7 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
}
List<CutDownPriceProductHelpUserBo> helpUsersBoList = convert.convertFromList(helpUsers, CutDownPriceProductHelpUserBo.class);
//获取用户头像
getUserInfo(userId, helpUsersBoList);
getUserInfo(helpUsersBoList);
return helpUsersBoList;
}
... ... @@ -152,13 +157,20 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
logger.warn(" the cutdown product not exist, activityId is {}, productSkn is {}", activityId, productSkn);
throw new ServiceException(ServiceError.ACTIVITY_CUTDOWNPRICE_PRODUCT_NOTEXIST_ERROR);
}
CutDownPriceUserRecord record = cutDownPriceUserRecordMapper.selectCutDownPriceUserRecord(userId, cutPriceHelpUserRequestBO.getActivityId(), cutPriceHelpUserRequestBO.getProductSkn());
// 查询已砍价记录
List<CutDownPriceProductHelpUserBo> helpUserBos = this.queryHelpInfo(cutPriceHelpUserRequestBO);
// 调product接口查询商品信息
ProductBo[] productBoArray = invokeBatchQueryNamesAndImageBySkns(Lists.newArrayList(productSkn));
// 调uid查询用户头像
UserInfoRspBO[] userInfoBoArray = invokeUicGetUserInfo(userId.toString());
// 组装信息
CutDownPriceActivityProductBo bo = buildCutDownPriceActivityProductBo(activityBo, cutDownPriceProductBo, helpUserBos, productBoArray);
bo.setCanUseCount(selectCutdownPriceOrderRecordCount(cutPriceHelpUserRequestBO));
bo.setUserImgUrl(buildUserImage(userId, userInfoBoArray));
bo.setCanUseCount(record!=null ? record.getUseCount() : 1);
bo.setCutTime(record!=null ? record.getCutTime() : record.getCreateTime());
bo.setCreateTime(record.getCreateTime());
buildCutStatus(bo);
return bo;
}
... ... @@ -223,6 +235,7 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
bo.setCanUseCount(record.getUseCount());
// 设置已砍价记录
bo.setHasJoinNum(record.getHelpCount());
bo.setCutRecordId(record.getId());
}
// 校验是否已经砍价成功
... ... @@ -291,7 +304,7 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
List<CutDownPriceProductBo> cutProductBoList = cutDownPriceProductService.queryCutDownPriceProductBoListByRecords(cutPriceUserRecordList);
// 查询已砍价记录
List<CutPriceHelpUserRequestBO> userRequestList = convert.convertFromList(cutPriceUserRecordList, CutPriceHelpUserRequestBO.class);
List<CutDownPriceProductHelpUserBo> helpUserBoList = this.queryHelpInfoList(uid, userRequestList);
List<CutDownPriceProductHelpUserBo> helpUserBoList = this.queryHelpInfoList(userRequestList);
// 根据skn列表查询商品信息
ProductBo[] productBoArray = invokeBatchQueryNamesAndImageBySkns(Lists.newArrayList(productSkns));
// 组装返回值
... ... @@ -305,7 +318,7 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
public void addCutPriceUseRecord(CutPriceHelpUserRequestBO requestBO) {
try{
// 扣减可使用次数
int affectRow = cutDownPriceUserRecordMapper.updateDecreaseUseCount(requestBO.getUserId(), requestBO.getActivityId(), requestBO.getProductSkn());
int affectRow = cutDownPriceUserRecordMapper.updateDecreaseUseCount(requestBO.getUserId(), requestBO.getActivityId(), requestBO.getProductSkn(), requestBO.getCutRecordId());
if(affectRow<=0){
logger.warn("updateDecreaseUseCount success!!!,affectRow is:{} ", affectRow);
throw new ServiceException(ServiceError.ACTIVITY_CUTDOWNPRICE_YOU_HAS_USED_CUTDOWNORDER_ERROR);
... ... @@ -319,7 +332,7 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
@Override
public int cancelCutPriceUseRecord(CutPriceHelpUserRequestBO requestBO) {
// 回补可使用次数
int result = cutDownPriceUserRecordMapper.updateIncreaseUseCount(requestBO.getUserId(), requestBO.getActivityId(), requestBO.getProductSkn());
int result = cutDownPriceUserRecordMapper.updateIncreaseUseCount(requestBO.getUserId(), requestBO.getActivityId(), requestBO.getProductSkn(), requestBO.getCutRecordId());
// 清理缓存
return result;
}
... ... @@ -357,12 +370,20 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
logger.warn(" the cutdown product not exist, activityId is {}, productSkn is {}", activityId, productSkn);
throw new ServiceException(ServiceError.ACTIVITY_CUTDOWNPRICE_PRODUCT_NOTEXIST_ERROR);
}
// 查询已砍的价格,判断是否已经砍价成功
List<CutDownPriceProductHelpUser> helpUsers = cutPriceUserHelpMapper.selectHelpInfo(userId, activityId, productSkn);
if(helpUsers!=null && helpUsers.size()>=cutDownPriceProductBo.getJoinNum()+1){
// 查询该用户是否已经发起过(之前发起过,没砍价成功,发起时间超过24小时)
CutDownPriceUserRecord recordDb = cutDownPriceUserRecordMapper.selectCutDownPriceUserRecord(userId, activityId, productSkn);
if(recordDb==null){
logger.warn(" you have not create cutprice, activityId is {}, productSkn is {}", activityId, productSkn);
throw new ServiceException(ServiceError.ACTIVITY_CUTDOWNPRICE_YOU_HASNOT_CREATE_CUTDOWN_ERROR);
}
if(recordDb!=null && (recordDb.getHelpCount()>=cutDownPriceProductBo.getJoinNum()+1)){
logger.warn(" this activity have help full, activityId is {}, productSkn is {}", activityId, productSkn);
throw new ServiceException(ServiceError.ACTIVITY_CUTDOWNPRICE_HAS_HELPED_FULL_ERROR);
}
// 查询已砍的价格
List<CutDownPriceProductHelpUser> helpUsers = cutPriceUserHelpMapper.selectHelpInfo(userId, activityId, productSkn, recordDb.getId());
// 计算本次应该砍的价格
BigDecimal cutPrice = new BigDecimal(0);
if(SYSTEM_HELP_ID.equals(helpUserId)){
... ... @@ -375,10 +396,11 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
CutDownPriceProductHelpUser helpUser = convertCutDownPriceProductHelpUser(cutPriceHelpUserRequestBO);
helpUser.setCutPrice(cutPrice);
helpUser.setCreateTime(DateUtils.getCurrentTimeSecond());
helpUser.setCutRecordId(recordDb.getId());
// 插入好友帮砍记录表
cutPriceUserHelpMapper.addHelpUserInfo(helpUser);
// 修改用户发起砍价记录表中的帮砍次数
cutDownPriceUserRecordMapper.updateCutDownHelpCount(userId, activityId, productSkn);
cutDownPriceUserRecordMapper.updateCutDownHelpCount(userId, activityId, productSkn, recordDb.getId());
// 用户帮忙砍价后,自动发起砍价活动,系统帮助砍价除外
if(!SYSTEM_HELP_ID.equals(helpUserId)){
try{
... ... @@ -503,9 +525,11 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
return new BigDecimal(averagePrice).add(randomPrice);
}
private void getUserInfo(Integer userId, List<CutDownPriceProductHelpUserBo> helpUserBos) {
private void getUserInfo(List<CutDownPriceProductHelpUserBo> helpUserBos) {
if(CollectionUtils.isEmpty(helpUserBos)){
return;
}
StringBuilder sb = new StringBuilder();
sb.append(userId);
for (CutDownPriceProductHelpUserBo helpUserBo : helpUserBos){
if(null!=helpUserBo.getHelpUserId() && SYSTEM_HELP_ID!=helpUserBo.getHelpUserId()){
sb.append(",").append(helpUserBo.getHelpUserId());
... ... @@ -525,14 +549,6 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
}
}
}
for (CutDownPriceProductHelpUserBo helpUserBo : helpUserBos){
for (UserInfoRspBO userInfoRspBO : userInfoBoArray){
if (userId.equals(userInfoRspBO.getUid())){
helpUserBo.setUserImgUrl(userInfoRspBO.getHeadIco());
break;
}
}
}
}
private UserInfoRspBO[] invokeUicGetUserInfo(String uids) {
... ... @@ -597,11 +613,11 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
BigDecimal hasCutPrice = new BigDecimal(0);
for(CutDownPriceProductHelpUserBo helpUserBo : helpUserBos){
hasCutPrice = hasCutPrice.add(helpUserBo.getCutPrice());
activityProductBo.setUserImgUrl(helpUserBo.getUserImgUrl());// 赋值一次就可以了
}
activityProductBo.setHasCutPrice(hasCutPrice);
activityProductBo.setHasJoinNum(helpUserBos.size());
}
return activityProductBo;
}
... ... @@ -701,6 +717,21 @@ public class CutDownPriceServiceImpl implements ICutDownPriceService {
}
}
private String buildUserImage(Integer userId, UserInfoRspBO[] userInfoBoArray) {
String imageUrl = null;
if(ArrayUtils.isEmpty(userInfoBoArray)){
return null;
}
for(UserInfoRspBO userInfoRspBO : userInfoBoArray){
if(userInfoRspBO.getUid().equals(userId)){
imageUrl = userInfoRspBO.getHeadIco();
break;
}
}
return imageUrl;
}
private void pushSuccessMessage(Integer userId, CutDownPriceActivityBo activityBo, CutDownPriceProductBo cutDownPriceProductBo) {
executorService.execute(new Runnable() {
@Override
... ...