Explorar el Código

[BUG][会员]:会员定时发券的逻辑更改,统一考虑多张券逻辑

release_toaliyun_real
hupeng hace 7 años
padre
commit
02653eab55
Se han modificado 2 ficheros con 76 adiciones y 42 borrados
  1. +62
    -36
      mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java
  2. +14
    -6
      mallinkService/src/main/resources/mapper/WxCUserBasicInfoMapper.xml

+ 62
- 36
mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java Ver fichero

@@ -44,6 +44,9 @@ public class CouponSendSchedule {
@Autowired @Autowired
private WxCUserBasicInfoMapper wxCUserBasicInfoMapper; private WxCUserBasicInfoMapper wxCUserBasicInfoMapper;


@Autowired
private WxCUserMapper wxCUserMapper;

@Autowired @Autowired
private WxCouponSendMapper wxCouponSendMapper; private WxCouponSendMapper wxCouponSendMapper;


@@ -59,7 +62,7 @@ public class CouponSendSchedule {


//@Scheduled(cron = "0 0 6 1 * ?") // 每月1号发放会员的权益券 //@Scheduled(cron = "0 0 6 1 * ?") // 每月1号发放会员的权益券
@Scheduled(cron = "0 0 6 * * ?") // 测试每天6点发券 @Scheduled(cron = "0 0 6 * * ?") // 测试每天6点发券
//@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
//@Scheduled(cron = "*/30 * * * * ?") // 测试30秒中一次
public void couponSendSchedule() { public void couponSendSchedule() {


List<WxMall> mallList = wxMallMapper.findList(new WxMall()); List<WxMall> mallList = wxMallMapper.findList(new WxMall());
@@ -89,12 +92,12 @@ public class CouponSendSchedule {
.sorted(Comparator.comparing(WxLevelConfig::getPoints)) .sorted(Comparator.comparing(WxLevelConfig::getPoints))
.collect(Collectors.toList()); .collect(Collectors.toList());


for (int i = 0; i < levelConfigList.size(); i++) {
WxLevelConfig l = levelConfigList.get(i);
for (int levelIndex = 0; levelIndex < levelConfigList.size(); levelIndex++) {
WxLevelConfig l = levelConfigList.get(levelIndex);
if (l.getCapability() == null) if (l.getCapability() == null)
continue; continue;
Integer levelStartScore = l.getPoints(); Integer levelStartScore = l.getPoints();
Integer levelEndScore = levelConfigList.size() < i+1? levelConfigList.get(i+1).getPoints() : null;
Integer levelEndScore = levelIndex+1 < levelConfigList.size() ? levelConfigList.get(levelIndex+1).getPoints() : null;


JSONObject jo = null; JSONObject jo = null;
Long result = null; Long result = null;
@@ -107,28 +110,37 @@ public class CouponSendSchedule {
} }
Long sendCount = result; Long sendCount = result;
if (sendCount > 0) { if (sendCount > 0) {
WxCouponSend wxCouponSend = new WxCouponSend();
wxCouponSend.setTenantId(l.getTenantId());
wxCouponSend.setSendType(EnumCouponSendSendType.TIMED.getCode());
wxCouponSend.setStatus(EnumCouponSendStatus.VALID.getCode());
List<WxCouponSendVo> couponSendList = wxCouponSendMapper.findListVo(wxCouponSend);
couponSendList.stream().forEach(cs->{
if (cs.getRemainInventory() < sendCount){
logger.error("定时发券:库存不足 levelId="+ l.getId() +" couponId="+ cs.getCouponId());
return; //下一个券

WxCUserBasicInfoDto wxCUserBasicInfoDto = new WxCUserBasicInfoDto();
wxCUserBasicInfoDto.setTenantId(l.getTenantId());
wxCUserBasicInfoDto.setLevelStartScore(levelStartScore);
wxCUserBasicInfoDto.setLevelEndScore(levelEndScore);
List<WxCUserBasicInfo> wxCUserBasicInfoList = wxCUserBasicInfoMapper.findListByScore(wxCUserBasicInfoDto);

logger.info("定时发券:给LEVEL["+ l.getLevel() +
"]积分["+levelStartScore+"-"+levelEndScore+"]发券"+sendCount+"张");

wxCUserBasicInfoList.stream().forEach(cu->{

WxCouponSend wxCouponSend = new WxCouponSend();
wxCouponSend.setTenantId(l.getTenantId());
wxCouponSend.setSendType(EnumCouponSendSendType.TIMED.getCode());
wxCouponSend.setStatus(EnumCouponSendStatus.VALID.getCode());
List<WxCouponSendVo> couponSendList = wxCouponSendMapper.findListVo(wxCouponSend);
int remain = couponSendList.stream().map(cs->{
int count = cs.getRemainInventory();
logger.info("定时发券:给["+ cu.getNickName() +
"]券[" + cs.getTitle() + "]还剩" +
count +"张");
return count;
}).reduce(Integer::sum).get();

if (remain < sendCount){
logger.error("定时发券:库存不足["+ cu.getNickName() +"]应发"+sendCount+"张,总共剩"+ remain + "张");
return;
} }
WxCUserBasicInfoDto wxCUserBasicInfoDto = new WxCUserBasicInfoDto();
wxCUserBasicInfoDto.setTenantId(l.getTenantId());
wxCUserBasicInfoDto.setLevelStartScore(levelStartScore);
wxCUserBasicInfoDto.setLevelEndScore(levelEndScore);
List<WxCUserBasicInfo> wxCUserBasicInfoList = wxCUserBasicInfoMapper.findListByScore(wxCUserBasicInfoDto);

wxCUserBasicInfoList.stream().forEach(cu->{
if (cs.getRemainInventory() < sendCount){
logger.error("定时发券:库存不足 levelId="+ l.getId() +" couponId="+ cs.getCouponId()+"userId="+cu.getId());
return; //下一个券
}


int sentCount = couponSendList.stream().map(cs->{
Map params = new HashMap<String, Object>(); Map params = new HashMap<String, Object>();
params.put("tenantId", cs.getTenantId()); params.put("tenantId", cs.getTenantId());
params.put("cUserId", cu.getId()); params.put("cUserId", cu.getId());
@@ -136,31 +148,45 @@ public class CouponSendSchedule {
params.put("startTime", startTime); params.put("startTime", startTime);
params.put("endTime", endTime); params.put("endTime", endTime);
params.put("channelType", EnumCouponSendSendType.TIMED.getCode()); params.put("channelType", EnumCouponSendSendType.TIMED.getCode());
int count = wxCouponActionLogMapper.getCountByUserAndCouponAndDate(params);
logger.info("定时发券:已经给["+ cu.getNickName() +
"]券[" + cs.getTitle() + "] " +
count +"张");
return count;
}).reduce(Integer::sum).get();


int sentCount = wxCouponActionLogMapper.getCountByUserAndCouponAndDate(params);


logger.info("定时发券:给["+ cu.getNickName() +
"]已发送券[" + cs.getTitle() + "] " +
sentCount +"张,还需发"+ (sendCount-sentCount) +"张");
logger.info("定时发券:给["+ cu.getNickName() +
"]总共已发送券" + sentCount + "张,还需发"+ (sendCount-sentCount) +"张");

for (int count = 0; count < sendCount - sentCount; count++) {
boolean sent = false;
for(int couponIndex = 0; couponIndex< couponSendList.size(); couponIndex++) {
WxCouponSendVo cs = couponSendList.get(couponIndex);
if (cs.getRemainInventory() <= 0)
continue;


for (int count = 0; count<sendCount-sentCount; count++) {
// 发放免费券 // 发放免费券
try { try {
WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cu.getId(), cs.getCouponId()); WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cu.getId(), cs.getCouponId());
wxCouponActionLogService.addOne(l.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId()); wxCouponActionLogService.addOne(l.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId());
} catch (Exception e) { } catch (Exception e) {
logger.error("定时发券:发券失败 levelId=" + l.getId() + " couponId=" + cs.getCouponId() + " userId=" + cu.getId() + e.getMessage()); logger.error("定时发券:发券失败 levelId=" + l.getId() + " couponId=" + cs.getCouponId() + " userId=" + cu.getId() + e.getMessage());
return;
continue;
} }


logger.info("定时发券:商场[" + mall.getName() + logger.info("定时发券:商场[" + mall.getName() +
"]会员等级[" + l.getLevel() +
"]发送券[" + cs.getTitle() +
"]给:" + cu.getNickName() +
" 电话-" + cu.getPhone() +
" id-" + cu.getId());
"]会员等级[" + l.getLevel() +
"]发送券[" + cs.getTitle() +
"]给:" + cu.getNickName() +
" 电话-" + cu.getPhone() +
" id-" + cu.getId());
sent = true;
break;
} }
});
if (!sent)
break;
}
}); });
} }
} }


+ 14
- 6
mallinkService/src/main/resources/mapper/WxCUserBasicInfoMapper.xml Ver fichero

@@ -181,24 +181,32 @@


</select> </select>


<sql id="allColumnsOfScoreList">
cub.id,cub.phone,cub.birthdate,cub.education,cub.sex,cub.email,cub.address,cub.poins,cub.tag_id,
cub.create_date,cub.update_date,cub.tenant_id,cub.name,cub.level,cub.nick_name
</sql>

<select id="findListByScore" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultMap="BaseResultMap"> <select id="findListByScore" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultMap="BaseResultMap">
select <include refid="allColumns"/>
from wx_c_user_basic_info where 1=1
select <include refid="allColumnsOfScoreList"/>
from wx_c_user_basic_info cub
inner join wx_c_user cu on cu.id = cub.id

where 1=1


<if test=" null != levelStartScore "> <if test=" null != levelStartScore ">
and poins &gt;= #{levelStartScore}
and cub.poins &gt;= #{levelStartScore}
</if> </if>


<if test=" null != levelEndScore"> <if test=" null != levelEndScore">
and poins &lt;= #{levelEndScore}
and cub.poins &lt;= #{levelEndScore}
</if> </if>


<if test=" null == levelStartScore and null == levelEndScore"> <if test=" null == levelStartScore and null == levelEndScore">
and poins is null
and cub.poins is null
</if> </if>


<if test="null != tenantId"> <if test="null != tenantId">
and tenant_id =#{tenantId}
and cub.tenant_id =#{tenantId}
</if> </if>


</select> </select>


Cargando…
Cancelar
Guardar