diff --git a/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java b/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java index 8b15c6077..f660943ea 100644 --- a/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java +++ b/mallinkAdmin/src/main/java/com/iformall/controller/market/WxCouponSendController.java @@ -10,6 +10,7 @@ import com.iformall.domain.po.BaseEntity; import com.iformall.domain.po.WxCoupon; import com.iformall.domain.po.WxCouponSend; import com.iformall.domain.vo.WxCouponSendVo; +import com.iformall.enums.EnumCouponSendSendType; import com.iformall.enums.EnumCouponSendStatus; import com.iformall.enums.EnumCouponSendType; import com.iformall.enums.EnumCouponStatus; @@ -25,7 +26,9 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.validation.constraints.NotNull; import java.util.Date; +import java.util.Objects; @RestController @RequestMapping("wxCouponSend") @@ -60,6 +63,31 @@ public class WxCouponSendController extends BaseController { return new ResultData(page); } + @ApiOperation("注券-添加配置") + @GetMapping("config/add") + @ApiImplicitParams({ + @ApiImplicitParam(name = "beforeDays", value = "提前n天发券", dataType = "int", paramType = "query", required = true), + @ApiImplicitParam(name = "sendType", value = "注券类型(生日券=8)", dataType = "int", paramType = "query", required = true), + }) + @SystemControllerLog(description = "注券-添加或更新配置") + public ResultData addConfig(@NotNull Integer beforeDays, @NotNull Integer sendType) { + if(!Objects.equals(sendType,EnumCouponSendSendType.BIRTHDAY.getCode())) { + return new ResultData(ErrorCode.COUPON_VALID_NOT_SUPPORTED); + } + wxCouponSendService.saveOrUpdateConfig(beforeDays, sendType, getTenantId()); + return new ResultData(); + } + + @ApiOperation("注券-获取配置") + @GetMapping("config/get") + @ApiImplicitParams({ + @ApiImplicitParam(name = "sendType", value = "注券类型(生日券=8)", dataType = "int", paramType = "query", required = true)}) + @SystemControllerLog(description = "注券-获取配置") + public ResultData getConfig(@NotNull Integer sendType) { + WxCouponSend wxCouponSend = wxCouponSendService.getConfig(sendType, getTenantId()); + return new ResultData(Objects.isNull(wxCouponSend) ? null : wxCouponSend.getConditions()); + } + @ApiOperation("新增接口") @PostMapping("add") @SystemControllerLog(description = "注券-新增") @@ -68,18 +96,20 @@ public class WxCouponSendController extends BaseController { if (null == wxCouponSend) { return new ResultData(ErrorCode.SYS_PARAMETER_ERROR); } - - WxCoupon wxCoupon = wxCouponService.getById(wxCouponSend.getCouponId()); - if (!wxCoupon.getStatus().equals(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode()) ){ - return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF); - } - if (!wxCoupon.getSendType().equals(EnumCouponSendType.PASSIVE.getCode())) { - return new ResultData(ErrorCode.COUPON_TYPE_IS_NOT_PASSIVE); + if(!Objects.equals(wxCouponSend.getSendType(),EnumCouponSendSendType.BIRTHDAY.getCode())) { + WxCoupon wxCoupon = wxCouponService.getById(wxCouponSend.getCouponId()); + if (!wxCoupon.getStatus().equals(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode()) ){ + return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF); + } + + if (!wxCoupon.getSendType().equals(EnumCouponSendType.PASSIVE.getCode())) { + return new ResultData(ErrorCode.COUPON_TYPE_IS_NOT_PASSIVE); + } + wxCouponSend.setTenantId(wxCoupon.getTenantId()); + wxCouponSend.setTitle(wxCoupon.getTitle()); } - wxCouponSend.setTenantId(wxCoupon.getTenantId()); - wxCouponSend.setTitle(wxCoupon.getTitle()); wxCouponSend.setCreateDate(new Date()); wxCouponSend.setUpdateDate(new Date()); @@ -99,9 +129,12 @@ public class WxCouponSendController extends BaseController { @ApiOperation("根据id删除接口") @GetMapping("/del") - @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true) + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true), + @ApiImplicitParam(name = "sendType", value = "注券类型(会员生日券时必传)", dataType = "int", paramType = "query") + }) @SystemControllerLog(description = "注券-删除") - public ResultData delete(Long id) { + public ResultData delete(Long id,Integer sendType) { logger.debug("[" + getIpAddr() + "] WxCouponSendController::delete"); WxCouponSend wxCouponSend = wxCouponSendService.getById(id); if (wxCouponSend == null) diff --git a/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java b/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java index f3bbbc16e..aa7efc333 100644 --- a/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java +++ b/mallinkSchedule/src/main/java/com/iformall/schedule/CouponSendSchedule.java @@ -1,25 +1,23 @@ package com.iformall.schedule; import com.alibaba.fastjson.JSONObject; -import com.iformall.common.ErrorCode; import com.iformall.domain.dto.WxCUserBasicInfoDto; import com.iformall.domain.po.*; +import com.iformall.domain.po.msg.WxMsgRecord; import com.iformall.domain.vo.WxCouponSendVo; -import com.iformall.enums.EnumCouponSendSendType; -import com.iformall.enums.EnumCouponSendStatus; -import com.iformall.exception.MallinkException; +import com.iformall.enums.*; import com.iformall.mapper.*; -import com.iformall.service.WxCouponActionLogService; -import com.iformall.service.WxCouponSendService; -import com.iformall.service.WxLevelConfigService; -import com.iformall.service.WxOrderService; -import io.swagger.models.auth.In; +import com.iformall.mq.MqBaseProducer; +import com.iformall.service.*; +import com.iformall.utils.DateUtils; +import org.apache.commons.collections.CollectionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; @@ -56,6 +54,18 @@ public class CouponSendSchedule { @Autowired private WxCouponActionLogService wxCouponActionLogService; + @Autowired + private WxCouponSendConfigMapper couponSendConfigMapper ; + + @Autowired + private WxCouponSendService wxCouponSendService ; + @Autowired + private WxScoreRulesService wxScoreRulesService ; + + @Autowired + private MqBaseProducer mqBaseProducer; + + //@Scheduled(cron = "0 0 6 1 * ?") // 每月1号发放会员的权益券 @Scheduled(cron = "0 0 6 * * ?") // 测试每天6点发券 @@ -79,9 +89,14 @@ public class CouponSendSchedule { .with(LocalTime.MAX) .atZone(ZoneId.systemDefault()) .toInstant()); - + //查找所有couponSendConfig + Map mallConfigMap = getAllCouponSendConfig(EnumCouponSendSendType.TIMED.getCode()); mallList.stream().forEach(mall-> { - + //判断启用状态,状态为关闭时跳过不执行 + if (isCouponSendDisable(mallConfigMap, mall)) { + logger.info("定时发券: {}发券状态关闭, 任务跳过不执行", mall.getName()); + return; + } WxLevelConfig wxLevelConfig = new WxLevelConfig(); wxLevelConfig.setTenantId(mall.getTenantId()); List levelConfigList = WxLevelConfigMapper.findList(wxLevelConfig); @@ -189,4 +204,172 @@ public class CouponSendSchedule { } }); } + + /** + * 生日券发放 + */ + @Scheduled(cron = "0 0 4 * * ?") // 测试每天6点发券 + public void birthdayCouponSendSchedule() { + logger.info("生日发券任务开始"); + List mallList = wxMallMapper.findList(new WxMall()); + if (mallList.size() == 0) { + logger.info("生日发券: No Mall info found"); + return; + } + + Date startTime = Date.from(LocalDateTime.now() + .with(TemporalAdjusters.firstDayOfYear()) + .with(LocalTime.MIN) + .atZone(ZoneId.systemDefault()) + .toInstant()); + Date endTime = Date.from(LocalDateTime.now() + .with(TemporalAdjusters.lastDayOfYear()) + .with(LocalTime.MAX) + .atZone(ZoneId.systemDefault()) + .toInstant()); + + //查找所有couponSendConfig + Map mallConfigMap = getAllCouponSendConfig(EnumCouponSendSendType.BIRTHDAY.getCode()); + mallList.forEach(mall -> { + //判断启用状态,状态为关闭时跳过不执行 + if (isCouponSendDisable(mallConfigMap, mall)) { + logger.info("生日发券: {} 发券状态关闭, 任务跳过不执行", mall.getName()); + return; + } + + WxCUserBasicInfoDto wxCUserBasicInfoDto = new WxCUserBasicInfoDto(); + wxCUserBasicInfoDto.setTenantId(mall.getTenantId()); + List wxCUserBasicInfoList = wxCUserBasicInfoMapper.findBirthdayList(wxCUserBasicInfoDto); + if (CollectionUtils.isEmpty(wxCUserBasicInfoList)) { + logger.info("生日发券: {} 用户列表为空 wxCUserBasicInfoList = {}, 任务跳过不执行",mall.getName(),wxCUserBasicInfoList); + return; + } + //每人可以发送几张生日券 + final int sendCount = 1; + wxCUserBasicInfoList.forEach(cu -> { + WxCouponSend wxCouponSend = new WxCouponSend(); + wxCouponSend.setTenantId(cu.getTenantId()); + wxCouponSend.setSendType(EnumCouponSendSendType.BIRTHDAY.getCode()); + wxCouponSend.setStatus(EnumCouponSendStatus.VALID.getCode()); + List couponSendList = wxCouponSendMapper.findListVo(wxCouponSend); + if(CollectionUtils.isEmpty(couponSendList)) { + logger.info("生日发券: 会员生日券投放列表为空 couponSendList = {}, 任务跳过不执行", couponSendList); + return; + } + + WxCouponSend config = wxCouponSendService.getConfig(EnumCouponSendSendType.BIRTHDAY.getCode(),cu.getTenantId()) ; + if(Objects.isNull(config)||Objects.isNull(config.getConditions())) { + logger.info("生日发券: 会员生日券配置不存在 config = {}, 任务跳过不执行", config); + return; + } + JSONObject configJo = JSONObject.parseObject(config.getConditions()) ; + int beforeDays = configJo.getIntValue(WxCouponSend.KEY_BEFOREDAYS); + int remain = couponSendList.stream().map(cs -> { + int count = cs.getRemainInventory(); + logger.info("定时发券:给[" + cu.getNickName() + + "]券[" + cs.getTitle() + "]还剩" + + count + "张"); + return count; + }).reduce(Integer::sum).orElse(0); + + if (remain < sendCount) { + logger.error("定时发券:库存不足[" + cu.getNickName() + "]应发" + sendCount + "张,总共剩" + remain + "张"); + return; + } + + int sentCount = couponSendList.stream().map(cs -> { + Map params = new HashMap<>(); + params.put("tenantId", cs.getTenantId()); + params.put("cUserId", cu.getId()); + params.put("couponId", cs.getCouponId()); + params.put("startTime", startTime); + params.put("endTime", endTime); + params.put("channelType", EnumCouponSendSendType.BIRTHDAY.getCode()); + int count = wxCouponActionLogMapper.getCountByUserAndCouponAndDate(params); + logger.info("生日发券:已经给[" + cu.getNickName() + + "]券[" + cs.getTitle() + "] " + + count + "张"); + return count; + }).reduce(Integer::sum).orElse(0); + + + 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); + //提前n天发送生日券,不满足跳过不执行 + if(DateUtils.daysBetween(cu.getBirthdate(), new Date()) == beforeDays) { + continue; + } + if (cs.getRemainInventory() <= 0) + continue; + // 发放免费券 + try { + WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(cu.getId(), cs.getCouponId()); + sendSMS(cu,mall); + wxCouponActionLogService.addOne(cu.getTenantId(), cs.getCouponId(), couponOrder.getId(), cs.getSendType(), cs.getId()); + } catch (Exception e) { + logger.error("定时发券:发券失败 levelId=" + cu.getId() + " couponId=" + cs.getCouponId() + " userId=" + cu.getId() + e.getMessage()); + logger.info("定时发券:发券失败 levelId=" + cu.getId() + " couponId=" + cs.getCouponId() + " userId=" + cu.getId() + e.getMessage()); + continue; + } + + logger.info("定时发券:商场[" + mall.getName() + + "]会员生日[" + cu.getBirthdate().toInstant() + + "]发送券[" + cs.getTitle() + + "]给:" + cu.getNickName() + + " 电话-" + cu.getPhone() + + " id-" + cu.getId()); + sent = true; + break; + } + if (!sent) + break; + } + }); + }); + logger.info("生日发券任务结束"); + } + + private Map getAllCouponSendConfig(Integer sendType) { + WxCouponSendConfig queryConfig = new WxCouponSendConfig(); + queryConfig.setSendType(sendType); + List couponSendConfigList = couponSendConfigMapper.findList(queryConfig); + Map mallConfigMap = CollectionUtils.isNotEmpty(couponSendConfigList) ? + couponSendConfigList.stream().collect(Collectors.toMap(WxCouponSendConfig::getTenantId, WxCouponSendConfig::getValue)) : null; + return mallConfigMap; + } + + private boolean isCouponSendDisable(Map mallConfigMap, WxMall mall) { + if (mallConfigMap != null && Objects.nonNull(mallConfigMap.get(mall.getTenantId()))) { + Integer disable = mallConfigMap.get(mall.getTenantId()); + return Objects.equals(disable, EnumEnableType.Disable.getCode()); + } + return false; + } + + private void sendSMS(WxCUserBasicInfo cUserBasicInfo, WxMall mall) { + WxMsgRecord wxMsgRecord = new WxMsgRecord(); + wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode()); + wxMsgRecord.setReceiver(cUserBasicInfo.getPhone()); + wxMsgRecord.setTenantId(cUserBasicInfo.getTenantId()); + Map dynamicContentMap = new HashMap<>(); + WxScoreRules rules = wxScoreRulesService.getScoreRules(cUserBasicInfo.getTenantId()); + if (Objects.nonNull(rules) && Objects.nonNull(rules.getScale())) { + int birthdayScale = rules.getScale(); + float creditScale = new BigDecimal(birthdayScale).divide(new BigDecimal(WxScoreRules.DEFAULT_SCALE)).floatValue(); + dynamicContentMap.put("creditScale", String.valueOf(creditScale)); + wxMsgRecord.setModelType(EnumMsgModel.COUPON_BIRTHDAY_OPENED.getCode()); + } else { + //没有配置积分规则 + wxMsgRecord.setModelType(EnumMsgModel.COUPON_BIRTHDAY_OPENED_NO.getCode()); + } + dynamicContentMap.put("userName", cUserBasicInfo.getNickName()); + dynamicContentMap.put("mallName", mall.getName()); + wxMsgRecord.setDynamicContentMap(dynamicContentMap); + mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode()); + } } \ No newline at end of file diff --git a/mallinkService/src/main/java/com/iformall/common/ErrorCode.java b/mallinkService/src/main/java/com/iformall/common/ErrorCode.java index 94228d3ed..f7a11166c 100644 --- a/mallinkService/src/main/java/com/iformall/common/ErrorCode.java +++ b/mallinkService/src/main/java/com/iformall/common/ErrorCode.java @@ -133,6 +133,7 @@ public enum ErrorCode{ COUPON_ONLY_FOR_NEW_MEMBER(2044, "您已经参加过新会员活动"), COUPON_ALREADY_IN_NEW_MEMBER(2045, "您正在参加别的新会员活动"), + COUPON_VALID_NOT_SUPPORTED(2046, "券类型不支持"), /** * 车流 2040 */ diff --git a/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java b/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java index 04730bb92..cb0d37953 100644 --- a/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java +++ b/mallinkService/src/main/java/com/iformall/domain/po/WxCouponSend.java @@ -15,6 +15,12 @@ import java.util.List; @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) public class WxCouponSend extends BaseEntity { + + @Transient + public static final String KEY_COUPONIDS = "couponIds" ; + @Transient + public static final String KEY_BEFOREDAYS = "beforeDays" ; + @Id protected Long id; diff --git a/mallinkService/src/main/java/com/iformall/enums/EnumMsgModel.java b/mallinkService/src/main/java/com/iformall/enums/EnumMsgModel.java index 65f7f97ca..f679a6f7a 100644 --- a/mallinkService/src/main/java/com/iformall/enums/EnumMsgModel.java +++ b/mallinkService/src/main/java/com/iformall/enums/EnumMsgModel.java @@ -30,6 +30,10 @@ public enum EnumMsgModel { FLOW_C_APPLY_NODIFY(20, "审批通知"), FLOW_C_PASS_NODIFY(21, "通过审批通知"), FLOW_C_REJECT_NODIFY(22, "驳回通知"), + + //会员生日券 + COUPON_BIRTHDAY_OPENED(28, "会员生日券开启"), + COUPON_BIRTHDAY_OPENED_NO(29, "会员生日券未开启"), ; public static EnumMsgModel getEnum(Integer code) { diff --git a/mallinkService/src/main/java/com/iformall/mapper/WxCouponSendMapper.java b/mallinkService/src/main/java/com/iformall/mapper/WxCouponSendMapper.java index 5673bf4bc..51649b3f8 100644 --- a/mallinkService/src/main/java/com/iformall/mapper/WxCouponSendMapper.java +++ b/mallinkService/src/main/java/com/iformall/mapper/WxCouponSendMapper.java @@ -12,5 +12,7 @@ public interface WxCouponSendMapper extends CommonMapper { void updateStatusByCouponId(WxCouponSend wxCouponSend); void offExpiriedCouponSendByCouponStatus(); - + + void updateBirthdayConfig(WxCouponSend wxCouponSend); + } diff --git a/mallinkService/src/main/java/com/iformall/service/WxCouponSendService.java b/mallinkService/src/main/java/com/iformall/service/WxCouponSendService.java index 29736170b..eed2e76e2 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxCouponSendService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxCouponSendService.java @@ -5,6 +5,8 @@ import com.iformall.domain.po.WxCouponSend; import com.iformall.domain.vo.WxCouponSendVo; import com.iformall.enums.EnumCouponSendSendType; +import javax.validation.constraints.NotNull; + public interface WxCouponSendService { /** @@ -16,7 +18,7 @@ public interface WxCouponSendService { * @return */ PageInfo listAsPage(WxCouponSend record, Integer pageIndex, Integer pageSize); - + /** * 根据Id获得实体 * @@ -48,12 +50,25 @@ public interface WxCouponSendService { boolean sendCouponToUser(EnumCouponSendSendType type, Object param); void updateStatusByCouponId(Long couponId,String tenantId, int status); - - - - - + /** + * 添加或更新生日券配置 + * @param beforeDays + * @param sendType + * @param tenantId + */ + void saveOrUpdateConfig(Integer beforeDays, Integer sendType, String tenantId) ; + /** + * 获取生日券配置 + * @param sendType + * @param tenantId + * @return + */ + WxCouponSend getConfig(Integer sendType,String tenantId) ; + + + + } diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java index 1d0a2619b..d9012e612 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxCouponSendServiceImpl.java @@ -1,5 +1,6 @@ package com.iformall.service.impl; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; @@ -16,6 +17,7 @@ import com.iformall.mapper.*; import com.iformall.mq.MqBaseProducer; import com.iformall.service.*; import com.iformall.utils.DateUtils; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,14 +74,18 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { @Override public void saveOrUpdate(WxCouponSend record) { + if(Objects.equals(record.getSendType(),EnumCouponSendSendType.BIRTHDAY.getCode())) { + WxCouponSend config = getConfig(record.getSendType(),record.getTenantId()) ; + if(Objects.nonNull(config)) { + record.setConditions(config.getConditions()); + } + } if (record.getId() == null) { //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); final IdWorker idWorker = IdWorker.get(); record.setId(idWorker.nextId()); - record = buildConditions(record); wxCouponSendMapper.insertSelective(record); } else { - record = buildConditions(record); wxCouponSendMapper.updateByPrimaryKeySelective(record); } } @@ -91,15 +97,44 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { * @return */ private WxCouponSend buildConditions(WxCouponSend record) { - if (Objects.equals(record.getSendType(), EnumCouponSendSendType.BIRTHDAY.getCode())) { - if (Objects.isNull(record.getTitle())) { - record.setTitle(""); - } - if (Objects.isNull(record.getCouponId())) { - record.setCouponId(0L); - } + if (!Objects.equals(record.getSendType(), EnumCouponSendSendType.BIRTHDAY.getCode())) { return record; } + + //添加会员生日券 + JSONObject conditions = JSONObject.parseObject(record.getConditions()); + Long couponId = record.getCouponId(); + if (Objects.nonNull(couponId)) { + JSONArray couponIds; + if (Objects.isNull(record.getId())) { + //添加默认值 + if (Objects.isNull(record.getTitle())) { + record.setTitle(""); + } + if (Objects.isNull(record.getCouponId())) { + record.setCouponId(0L); + } + //添加新卡券 + couponIds = new JSONArray(); + couponIds.add(couponId); + } else { + //更新卡券列表 + WxCouponSend couponSend = wxCouponSendMapper.selectByPrimaryKey(record.getId()); + conditions = JSONObject.parseObject(couponSend.getConditions()); + couponIds = conditions.getJSONArray(WxCouponSend.KEY_COUPONIDS); + if (Objects.isNull(couponIds)) { + couponIds = new JSONArray(); + couponIds.add(couponId); + } else { + //添加新卡券 + if (!couponIds.contains(couponId)) { + couponIds.add(couponId); + } + } + } + conditions.put(WxCouponSend.KEY_COUPONIDS, couponIds); + } + record.setConditions(conditions.toJSONString()); return record; } @@ -110,8 +145,8 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { private boolean isConditionAllowed(WxCouponSend wxCouponSend, Object param) { - if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.C_ORDER.getCode())){ - WxOrder wxOrder = (WxOrder)param; + if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.C_ORDER.getCode())) { + WxOrder wxOrder = (WxOrder) param; try { JSONObject jo = JSONObject.parseObject(wxCouponSend.getConditions()); @@ -122,12 +157,12 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { } return false; - } else if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.CAR_STOP.getCode())){ + } else if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.CAR_STOP.getCode())) { try { JSONObject jo = JSONObject.parseObject(wxCouponSend.getConditions()); - if (DateUtils.isInDate(new Date(), jo.getString("startTime"), jo.getString("endTime"))){ + if (DateUtils.isInDate(new Date(), jo.getString("startTime"), jo.getString("endTime"))) { return true; } @@ -136,15 +171,15 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { } return false; - }else if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.B_MICROPAY.getCode())){ + } else if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.B_MICROPAY.getCode())) { - WxOrder wxOrder = (WxOrder)param; + WxOrder wxOrder = (WxOrder) param; try { JSONObject jo = JSONObject.parseObject(wxCouponSend.getConditions()); WxMerchantBUser wxMerchantBUser = wxMerchantBUserMapper.selectByPrimaryKey(wxOrder.getProductId()); - if (wxMerchantBUser.getMerchantId()!= null + if (wxMerchantBUser.getMerchantId() != null && wxMerchantBUser.getMerchantId().equals(jo.getLong("id"))) { return true; } @@ -153,8 +188,8 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { } return false; - }else if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.COUPON_VERIFY.getCode())){ - WxOrder wxOrder = (WxOrder)param; + } else if (wxCouponSend.getSendType().equals(EnumCouponSendSendType.COUPON_VERIFY.getCode())) { + WxOrder wxOrder = (WxOrder) param; try { JSONObject jo = JSONObject.parseObject(wxCouponSend.getConditions()); @@ -166,7 +201,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { } return false; - }else { + } else { return false; } @@ -182,7 +217,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { case CAR_STOP: if (!param.getClass().equals(WxCUserCar.class)) return false; - WxCUserCar wxCUserCar= (WxCUserCar)param; + WxCUserCar wxCUserCar = (WxCUserCar) param; cUserId = wxCUserCar.getCUserId(); tenantId = wxCUserCar.getTenantId(); break; @@ -191,7 +226,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { case C_ORDER: if (!param.getClass().equals(WxOrder.class)) return false; - WxOrder order = (WxOrder)param; + WxOrder order = (WxOrder) param; cUserId = order.getCUserId(); tenantId = order.getTenantId(); break; @@ -243,7 +278,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { } //检查条件 - if (!isConditionAllowed(send,param)) + if (!isConditionAllowed(send, param)) continue; // 发放免费券 @@ -257,12 +292,12 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { // 查找cUser WxCUser user = wxCUserMapper.selectByPrimaryKey(cUserId); - if(user == null) { + if (user == null) { continue; } // 发送消息 - if(couponOrder != null) { + if (couponOrder != null) { sendMsgForSendCoupon(tenantId, send, couponOrder, user); } @@ -317,7 +352,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String gotopage = "pages/index/index?type=mc"; // 7. 公众号消息 - if(StringUtils.isNotBlank(user.getMpAppId()) && + if (StringUtils.isNotBlank(user.getMpAppId()) && StringUtils.isNotBlank(user.getMpOpenId()) && user.getMpSubscribe().equals(EnumUserIsSubscribe.YES.getCode())) { // 检查是否超限 @@ -325,7 +360,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { msgLimit.setTenantId(user.getTenantId()); msgLimit.setType(EnumMsgLimitType.WXCHAT_OPENID_SEND.getCode()); msgLimit.setLimitId(user.getMpOpenId()); - if(wxMsgLimitService.checkIsLimit(msgLimit)) { + if (wxMsgLimitService.checkIsLimit(msgLimit)) { logger.error("用户公众号模板消息超限" + user.getId()); return true; } @@ -361,7 +396,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { )); try { - mqBaseProducer.sendMessage(mpAppMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(),EnumMsgMqKey.DEFAULT.getCode()); + mqBaseProducer.sendMessage(mpAppMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode()); } catch (Exception e) { logger.error("购票成功-公众号-模板消息发送失败: " + e.getMessage()); //throw new MallinkException(ErrorCode.MP_TEMPLATE_SEND_FAILED); @@ -374,7 +409,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { msgLimit.setTenantId(user.getTenantId()); msgLimit.setType(EnumMsgLimitType.WEAPP_OPENID_SEND.getCode()); msgLimit.setLimitId(user.getOpenId()); - if(wxMsgLimitService.checkIsLimit(msgLimit)) { + if (wxMsgLimitService.checkIsLimit(msgLimit)) { logger.error("用户小程序统一模板消息超限:" + user.getId()); return true; } @@ -383,7 +418,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { authQ.setAuthorizerAppid(user.getAppId()); authQ.setTenantId(user.getTenantId()); WxAuthorizerInfo weappInfo = authorizerInfoMapper.findMp(authQ); - if(weappInfo != null) { + if (weappInfo != null) { AppUniformMsg appUniformMsg = new AppUniformMsg(); appUniformMsg.setMsgType(EnumMsgRecordType.SMART_APP_UNIFORM.getCode()); appUniformMsg.setAppId(user.getAppId()); @@ -401,7 +436,7 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { user.getNickName() )); try { - mqBaseProducer.sendMessage(appUniformMsg, EnumMsgMqTopic.DEFAULT.getCode(),EnumMsgMqTag.DEFAULT.getCode(),EnumMsgMqKey.DEFAULT.getCode()); + mqBaseProducer.sendMessage(appUniformMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode()); } catch (Exception e) { logger.error("核销-公众号-模板消息发送失败: " + e.getMessage()); //throw new MallinkException(ErrorCode.UNIFORM_SEND_FAILED); @@ -420,4 +455,52 @@ public class WxCouponSendServiceImpl implements WxCouponSendService { wxCouponSend.setUpdateDate(new Date()); wxCouponSendMapper.updateStatusByCouponId(wxCouponSend); } + + @Override + public void saveOrUpdateConfig(Integer beforeDays, Integer sendType, String tenantId) { + WxCouponSend couponSendQuery = new WxCouponSend(); + couponSendQuery.setSendType(sendType); + couponSendQuery.setTenantId(tenantId); + List wxCouponSendList = wxCouponSendMapper.select(couponSendQuery); + if (CollectionUtils.isEmpty(wxCouponSendList)) { + WxCouponSend couponSend = new WxCouponSend(); + couponSend.setTitle(EnumCouponSendSendType.BIRTHDAY.getMessage()+"配置信息 "); + couponSend.setCouponId(0L); + couponSend.setSendType(sendType); + final IdWorker idWorker = IdWorker.get(); + couponSend.setId(idWorker.nextId()); + couponSend.setTenantId(tenantId); + couponSend.setStatus(EnumCouponSendStatus.INVALID.getCode()); + couponSend.setCreateDate(new Date()); + JSONObject couponSendJo = new JSONObject(); + couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays); + couponSend.setConditions(couponSendJo.toJSONString()); + wxCouponSendMapper.insertSelective(couponSend); + } else { + WxCouponSend couponSend = wxCouponSendList.get(0); + JSONObject couponSendJo = JSONObject.parseObject(couponSend.getConditions()); + if(Objects.isNull(couponSendJo)) { + couponSendJo = new JSONObject(); + } + couponSendJo.put(WxCouponSend.KEY_BEFOREDAYS, beforeDays); + couponSend.setConditions(couponSendJo.toJSONString()); + couponSend.setUpdateDate(new Date()); + wxCouponSendMapper.updateBirthdayConfig(couponSend); + } + } + + @Override + public WxCouponSend getConfig(Integer sendType,String tenantId) { + WxCouponSend couponSendQuery = new WxCouponSend(); + couponSendQuery.setSendType(sendType); + couponSendQuery.setTenantId(tenantId); + couponSendQuery.setCouponId(0L); + //读取会员生日券配置信息 + couponSendQuery.setStatus(EnumCouponSendStatus.INVALID.getCode()); + List wxCouponSendList = wxCouponSendMapper.select(couponSendQuery); + if (CollectionUtils.isEmpty(wxCouponSendList)) { + return null; + } + return wxCouponSendList.get(0); + } } diff --git a/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml b/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml index ed6e2d3a4..c3fc8750b 100644 --- a/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml +++ b/mallinkService/src/main/resources/mapper/WxCouponSendMapper.xml @@ -103,6 +103,13 @@ + + update wx_coupon_send + set conditions=#{conditions},update_date = #{updateDate} + where send_type = #{sendType} + and tenant_id = #{tenantId} + + update wx_coupon_send cs, wx_coupon c set cs.status=1, cs.update_date = now()