package com.hx.phip.service.questionnaire.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.common.dao.CommonDao; import com.hx.common.service.CommonService; import com.hx.exception.TipsException; import com.hx.guide.dao.mapper.VOpTriageMapper; import com.hx.guide.dao.mapper.VTreatProjectMapper; import com.hx.guide.model.VTreatProject; import com.hx.guide.model.VisitOrder; import com.hx.guide.model.operate.VOpTriage; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.common.OperationReasonConstants; import com.hx.phiappt.common.OrderGoodsConstants; import com.hx.phiappt.common.OriginChannelConstants; import com.hx.phiappt.common.RoleType; import com.hx.phiappt.constants.tool.CouponTool; import com.hx.phiappt.constants.tool.employee.EmployeeTool; import com.hx.phiappt.dao.mapper.InvestigateAwardMapper; import com.hx.phiappt.dao.mapper.RewardGoodsMapper; import com.hx.phiappt.dao.mapper.RewardRecordMapper; import com.hx.phiappt.dao.mapper.ShopMapper; import com.hx.phiappt.model.*; import com.hx.phiappt.model.coupon.CouponReleaseRecord; import com.hx.phiappt.model.coupon.CouponReleaseRecordItem; import com.hx.phiappt.model.deduction.DeductionJoin; import com.hx.phiappt.model.deduction.DeductionProject; import com.hx.phiappt.model.deduction.DeductionSingle; import com.hx.phiappt.model.order.OrdersTotal; import com.hx.phiappt.model.questionnaire.*; import com.hx.phiappt.model.reward.RewardGoods; import com.hx.phiappt.model.reward.RewardRecord; import com.hx.phiappt.model.treat.TreatSingle; import com.hx.phiappt.model.user.UserProject; import com.hx.phiappt.model.user.UserProjectItem; import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed; import com.hx.phip.config.CustomParameter; import com.hx.phip.config.QuestionInvestigateConfig; import com.hx.phip.dao.mapper.*; import com.hx.phip.service.UserMoneyService; import com.hx.phip.service.questionnaire.QuestionnaireService; import com.hx.phip.util.api.UserMoneyUtil; import com.hx.util.DateUtil; import com.hx.util.StringUtils; import com.hz.phis.dt.reward.AnswerDto; import com.hz.phis.dt.reward.InvestigateGroupDto; import com.hz.phis.dt.reward.QuestionInvestigateDto; import com.hz.phis.dt.reward.RewardRecordDto; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; @Transactional @Service public class QuestionnaireServiceImpl implements QuestionnaireService { // log4j private static final Logger logger = LoggerFactory.getLogger(QuestionnaireServiceImpl.class.getName()); @Resource private QuestionnaireMapper questionnaireMapper; @Resource private InvestigateDataMapper investigateDataMapper; @Resource private QuestionInvestigateMapper questionInvestigateMapper; @Resource private CommonDao commonDao; @Resource private CommonService commonService; @Resource private UserMoneyService userMoneyService; @Resource private CustomParameter customParameter; @Resource private SendingRecordsMapper sendingRecordsMapper; @Resource private UserProjectMapper userProjectMapper; @Resource private DeductionSingleMapper deductionSingleMapper; @Resource private UserProjectItemMapper userProjectItemMapper; @Resource private DeductionProjectMapper deductionProjectMapper; @Resource private QuestionInvestigateConfig questionInvestigateConfig; @Override public int selectCount(SqlSentence sqlSentence) { return questionnaireMapper.selectCount(sqlSentence); } @Override public void insert(Questionnaire questionnaire) { checkRepeat(questionnaire.getDeductionSingleId()); Shop shop = commonDao.selectOneByKey(ShopMapper.class, questionnaire.getShopId()); if (null == shop) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "门店信息错误!!"); } questionnaire.setShopName(shop.getName()); User user = commonDao.selectOneByKey(UserMapper.class, questionnaire.getUserId()); if (null == user) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "用户信息错误!!"); } questionnaire.setUserName(user.getName()); Employee employee = commonDao.selectOneByKey(EmployeeMapper.class, questionnaire.getDoctorCorpUserId()); if (null != employee) { questionnaire.setDoctorCorpUserName(employee.getCnName()); } Employee nurse = commonDao.selectOneByKey(EmployeeMapper.class, questionnaire.getNurseCorpUserId()); if (null == nurse) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "护士信息错误!!"); } questionnaire.setNurseCorpUserName(nurse.getCnName()); Project project = commonDao.selectOneByKey(ProjectMapper.class, questionnaire.getProjectId()); if (null == project) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "项目信息错误!!"); } questionnaire.setProjectName(project.getName()); DeductionSingle deductionSingle = commonDao.selectOneByKey(DeductionSingleMapper.class, questionnaire.getDeductionSingleId()); if (null == deductionSingle) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "划扣信息错误!!"); } //关联订单号 SqlSentence sqlSentence = new SqlSentence(); String sql = "select ot.id,ot.orderNo from orders_total ot " + " join (select * from order_item where isDel = #{m.isDel} ) oi on ot.id = oi.orderId " + " join (select * from user_project_item where isDel = #{m.isDel} ) up on oi.id = up.orderItemId " + " join (select * from deduction_project where isDel = #{m.isDel} ) dp on up.id = dp.userProjectItemId " + " WHERE ot.isDel = #{m.isDel} and dp.deductionSingleId = #{m.deductionSingleId}" + " ORDER BY dp.createTime DESC LIMIT 1"; Map map = new HashMap<>(); map.put("isDel", BaseEntity.NO); map.put("deductionSingleId", questionnaire.getDeductionSingleId()); sqlSentence.setM(map); sqlSentence.setSqlSentence(sql); OrdersTotal ordersTotal = commonDao.selectOne(OrdersTotalMapper.class, sqlSentence); if (null != ordersTotal) { questionnaire.setOrderId(ordersTotal.getId()); questionnaire.setOrderNo(ordersTotal.getOrderNo()); } //处理打赏护士数据 StringBuilder sql1 = new StringBuilder(); Map sqlMap = new HashMap<>(); sqlMap.put("isDel", BaseEntity.NO); sqlMap.put("commonId", questionnaire.getDeductionSingleId()); sqlMap.put("employeeId", questionnaire.getNurseCorpUserId()); sql1.append(" SELECT goodsName,sum(number) as number,sum(deduIntegral) as total,deduIntegral FROM reward_record WHERE isDel = #{m.isDel} and commonId = #{m.commonId} and employeeId = #{m.employeeId} GROUP BY goodsId "); sqlSentence.sqlSentence(sql1.toString(), sqlMap); Integer total = 0; List list = commonDao.selectList(RewardRecordMapper.class, sqlSentence); JSONArray array = new JSONArray(); JSONObject jsonObject = null; for (RewardRecord rewardRecord : list) { total += (rewardRecord.getDeduIntegral() * rewardRecord.getNumber()); jsonObject = new JSONObject(); jsonObject.put("goodsName", rewardRecord.getGoodsName()); jsonObject.put("deduIntegral", rewardRecord.getDeduIntegral()); jsonObject.put("number", rewardRecord.getNumber()); jsonObject.put("total", rewardRecord.getTotal()); array.add(jsonObject); } questionnaire.setTotalIntegral(total); questionnaire.setRewardGoods(array.toJSONString()); int count = questionnaireMapper.insert(questionnaire); if (count != 1) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "保存失败!!"); } //评价成功赠送500积分 // 构建操作资金对象 UserMoneyUnclaimed paramUserMoneyUnclaimed = new UserMoneyUnclaimed(); paramUserMoneyUnclaimed.setUserId(user.getId()); paramUserMoneyUnclaimed.setFundType(UserMoneyUnclaimed.FUND_TYPE_INTEGRAL); paramUserMoneyUnclaimed.setOriginChannel(OriginChannelConstants.ORIGIN_CHANNEL_CRM); paramUserMoneyUnclaimed.setOriginSubject("划扣评价赠送积分"); paramUserMoneyUnclaimed.setOperationReason(OperationReasonConstants.OP_REASON_QUESTIONNAIRE); paramUserMoneyUnclaimed.setCommonId(questionnaire.getId()); // 减扣原赠送积分 paramUserMoneyUnclaimed.setOpNumber(new BigDecimal(500)); // 处理操作人信息 paramUserMoneyUnclaimed.setOperatorType(UserMoneyUnclaimed.OPERATOR_TYPE_USER); paramUserMoneyUnclaimed.setOperatorId(user.getId()); // 添充详细消息 paramUserMoneyUnclaimed.setRemarks("划扣评价赠送积分"); // 调用方法处理 UserMoneyUtil.addUserMoneyUnclaimedData(paramUserMoneyUnclaimed); } @Override public List selectList(SqlSentence sqlSentence) { return questionnaireMapper.selectList(sqlSentence); } @Override public List> selectListMap(SqlSentence sqlSentence) { return questionnaireMapper.selectListMap(sqlSentence); } @Override public Questionnaire selectOne(SqlSentence sqlSentence) { return questionnaireMapper.selectOne(sqlSentence); } @Override public Map selectOneMap(SqlSentence sqlSentence) { return questionnaireMapper.selectOneMap(sqlSentence); } @Override public Questionnaire selectOneByKey(Object object) { return questionnaireMapper.selectOneByKey(object); } @Override public Questionnaire selectOneByKeyBlob(Object object) { return questionnaireMapper.selectOneByKeyBlob(object); } @Override public void updateAll(Questionnaire questionnaire) { } @Override public void updateWhere(SqlSentence sqlSentence) { } @Override public void deleteOne(String delId) { } //判断是否重复 @Override public void checkRepeat(String id) { SqlSentence sqlSentence = new SqlSentence(); String sql = "SELECT * FROM questionnaire WHERE isDel=#{m.isDel} and deductionSingleId = #{m.deductionSingleId}"; Map map = new HashMap<>(); map.put("isDel", BaseEntity.NO); map.put("deductionSingleId", id); sqlSentence.setM(map); sqlSentence.setSqlSentence(sql); Questionnaire old = questionnaireMapper.selectOne(sqlSentence); if (null != old) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "请勿重复填写调查问卷!!"); } } //打赏 @Override public void reward(List list) { if (null != list && list.size() > 0) { RewardGoods goods = null; Employee employee = null; DeductionSingle deductionSingle = null; User user = null; RewardRecord record = null; //扣除总积分 int total = 0; for (RewardRecordDto rewardRecord : list) { if (StringUtils.isEmpty(rewardRecord.getGoodsId())) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏物品标识为空!!"); } goods = commonDao.selectOneByKey(RewardGoodsMapper.class, rewardRecord.getGoodsId()); if (null == goods) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏物品标识错误!!"); } if (StringUtils.isEmpty(rewardRecord.getEmployeeId())) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏员工标识为空!!"); } employee = commonDao.selectOneByKey(EmployeeMapper.class, rewardRecord.getEmployeeId()); if (null == employee) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏员工标识错误!!"); } if (StringUtils.isEmpty(rewardRecord.getCommonId())) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏划扣记录标识为空!!"); } deductionSingle = commonDao.selectOneByKey(DeductionSingleMapper.class, rewardRecord.getCommonId()); if (null == deductionSingle) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏划扣记录标识错误!!"); } if (StringUtils.isEmpty(rewardRecord.getUserId())) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏用户标识为空!!"); } user = commonDao.selectOneByKey(UserMapper.class, rewardRecord.getUserId()); if (null == user) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏用户标识错误!!"); } total += (rewardRecord.getDeduIntegral() * rewardRecord.getNumber()); rewardRecord.setGoodsName(goods.getGoodsName()); record = new RewardRecord(); record.setGoodsName(goods.getGoodsName()); record.setGoodsId(goods.getId()); record.setCommonId(rewardRecord.getCommonId()); record.setUserId(rewardRecord.getUserId()); record.setDeduIntegral(rewardRecord.getDeduIntegral()); record.setNumber(rewardRecord.getNumber()); record.setType(rewardRecord.getType()); record.setEmployeeId(rewardRecord.getEmployeeId()); record.setShopId(deductionSingle.getShopId()); if (commonDao.insert(RewardRecordMapper.class, record) != 1) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏失败!!"); } } //扣减积分 UserMoney userMoney = userMoneyService.selectUserIdDecrypt(user.getId()); BigDecimal intergerl = new BigDecimal(userMoney.getIntegral()); if (intergerl.compareTo(new BigDecimal(total)) < 0) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "打赏失败,用户积分不足!!"); } // 构建操作资金对象 UserMoneyUnclaimed paramUserMoneyUnclaimed = new UserMoneyUnclaimed(); paramUserMoneyUnclaimed.setUserId(user.getId()); paramUserMoneyUnclaimed.setFundType(UserMoneyUnclaimed.FUND_TYPE_INTEGRAL); paramUserMoneyUnclaimed.setOriginChannel(OriginChannelConstants.ORIGIN_CHANNEL_CRM); paramUserMoneyUnclaimed.setOriginSubject("打赏护士扣减积分"); paramUserMoneyUnclaimed.setOperationReason(OperationReasonConstants.OP_REASON_REWARD_NURSE); paramUserMoneyUnclaimed.setCommonId(deductionSingle.getId()); // 减扣原赠送积分 paramUserMoneyUnclaimed.setOpNumber(new BigDecimal(total).negate()); // 处理操作人信息 paramUserMoneyUnclaimed.setOperatorType(UserMoneyUnclaimed.OPERATOR_TYPE_USER); paramUserMoneyUnclaimed.setOperatorId(user.getId()); // 添充详细消息 paramUserMoneyUnclaimed.setRemarks("打赏护士扣减积分"); // 调用方法处理 UserMoneyUtil.addUserMoneyUnclaimedData(paramUserMoneyUnclaimed); } else { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "参数为空!!"); } } @Override public String investigateSubmit(QuestionInvestigateDto questionnaire) { if (null == questionnaire) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "参数为空!!"); } if (null == questionnaire.getType()) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "参数类型为空!!"); } if (StringUtils.isEmpty(questionnaire.getInvestigateId())) { throw new TipsException("问卷标识为空!"); } QuestionInvestigate investigate = questionInvestigateMapper.selectOneByKeyBlob(questionnaire.getInvestigateId()); if (null == investigate) { throw new TipsException("问卷标识错误!"); } if (BaseEntity.NO.equals(investigate.getStatus()) && BaseEntity.YES.equals(questionnaire.getType())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS, "问卷编号暂未启用!!"); } if (BaseEntity.NO.equals(investigate.getIsPublish()) && BaseEntity.YES.equals(questionnaire.getType())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS, "问卷编号暂未发布!!"); } User user = commonDao.selectOneByKey(UserMapper.class, questionnaire.getUserId()); if (null == user) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "用户信息错误!!"); } if (null == questionnaire.getAnswerDtoList() || questionnaire.getAnswerDtoList().size() == 0) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "答案数据为空!!"); } SqlSentence sqlSentence = new SqlSentence(); Map sqlMap = new HashMap<>(); //if (PlatformPattern.PROD.equals(customParameter.getPlatformPattern())) { //线上判断是否重复填写 sqlMap.put("investigateId", questionnaire.getInvestigateId()); sqlMap.put("userId", questionnaire.getUserId()); sqlMap.put("isDel", BaseEntity.NO); sqlMap.put("type", BaseEntity.YES); sqlMap.put("startTime", DateUtil.dayToStartDate(new Date())); sqlMap.put("endTime", DateUtil.dayToEndDate(new Date())); sqlSentence.sqlSentence("SELECT id FROM investigate_data where isDel = #{m.isDel} and userId = #{m.userId} and investigateId = #{m.investigateId}" + " and type = #{m.type} and createTime >= #{m.startTime} and createTime <= #{m.endTime} limit 1",sqlMap); InvestigateData investigateData = investigateDataMapper.selectOne(sqlSentence); if (null != investigateData){ throw new PlatTipsException(PlatformCode.ERROR_TIPS, "今天已填写过调查问卷,请勿重复填写!!"); } // } SendingRecords records = sendingRecordsMapper.selectOneByKey(questionnaire.getDeductionSingleId()); InvestigateData data = new InvestigateData(); data.setInvestigateId(questionnaire.getInvestigateId()); data.setUserId(questionnaire.getUserId()); data.setUserName(user.getName()); data.setType(questionnaire.getType()); data.setStartTime(questionnaire.getStartTime()); data.setDoneTime(questionnaire.getDoneTime()); data.setProposal(questionnaire.getProposal()); data.setData(JSON.toJSONString(questionnaire.getAnswerDtoList())); if (null != records){ //判断是否已有填写记录 sqlMap.put("deductionSingleId", records.getDeductionSingleId()); sqlSentence.sqlSentence("SELECT count(*) FROM investigate_data where isDel = 0 and deductionSingleId = #{m.deductionSingleId}",sqlMap); int i = commonService.selectCountSql(sqlSentence); if (i > 0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS, "该划扣已填写过评价,请勿重复填写!!"); } data.setDeductionSingleId(records.getDeductionSingleId()); if (SendingRecords.TYPE_DEDUCTION.equals(records.getType())){ //处理划扣数据 handleDeuction(data,records.getDeductionSingleId()); }else if (SendingRecords.TYPE_LEAVE.equals(records.getType())){ //处理离店数据 handleLeaveData(data,records.getVisitOrderId()); } //更新是否完成状态 updateIsComplete(records.getId()); }else { if (StringUtils.isEmpty(questionnaire.getCode())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS, "记录数据为空!!"); } } data.setIsDone(BaseEntity.YES); //是否完答 boolean flag = true; //List nurseList = Arrays.asList(questionInvestigateConfig.getNewNurse(), questionInvestigateConfig.getOldNurse()); List answerDtoList = questionnaire.getAnswerDtoList(); //顾问评价题目id数组 StringBuilder consultantstr = new StringBuilder(); //List consultantList = Arrays.asList(questionInvestigateConfig.getOldConsultant(), questionInvestigateConfig.getNewConsultant()); //建议 StringBuilder proposal = new StringBuilder(); StringBuilder nurse = new StringBuilder(); StringBuilder doctor = new StringBuilder(); StringBuilder reception = new StringBuilder(); //List clozeIds = Arrays.asList(questionInvestigateConfig.getNewProposal(), questionInvestigateConfig.getOldProposal()); Integer isHighQuality = 0; QuestionTitle questionTitle = null; StringBuilder str = new StringBuilder(); for (AnswerDto answerDto : answerDtoList) { if (null == answerDto.getAnswer() || answerDto.getAnswer().length == 0) { flag = false; data.setIsDone(BaseEntity.NO); } if (null != answerDto.getAnswer() && answerDto.getAnswer().length > 0){ questionTitle = commonDao.selectOneByKey(com.hx.phiappt.dao.mapper.QuestionTitleMapper.class,answerDto.getTitleId()); if (null != questionTitle){ if (QuestionTitle.SPECIAL_TYPE_NURSE.equals(questionTitle.getSpecialType())){ for (int k = 0; k < answerDto.getAnswer().length; k++) { nurse.append(answerDto.getAnswer()[k]); if (k != answerDto.getAnswer().length - 1) { nurse.append(","); } } }else if (QuestionTitle.SPECIAL_TYPE_CONSULTANT.equals(questionTitle.getSpecialType())){ for (int s = 0; s < answerDto.getAnswer().length; s++) { consultantstr.append(answerDto.getAnswer()[s]); if (s != answerDto.getAnswer().length - 1) { consultantstr.append(","); } } } else if (QuestionTitle.SPECIAL_TYPE_PROPOSAL.equals(questionTitle.getSpecialType())){ if (null != answerDto.getAnswer() && answerDto.getAnswer().length > 0){ for (int s = 0; s < answerDto.getAnswer().length; s++) { proposal.append(answerDto.getAnswer()[s]); } String s = answerDto.getAnswer()[0]; if (s.length() >= 8){ isHighQuality = 1; } } }else if (QuestionTitle.SPECIAL_TYPE_DOCTOR.equals(questionTitle.getSpecialType())){ for (int s = 0; s < answerDto.getAnswer().length; s++) { doctor.append(answerDto.getAnswer()[s]); if (s != answerDto.getAnswer().length - 1) { doctor.append(","); } } }else if (QuestionTitle.SPECIAL_TYPE_RECEPTION.equals(questionTitle.getSpecialType())){ for (int s = 0; s < answerDto.getAnswer().length; s++) { reception.append(answerDto.getAnswer()[s]); if (s != answerDto.getAnswer().length - 1) { reception.append(","); } } } } } } if (nurse.length() > 0){ data.setNurseScore(nurse.toString()); } if (consultantstr.length() > 0){ data.setConsultantScore(consultantstr.toString()); } if (proposal.length() > 0){ data.setProposal(proposal.toString()); } if (doctor.length() > 0){ data.setDoctorScore(doctor.toString()); } if (reception.length() > 0){ data.setReceptionScore(reception.toString()); } data.setIsHighQuality(isHighQuality); int i = investigateDataMapper.insert(data); if (i != 1) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "提交失败!!"); } if (null != records){ //更新评价记录状态 sqlMap.put("isEvaluated",BaseEntity.YES); sqlMap.put("id",records.getId()); sqlMap.put("investigateDataId",data.getId()); sqlSentence.sqlSentence(" isEvaluated = #{m.isEvaluated},investigateDataId = #{m.investigateDataId} where id = #{m.id}",sqlMap); if (sendingRecordsMapper.updateWhere(sqlSentence) != 1){ throw new PlatTipsException(PlatformCode.ERROR_TIPS, "更新记录状态失败!!"); } } // 赠送奖励 if (flag && BaseEntity.YES.equals(questionnaire.getType())) { //1.调查问卷奖励 sqlMap.clear(); sqlMap.put("isDel", BaseEntity.NO); sqlMap.put("investigateId", investigate.getId()); sqlSentence.sqlSentence("select i.*" + " from investigate_award i where i.isDel = #{m.isDel} AND i.investigateId = #{m.investigateId} order by i.createTime ", sqlMap); List awardList = commonDao.selectList(InvestigateAwardMapper.class, sqlSentence); String sql = String.format("SELECT * FROM `user_money` WHERE isDel=0 AND userId='%s'", user.getId()); sqlSentence.setSqlSentence(sql); UserMoney oldUserMoney = userMoneyService.selectOne(sqlSentence); BigDecimal total = BigDecimal.ZERO; for (InvestigateAward investigateAward : awardList) { if (InvestigateAward.TYPE_COUPON.equals(investigateAward.getType())) { sendCoupon(user, investigateAward.getCommonId(), investigate.getId()); } else if (InvestigateAward.TYPE_INTEGRAL.equals(investigateAward.getType())) { //积分 total = total.add(investigateAward.getAmount()); } } //2.题组奖励 for (InvestigateGroupDto investigateGroupDto : questionnaire.getInvestigateGroupDtos()) { sqlMap.put("groupId", investigateGroupDto.getGroupId()); sqlMap.put("investigateId", questionnaire.getInvestigateId()); sqlSentence.sqlSentence("select i.* " + " from investigate_group_award i where i.isDel = #{m.isDel} AND i.investigateId = #{m.investigateId} and i.groupId = #{m.groupId}", sqlMap); List awards = commonService.selectList(InvestigateGroupAwardMapper.class, sqlSentence); for (InvestigateGroupAward award : awards) { if (InvestigateGroupAward.TYPE_COUPON.equals(award.getType())) { //优惠券 sendCoupon(user, award.getCommonId(), investigate.getId()); } else if (InvestigateGroupAward.TYPE_INTEGRAL.equals(award.getType())) { total = total.add(award.getAmount()); } } } //赠送积分 if (total.compareTo(BigDecimal.ZERO) > 0){ UserMoney userMoney = new UserMoney(); userMoney.setIntegral(total.toPlainString()); //userMoneyService.updateData(user.getId(), oldUserMoney, userMoney, user.getId(), user.getName(), "调查问卷赠送积分"); // 构建操作资金对象 UserMoneyUnclaimed paramUserMoneyUnclaimed = new UserMoneyUnclaimed(); paramUserMoneyUnclaimed.setUserId(user.getId()); paramUserMoneyUnclaimed.setFundType(UserMoneyUnclaimed.FUND_TYPE_INTEGRAL); paramUserMoneyUnclaimed.setOriginChannel(OriginChannelConstants.ORIGIN_CHANNEL_CRM); paramUserMoneyUnclaimed.setOriginSubject("调查问卷赠送积分"); paramUserMoneyUnclaimed.setOperationReason(OperationReasonConstants.OP_REASON_QUESTIONNAIRE); paramUserMoneyUnclaimed.setCommonId(questionnaire.getId()); // 减扣原赠送积分 paramUserMoneyUnclaimed.setOpNumber(total); // 处理操作人信息 paramUserMoneyUnclaimed.setOperatorType(UserMoneyUnclaimed.OPERATOR_TYPE_USER); paramUserMoneyUnclaimed.setOperatorId(user.getId()); // 添充详细消息 paramUserMoneyUnclaimed.setRemarks("调查问卷赠送积分"); // 调用方法处理 UserMoneyUtil.addUserMoneyUnclaimedData(paramUserMoneyUnclaimed); } } // 返回调查问卷id return data.getId(); } @Override public void updateIsOpen(String id) { Map objectMap=new HashMap<>(); SqlSentence sqlSentence = new SqlSentence(); sqlSentence.setM(objectMap); objectMap.put("isOpen",BaseEntity.YES); objectMap.put("id",id); sqlSentence.setSqlSentence(" isOpen = #{m.isOpen} where id = #{m.id}"); int count = commonService.updateWhere(SendingRecordsMapper.class,sqlSentence); if (count != 1){ logger.error("更改问卷记录状态失败"+id); //throw new PlatTipsException(PlatformCode.ERROR_TIPS, "更改问卷记录状态失败!!"); } } //更新完成状态 public void updateIsComplete(String id) { Map objectMap=new HashMap<>(); SqlSentence sqlSentence = new SqlSentence(); sqlSentence.setM(objectMap); objectMap.put("isComplete",BaseEntity.YES); objectMap.put("id",id); sqlSentence.setSqlSentence(" isComplete = #{m.isComplete} where id = #{m.id}"); int count = commonService.updateWhere(SendingRecordsMapper.class,sqlSentence); if (count != 1 ){ logger.error("更改问卷记录状态失败"+id); throw new PlatTipsException(PlatformCode.ERROR_TIPS, "更改问卷记录状态失败!!"); } } private void sendCoupon(User user, String couponId, String commonId) { net.sf.json.JSONObject object = new net.sf.json.JSONObject(); object.put("apiId", user.getApiId()); object.put("createTime", user.getCreateTime()); object.put("gender", user.getGender()); object.put("id", user.getId()); object.put("imgUrl", user.getImgUrl()); object.put("name", user.getName()); object.put("tel", user.getTel()); object.put("amount", 1); net.sf.json.JSONArray array = new net.sf.json.JSONArray(); array.add(object); CouponReleaseRecordItem couponReleaseRecordItem = new CouponReleaseRecordItem(); couponReleaseRecordItem.setCouponId(couponId); couponReleaseRecordItem.setRecordName("填写调查问卷赠送"); couponReleaseRecordItem.setCommonId(commonId); couponReleaseRecordItem.setCommonType(CouponReleaseRecordItem.TYPE_COMMON_INVESTIGATE); //赠送优惠券给用户 CouponTool.sendCoupon(couponReleaseRecordItem, user.getId(), user.getName(), CouponReleaseRecord.OPERATOR_TYPE_SYSTEM, "", array, "crm", commonService); } //处理划扣数据 private void handleDeuction(InvestigateData data,String deductionSingleId){ DeductionSingle deductionSingle = deductionSingleMapper.selectOneByKey(deductionSingleId); if (deductionSingle == null) { throw new TipsException("划扣数据不存在!"); } // 判断划扣项目 DeductionProject deductionProject = getDeductionProjectBySingleId(deductionSingle.getId()); if (deductionProject == null) { throw new TipsException("划扣项目数据不存在!"); } if (StringUtils.isEmpty(deductionProject.getUserProjectItemId())) { throw new TipsException("用户项目信息id为空!"); } // 判断用户项目子项 UserProjectItem userProjectItem = userProjectItemMapper.selectOneByKey(deductionProject.getUserProjectItemId()); if (userProjectItem == null) { throw new TipsException("用户项目子项信息没有找到!"); } // 判断用户项目 UserProject userProject = userProjectMapper.selectOneByKey(userProjectItem.getUserProjectId()); if (userProject == null) { throw new TipsException("用户项目信息没有找到!"); } // 获取项目信息 Project project = null; if (OrderGoodsConstants.TYPE_PROJECT.equals(userProject.getType())) { if (!StringUtils.isEmpty(userProject.getGoodsId())) { project = commonService.selectOneByKey(ProjectMapper.class, userProject.getGoodsId()); if (project != null) { data.setProjectId(project.getId()); data.setProjectName(project.getName()); } } } if (!StringUtils.isEmpty(deductionSingle.getShopId())) { Shop shop = commonService.selectOneByKey(com.hx.phip.dao.mapper.ShopMapper.class, deductionSingle.getShopId()); if (shop != null) { data.setShopId(shop.getId()); data.setShopName(shop.getName()); } } data.setDeductionSingleTime(deductionSingle.getCreateTime()); //查询人员信息 SqlSentence sqlSentence = new SqlSentence(); String sql = "select dj.* from deduction_join dj" + " join (select * from employee_role where isDel = #{m.isDel}) er on dj.roleId = er.id" + " join (select * from employee_role_type where isDel = #{m.isDel}) ert on ert.id = er.roleTypeId" + " WHERE dj.isDel =#{m.isDel} AND ert.uniqueStr = #{m.uniqueStr} " + " AND dj.deductionSingleId = #{m.deductionSingleId} AND dj.deductionProjectId = #{m.deductionProjectId}" + " ORDER BY dj.createTime DESC LIMIT 1"; Map map = new HashMap<>(); map.put("isDel", BaseEntity.NO); map.put("uniqueStr", RoleType.UNIQUE_STR_NURSE); map.put("deductionSingleId", deductionSingle.getId()); map.put("deductionProjectId", deductionProject.getId()); sqlSentence.setM(map); sqlSentence.setSqlSentence(sql); DeductionJoin nurse = commonDao.selectOne(DeductionJoinMapper.class,sqlSentence); if (null != nurse){ data.setNurseCorpUserName(nurse.getEmployeeName()); data.setNurseCorpUserId(nurse.getEmployeeId()); }else { logger.info("划扣护士数据为空!"+deductionSingleId); } //查询医生信息 map.put("uniqueStr", RoleType.UNIQUE_STR_DOCTOR); sqlSentence.setM(map); sqlSentence.setSqlSentence(sql); DeductionJoin doctor = commonDao.selectOne(DeductionJoinMapper.class,sqlSentence); if (null != doctor){ data.setDoctorCorpUserName(doctor.getEmployeeName()); data.setDoctorCorpUserId(doctor.getEmployeeId()); }else { logger.info("划扣医生数据为空!"+deductionSingleId); } } private DeductionProject getDeductionProjectBySingleId(String id){ SqlSentence sqlSentence = new SqlSentence(); Map sqlValue = new HashMap<>(); sqlValue.put("isDel", BaseEntity.NO); sqlValue.put("singleId", id); String sql = "SELECT * FROM deduction_project WHERE deductionSingleId=#{m.singleId} AND isDel=#{m.isDel}"; sqlSentence.sqlSentence(sql, sqlValue); return deductionProjectMapper.selectOne(sqlSentence); } //处理划扣数据 private void handleLeaveData(InvestigateData data,String visitOrderId){ SqlSentence sqlSentence = new SqlSentence(); Map sqlMap = new HashMap<>(); sqlMap.put("isDel", BaseEntity.NO); sqlMap.put("visitOrderId", visitOrderId); sqlSentence.sqlSentence("select * from v_treat_project where isDel = 0 AND visitOrderId = #{m.visitOrderId} limit 1", sqlMap); VTreatProject vTreatProject = commonService.selectOne(VTreatProjectMapper.class, sqlSentence); if (null != vTreatProject) { data.setProjectId(vTreatProject.getProjectId()); data.setProjectName(vTreatProject.getProjectName()); } sqlSentence.sqlSentence("select * from v_op_triage where isDel = 0 AND visitOrderId = #{m.visitOrderId} limit 1", sqlMap); VOpTriage vOpTriage = commonService.selectOne(VOpTriageMapper.class, sqlSentence); if (null != vOpTriage){ data.setDoctorCorpUserName(vOpTriage.getDoctorName()); data.setDoctorCorpUserId(vOpTriage.getDoctorId()); data.setNurseCorpUserId(vOpTriage.getNurseId()); data.setNurseCorpUserName(vOpTriage.getNurseName()); Shop shop = commonDao.selectOneByKey(ShopMapper.class,vOpTriage.getShopId()); if (null != shop){ data.setShopName(shop.getName()); data.setShopId(shop.getId()); } } } /** * 获取需要推送消息的员工列表 * 原:客户提交之后,对应推送所属门店店长,对应治疗单分配-MIC,对应治疗单分配-护士,所属顾问 * 改:客户提交之后,对应推送所属门店店长,对应治疗单门店-所有MIC,对应治疗单分配-护士,所属顾问 * @param investigateDataId 数据id * @return 返回 */ public Map getSentNoticeEmployeeList(String investigateDataId) { // 将 Set 转换为 List Map resultData = new HashMap<>(); // 获取信息 InvestigateData investigateData = investigateDataMapper.selectOneByKey(investigateDataId); if (investigateData != null) { // id 集合 resultData = new HashMap<>(); // 获取用户信息 User user = commonService.selectOneByKey(UserMapper.class, investigateData.getUserId()); if (user != null && !StringUtils.isEmpty(user.getHisCorpUserId())) { // 保存值处理 resultData.put(user.getHisCorpUserId(), ""); // 获取主要职称门 Shop mainShop = EmployeeTool.getEmployeeMainShop(commonService, user.getHisCorpUserId()); if (mainShop != null) { // 先找顾问角色 EmployeeRole employeeRole = EmployeeTool.getEmployeeRoleByShopAndRole(commonService, user.getHisCorpUserId(), mainShop.getId(), RoleType.UNIQUE_STR_ADVISER_LEADER); if (employeeRole != null) { resultData.put(user.getHisCorpUserId(), employeeRole.getId()); } else { // 再找顾问助理 employeeRole = EmployeeTool.getEmployeeRoleByShopAndRole(commonService, user.getHisCorpUserId(), mainShop.getId(), RoleType.UNIQUE_STR_ADVISER_ASSISTANT); if (employeeRole != null) { resultData.put(user.getHisCorpUserId(), employeeRole.getId()); } } } } // 获取划扣信息 if (!StringUtils.isEmpty(investigateData.getDeductionSingleId())) { DeductionSingle deductionSingle = commonService.selectOneByKey(DeductionSingleMapper.class, investigateData.getDeductionSingleId()); if (deductionSingle != null) { // 获取当前划扣门店的店长名单 if (!StringUtils.isEmpty(deductionSingle.getShopId())) { List shopOwnerList = EmployeeTool.getEmployeeRoleByShopAndRoleList(commonService, deductionSingle.getShopId(), RoleType.UNIQUE_STR_SHOPOWNER); if (shopOwnerList != null) { // 添加到发送列表 for (EmployeeRole employeeRole : shopOwnerList) { resultData.put(employeeRole.getEmployeeId(), employeeRole.getId()); } } } // 获取治疗通知单 if (!StringUtils.isEmpty(deductionSingle.getTreatSingleId())) { TreatSingle treatSingle = commonService.selectOneByKey(TreatSingleMapper.class, deductionSingle.getTreatSingleId()); if (treatSingle != null) { // 到访订单 if (!StringUtils.isEmpty(treatSingle.getVisitOrderId())) { VisitOrder visitOrder = commonService.selectOneByKey(VisitOrderMapper.class, treatSingle.getVisitOrderId()); if (visitOrder != null) { // 获取治疗通知单护士 if (!StringUtils.isEmpty(visitOrder.gettNurseId())) { // 保存值处理 resultData.put(visitOrder.gettNurseId(), ""); // 先查询下护士角色 EmployeeRole employeeRole = EmployeeTool.getEmployeeRoleByShopAndRole(commonService, visitOrder.gettNurseId(), visitOrder.getShopId(), RoleType.UNIQUE_STR_NURSE); if (employeeRole != null) { resultData.put(visitOrder.gettNurseId(), employeeRole.getId()); } else { // 再查询下首席护士角色 employeeRole = EmployeeTool.getEmployeeRoleByShopAndRole(commonService, visitOrder.gettNurseId(), visitOrder.getShopId(), RoleType.UNIQUE_HEAD_NURSE); if (employeeRole != null) { resultData.put(visitOrder.gettNurseId(), employeeRole.getId()); } } } // 获取治疗通知单MIC /* List micList = EmployeeTool.getMICList(commonService, visitOrder.getId()); if (micList != null){ micList.forEach(employee -> idSet.add(employee.getId())); }*/ } } // 对应治疗单门店-所有MIC List micList = EmployeeTool.getEmployeeRoleByShopAndRoleList(commonService, treatSingle.getShopId(), RoleType.UNIQUE_STR_MIC); if (micList != null) { // 添加到发送列表 for (EmployeeRole employeeRole : micList) { resultData.put(employeeRole.getEmployeeId(), employeeRole.getId()); } } } } } } } return resultData; } /** * 消息发送列表 * 原:客户提交之后,对应推送所属门店店长,对应治疗单分配-MIC,对应治疗单分配-护士,所属顾问 * 改:客户提交之后,对应推送所属门店店长,对应治疗单门店-所有MIC,对应治疗单分配-护士,所属顾问 * @param investigateDataId 数据id * @return 返回 */ @Override public List getSentNoticeIdList(String investigateDataId) { // id 列表 List idList = new ArrayList<>(); // 获取需要发送消息的员工列表 Map sentNoticeEmployeeMap = this.getSentNoticeEmployeeList(investigateDataId); for (String employeeId : sentNoticeEmployeeMap.keySet()) { NotificationLog notificationLog = new NotificationLog(); notificationLog.setCommonId(employeeId); notificationLog.setCommonRoleId(sentNoticeEmployeeMap.get(employeeId)); notificationLog.setCommonType(NotificationLog.COMMON_TYPE_EMPLOYEE); notificationLog.setNotificationType(NotificationLog.NOTIFICATION_TYPE_HIS_NOTICE); notificationLog.setNotificationSubject(NotificationLog.NOTIFICATION_SUBJECT_EVALUATION_SERVICE_NOTICE); notificationLog.setUserMoneyAssociationId(investigateDataId); int insert = commonService.insert(NotificationLogMapper.class, notificationLog); if (insert > 0) { idList.add(notificationLog.getId()); } } return idList; } }