|
|
|
@@ -5,16 +5,18 @@ import com.github.pagehelper.PageInfo; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.IdWorker; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.domain.po.WxAppinfo; |
|
|
|
import com.iformall.domain.po.WxCoupon; |
|
|
|
import com.iformall.domain.po.WxCouponPassword; |
|
|
|
import com.iformall.domain.po.WxCouponPresent; |
|
|
|
import com.iformall.enums.EnumCouponPasswordStatus; |
|
|
|
import com.iformall.enums.EnumCouponStatus; |
|
|
|
import com.iformall.enums.EnumCouponValidType; |
|
|
|
import com.iformall.domain.po.msg.WxMsgRecord; |
|
|
|
import com.iformall.enums.*; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
import com.iformall.mapper.WxCouponPasswordMapper; |
|
|
|
import com.iformall.mapper.WxCouponPresentMapper; |
|
|
|
import com.iformall.mq.MqBaseProducer; |
|
|
|
import com.iformall.service.PushLimitService; |
|
|
|
import com.iformall.service.WxAppinfoService; |
|
|
|
import com.iformall.service.WxCouponPresentService; |
|
|
|
import com.iformall.service.WxCouponService; |
|
|
|
import org.slf4j.Logger; |
|
|
|
@@ -22,11 +24,7 @@ import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author gongbiao |
|
|
|
@@ -47,6 +45,12 @@ public class WxCouponPresentServiceImpl implements WxCouponPresentService { |
|
|
|
@Autowired |
|
|
|
WxCouponPasswordMapper wxCouponPasswordMapper; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
WxAppinfoService wxAppinfoService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private MqBaseProducer mqBaseProducer; |
|
|
|
|
|
|
|
@Override |
|
|
|
public PageInfo<WxCouponPresent> listAsPage(WxCouponPresent record, Integer pageIndex, Integer pageSize) { |
|
|
|
return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCouponPresentMapper.findList(record)); |
|
|
|
@@ -59,13 +63,13 @@ public class WxCouponPresentServiceImpl implements WxCouponPresentService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public ResultData add(WxCouponPresent record) { |
|
|
|
String tenantId = record.getTenantId(); |
|
|
|
try { |
|
|
|
pushLimitService.checkSendTime(record.getTenantId()); |
|
|
|
pushLimitService.checkSendTime(tenantId); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
return new ResultData(e.getErrorCode(), e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
WxCoupon wxCoupon = wxCouponService.getById(record.getCouponId()); |
|
|
|
if (wxCoupon.getValidType().equals(EnumCouponValidType.BETWEEN_TWO_TIME.getCode())) { //时间范围 |
|
|
|
if (new Date().after(wxCoupon.getValidEndDate())) { |
|
|
|
@@ -75,32 +79,69 @@ public class WxCouponPresentServiceImpl implements WxCouponPresentService { |
|
|
|
if (!wxCoupon.getStatus().equals(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode())) { |
|
|
|
return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF); |
|
|
|
} |
|
|
|
|
|
|
|
//获取手机号 |
|
|
|
record.setCouponName(wxCoupon.getTitle()); |
|
|
|
|
|
|
|
String phones = record.getPhones(); |
|
|
|
String[] phoneSplit = phones.split(","); |
|
|
|
Set<String> phoneSetTemp = new HashSet<>(Arrays.asList(phoneSplit)); |
|
|
|
Set<String> phoneSet = phoneSetTemp.stream().filter(p -> !p.isEmpty()).collect(Collectors.toSet()); |
|
|
|
Set<String> phoneSet = new HashSet<>(phoneSplit.length); |
|
|
|
for (String phone : phoneSplit) { |
|
|
|
if (!phone.isEmpty()) { |
|
|
|
phoneSet.add(phone); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//查询有效券数量 |
|
|
|
WxCouponPassword wxCouponPassword = new WxCouponPassword(); |
|
|
|
wxCouponPassword.setTenantId(record.getTenantId()); |
|
|
|
wxCouponPassword.setTenantId(tenantId); |
|
|
|
wxCouponPassword.setCouponId(record.getCouponId()); |
|
|
|
wxCouponPassword.setStatus(EnumCouponPasswordStatus.INIT.getCode()); |
|
|
|
int inventory = wxCouponPasswordMapper.selectCount(wxCouponPassword); |
|
|
|
if (phoneSet.size() > inventory) { |
|
|
|
List<WxCouponPassword> list = wxCouponPasswordMapper.findList(wxCouponPassword); |
|
|
|
if (phoneSet.size() > list.size()) { |
|
|
|
return new ResultData(ErrorCode.REMAIN_IS_EMPTY); |
|
|
|
} |
|
|
|
|
|
|
|
//添加记录 |
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
record.setId(idWorker.nextId()); |
|
|
|
|
|
|
|
record.setCouponName(wxCoupon.getTitle()); |
|
|
|
record.setSendAmount(phoneSet.size()); |
|
|
|
|
|
|
|
wxCouponPresentMapper.insertSelective(record); |
|
|
|
//发送短信 |
|
|
|
//小程序 |
|
|
|
WxAppinfo appInfo = wxAppinfoService.getCAppInfo(tenantId); |
|
|
|
String priceStr = wxCoupon.getPriceStr(); |
|
|
|
String name = appInfo.getName(); |
|
|
|
List<WxCouponPassword> tempList = wxCouponPasswordMapper.findList(wxCouponPassword); |
|
|
|
//循环发送 |
|
|
|
int i = 0; |
|
|
|
for (String phone : phoneSet) { |
|
|
|
WxCouponPassword couponPassword = list.get(i); |
|
|
|
//发送 |
|
|
|
sendMsg(tenantId, phone, priceStr, name, couponPassword.getPassword()); |
|
|
|
//记录发送数据便于更新状态 |
|
|
|
tempList.add(couponPassword); |
|
|
|
i++; |
|
|
|
} |
|
|
|
//修改状态 |
|
|
|
wxCouponPasswordMapper.updateStatus(tempList); |
|
|
|
return new ResultData(); |
|
|
|
} |
|
|
|
|
|
|
|
private void sendMsg(String tenantId, String phone, String money, String appName, String password) { |
|
|
|
logger.info("》》》》》》》》》》》"); |
|
|
|
logger.info("系统发卡发送短信开始"); |
|
|
|
logger.info("》》》》》》》》》》》"); |
|
|
|
Map<String, String> msgReplaceMap = new HashMap(); |
|
|
|
msgReplaceMap.put("app", appName); |
|
|
|
msgReplaceMap.put("money", money); |
|
|
|
msgReplaceMap.put("password", password); |
|
|
|
WxMsgRecord wxMsgRecord = new WxMsgRecord(); |
|
|
|
wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode()); |
|
|
|
wxMsgRecord.setModelType(EnumMsgModel.COUPON_CARD_SEND.getCode()); |
|
|
|
wxMsgRecord.setReceiver(phone); |
|
|
|
wxMsgRecord.setTenantId(tenantId); |
|
|
|
wxMsgRecord.setDynamicContentMap(msgReplaceMap); |
|
|
|
mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode()); |
|
|
|
logger.info("》》》》》》》》》》》"); |
|
|
|
logger.info("系统发卡发送短信结束"); |
|
|
|
logger.info("》》》》》》》》》》》"); |
|
|
|
} |
|
|
|
} |