rx
2024-01-09 11c481fb398685a8a73d33bcbec4c9f984c5d899
提交 | 用户 | age
a2688a 1 package com.hx.phip.service.market.impl;
F 2
3 import com.hx.common.service.CommonService;
4 import com.hx.exception.TipsException;
026734 5 import com.hx.guide.util.CommonQueryUtil;
496a49 6 import com.hx.guide.util.mp.WeiXinCorpMpUtil;
a2688a 7 import com.hx.mybatisTool.SqlSentence;
F 8 import com.hx.phiappt.common.OperatorConstants;
9 import com.hx.phiappt.constants.tool.CouponTool;
496a49 10 import com.hx.phiappt.constants.tool.SendMessageTool;
f931ff 11 import com.hx.phiappt.constants.tool.employee.EmployeeTool;
496a49 12 import com.hx.phiappt.constants.tool.model.MessageTemplate;
f931ff 13 import com.hx.phiappt.constants.tool.shop.ShopTool;
F 14 import com.hx.phiappt.dao.mapper.*;
a2688a 15 import com.hx.phiappt.model.BaseEntity;
496a49 16 import com.hx.phiappt.model.Employee;
R 17 import com.hx.phiappt.model.Shop;
a2688a 18 import com.hx.phiappt.model.User;
F 19 import com.hx.phiappt.model.coupon.Coupon;
20 import com.hx.phiappt.model.coupon.CouponReleaseRecordItem;
21 import com.hx.phiappt.model.market.*;
22 import com.hx.phip.config.GlobalExceptionHandler;
026734 23 import com.hx.phip.config.ProcessBean;
a2688a 24 import com.hx.phip.service.market.MarketActivityService;
026734 25 import com.hx.resultTool.Result;
a2688a 26 import com.hx.util.DateUtil;
d6af92 27 import com.hz.his.dto.marketing.common.*;
026734 28 import com.hz.his.feign.service.marketing.MCommonService;
a2688a 29 import lombok.extern.slf4j.Slf4j;
F 30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONObject;
32 import org.apache.commons.lang.StringUtils;
33 import org.springframework.stereotype.Service;
34 import org.springframework.transaction.annotation.Transactional;
496a49 35 import com.hx.phip.config.CustomParameter;
a2688a 36 import javax.annotation.Resource;
F 37 import java.util.*;
38
39
40 @Slf4j
41 @Transactional
42 @Service
43 public class MarketActivityServiceImpl implements MarketActivityService {
44
45     @Resource
46     private CommonService commonService;
026734 47     @Resource
F 48     private MCommonService mCommonService;
49     @Resource
50     private ProcessBean processBean;
496a49 51     @Resource
R 52     private CustomParameter customParameter;
a2688a 53
026734 54     /** 领取优惠券 */
a2688a 55     @Override
F 56     public void receiveCoupon(MarketActivity marketActivity, List<MarketActivityItem> list, User user
57             , MarketActivityJoinRecord joinRecord, MarketActivityReceiveRecord receiveRecord) {
58
59         //优惠券操作人类型,默认员工
60         int couponOpType = OperatorConstants.changeCouponOpType(receiveRecord.getOpType());
61
62         //拼接用户数据
63         JSONArray userArr = new JSONArray();
64         JSONObject json = new JSONObject();
65         json.put("id", user.getId());
66         json.put("amount", 1);
67         userArr.add(json);
68
69         SqlSentence sqlSentence = new SqlSentence();
70         Map<String, Object> values = new HashMap<>();
71         sqlSentence.setM(values);
72
73         //领取记录子项list
733bd8 74         MarketActivityReceiveRecordItem recordItem = null;
a2688a 75         List<MarketActivityReceiveRecordItem> recordItemList = new ArrayList<>();
F 76
77         //遍历领取
496a49 78         String couponName = "";
d5e9d0 79         Integer isApprove = BaseEntity.NO;
026734 80         for(MarketActivityItem activityCoupon : list)
F 81         {
82             //新增
733bd8 83             recordItem = new MarketActivityReceiveRecordItem(activityCoupon);
F 84             recordItemList.add(recordItem);
11c481 85             //获取优惠券名称
R 86             couponName = activityCoupon.getCommonName();
026734 87             //判断是否需要审批,如果需要审批先跳过不发放优惠券
F 88             if(activityCoupon.getIsApprove() == BaseEntity.YES){
1ac50b 89                 isApprove = activityCoupon.getIsApprove();
026734 90                 continue;
F 91             }
733bd8 92
026734 93             //发放优惠券
a2688a 94             try{
733bd8 95                 sendCoupon(couponOpType, userArr, receiveRecord, recordItem);
a2688a 96             }catch (Exception e){
F 97                 log.error("参数:{}", JSONObject.fromObject(activityCoupon));
98                 log.error("遍历发放优惠券失败:{}", GlobalExceptionHandler.getExceptionInformation(e));
99                 //统一提示
100                 throw new TipsException("活动爆满【"+activityCoupon.getCommonName()+"】已领取完,请联系活动负责人"+marketActivity.getManagerName()+"咨询情况");
101             }
102
103         }
104
105         //处理参与记录--------------------------------------------------------------------------------
106         if(StringUtils.isEmpty(joinRecord.getId())){
107             if(commonService.insert(MarketActivityJoinRecordMapper.class, joinRecord) != 1){
108                 throw new TipsException("新增活动参与记录失败!");
109             }
110         }else{
111             values.clear();
112             values.put("id", joinRecord.getId());
113             values.put("couponNum", list.size());
733bd8 114             sqlSentence.setSqlSentence(" couponNum = couponNum + #{m.couponNum}, receiveNum = receiveNum + 1, editTime = now() where id = #{m.id} ");
a2688a 115             if(commonService.updateWhere(MarketActivityJoinRecordMapper.class, sqlSentence) != 1){
F 116                 throw new TipsException("更新活动参与记录失败!");
117             }
118          }
119
120         //处理领取记录---------------------------------------------------------------------------------
121
122         receiveRecord.setJoinRecordId(joinRecord.getId());
123         if(commonService.insert(MarketActivityReceiveRecordMapper.class, receiveRecord) != 1){
124             throw new TipsException("新增领取记录失败1!");
125         }
126
127         //遍历新增子项
733bd8 128         for(MarketActivityReceiveRecordItem receiveRecordItem : recordItemList){
F 129             receiveRecordItem.setReceiveRecordId(receiveRecord.getId());
130             if(commonService.insert(MarketActivityReceiveRecordItemMapper.class, receiveRecordItem) != 1){
a2688a 131                 throw new TipsException("新增领取记录失败2!");
026734 132             }
F 133
134             //如果状态是待审核的,创建营销助手审批记录
447010 135 //            if(receiveRecordItem.getApproveStatus() == MarketActivityReceiveRecordItem.APPROVE_STATUS_WAIT){
R 136 //                handleApprove(joinRecord, receiveRecord, receiveRecordItem);
137 //            }
a2688a 138         }
F 139
beafc2 140         //发送审批通知给活动负责人-------------------------------------------------------------------------------------
d5e9d0 141 //        if(isApprove == BaseEntity.YES) {
1ac50b 142             try {
R 143                 String empUserId;
144                 Shop shop = commonService.selectOneByKey(ShopMapper.class, receiveRecord.getOpShopId());
145                 //正式环境发给对应的负责人 测试服发给彭程
146                 if ("prod".equals(customParameter.getPlatformPattern())) {
147                     //查询员工数据
148                     Employee employee = commonService.selectOneByKey(EmployeeMapper.class, marketActivity.getManagerId());
149                     if (employee == null) {
150                         throw new TipsException("找不到活动负责人!");
151                     }
152                     empUserId = employee.getUserId();
153                 } else {
154                     empUserId = "18023612893";
155                 }
a2688a 156
d5e9d0 157                 String sendDate = MessageTemplate.receiveCouponSendToManager(isApprove,empUserId, user.getName(), receiveRecord.getOpName(), shop.getName(), marketActivity.getTitle(), couponName, customParameter.getCorpMpAppId());
1ac50b 158                 log.info("小程序营销活动领取优惠券通知活动负责人,userId为:{}", empUserId);
R 159                 String token = WeiXinCorpMpUtil.getApplicationAccessToken(commonService, customParameter.getCorpAppId(), customParameter.getCorpMpSecret());
160                 SendMessageTool.sendMessge(sendDate, token);
161             } catch (Exception e) {
162                 log.info("小程序用户营销活动发送通知失败");
163                 log.info(GlobalExceptionHandler.getExceptionInformation(e));
164             }
d5e9d0 165 //        }
a2688a 166         //-------------------------------------------------------------------------------------------
F 167     }
f931ff 168
F 169     /** 更新查看记录 */
170     @Override
171     public void updateViewRecord(String marketActivityId, String platformType, User user) {
172         SqlSentence sqlSentence = new SqlSentence();
173         Map<String, Object> values = new HashMap<>();
174         sqlSentence.setM(values);
175         values.put("userId", user.getId());
176         values.put("marketActivityId", marketActivityId);
177         sqlSentence.setSqlSentence(" select id from market_activity_view_record where isDel = 0 and userId = #{m.userId} and marketActivityId = #{m.marketActivityId} ");
178         Map<String, Object> map = commonService.selectOneMap(MarketActivityViewRecordMapper.class, sqlSentence);
179         if(map != null && map.get("id") != null){
180             //更新查看次数和时间
181             values.clear();
182             values.put("id", map.get("id"));
183             sqlSentence.setSqlSentence(" viewNum = viewNum + 1, editTime = now() where id = #{m.id} ");
184             if(commonService.updateWhere(MarketActivityViewRecordMapper.class, sqlSentence) != 1){
185                 throw new TipsException("更新营销活动查看记录的查看次数和时间失败!");
186             }
187         }else{
188             //新增查看记录
189             MarketActivityViewRecord viewRecord = new MarketActivityViewRecord();
190             viewRecord.setMarketActivityId(marketActivityId);
191             viewRecord.setPlatformType(platformType);
192             viewRecord.setViewNum(1);
193             //用户信息
194             viewRecord.setUserId(user.getId());
195             viewRecord.setUserName(user.getName());
196             viewRecord.setCIQ(user.getCIQ());
197             viewRecord.setUserLevel(user.getUserLevel());
198             viewRecord.setUserStatus(user.getUserStatus());
199             viewRecord.setUserConsultantId(user.getHisCorpUserId());
200             viewRecord.setUserConsultantName(EmployeeTool.getCnName(viewRecord.getUserConsultantId(), commonService));
201             viewRecord.setUserShopId(user.getShopId());
202             viewRecord.setUserShopName(ShopTool.getShopName(viewRecord.getUserShopId(), commonService));
203             if(commonService.insert(MarketActivityViewRecordMapper.class, viewRecord) != 1){
204                 throw new TipsException("新增营销活动查看记录失败!");
205             }
026734 206         }
F 207     }
208
209
210     /** 营销助手审批回调处理 */
211     @Override
733bd8 212     public void receiveApproveHandle(MarketActivityJoinRecord joinRecord, MarketActivityReceiveRecord receiveRecord
F 213             , MarketActivityReceiveRecordItem recordItem) {
026734 214
F 215         SqlSentence sqlSentence = new SqlSentence();
216         Map<String, Object> values = new HashMap<>();
217         sqlSentence.setM(values);
218
496a49 219         String result = "";
R 220
026734 221         //审批成功
F 222         if(recordItem.getApproveStatus() == MarketActivityReceiveRecordItem.APPROVE_STATUS_SUC){
223
224             //优惠券操作人类型,默认员工
225             int couponOpType = OperatorConstants.changeCouponOpType(receiveRecord.getOpType());
226
227             //拼接用户数据
228             JSONArray userArr = new JSONArray();
229             JSONObject json = new JSONObject();
230             json.put("id", receiveRecord.getUserId());
231             json.put("amount", 1);
232             userArr.add(json);
233
234             //发放优惠券
235             try{
733bd8 236                sendCoupon(couponOpType, userArr, receiveRecord, recordItem);
026734 237             }catch (Exception e){
F 238                 log.error("参数:{}", JSONObject.fromObject(recordItem));
239                 log.error("营销助手审批回调处理,发放优惠券失败:{}", GlobalExceptionHandler.getExceptionInformation(e));
240                 //统一提示
241                 throw new TipsException("营销助手审批回调处理,发放优惠券失败," + e.getMessage());
242             }
243
244             //更新状态
245             values.put("approveStatus", recordItem.getApproveStatus());
62ccfe 246             values.put("approveRemark", recordItem.getApproveRemark());
F 247             values.put("id", recordItem.getId());
248             sqlSentence.setSqlSentence(" approveStatus = #{m.approveStatus}, approveRemark = #{m.approveRemark}, editTime = now() where id = #{m.id} ");
026734 249             if(commonService.updateWhere(MarketActivityReceiveRecordItemMapper.class, sqlSentence) != 1){
F 250                 throw new TipsException("更新领取记录审批状态失败!");
251             }
496a49 252             result = "通过";
026734 253         }
F 254         //审批失败-------------------------------------------------------------------------------------
62ccfe 255         else if(recordItem.getApproveStatus() == MarketActivityReceiveRecordItem.APPROVE_STATUS_FAIL){
733bd8 256             //更新状态和删除状态
F 257             values.put("approveStatus", recordItem.getApproveStatus());
62ccfe 258             values.put("approveRemark", recordItem.getApproveRemark());
F 259             values.put("id", recordItem.getId());
260             sqlSentence.setSqlSentence(" approveStatus = #{m.approveStatus}, approveRemark = #{m.approveRemark}, isDel = 2, editTime = now() where id = #{m.id} ");
733bd8 261             if(commonService.updateWhere(MarketActivityReceiveRecordItemMapper.class, sqlSentence) != 1){
F 262                 throw new TipsException("更新领取记录审批状态失败!");
263             }
f931ff 264
733bd8 265             //更新减少用户参与记录领取优惠券数量
F 266             values.clear();
267             values.put("couponNum", joinRecord.getCouponNum());
268             values.put("id", joinRecord.getId());
269             sqlSentence.setSqlSentence(" couponNum = couponNum - 1 where id = #{m.id} and couponNum = #{m.couponNum} ");
270             if(commonService.updateWhere(MarketActivityJoinRecordMapper.class, sqlSentence) != 1){
271                 throw new TipsException("更新活动参与记录信息失败!");
272             }
496a49 273
R 274             result = "拒绝";
f931ff 275         }
F 276
beafc2 277         //发送审批结果通知-------------------------------------------------------------------------------------
496a49 278         MarketActivity activity = commonService.selectOneByKey(MarketActivityMapper.class,receiveRecord.getMarketActivityId());
R 279         if(activity == null){
280             throw new TipsException("找不到活动!");
281         }
282
283         //查询员工数据
284         String empUserId;
285         //正式环境发给对应的 测试服发给彭程
286         if("prod".equals(customParameter.getPlatformPattern())) {
beafc2 287             Employee employee = commonService.selectOneByKey(EmployeeMapper.class,receiveRecord.getOpId());
R 288             if(employee == null){
289                 throw new TipsException("找不到活动负责人!");
290             }
496a49 291             empUserId = employee.getUserId();
R 292         }else {
293             empUserId = "18023612893";
294         }
295
296         String sendDate = MessageTemplate.receiveResultSendToAdvice(activity.getManagerName(), empUserId, activity.getTitle(), recordItem.getCommonName(),result, customParameter.getCorpMpAppId());
297         log.info("小程序营销活动领取优惠券审核结果通知,userId为:{}", empUserId);
5168cd 298         String token = WeiXinCorpMpUtil.getApplicationAccessToken(commonService, customParameter.getCorpAppId(), customParameter.getCorpMpSecret());
496a49 299         SendMessageTool.sendMessge(sendDate, token);
f931ff 300     }
026734 301
F 302     ///////////////////////////////////////////////////////////////////////////////
303
733bd8 304     /** 统一发送优惠券逻辑 */
F 305     private void sendCoupon(Integer couponOpType, JSONArray userArr, MarketActivityReceiveRecord receiveRecord, MarketActivityReceiveRecordItem recordItem){
306
307         Coupon coupon = commonService.selectOneByKeyBlob(CouponMapper.class, recordItem.getCommonId());
308         if(coupon!=null && BaseEntity.YES==coupon.getIsBirthday()){
309             for(int i=0;i<userArr.size();i++){
310                 JSONObject object = userArr.getJSONObject(i);
311                 object.put("startTime", DateUtil.formatDate_2(new Date()));
312                 object.put("endTime", DateUtil.formatDate_2(DateUtil.addMonth(new Date(),1)));
313             }
314         }
315
316         CouponReleaseRecordItem couponReleaseRecordItem = new CouponReleaseRecordItem();
317         couponReleaseRecordItem.setCouponId(recordItem.getCommonId());
318         couponReleaseRecordItem.setCommonType(CouponReleaseRecordItem.TYPE_COMMON_MARKET_ACTIVITY);
319         couponReleaseRecordItem.setCommonId(recordItem.getMarketActivityId());
320         couponReleaseRecordItem.setCommonItemId(recordItem.getActivityItemId());
321         couponReleaseRecordItem.setRecordName("门店助手热门活动领取");
322         //调用方法优惠券方法
323         CouponTool.sendCoupon(couponReleaseRecordItem, receiveRecord.getOpId(),receiveRecord.getOpName()
324                 , couponOpType,receiveRecord.getOpRoleId(),userArr
325                 , receiveRecord.getPlatformType(),commonService);
326
327     }
328
329     /** 处理营销助手申请 */
d6af92 330     private void handleApprove(MarketActivityJoinRecord joinRecord, MarketActivityReceiveRecord receiveRecord, MarketActivityReceiveRecordItem recordItem){
026734 331         MarketingTotalDto marketingTotalDto = new MarketingTotalDto();
733bd8 332         marketingTotalDto.setTitle(processBean.getMarcaTitle());
F 333         marketingTotalDto.setTypeStr(processBean.getMarcaTypeStr());
026734 334         marketingTotalDto.setType(processBean.getMarcaWorkflowCode());
F 335         marketingTotalDto.setUniqueId(recordItem.getId());
336         marketingTotalDto.setOperatorId(receiveRecord.getOpId());
337         marketingTotalDto.setOpName(receiveRecord.getOpName());
338         marketingTotalDto.setShopId(receiveRecord.getOpShopId());
339         marketingTotalDto.setShopName(CommonQueryUtil.getShopName(marketingTotalDto.getShopId(), commonService));
62ccfe 340         marketingTotalDto.setRoleId(CommonQueryUtil.getEmployeeRoleTypeId(receiveRecord.getOpRoleStr(), commonService));
026734 341         marketingTotalDto.setRoleUniqueStr(receiveRecord.getOpRoleStr());
F 342         marketingTotalDto.setUserId(receiveRecord.getUserId());
343         marketingTotalDto.setRemark("领取申请");
344         marketingTotalDto.setCallbackUrl(processBean.getMarcaNotifyUrl());
345
346         //---审核信息组拼
347         MarketingContentDto marketingContentDto = new MarketingContentDto();
348         List<MarketingMsgItemDto> contentList = new ArrayList<>();
349
350         MarketingMsgItemDto marketingMsgItemDto = new MarketingMsgItemDto();
351         marketingMsgItemDto.setKey("审核类型");
352         marketingMsgItemDto.setValue("营销活动领取优惠券");
353         contentList.add(marketingMsgItemDto);
354
355         marketingMsgItemDto = new MarketingMsgItemDto();
356         marketingMsgItemDto.setKey("优惠券");
357         marketingMsgItemDto.setValue(recordItem.getCommonName());
358         contentList.add(marketingMsgItemDto);
359
360         marketingContentDto.setContentList(contentList);
361         marketingTotalDto.setMarketingContentDto(marketingContentDto);
362         //审核信息组拼结束
363
364         //---企业微信信息组拼
365         MarketingMsgDto marketingMsgDto = new MarketingMsgDto();
366         marketingMsgDto.setEmphasisFirstItem(true);
367         List<MarketingMsgItemDto> contentItem = new ArrayList<>();
368         contentItem.addAll(contentList);
369         marketingMsgDto.setContentItem(contentItem);
370         marketingTotalDto.setMarketingMsgDto(marketingMsgDto);
371         //---企业微信信息结束
372
62ccfe 373         log.info("请求创建审批记录参数:{}", com.alibaba.fastjson.JSONObject.toJSONString(marketingTotalDto));
de6258 374         Result result =  mCommonService.commonApply(marketingTotalDto);
62ccfe 375         log.info("请求创建审批记录返回结果:{}", com.alibaba.fastjson.JSONObject.toJSONString(result));
F 376         if(!result.checkCode()){
026734 377             log.info("领取优惠券失败,新增领取审批记录失败,返回结果:{}", JSONObject.fromObject(result));
F 378             throw new TipsException("领取优惠券失败,新增领取审批记录失败!");
379         }
d6af92 380
F 381         //获取返回值,判断是否直接审核通过
382         MarketingResultDto marketingResultDto = result.getObject(result.getData(),MarketingResultDto.class);
383         if(marketingResultDto == null){
384             throw new TipsException("创建审批记录返回信息为空!");
385         }
386
387         //如果这里发券失败,目前会对应记录都没有创建,但是营销助手会有一条记录的情况
388         if(marketingResultDto != null && marketingResultDto.getIsPass() == 1){
389             recordItem.setApproveStatus(MarketActivityReceiveRecordItem.APPROVE_STATUS_SUC);
595265 390             recordItem.setApproveRemark("审批直接通过");
d6af92 391             receiveApproveHandle(joinRecord, receiveRecord, recordItem);
F 392         }
393
026734 394     }
F 395
396
a2688a 397 }