package com.hx.phip.service.market.impl; import com.hx.common.service.CommonService; import com.hx.exception.TipsException; import com.hx.guide.util.CommonQueryUtil; import com.hx.guide.util.mp.WeiXinCorpMpUtil; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.OperatorConstants; import com.hx.phiappt.constants.tool.CouponTool; import com.hx.phiappt.constants.tool.SendMessageTool; import com.hx.phiappt.constants.tool.employee.EmployeeTool; import com.hx.phiappt.constants.tool.model.MessageTemplate; import com.hx.phiappt.constants.tool.shop.ShopTool; import com.hx.phiappt.dao.mapper.*; import com.hx.phiappt.model.BaseEntity; import com.hx.phiappt.model.Employee; import com.hx.phiappt.model.Shop; import com.hx.phiappt.model.User; import com.hx.phiappt.model.coupon.Coupon; import com.hx.phiappt.model.coupon.CouponReleaseRecordItem; import com.hx.phiappt.model.market.*; import com.hx.phip.config.GlobalExceptionHandler; import com.hx.phip.config.ProcessBean; import com.hx.phip.service.market.MarketActivityService; import com.hx.resultTool.Result; import com.hx.util.DateUtil; import com.hz.his.dto.marketing.common.*; import com.hz.his.feign.service.marketing.MCommonService; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.hx.phip.config.CustomParameter; import javax.annotation.Resource; import java.util.*; @Slf4j @Transactional @Service public class MarketActivityServiceImpl implements MarketActivityService { @Resource private CommonService commonService; @Resource private MCommonService mCommonService; @Resource private ProcessBean processBean; @Resource private CustomParameter customParameter; /** 领取优惠券 */ @Override public void receiveCoupon(MarketActivity marketActivity, List list, User user , MarketActivityJoinRecord joinRecord, MarketActivityReceiveRecord receiveRecord) { //优惠券操作人类型,默认员工 int couponOpType = OperatorConstants.changeCouponOpType(receiveRecord.getOpType()); //拼接用户数据 JSONArray userArr = new JSONArray(); JSONObject json = new JSONObject(); json.put("id", user.getId()); json.put("amount", 1); userArr.add(json); SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); sqlSentence.setM(values); //领取记录子项list MarketActivityReceiveRecordItem recordItem = null; List recordItemList = new ArrayList<>(); //遍历领取 String couponName = ""; Integer isApprove = BaseEntity.NO; for(MarketActivityItem activityCoupon : list) { //新增 recordItem = new MarketActivityReceiveRecordItem(activityCoupon); recordItemList.add(recordItem); //获取优惠券名称 couponName = activityCoupon.getCommonName(); //判断是否需要审批,如果需要审批先跳过不发放优惠券 if(activityCoupon.getIsApprove() == BaseEntity.YES){ isApprove = activityCoupon.getIsApprove(); continue; } //发放优惠券 try{ sendCoupon(couponOpType, userArr, receiveRecord, recordItem); }catch (Exception e){ log.error("参数:{}", JSONObject.fromObject(activityCoupon)); log.error("遍历发放优惠券失败:{}", GlobalExceptionHandler.getExceptionInformation(e)); //统一提示 throw new TipsException("活动爆满【"+activityCoupon.getCommonName()+"】已领取完,请联系活动负责人"+marketActivity.getManagerName()+"咨询情况"); } } //处理参与记录-------------------------------------------------------------------------------- if(StringUtils.isEmpty(joinRecord.getId())){ if(commonService.insert(MarketActivityJoinRecordMapper.class, joinRecord) != 1){ throw new TipsException("新增活动参与记录失败!"); } }else{ values.clear(); values.put("id", joinRecord.getId()); values.put("couponNum", list.size()); sqlSentence.setSqlSentence(" couponNum = couponNum + #{m.couponNum}, receiveNum = receiveNum + 1, editTime = now() where id = #{m.id} "); if(commonService.updateWhere(MarketActivityJoinRecordMapper.class, sqlSentence) != 1){ throw new TipsException("更新活动参与记录失败!"); } } //处理领取记录--------------------------------------------------------------------------------- receiveRecord.setJoinRecordId(joinRecord.getId()); if(commonService.insert(MarketActivityReceiveRecordMapper.class, receiveRecord) != 1){ throw new TipsException("新增领取记录失败1!"); } //遍历新增子项 for(MarketActivityReceiveRecordItem receiveRecordItem : recordItemList){ receiveRecordItem.setReceiveRecordId(receiveRecord.getId()); if(commonService.insert(MarketActivityReceiveRecordItemMapper.class, receiveRecordItem) != 1){ throw new TipsException("新增领取记录失败2!"); } //如果状态是待审核的,创建营销助手审批记录 // if(receiveRecordItem.getApproveStatus() == MarketActivityReceiveRecordItem.APPROVE_STATUS_WAIT){ // handleApprove(joinRecord, receiveRecord, receiveRecordItem); // } } //发送审批通知给活动负责人------------------------------------------------------------------------------------- // if(isApprove == BaseEntity.YES) { try { String empUserId; Shop shop = commonService.selectOneByKey(ShopMapper.class, receiveRecord.getOpShopId()); //正式环境发给对应的负责人 测试服发给彭程 if ("prod".equals(customParameter.getPlatformPattern())) { //查询员工数据 Employee employee = commonService.selectOneByKey(EmployeeMapper.class, marketActivity.getManagerId()); if (employee == null) { throw new TipsException("找不到活动负责人!"); } empUserId = employee.getUserId(); } else { empUserId = "18023612893"; } String sendDate = MessageTemplate.receiveCouponSendToManager(isApprove,empUserId, user.getName(), receiveRecord.getOpName(), shop.getName(), marketActivity.getTitle(), couponName, customParameter.getCorpMpAppId()); log.info("小程序营销活动领取优惠券通知活动负责人,userId为:{}", empUserId); String token = WeiXinCorpMpUtil.getApplicationAccessToken(commonService, customParameter.getCorpAppId(), customParameter.getCorpMpSecret()); SendMessageTool.sendMessge(sendDate, token); } catch (Exception e) { log.info("小程序用户营销活动发送通知失败"); log.info(GlobalExceptionHandler.getExceptionInformation(e)); } // } //------------------------------------------------------------------------------------------- } /** 更新查看记录 */ @Override public void updateViewRecord(String marketActivityId, String platformType, User user) { SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); sqlSentence.setM(values); values.put("userId", user.getId()); values.put("marketActivityId", marketActivityId); sqlSentence.setSqlSentence(" select id from market_activity_view_record where isDel = 0 and userId = #{m.userId} and marketActivityId = #{m.marketActivityId} "); Map map = commonService.selectOneMap(MarketActivityViewRecordMapper.class, sqlSentence); if(map != null && map.get("id") != null){ //更新查看次数和时间 values.clear(); values.put("id", map.get("id")); sqlSentence.setSqlSentence(" viewNum = viewNum + 1, editTime = now() where id = #{m.id} "); if(commonService.updateWhere(MarketActivityViewRecordMapper.class, sqlSentence) != 1){ throw new TipsException("更新营销活动查看记录的查看次数和时间失败!"); } }else{ //新增查看记录 MarketActivityViewRecord viewRecord = new MarketActivityViewRecord(); viewRecord.setMarketActivityId(marketActivityId); viewRecord.setPlatformType(platformType); viewRecord.setViewNum(1); //用户信息 viewRecord.setUserId(user.getId()); viewRecord.setUserName(user.getName()); viewRecord.setCIQ(user.getCIQ()); viewRecord.setUserLevel(user.getUserLevel()); viewRecord.setUserStatus(user.getUserStatus()); viewRecord.setUserConsultantId(user.getHisCorpUserId()); viewRecord.setUserConsultantName(EmployeeTool.getCnName(viewRecord.getUserConsultantId(), commonService)); viewRecord.setUserShopId(user.getShopId()); viewRecord.setUserShopName(ShopTool.getShopName(viewRecord.getUserShopId(), commonService)); if(commonService.insert(MarketActivityViewRecordMapper.class, viewRecord) != 1){ throw new TipsException("新增营销活动查看记录失败!"); } } } /** 营销助手审批回调处理 */ @Override public void receiveApproveHandle(MarketActivityJoinRecord joinRecord, MarketActivityReceiveRecord receiveRecord , MarketActivityReceiveRecordItem recordItem) { SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); sqlSentence.setM(values); String result = ""; //审批成功 if(recordItem.getApproveStatus() == MarketActivityReceiveRecordItem.APPROVE_STATUS_SUC){ //优惠券操作人类型,默认员工 int couponOpType = OperatorConstants.changeCouponOpType(receiveRecord.getOpType()); //拼接用户数据 JSONArray userArr = new JSONArray(); JSONObject json = new JSONObject(); json.put("id", receiveRecord.getUserId()); json.put("amount", 1); userArr.add(json); //发放优惠券 try{ sendCoupon(couponOpType, userArr, receiveRecord, recordItem); }catch (Exception e){ log.error("参数:{}", JSONObject.fromObject(recordItem)); log.error("营销助手审批回调处理,发放优惠券失败:{}", GlobalExceptionHandler.getExceptionInformation(e)); //统一提示 throw new TipsException("营销助手审批回调处理,发放优惠券失败," + e.getMessage()); } //更新状态 values.put("approveStatus", recordItem.getApproveStatus()); values.put("approveRemark", recordItem.getApproveRemark()); values.put("id", recordItem.getId()); sqlSentence.setSqlSentence(" approveStatus = #{m.approveStatus}, approveRemark = #{m.approveRemark}, editTime = now() where id = #{m.id} "); if(commonService.updateWhere(MarketActivityReceiveRecordItemMapper.class, sqlSentence) != 1){ throw new TipsException("更新领取记录审批状态失败!"); } result = "通过"; } //审批失败------------------------------------------------------------------------------------- else if(recordItem.getApproveStatus() == MarketActivityReceiveRecordItem.APPROVE_STATUS_FAIL){ //更新状态和删除状态 values.put("approveStatus", recordItem.getApproveStatus()); values.put("approveRemark", recordItem.getApproveRemark()); values.put("id", recordItem.getId()); sqlSentence.setSqlSentence(" approveStatus = #{m.approveStatus}, approveRemark = #{m.approveRemark}, isDel = 2, editTime = now() where id = #{m.id} "); if(commonService.updateWhere(MarketActivityReceiveRecordItemMapper.class, sqlSentence) != 1){ throw new TipsException("更新领取记录审批状态失败!"); } //更新减少用户参与记录领取优惠券数量 values.clear(); values.put("couponNum", joinRecord.getCouponNum()); values.put("id", joinRecord.getId()); sqlSentence.setSqlSentence(" couponNum = couponNum - 1 where id = #{m.id} and couponNum = #{m.couponNum} "); if(commonService.updateWhere(MarketActivityJoinRecordMapper.class, sqlSentence) != 1){ throw new TipsException("更新活动参与记录信息失败!"); } result = "拒绝"; } //发送审批结果通知------------------------------------------------------------------------------------- MarketActivity activity = commonService.selectOneByKey(MarketActivityMapper.class,receiveRecord.getMarketActivityId()); if(activity == null){ throw new TipsException("找不到活动!"); } //查询员工数据 String empUserId; //正式环境发给对应的 测试服发给彭程 if("prod".equals(customParameter.getPlatformPattern())) { Employee employee = commonService.selectOneByKey(EmployeeMapper.class,receiveRecord.getOpId()); if(employee == null){ throw new TipsException("找不到活动负责人!"); } empUserId = employee.getUserId(); }else { empUserId = "18023612893"; } String sendDate = MessageTemplate.receiveResultSendToAdvice(activity.getManagerName(), empUserId, activity.getTitle(), recordItem.getCommonName(),result, customParameter.getCorpMpAppId()); log.info("小程序营销活动领取优惠券审核结果通知,userId为:{}", empUserId); String token = WeiXinCorpMpUtil.getApplicationAccessToken(commonService, customParameter.getCorpAppId(), customParameter.getCorpMpSecret()); SendMessageTool.sendMessge(sendDate, token); } /////////////////////////////////////////////////////////////////////////////// /** 统一发送优惠券逻辑 */ private void sendCoupon(Integer couponOpType, JSONArray userArr, MarketActivityReceiveRecord receiveRecord, MarketActivityReceiveRecordItem recordItem){ Coupon coupon = commonService.selectOneByKeyBlob(CouponMapper.class, recordItem.getCommonId()); if(coupon!=null && BaseEntity.YES==coupon.getIsBirthday()){ for(int i=0;i contentList = new ArrayList<>(); MarketingMsgItemDto marketingMsgItemDto = new MarketingMsgItemDto(); marketingMsgItemDto.setKey("审核类型"); marketingMsgItemDto.setValue("营销活动领取优惠券"); contentList.add(marketingMsgItemDto); marketingMsgItemDto = new MarketingMsgItemDto(); marketingMsgItemDto.setKey("优惠券"); marketingMsgItemDto.setValue(recordItem.getCommonName()); contentList.add(marketingMsgItemDto); marketingContentDto.setContentList(contentList); marketingTotalDto.setMarketingContentDto(marketingContentDto); //审核信息组拼结束 //---企业微信信息组拼 MarketingMsgDto marketingMsgDto = new MarketingMsgDto(); marketingMsgDto.setEmphasisFirstItem(true); List contentItem = new ArrayList<>(); contentItem.addAll(contentList); marketingMsgDto.setContentItem(contentItem); marketingTotalDto.setMarketingMsgDto(marketingMsgDto); //---企业微信信息结束 log.info("请求创建审批记录参数:{}", com.alibaba.fastjson.JSONObject.toJSONString(marketingTotalDto)); Result result = mCommonService.commonApply(marketingTotalDto); log.info("请求创建审批记录返回结果:{}", com.alibaba.fastjson.JSONObject.toJSONString(result)); if(!result.checkCode()){ log.info("领取优惠券失败,新增领取审批记录失败,返回结果:{}", JSONObject.fromObject(result)); throw new TipsException("领取优惠券失败,新增领取审批记录失败!"); } //获取返回值,判断是否直接审核通过 MarketingResultDto marketingResultDto = result.getObject(result.getData(),MarketingResultDto.class); if(marketingResultDto == null){ throw new TipsException("创建审批记录返回信息为空!"); } //如果这里发券失败,目前会对应记录都没有创建,但是营销助手会有一条记录的情况 if(marketingResultDto != null && marketingResultDto.getIsPass() == 1){ recordItem.setApproveStatus(MarketActivityReceiveRecordItem.APPROVE_STATUS_SUC); recordItem.setApproveRemark("审批直接通过"); receiveApproveHandle(joinRecord, receiveRecord, recordItem); } } }