package com.hx.phip.service.consultation.impl; 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.mybatisTool.SqlSentence; import com.hx.phiappt.common.PlatformConstants; import com.hx.phiappt.constants.tool.UserInfoTool; import com.hx.phiappt.dao.mapper.*; import com.hx.phiappt.model.*; import com.hx.phip.service.SystemParameterService; import com.hx.phip.service.consultation.ConsultationRecordService; import com.hx.util.StringUtils; import com.hz.his.dto.consultation.ConsultAppearancePlanVo; import com.hz.his.dto.consultation.ConsultRecordDto; import com.hz.his.dto.consultation.ConsultTreatProjectVo; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @Service @Transactional public class ConsultationRecordServiceImpl implements ConsultationRecordService { @Resource private ConsultationRecordMapper consultationRecordMapper; @Resource private CommonDao commonDao; @Resource private ConsultationTreatProjectMapper consultationTreatProjectMapper; @Resource private ConsultationRecommendedProjectMapper consultationRecommendedProjectMapper; @Resource private ProjectMapper projectMapper; @Resource private ConsultationAppearancePlanMapper appearancePlanMapper; @Override public String insertOrEdit(ConsultRecordDto consultRecordDto, SystemParameterService systemParameterService, CommonService commonService) { //咨询信息部分 ConsultationRecord consultationRecord = new ConsultationRecord(); if (!StringUtils.isEmpty(consultationRecord.getContent()) && consultRecordDto.getContent().equals("")) { consultRecordDto.setContent(null); } BeanUtils.copyProperties(consultRecordDto,consultationRecord); if(StringUtils.isEmpty(consultationRecord.getId())){ if (consultationRecordMapper.insert(consultationRecord) != 1) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "新增咨询记录失败!"); } // 添加咨询用户日志 User user = commonDao.selectOneByKey(UserMapper.class, consultationRecord.getUserId()); if (user != null) { String opEmployeeId = null; SysAdmin opSysAdmin = null; if (BaseEntity.NO.equals(consultationRecord.getAdminType())) { opEmployeeId = consultationRecord.getAdminId(); } else { opSysAdmin = commonDao.selectOneByKey(SysAdminMapper.class, consultationRecord.getAdminId()); } // 用户日志 JSONArray logArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); jsonObject.put("key", "(新版)添加咨询记录"); logArray.add(jsonObject); // 记录编号 String recordNo = systemParameterService.createUSLGNo(user.getId(), user.getCIQ()); //操作平台类型 String opType = null; if(consultRecordDto.getAdminType() == 1){ opType = PlatformConstants.TYPE_PLATFORM_PHIS; }else if(consultRecordDto.getAdminType() == 0){ opType = PlatformConstants.TYPE_PLATFORM_HIS; } // 添加用户日志 UserInfoTool.addUserUpdateLog(commonService, opType, user, user.getShopId(), opEmployeeId, opSysAdmin, null, "(新版)添加咨询记录", logArray, null, recordNo); } }else { if (consultationRecordMapper.updateAll(consultationRecord) != 1) { throw new PlatTipsException(PlatformCode.ERROR_TIPS, "更新咨询记录失败!"); } } //推荐项目 if (!StringUtils.isEmpty(consultRecordDto.getRecommendProjectIds())) { List recommendIdList = new ArrayList<>(); for (String s:consultRecordDto.getRecommendProjectIds().split(",")) { recommendIdList.add(s); } handleRecommendPro(recommendIdList,consultationRecord.getId(), consultRecordDto.getUserId()); } //治疗项目 if (!StringUtils.isEmpty(consultRecordDto.getTreatProjectIds())) { List treatIdList = new ArrayList<>(); for (String s:consultRecordDto.getTreatProjectIds().split(",")) { treatIdList.add(s); } handleTreatPro(treatIdList,consultationRecord.getId(), consultRecordDto.getUserId()); } //预约项目 preProjectHandle(consultationRecord.getId(),consultationRecord.getUserId(),consultRecordDto.getPreProjectList()); //颜值规划 appearancePlanHandle(consultationRecord.getId(),consultRecordDto.getAppearancePlanList()); return consultationRecord.getId(); } ///////////////////////////////////////////////// public Project getProjectById(String projectId) { Project project = commonDao.selectOneByKey(ProjectMapper.class, projectId); return project; } //--------------------------------------------------------------------- /**处理治疗项目*/ public void handleTreatPro(List treatProjectIds,String recordId,String userId){ SqlSentence sqlSentence = new SqlSentence(); StringBuffer sql = new StringBuffer(); Map values = new HashMap<>(); sqlSentence.setM(values); //存储需要删除的数据 Map delMap = new HashMap<>(); values.put("recordId", recordId); values.put("type", ConsultationTreatProject.PROJECT_TYPE_TREAT); values.put("userId", userId); String sqlStr = "select * from consultation_treat_project where consultationRecordId = #{m.recordId} and isDel = 0 and userId = #{m.userId} and type = #{m.type}"; //原本的数据 sql.append(sqlStr); sqlSentence.setSqlSentence(sql.toString()); List treatProjectList = consultationTreatProjectMapper.selectList(sqlSentence); if (treatProjectList != null && treatProjectList.size() != 0) { delMap = treatProjectList.stream().collect(Collectors.toMap(ConsultationTreatProject::getId, Function.identity())); } //当前的项目数据 List thisTreatProjectList = null; if (treatProjectIds != null && treatProjectIds.size() > 0) { sql.delete(0, sql.length()); sql.append(sqlStr).append(" and projectId in ( "); for (String proId : treatProjectIds) { sql.append("'").append(proId).append("',"); } sql.delete(sql.length()-1, sql.length()).append(" ) "); sqlSentence.setSqlSentence(sql.toString()); thisTreatProjectList = consultationTreatProjectMapper.selectList(sqlSentence); } //不存在则新增 if(thisTreatProjectList == null || thisTreatProjectList.size() <=0){ for (String proId : treatProjectIds) { Project project = getProjectById(proId); if (project == null) { throw new TipsException("找不到项目信息!"); } ConsultationTreatProject treatPro= new ConsultationTreatProject(userId, project.getId(), project.getName(), recordId, ConsultationTreatProject.PROJECT_TYPE_TREAT); if (consultationTreatProjectMapper.insert(treatPro) != 1) { throw new TipsException("新增治疗项目信息信息失败!"); } } }else { for (ConsultationTreatProject treatPro : thisTreatProjectList) { //如果存在,表示没有变动 if (delMap != null && delMap.size() != 0 && delMap.containsKey(treatPro.getId())) { delMap.remove(treatPro.getId()); continue; } Project project = getProjectById(treatPro.getProjectId()); if (project == null) { throw new TipsException("找不到项目信息!"); } //不存在则更新 treatPro = new ConsultationTreatProject(userId, project.getId(), project.getName(), recordId, ConsultationTreatProject.PROJECT_TYPE_TREAT); if (consultationTreatProjectMapper.insert(treatPro) != 1) { throw new TipsException("新增治疗项目信息信息失败!"); } } if (delMap != null && delMap.size() != 0) { //删除 for (Map.Entry entry : delMap.entrySet()) { if (consultationTreatProjectMapper.deleteById(entry.getValue().getId()) != 1) { throw new TipsException("删除治疗项目失败!"); } } } } } /**预约项目*/ public void preProjectHandle(String recordId,String userId,List preProjectList){ //先删除旧的 consultationTreatProjectMapper.deleteByRecordId(recordId,ConsultationTreatProject.PROJECT_TYPE_PRE); if(preProjectList == null || preProjectList.size() == 0){ return; } ConsultationTreatProject consultationTreatProject; Project project; for(ConsultTreatProjectVo consultTreatProjectVo:preProjectList){ if(StringUtils.isEmpty(consultTreatProjectVo.getProjectId())){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"预约项目标识不能空!"); } project = projectMapper.selectOneByKey(consultTreatProjectVo.getProjectId()); if(project == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"预约项目标识错误!"); } consultationTreatProject = new ConsultationTreatProject(userId,project.getId(),project.getName(),recordId,ConsultationTreatProject.PROJECT_TYPE_PRE); consultationTreatProjectMapper.insert(consultationTreatProject); } } /**颜值规划*/ public void appearancePlanHandle(String recordId,List appearancePlanList){ //先删除旧的 appearancePlanMapper.deleteByRecordId(recordId); if(appearancePlanList == null || appearancePlanList.size() == 0){ return; } ConsultationAppearancePlan consultationAppearancePlan; for(ConsultAppearancePlanVo consultAppearancePlanVo:appearancePlanList){ consultationAppearancePlan = new ConsultationAppearancePlan(consultAppearancePlanVo.getDetails(),recordId); appearancePlanMapper.insert(consultationAppearancePlan); } } //处理推荐项目 public void handleRecommendPro(List recommendProjectIds,String recordId,String userId){ SqlSentence sqlSentence = new SqlSentence(); StringBuffer sql = new StringBuffer(); Map values = new HashMap<>(); sqlSentence.setM(values); //存储需要删除的数据 Map delMap = new HashMap<>(); values.put("recordId", recordId); values.put("userId", userId); String sqlStr = "select * from consultation_recommended_project where consultationRecordId = #{m.recordId} and isDel = 0 and userId = #{m.userId} "; //原本的数据 sql.append(sqlStr); sqlSentence.setSqlSentence(sql.toString()); List recommendProjectList = consultationRecommendedProjectMapper.selectList(sqlSentence); if (recommendProjectList != null && recommendProjectList.size() != 0) { delMap = recommendProjectList.stream().collect(Collectors.toMap(ConsultationRecommendedProject::getId, Function.identity())); } List thisRecommendProjectList = null; if(recommendProjectIds!= null && recommendProjectIds.size() > 0){ sql.delete(0,sql.length()); sql.append(sqlStr).append(" and projectId in ( "); for (String proId: recommendProjectIds) { sql.append("'").append(proId).append("',"); } sql.delete(sql.length()-1, sql.length()).append(" ) "); sqlSentence.setSqlSentence(sql.toString()); thisRecommendProjectList = consultationRecommendedProjectMapper.selectList(sqlSentence); } if(thisRecommendProjectList== null || thisRecommendProjectList.size() <=0 ){ for (String proId: recommendProjectIds) { Project project = getProjectById(proId); if(project == null){ throw new TipsException("找不到推荐项目信息!"); } //不存在则更新 ConsultationRecommendedProject recommendedProject = new ConsultationRecommendedProject(userId,project.getId(), project.getName(), recordId); if (consultationRecommendedProjectMapper.insert(recommendedProject) != 1) { throw new TipsException("新增推荐项目息信息失败!"); } } }else { for (ConsultationRecommendedProject recommendedProject: thisRecommendProjectList) { //如果存在,表示没有变动 if (delMap != null && delMap.size() != 0 && delMap.containsKey(recommendedProject.getId())) { delMap.remove(recommendedProject.getId()); continue; } Project project = getProjectById(recommendedProject.getProjectId()); if(project == null){ throw new TipsException("找不到推荐项目信息!"); } //不存在则更新 recommendedProject = new ConsultationRecommendedProject(userId,project.getId(), project.getName(), recordId); if (consultationRecommendedProjectMapper.insert(recommendedProject) != 1) { throw new TipsException("新增推荐项目息信息失败!"); } } if (delMap != null && delMap.size() != 0) { //删除 for (Map.Entry entry : delMap.entrySet()) { if (consultationRecommendedProjectMapper.deleteById(entry.getValue().getId()) != 1) { throw new TipsException("删除推荐项目息失败!"); } } } } } }