| @@ -15,11 +15,14 @@ import com.iformall.common.ResultData; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.vo.WxCreditHistoryVo; | |||
| import com.iformall.enums.EnumBusiness; | |||
| import com.iformall.enums.EnumCouponStatus; | |||
| import com.iformall.enums.EnumScoreType; | |||
| import com.iformall.enums.EnumUserType; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.mapper.*; | |||
| import com.iformall.service.WxCreditHistoryService; | |||
| import com.iformall.service.WxScoreRulesService; | |||
| import com.iformall.utils.RedisLock; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| @@ -30,31 +33,37 @@ import org.springframework.transaction.annotation.Transactional; | |||
| @Service | |||
| @Slf4j | |||
| public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| @Autowired | |||
| @Autowired | |||
| WxCreditHistoryMapper wxCreditHistoryMapper; | |||
| @Autowired | |||
| @Autowired | |||
| WxCUserMapper wxCUserMapper; | |||
| @Autowired | |||
| @Autowired | |||
| WxCUserBasicInfoMapper wxCUserBasicInfoMapper; | |||
| @Autowired | |||
| @Autowired | |||
| WxMerchantBUserMapper wxMerchantBUserMapper; | |||
| @Autowired | |||
| @Autowired | |||
| MallUserInfoMapper mallUserInfoMapper; | |||
| @Autowired | |||
| @Autowired | |||
| WxScoreRulesService wxScoreRulesService; | |||
| @Autowired | |||
| WxMerchantMapper wxMerchantMapper; | |||
| @Autowired | |||
| WxCouponMapper wxCouponMapper; | |||
| @Autowired | |||
| RedisLock redisLock; | |||
| @Override | |||
| public PageInfo<WxCreditHistory> listAsPage(WxCreditHistory record, Integer pageIndex, Integer pageSize) { | |||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCreditHistoryMapper.findList(record)); | |||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCreditHistoryMapper.findList(record)); | |||
| } | |||
| @Override | |||
| @@ -62,7 +71,7 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| PageHelper.startPage(pageIndex, pageSize); | |||
| List<WxCreditHistoryVo> wxCreditHistoryVoList = wxCreditHistoryMapper.findListMore(record); | |||
| if (wxCreditHistoryVoList == null || wxCreditHistoryVoList.size() == 0) { | |||
| return new PageInfo<>(wxCreditHistoryVoList); | |||
| return new PageInfo<>(wxCreditHistoryVoList); | |||
| } | |||
| List<Long> cUserIds = wxCreditHistoryVoList.stream().filter(a -> a.getOperatorType().equals(EnumUserType.CUSER.getCode())).map(WxCreditHistoryVo::getOperatorId).collect(Collectors.toList()); | |||
| List<Long> cUserBasicIds = wxCreditHistoryVoList.stream().filter(a -> a.getOperatorType().equals(EnumUserType.CUSERBASIC.getCode())).map(WxCreditHistoryVo::getOperatorId).collect(Collectors.toList()); | |||
| @@ -94,35 +103,35 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| } | |||
| for (WxCreditHistoryVo credit : wxCreditHistoryVoList) { | |||
| if (wxCUserList != null && wxCUserList.size() > 0) { | |||
| wxCUserList.stream().forEach(cUser->{ | |||
| wxCUserList.stream().forEach(cUser -> { | |||
| if (cUser.getId().longValue() == credit.getOperatorId().longValue()) { | |||
| credit.setOperator(cUser.getNickName()); | |||
| } | |||
| }); | |||
| } | |||
| if (wxCUserBasicInfoList != null && wxCUserBasicInfoList.size() > 0) { | |||
| wxCUserBasicInfoList.stream().forEach(cUserBasicInfo->{ | |||
| wxCUserBasicInfoList.stream().forEach(cUserBasicInfo -> { | |||
| if (cUserBasicInfo.getId().longValue() == credit.getOperatorId().longValue()) { | |||
| credit.setOperator(cUserBasicInfo.getNickName()); | |||
| } | |||
| }); | |||
| } | |||
| if (wxMerchantBUserList != null && wxMerchantBUserList.size() > 0) { | |||
| wxMerchantBUserList.stream().forEach(bUser->{ | |||
| wxMerchantBUserList.stream().forEach(bUser -> { | |||
| if (bUser.getId().longValue() == credit.getOperatorId().longValue()) { | |||
| credit.setOperator(bUser.getName()); | |||
| } | |||
| }); | |||
| } | |||
| if (mallUserInfoList != null && mallUserInfoList.size() > 0) { | |||
| mallUserInfoList.stream().forEach(mallUser->{ | |||
| mallUserInfoList.stream().forEach(mallUser -> { | |||
| if (mallUser.getId().longValue() == credit.getOperatorId().longValue()) { | |||
| credit.setOperator(mallUser.getName()); | |||
| } | |||
| }); | |||
| } | |||
| } | |||
| return new PageInfo<>(wxCreditHistoryVoList); | |||
| return new PageInfo<>(wxCreditHistoryVoList); | |||
| } | |||
| @Override | |||
| @@ -162,7 +171,7 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| record.setCreditAmount(currentCreditAmount + creditChangeNum); | |||
| if (record.getId() == null) { | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| record.setId(idWorker.nextId()); | |||
| record.setId(idWorker.nextId()); | |||
| record.setCreateDate(new Date()); | |||
| wxCreditHistoryMapper.insertSelective(record); | |||
| //将计算出来新的总积分 更新到两张用户表里 | |||
| @@ -187,7 +196,7 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| } | |||
| @Override | |||
| public Map<String, Integer> findByMerchantIdAndSpend(Long merchantId, Integer spend ,String tenantId) { | |||
| public Map<String, Integer> findByMerchantIdAndSpend(Long merchantId, Integer spend, String tenantId) { | |||
| WxMerchant wxMerchant = wxMerchantMapper.selectByPrimaryKey(merchantId); | |||
| Map<String, Integer> creditMap = Maps.newHashMap(); | |||
| if (wxMerchant != null && wxMerchant.getBusinessId() != null) { | |||
| @@ -196,7 +205,7 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| wxCreditHistory.setTenantId(tenantId); | |||
| wxCreditHistory.setSpend(spend); | |||
| int credit = payAddCredit(wxCreditHistory); | |||
| creditMap.put("credit",credit); | |||
| creditMap.put("credit", credit); | |||
| } | |||
| return creditMap; | |||
| } | |||
| @@ -244,14 +253,14 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| // 1. 获取当前租户下积分的增加规则 | |||
| WxScoreRules wxScoreRules = wxScoreRulesService.getCreditRules(record.getTenantId()); | |||
| // 2. 增长的积分 | |||
| return wxScoreRules.getRule(EnumScoreType.LOGIN,WxScoreRules.SCORE); | |||
| return wxScoreRules.getRule(EnumScoreType.LOGIN, WxScoreRules.SCORE); | |||
| } | |||
| private int bindCarAddCredit(WxCreditHistory record) { | |||
| // 1. 获取score rules | |||
| WxScoreRules wxScoreRules = wxScoreRulesService.getCreditRules(record.getTenantId()); | |||
| // 2. 增长的积分 | |||
| return wxScoreRules.getRule(EnumScoreType.BIND_CAR,WxScoreRules.SCORE); | |||
| return wxScoreRules.getRule(EnumScoreType.BIND_CAR, WxScoreRules.SCORE); | |||
| } | |||
| private int wifiAddCredit() { | |||
| @@ -263,14 +272,14 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| // 1. 获取score rules | |||
| WxScoreRules wxScoreRules = wxScoreRulesService.getCreditRules(record.getTenantId()); | |||
| // 2. 增长的积分 | |||
| return wxScoreRules.getRule(EnumScoreType.WECHAT_PERSION,WxScoreRules.SCORE); | |||
| return wxScoreRules.getRule(EnumScoreType.WECHAT_PERSION, WxScoreRules.SCORE); | |||
| } | |||
| private int phoneAddCredit(WxCreditHistory record) { | |||
| // 1. 获取score rules | |||
| WxScoreRules wxScoreRules = wxScoreRulesService.getCreditRules(record.getTenantId()); | |||
| // 2. 获取成长值 | |||
| return wxScoreRules.getRule(EnumScoreType.WECHAT_PERSION,WxScoreRules.SCORE); | |||
| return wxScoreRules.getRule(EnumScoreType.WECHAT_PERSION, WxScoreRules.SCORE); | |||
| } | |||
| private int checkCompleteInfoScoreCount(WxCUser cUser) { | |||
| @@ -288,19 +297,77 @@ public class WxCreditHistoryServiceImpl implements WxCreditHistoryService { | |||
| // 1. 获取score rules | |||
| WxScoreRules scoreRules = wxScoreRulesService.getCreditRules(user.getTenantId()); | |||
| // 2. 获取成长值 | |||
| return scoreRules.getRule(EnumScoreType.COMPLETE_INFO,WxScoreRules.SCORE); | |||
| return scoreRules.getRule(EnumScoreType.COMPLETE_INFO, WxScoreRules.SCORE); | |||
| } | |||
| private int spendCredit(WxCreditHistory record){ | |||
| private int spendCredit(WxCreditHistory record) { | |||
| int spendCredit = record.getCreditNum(); | |||
| return spendCredit; | |||
| } | |||
| private int changeCredit(WxCreditHistory record){ | |||
| int changeCredit = record.getCreditNum(); | |||
| private int changeCredit(WxCreditHistory record) { | |||
| if (record == null || record.getCouponId() == null) { | |||
| log.error("券不存在"); | |||
| throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); | |||
| } | |||
| // 检查券状态 | |||
| String couponIdStr = String.valueOf(record.getCouponId()); | |||
| WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(record.getCouponId()); | |||
| if (coupon == null) { | |||
| log.error("券不存在, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); | |||
| } | |||
| if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) { | |||
| log.error("券已下架, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.COUPON_IS_TAKE_OFF); | |||
| } | |||
| if (coupon.getSalePrice() != 0) { | |||
| log.error("券不免费, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.COUPON_IS_NOT_FREE); | |||
| } | |||
| // 减库存操作 | |||
| try { | |||
| stockReduce(coupon, couponIdStr); | |||
| } catch (Exception e) { | |||
| log.error(e.getMessage()); | |||
| } | |||
| int changeCredit = coupon.getCreditPrice(); | |||
| return -changeCredit; | |||
| } | |||
| private void stockReduce(WxCoupon coupon, String couponIdStr) { | |||
| long time = System.currentTimeMillis() + RedisLock.TIMEOUT; | |||
| String timeStr = String.valueOf(time); | |||
| // 库存加锁 | |||
| if (!redisLock.lock(couponIdStr, timeStr)) { | |||
| log.error("此券被锁定, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.TOO_MANY_REQUEST); | |||
| } | |||
| // 检查 优惠券 库存 | |||
| if (coupon.getRemainInventory() <= 0) { | |||
| //解锁 | |||
| redisLock.unlock(couponIdStr, timeStr); | |||
| log.error("此券库存为0, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); | |||
| } | |||
| try { | |||
| WxCoupon updateRecord = new WxCoupon(); | |||
| updateRecord.setId(coupon.getId()); | |||
| updateRecord.setRemainInventory(coupon.getRemainInventory() - 1); | |||
| updateRecord.setUpdateDate(new Date()); | |||
| // 减库存 | |||
| wxCouponMapper.updateByPrimaryKeySelective(updateRecord); | |||
| } catch (RuntimeException e) { | |||
| log.error("此券减库存失败, couponId: " + couponIdStr); | |||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | |||
| } finally { | |||
| //解锁 | |||
| redisLock.unlock(couponIdStr, timeStr); | |||
| } | |||
| } | |||
| private int payAddCredit(WxCreditHistory record) { | |||
| int addCreditNumber = 0; | |||
| // 订单金额为0时不计入 | |||