|
|
|
@@ -5,6 +5,7 @@ import com.github.pagehelper.PageHelper; |
|
|
|
import com.github.pagehelper.PageInfo; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.common.IdWorker; |
|
|
|
import com.iformall.common.Result; |
|
|
|
import com.iformall.common.ResultData; |
|
|
|
import com.iformall.domain.po.*; |
|
|
|
import com.iformall.enums.*; |
|
|
|
@@ -80,6 +81,13 @@ public class CouponInjectServiceImpl implements CouponInjectService { |
|
|
|
@Override |
|
|
|
public ResultData add(CouponInject record) { |
|
|
|
|
|
|
|
try { |
|
|
|
pushLimitService.checkSendTime(record.getTenantId()); |
|
|
|
} 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())) { |
|
|
|
@@ -125,34 +133,30 @@ public class CouponInjectServiceImpl implements CouponInjectService { |
|
|
|
record.setSendAmount(cUsers.size()); |
|
|
|
couponInjectMapper.insertSelective(record); |
|
|
|
if(record.getSendType().equals(EnumCouponInjectSendType.IMMEDIATE.getCode())) { |
|
|
|
try { |
|
|
|
sendNow(wxCoupon, cUsers, record); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
if (e.getErrorCode() == ErrorCode.PUSH_LIMIT_NOT_INRANG.getCode()) { |
|
|
|
return new ResultData(ErrorCode.PUSH_LIMIT_NOT_INRANG); |
|
|
|
} |
|
|
|
|
|
|
|
List<WxCUser> sentUsers = sendCoupon(wxCoupon, cUsers, record); |
|
|
|
if (sentUsers.size() > 0) { |
|
|
|
record.setStatus(EnumCouponInjectStatus.HAS_SENT.getCode()); |
|
|
|
ResultData msgSendRes = sendMsg(record, sentUsers); |
|
|
|
if (msgSendRes.code == Result.SUCCESS) |
|
|
|
record.setMsgId((Long)msgSendRes.data); |
|
|
|
} else { |
|
|
|
record.setStatus(EnumCouponInjectStatus.SEND_FAILED.getCode()); |
|
|
|
} |
|
|
|
record.setSendAmount(sentUsers.size()); |
|
|
|
|
|
|
|
couponInjectMapper.updateByPrimaryKeySelective(record); |
|
|
|
} |
|
|
|
|
|
|
|
return new ResultData(); |
|
|
|
} |
|
|
|
|
|
|
|
private void sendMsg(CouponInject record, List<WxCUser> cUsers) { |
|
|
|
boolean checkMsg = false; |
|
|
|
try { |
|
|
|
checkMsg = pushLimitService.checkMsg(record.getTenantId()); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
} |
|
|
|
if (!checkMsg) { |
|
|
|
return; |
|
|
|
} |
|
|
|
private ResultData sendMsg(CouponInject record, List<WxCUser> cUsers) { |
|
|
|
|
|
|
|
//根据模板ID查询短信信息 |
|
|
|
WxMsgModel model = wxMsgModelService.getById(record.getModelId()); |
|
|
|
if(model==null){ |
|
|
|
return; |
|
|
|
return new ResultData(ErrorCode.MSG_TEMPLATE_NOT_FOUND); |
|
|
|
} |
|
|
|
|
|
|
|
WxMsg wxmsg=new WxMsg(); |
|
|
|
@@ -161,26 +165,21 @@ public class CouponInjectServiceImpl implements CouponInjectService { |
|
|
|
wxMsgConfig.setTenantId(wxmsg.getTenantId()); |
|
|
|
wxMsgConfig = wxMsgConfigService.findObject(wxMsgConfig); |
|
|
|
if (wxMsgConfig==null){ |
|
|
|
logger.info("您还未接入短信运营商,请联系平台管理员"); |
|
|
|
return; |
|
|
|
return new ResultData(ErrorCode.MSG_SERVER_NOT_FIND); |
|
|
|
} |
|
|
|
|
|
|
|
if(wxMsgConfig.getRemains()==0){ |
|
|
|
logger.info("短信数量为0"); |
|
|
|
return; |
|
|
|
return new ResultData(ErrorCode.MSG_SUM_ZERO); |
|
|
|
} |
|
|
|
|
|
|
|
wxmsg.setExpectSendNumber(cUsers.size());//预计发送数量 |
|
|
|
if(wxMsgConfig.getRemains()<cUsers.size()){ |
|
|
|
logger.info("短信数量不足"); |
|
|
|
return; |
|
|
|
|
|
|
|
return new ResultData(ErrorCode.MSG_SUM_INSUFFICENT); |
|
|
|
} |
|
|
|
|
|
|
|
//获取手机号 |
|
|
|
if(cUsers.size()==0){ |
|
|
|
logger.info("没有要发送的手机号"); |
|
|
|
return; |
|
|
|
return new ResultData(ErrorCode.MSG_NO_VALID_PHONE); |
|
|
|
} |
|
|
|
StringBuffer sb=new StringBuffer(); |
|
|
|
for(WxCUser user:cUsers){ |
|
|
|
@@ -191,19 +190,13 @@ public class CouponInjectServiceImpl implements CouponInjectService { |
|
|
|
wxmsg.setPhones(sb.deleteCharAt(sb.length()-1).toString()); |
|
|
|
wxmsg.setLabel(""); |
|
|
|
if(wxmsg.getPhones().equals("")){ |
|
|
|
logger.info("手机号未发现"); |
|
|
|
return; |
|
|
|
return new ResultData(ErrorCode.MSG_NO_VALID_PHONE); |
|
|
|
} |
|
|
|
if(record.getSendType().equals(EnumCouponInjectSendType.TIMING.getCode())){ |
|
|
|
wxmsg.setIsright(EnumMsgSend.MSG_SEND_NOT_IMMEDIATELY.getCode());//值是相反的 |
|
|
|
wxmsg.setSendtime(DateUtils.date2String(record.getSendTime(),"yyyy-MM-dd HH:mm:ss")); |
|
|
|
wxmsg.setStatus(EnumMsgStatus.MSG_STATUS_NOT_SEND.getCode()); |
|
|
|
}else{ |
|
|
|
wxmsg.setIsright(EnumMsgSend.MSG_SEND_IMMEDIATELY.getCode());//立即发送 |
|
|
|
wxmsg.setStatus(EnumMsgStatus.MSG_STATUS_SENDED.getCode()); |
|
|
|
wxmsg.setSendtime(com.iformall.utils.DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss")); |
|
|
|
|
|
|
|
} |
|
|
|
wxmsg.setIsright(EnumMsgSend.MSG_SEND_IMMEDIATELY.getCode());//立即发送 |
|
|
|
wxmsg.setStatus(EnumMsgStatus.MSG_STATUS_SENDED.getCode()); |
|
|
|
wxmsg.setSendtime(com.iformall.utils.DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss")); |
|
|
|
|
|
|
|
wxmsg.setModelId(record.getModelId()); |
|
|
|
wxmsg.setMsg(model.getContent()); |
|
|
|
wxmsg.setName(model.getName()); |
|
|
|
@@ -211,37 +204,20 @@ public class CouponInjectServiceImpl implements CouponInjectService { |
|
|
|
wxmsg.setSuccessNumber(0); |
|
|
|
wxmsg.setErrorNumber(0); |
|
|
|
wxmsg.setWay(EnumSendWay.COUPON.getCode());//1、标签短信2、精准发券 |
|
|
|
ResultData resultData = wxMsgService.saveOrUpdate(wxmsg); |
|
|
|
if(resultData.data!=null){ |
|
|
|
//更新MSG_ID |
|
|
|
record.setMsgId((Long)resultData.data); |
|
|
|
couponInjectMapper.updateByPrimaryKeySelective(record); |
|
|
|
} |
|
|
|
return wxMsgService.saveOrUpdate(wxmsg); |
|
|
|
} |
|
|
|
|
|
|
|
private void sendNow(WxCoupon wxCoupon,List<WxCUser> cUsers,CouponInject record){ |
|
|
|
boolean checkTime = false; |
|
|
|
try { |
|
|
|
checkTime = pushLimitService.checkSendTime(wxCoupon.getTenantId()); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
throw new MallinkException(e.getErrorCode(), e.getMessage()); |
|
|
|
} |
|
|
|
if (checkTime) { |
|
|
|
//查询标签用户 |
|
|
|
int sendRealAmount=0; |
|
|
|
List<WxCUser> sentUsers = new ArrayList<WxCUser>(); |
|
|
|
for (WxCUser tempCUser : cUsers) { |
|
|
|
boolean bResult = sendCouponToUser(tempCUser,wxCoupon,record.getId()); |
|
|
|
if (bResult) { |
|
|
|
sentUsers.add(tempCUser); |
|
|
|
sendRealAmount += 1; |
|
|
|
} |
|
|
|
} |
|
|
|
private List<WxCUser> sendCoupon(WxCoupon wxCoupon,List<WxCUser> cUsers,CouponInject record){ |
|
|
|
|
|
|
|
//发送短信 |
|
|
|
sendMsg(record,sentUsers); |
|
|
|
//查询标签用户 |
|
|
|
List<WxCUser> sentUsers = new ArrayList<WxCUser>(); |
|
|
|
for (WxCUser tempCUser : cUsers) { |
|
|
|
boolean bResult = sendCouponToUser(tempCUser,wxCoupon,record.getId()); |
|
|
|
if (bResult) { |
|
|
|
sentUsers.add(tempCUser); |
|
|
|
} |
|
|
|
} |
|
|
|
return sentUsers; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@@ -257,7 +233,12 @@ public class CouponInjectServiceImpl implements CouponInjectService { |
|
|
|
return false; |
|
|
|
} |
|
|
|
// 发放免费券 |
|
|
|
WxCouponOrder couponOrder = wxOrderService.sendFreeCouponToUser(tempCUser.getId(), wxCoupon.getId()); |
|
|
|
WxCouponOrder couponOrder = null; |
|
|
|
try { |
|
|
|
couponOrder = wxOrderService.sendFreeCouponToUser(tempCUser.getId(), wxCoupon.getId()); |
|
|
|
} catch (MallinkException e) { |
|
|
|
logger.error(e.getMessage()); |
|
|
|
} |
|
|
|
if (couponOrder != null) { |
|
|
|
wxCouponActionLogService.addOne(tempCUser.getTenantId(), wxCoupon.getId(), couponOrder.getId(), EnumCouponSendSendType.INJECT.getCode(), couponInjectId); |
|
|
|
} |
|
|
|
|