chenjiahe
2024-05-30 a5704a4dd10ad7de4b91f05951e3bb019ccfa256
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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<String> recommendIdList = new ArrayList<>();
            for (String s:consultRecordDto.getRecommendProjectIds().split(",")) {
                recommendIdList.add(s);
            }
            handleRecommendPro(recommendIdList,consultationRecord.getId(), consultRecordDto.getUserId());
        }
 
        //治疗项目
        if (!StringUtils.isEmpty(consultRecordDto.getTreatProjectIds())) {
            List<String> 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<String> treatProjectIds,String recordId,String userId){
 
        SqlSentence sqlSentence = new SqlSentence();
        StringBuffer sql = new StringBuffer();
        Map<String, Object> values = new HashMap<>();
        sqlSentence.setM(values);
        //存储需要删除的数据
        Map<String, ConsultationTreatProject> 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<ConsultationTreatProject> treatProjectList = consultationTreatProjectMapper.selectList(sqlSentence);
        if (treatProjectList != null && treatProjectList.size() != 0) {
            delMap = treatProjectList.stream().collect(Collectors.toMap(ConsultationTreatProject::getId, Function.identity()));
        }
 
        //当前的项目数据
        List<ConsultationTreatProject> 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<String, ConsultationTreatProject> entry : delMap.entrySet()) {
                    if (consultationTreatProjectMapper.deleteById(entry.getValue().getId()) != 1) {
                        throw new TipsException("删除治疗项目失败!");
                    }
                }
            }
        }
    }
 
 
    /**预约项目*/
    public void preProjectHandle(String recordId,String userId,List<ConsultTreatProjectVo> 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<ConsultAppearancePlanVo> 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<String> recommendProjectIds,String recordId,String userId){
 
        SqlSentence sqlSentence = new SqlSentence();
        StringBuffer sql = new StringBuffer();
        Map<String, Object> values = new HashMap<>();
        sqlSentence.setM(values);
        //存储需要删除的数据
        Map<String, ConsultationRecommendedProject> 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<ConsultationRecommendedProject> recommendProjectList = consultationRecommendedProjectMapper.selectList(sqlSentence);
        if (recommendProjectList != null && recommendProjectList.size() != 0) {
            delMap = recommendProjectList.stream().collect(Collectors.toMap(ConsultationRecommendedProject::getId, Function.identity()));
        }
 
        List<ConsultationRecommendedProject> 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<String, ConsultationRecommendedProject> entry : delMap.entrySet()) {
                    if (consultationRecommendedProjectMapper.deleteById(entry.getValue().getId()) != 1) {
                        throw new TipsException("删除推荐项目息失败!");
                    }
                }
            }
        }
 
 
    }
 
}