chenjiahe
2024-05-30 a5704a4dd10ad7de4b91f05951e3bb019ccfa256
提交 | 用户 | age
70831a 1 package com.hx.phip.service.consultation.impl;
R 2
3 import com.alibaba.fastjson.JSONArray;
fac939 4 import com.alibaba.fastjson.JSONObject;
70831a 5 import com.hx.common.dao.CommonDao;
fac939 6 import com.hx.common.service.CommonService;
70831a 7 import com.hx.exception.TipsException;
R 8 import com.hx.mybatisTool.SqlSentence;
fac939 9 import com.hx.phiappt.common.PlatformConstants;
R 10 import com.hx.phiappt.constants.tool.UserInfoTool;
b33cd3 11 import com.hx.phiappt.dao.mapper.*;
fac939 12 import com.hx.phiappt.model.*;
R 13 import com.hx.phip.service.SystemParameterService;
70831a 14 import com.hx.phip.service.consultation.ConsultationRecordService;
R 15 import com.hx.util.StringUtils;
a5704a 16 import com.hz.his.dto.consultation.ConsultAppearancePlanVo;
70831a 17 import com.hz.his.dto.consultation.ConsultRecordDto;
a5704a 18 import com.hz.his.dto.consultation.ConsultTreatProjectVo;
70831a 19 import com.platform.exception.PlatTipsException;
R 20 import com.platform.resultTool.PlatformCode;
21 import org.springframework.beans.BeanUtils;
22 import org.springframework.stereotype.Service;
23 import org.springframework.transaction.annotation.Transactional;
24
25 import javax.annotation.Resource;
fac939 26 import java.util.*;
70831a 27 import java.util.function.Function;
R 28 import java.util.stream.Collectors;
29
30 @Service
31 @Transactional
32 public class ConsultationRecordServiceImpl implements ConsultationRecordService {
33
34     @Resource
35     private ConsultationRecordMapper consultationRecordMapper;
36     @Resource
37     private CommonDao commonDao;
38     @Resource
39     private ConsultationTreatProjectMapper consultationTreatProjectMapper;
40     @Resource
41     private ConsultationRecommendedProjectMapper consultationRecommendedProjectMapper;
a5704a 42     @Resource
C 43     private ProjectMapper projectMapper;
44     @Resource
45     private ConsultationAppearancePlanMapper appearancePlanMapper;
70831a 46
R 47     @Override
fac939 48     public String insertOrEdit(ConsultRecordDto consultRecordDto, SystemParameterService systemParameterService, CommonService commonService) {
70831a 49
R 50         //咨询信息部分
51         ConsultationRecord consultationRecord = new ConsultationRecord();
806848 52         if (!StringUtils.isEmpty(consultationRecord.getContent()) && consultRecordDto.getContent().equals("")) {
R 53             consultRecordDto.setContent(null);
54         }
70831a 55         BeanUtils.copyProperties(consultRecordDto,consultationRecord);
R 56
57         if(StringUtils.isEmpty(consultationRecord.getId())){
58             if (consultationRecordMapper.insert(consultationRecord) != 1) {
59                 throw new PlatTipsException(PlatformCode.ERROR_TIPS, "新增咨询记录失败!");
60             }
fac939 61
R 62             // 添加咨询用户日志
63             User user = commonDao.selectOneByKey(UserMapper.class, consultationRecord.getUserId());
64             if (user != null) {
65                 String opEmployeeId = null;
66                 SysAdmin opSysAdmin = null;
67                 if (BaseEntity.NO.equals(consultationRecord.getAdminType())) {
68                     opEmployeeId = consultationRecord.getAdminId();
69                 } else {
70                     opSysAdmin = commonDao.selectOneByKey(SysAdminMapper.class, consultationRecord.getAdminId());
71                 }
72                 // 用户日志
73                 JSONArray logArray = new JSONArray();
74                 JSONObject jsonObject = new JSONObject();
75                 jsonObject.put("key", "(新版)添加咨询记录");
76                 logArray.add(jsonObject);
77                 // 记录编号
78                 String recordNo = systemParameterService.createUSLGNo(user.getId(), user.getCIQ());
79
80                 //操作平台类型
81                 String opType = null;
82                 if(consultRecordDto.getAdminType() == 1){
83                     opType = PlatformConstants.TYPE_PLATFORM_PHIS;
84                 }else if(consultRecordDto.getAdminType() == 0){
85                     opType = PlatformConstants.TYPE_PLATFORM_HIS;
86                 }
87                 // 添加用户日志
88                 UserInfoTool.addUserUpdateLog(commonService, opType, user, user.getShopId(), opEmployeeId, opSysAdmin, null, "(新版)添加咨询记录", logArray, null, recordNo);
89             }
70831a 90         }else {
R 91             if (consultationRecordMapper.updateAll(consultationRecord) != 1) {
92                 throw new PlatTipsException(PlatformCode.ERROR_TIPS, "更新咨询记录失败!");
93             }
94         }
788548 95
a5704a 96         //推荐项目
C 97         if (!StringUtils.isEmpty(consultRecordDto.getRecommendProjectIds())) {
98             List<String> recommendIdList = new ArrayList<>();
99             for (String s:consultRecordDto.getRecommendProjectIds().split(",")) {
100                 recommendIdList.add(s);
101             }
102             handleRecommendPro(recommendIdList,consultationRecord.getId(), consultRecordDto.getUserId());
103         }
104
70831a 105         //治疗项目
R 106         if (!StringUtils.isEmpty(consultRecordDto.getTreatProjectIds())) {
788548 107             List<String> treatIdList = new ArrayList<>();
R 108             for (String s:consultRecordDto.getTreatProjectIds().split(",")) {
109                 treatIdList.add(s);
110             }
70831a 111             handleTreatPro(treatIdList,consultationRecord.getId(), consultRecordDto.getUserId());
R 112         }
113
a5704a 114         //预约项目
C 115         preProjectHandle(consultationRecord.getId(),consultationRecord.getUserId(),consultRecordDto.getPreProjectList());
116         //颜值规划
117         appearancePlanHandle(consultationRecord.getId(),consultRecordDto.getAppearancePlanList());
70831a 118
R 119         return consultationRecord.getId();
120     }
121
122
123     /////////////////////////////////////////////////
124
125     public Project getProjectById(String projectId) {
126
127         Project project = commonDao.selectOneByKey(ProjectMapper.class, projectId);
128         return project;
129
130     }
131
132
133
134     //---------------------------------------------------------------------
135
a5704a 136     /**处理治疗项目*/
70831a 137     public void handleTreatPro(List<String> treatProjectIds,String recordId,String userId){
R 138
139         SqlSentence sqlSentence = new SqlSentence();
140         StringBuffer sql = new StringBuffer();
141         Map<String, Object> values = new HashMap<>();
142         sqlSentence.setM(values);
143         //存储需要删除的数据
144         Map<String, ConsultationTreatProject> delMap = new HashMap<>();
145
146         values.put("recordId", recordId);
147         values.put("type", ConsultationTreatProject.PROJECT_TYPE_TREAT);
148         values.put("userId", userId);
149
150         String sqlStr = "select * from consultation_treat_project where consultationRecordId = #{m.recordId} and isDel = 0 and userId = #{m.userId} and type = #{m.type}";
151
152         //原本的数据
153         sql.append(sqlStr);
154         sqlSentence.setSqlSentence(sql.toString());
155         List<ConsultationTreatProject> treatProjectList = consultationTreatProjectMapper.selectList(sqlSentence);
156         if (treatProjectList != null && treatProjectList.size() != 0) {
157             delMap = treatProjectList.stream().collect(Collectors.toMap(ConsultationTreatProject::getId, Function.identity()));
158         }
159
788548 160         //当前的项目数据
70831a 161         List<ConsultationTreatProject> thisTreatProjectList = null;
R 162         if (treatProjectIds != null && treatProjectIds.size() > 0) {
163             sql.delete(0, sql.length());
164             sql.append(sqlStr).append(" and projectId in ( ");
165             for (String proId : treatProjectIds) {
166                 sql.append("'").append(proId).append("',");
167             }
788548 168             sql.delete(sql.length()-1, sql.length()).append(" ) ");
70831a 169             sqlSentence.setSqlSentence(sql.toString());
R 170             thisTreatProjectList = consultationTreatProjectMapper.selectList(sqlSentence);
171         }
172
788548 173         //不存在则新增
R 174         if(thisTreatProjectList == null || thisTreatProjectList.size() <=0){
175             for (String proId : treatProjectIds) {
176                 Project project = getProjectById(proId);
177                 if (project == null) {
178                     throw new TipsException("找不到项目信息!");
179                 }
180                 ConsultationTreatProject treatPro= new ConsultationTreatProject(userId, project.getId(), project.getName(), recordId, ConsultationTreatProject.PROJECT_TYPE_TREAT);
181                 if (consultationTreatProjectMapper.insert(treatPro) != 1) {
182                     throw new TipsException("新增治疗项目信息信息失败!");
183                 }
70831a 184             }
788548 185         }else {
R 186             for (ConsultationTreatProject treatPro : thisTreatProjectList) {
187                 //如果存在,表示没有变动
188                 if (delMap != null && delMap.size() != 0 && delMap.containsKey(treatPro.getId())) {
189                     delMap.remove(treatPro.getId());
190                     continue;
191                 }
70831a 192
788548 193                 Project project = getProjectById(treatPro.getProjectId());
R 194                 if (project == null) {
195                     throw new TipsException("找不到项目信息!");
196                 }
197
198                 //不存在则更新
199                 treatPro = new ConsultationTreatProject(userId, project.getId(), project.getName(), recordId, ConsultationTreatProject.PROJECT_TYPE_TREAT);
200                 if (consultationTreatProjectMapper.insert(treatPro) != 1) {
201                     throw new TipsException("新增治疗项目信息信息失败!");
202                 }
203
70831a 204             }
788548 205             if (delMap != null && delMap.size() != 0) {
R 206                 //删除
207                 for (Map.Entry<String, ConsultationTreatProject> entry : delMap.entrySet()) {
208                     if (consultationTreatProjectMapper.deleteById(entry.getValue().getId()) != 1) {
209                         throw new TipsException("删除治疗项目失败!");
210                     }
70831a 211                 }
R 212             }
213         }
214     }
215
216
a5704a 217     /**预约项目*/
C 218     public void preProjectHandle(String recordId,String userId,List<ConsultTreatProjectVo> preProjectList){
219
220         //先删除旧的
221         consultationTreatProjectMapper.deleteByRecordId(recordId,ConsultationTreatProject.PROJECT_TYPE_PRE);
222
223         if(preProjectList == null || preProjectList.size() == 0){
224             return;
225         }
226
227         ConsultationTreatProject consultationTreatProject;
228         Project project;
229         for(ConsultTreatProjectVo consultTreatProjectVo:preProjectList){
230             if(StringUtils.isEmpty(consultTreatProjectVo.getProjectId())){
231                 throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"预约项目标识不能空!");
232             }
233             project = projectMapper.selectOneByKey(consultTreatProjectVo.getProjectId());
234             if(project == null){
235                 throw new PlatTipsException(PlatformCode.ERROR_TIPS,"预约项目标识错误!");
236             }
237             consultationTreatProject = new ConsultationTreatProject(userId,project.getId(),project.getName(),recordId,ConsultationTreatProject.PROJECT_TYPE_PRE);
238             consultationTreatProjectMapper.insert(consultationTreatProject);
239         }
240     }
241
242     /**颜值规划*/
243     public void appearancePlanHandle(String recordId,List<ConsultAppearancePlanVo> appearancePlanList){
244
245         //先删除旧的
246         appearancePlanMapper.deleteByRecordId(recordId);
247
248         if(appearancePlanList == null || appearancePlanList.size() == 0){
249             return;
250         }
251
252         ConsultationAppearancePlan consultationAppearancePlan;
253         for(ConsultAppearancePlanVo consultAppearancePlanVo:appearancePlanList){
254             consultationAppearancePlan = new ConsultationAppearancePlan(consultAppearancePlanVo.getDetails(),recordId);
255             appearancePlanMapper.insert(consultationAppearancePlan);
256         }
257     }
258
259
70831a 260     //处理推荐项目
R 261     public void handleRecommendPro(List<String> recommendProjectIds,String recordId,String userId){
262
263         SqlSentence sqlSentence = new SqlSentence();
264         StringBuffer sql = new StringBuffer();
265         Map<String, Object> values = new HashMap<>();
266         sqlSentence.setM(values);
267         //存储需要删除的数据
268         Map<String, ConsultationRecommendedProject> delMap = new HashMap<>();
269
270         values.put("recordId", recordId);
271         values.put("userId", userId);
272
273         String sqlStr = "select * from consultation_recommended_project where consultationRecordId = #{m.recordId} and isDel = 0 and userId = #{m.userId} ";
274
275         //原本的数据
276         sql.append(sqlStr);
277         sqlSentence.setSqlSentence(sql.toString());
278         List<ConsultationRecommendedProject> recommendProjectList = consultationRecommendedProjectMapper.selectList(sqlSentence);
279         if (recommendProjectList != null && recommendProjectList.size() != 0) {
280             delMap = recommendProjectList.stream().collect(Collectors.toMap(ConsultationRecommendedProject::getId, Function.identity()));
281         }
282
283         List<ConsultationRecommendedProject> thisRecommendProjectList = null;
284         if(recommendProjectIds!= null && recommendProjectIds.size() > 0){
285             sql.delete(0,sql.length());
286             sql.append(sqlStr).append(" and projectId in ( ");
287             for (String proId: recommendProjectIds) {
288                 sql.append("'").append(proId).append("',");
289             }
788548 290             sql.delete(sql.length()-1, sql.length()).append(" ) ");
70831a 291             sqlSentence.setSqlSentence(sql.toString());
R 292             thisRecommendProjectList = consultationRecommendedProjectMapper.selectList(sqlSentence);
293         }
294
788548 295         if(thisRecommendProjectList== null || thisRecommendProjectList.size() <=0 ){
R 296             for (String proId: recommendProjectIds) {
297                 Project project = getProjectById(proId);
298                 if(project == null){
299                     throw new TipsException("找不到推荐项目信息!");
300                 }
301
302                 //不存在则更新
303                 ConsultationRecommendedProject recommendedProject = new ConsultationRecommendedProject(userId,project.getId(), project.getName(), recordId);
304                 if (consultationRecommendedProjectMapper.insert(recommendedProject) != 1) {
305                     throw new TipsException("新增推荐项目息信息失败!");
306                 }
307             }
308         }else {
309             for (ConsultationRecommendedProject recommendedProject: thisRecommendProjectList) {
310                 //如果存在,表示没有变动
311                 if (delMap != null && delMap.size() != 0 && delMap.containsKey(recommendedProject.getId())) {
312                     delMap.remove(recommendedProject.getId());
313                     continue;
314                 }
315
316                 Project project = getProjectById(recommendedProject.getProjectId());
317                 if(project == null){
318                     throw new TipsException("找不到推荐项目信息!");
319                 }
320
321                 //不存在则更新
322                 recommendedProject = new ConsultationRecommendedProject(userId,project.getId(), project.getName(), recordId);
323                 if (consultationRecommendedProjectMapper.insert(recommendedProject) != 1) {
324                     throw new TipsException("新增推荐项目息信息失败!");
325                 }
70831a 326             }
R 327
788548 328             if (delMap != null && delMap.size() != 0) {
R 329                 //删除
330                 for (Map.Entry<String, ConsultationRecommendedProject> entry : delMap.entrySet()) {
331                     if (consultationRecommendedProjectMapper.deleteById(entry.getValue().getId()) != 1) {
332                         throw new TipsException("删除推荐项目息失败!");
333                     }
70831a 334                 }
R 335             }
336         }
788548 337
R 338
70831a 339     }
R 340
341 }