fhx
2023-12-08 37022a5aa114261dcb9e4f312b07c6e029f59e24
提交 | 用户 | age
2cfe2a 1 package com.hx.phip.service.treat.impl;
F 2
3 import com.hx.common.dao.CommonDao;
4 import com.hx.common.service.CommonService;
5 import com.hx.exception.TipsException;
6 import com.hx.guide.common.VisitOrderCommon;
7 import com.hx.guide.common.VisitOrderLogConstants;
8 import com.hx.guide.config.BaseConfig;
9 import com.hx.guide.model.VisitOrder;
10 import com.hx.guide.service.tools.TreatSingleLogTool;
11 import com.hx.guide.service.tools.VGuideLogTool;
12 import com.hx.guide.service.tools.VLatelyLogTool;
13 import com.hx.guide.util.CreateNo;
14 import com.hx.guide.util.TreatNoticeUtil;
15 import com.hx.mybatisTool.SqlSentence;
16 import com.hx.phiappt.common.RoleType;
17 import com.hx.phiappt.common.TreatSingleConstants;
d6e1c5 18 import com.hx.phiappt.constants.tool.employee.EmployeeTool;
2cfe2a 19 import com.hx.phiappt.dao.mapper.TreatProjectIndicationMapper;
F 20 import com.hx.phiappt.dao.mapper.TreatSingleSignMapper;
21 import com.hx.phiappt.model.*;
22 import com.hx.phiappt.model.treat.*;
d6e1c5 23 import com.hx.phip.common.wx.corp.WeiXinCorpMpUtil;
F 24 import com.hx.phip.common.wx.corp.WeiXinMpUtil;
2cfe2a 25 import com.hx.phip.config.CustomParameter;
F 26 import com.hx.phip.config.GlobalExceptionHandler;
27 import com.hx.phip.dao.mapper.*;
28 import com.hx.phip.service.treat.TreatV3Service;
29 import com.hx.phip.tool.SendSMSTool;
30 import com.hx.phip.util.api.CrmUtil;
31 import com.hx.phip.util.api.TreatUtil;
d6e1c5 32 import com.hx.util.MapUtil;
2cfe2a 33 import com.hx.util.StringUtils;
F 34 import com.platform.constants.PlatformPattern;
35 import com.platform.exception.PlatTipsException;
36 import com.platform.resultTool.PlatformCode;
37 import net.sf.json.JSONObject;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
41 import org.springframework.transaction.annotation.Transactional;
42
43 import javax.annotation.Resource;
44 import java.util.*;
45 import java.util.function.Function;
46 import java.util.stream.Collectors;
47
48 @Transactional
49 @Service
50 public class TreatV3ServiceImpl implements TreatV3Service {
51
52     private static Logger logger = LoggerFactory.getLogger(TreatV3ServiceImpl.class);
53
54
55     @Resource
56     private ShopMapper shopMapper;
57     @Resource
58     private EmployeeMapper employeeMapper;
59     @Resource
60     private TreatSingleMapper treatSingleMapper;
61     @Resource
62     private TreatProjectMapper treatProjectMapper;
63     @Resource
64     private UserMapper userMapper;
65     @Resource
66     private CommonService commonService;
67     @Resource
68     private CustomParameter customParameter;
69     @Resource
70     private VisitOrderMapper visitOrderMapper;
71     @Resource
72     private CommonDao commonDao;
73     @Resource
74     private TreatProjectDoctorMapper treatProjectDoctorMapper;
75     @Resource
76     private TreatBodyPartMapper treatBodyPartMapper;
d6e1c5 77
2cfe2a 78
F 79
80     /** 新增治疗通知单 */
81     @Override
82     public void add(TreatSingle treatSingle, List<TreatProject> projectList, VisitOrder visitOrder
1c1cdf 83             , EmployeeRole employeeRole, Integer isMicNotice, List<TreatSingleSign> signList) {
2cfe2a 84
F 85         SqlSentence sqlSentence = new SqlSentence();
86         Map<String, Object> values = new HashMap<>();
87         sqlSentence.setM(values);
88
89         //判断查询操作员工信息
90         Employee employee = employeeMapper.selectOneByKey(treatSingle.getOperatorId());
91         if (employee == null) {
92             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到操作人的信息[01]:" + treatSingle.getOperatorId());
93         }
94         treatSingle.setOperatorName(employee.getCnName());
95         //创建编号
96         treatSingle.setTreatSingleNo(CreateNo.createGivenNo("ZLD", employee.getCnName()));
97
98
99         //判断查询操作员工门店信息
100         Shop shop = shopMapper.selectOneByKey(treatSingle.getShopId());
101         if (shop == null) {
102             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到操作人门店信息" + treatSingle.getShopId());
103         }
104         treatSingle.setShopName(shop.getName());
105
106         //判断查询开单人
107         employee = employeeMapper.selectOneByKey(treatSingle.getDeveloperId());
108         if (employee == null) {
109             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到开单人的信息" + treatSingle.getDeveloperId());
110         }
111         treatSingle.setDeveloperName(employee.getCnName());
112
113         //判断查询开单人门店信息
114         shop = shopMapper.selectOneByKey(treatSingle.getDeveloperShopId());
115         if (shop == null) {
116             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到开单人门店信息" + treatSingle.getDeveloperShopId());
117         }
118         treatSingle.setDeveloperShopName(shop.getName());
119
120         //创建治疗通知单
121         if (treatSingleMapper.insert(treatSingle) != 1) {
122             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "新增治疗通知单失败!");
123         }
124
125         //更改到访订单中的的是否开治疗单状态-------------------------------------------------------
126         values.clear();
127         values.put("id", visitOrder.getId());
128         sqlSentence.setSqlSentence("  isTreatOrder = isTreatOrder + 1 where id = #{m.id}  ");
129         //如果到访单为待沟通,则创建治疗通知单后修改状态为:分配治疗
130         if(visitOrder.getStatus() == VisitOrderCommon.WAIT_GT){
131             values.put("status", VisitOrderCommon.STATUS_DISTRIBUTIVE_TREAT);
132             sqlSentence.setSqlSentence(" status = #{m.status}, isTreatOrder = isTreatOrder + 1 where id = #{m.id}  ");
133         }
134         if (visitOrderMapper.updateWhere(sqlSentence) != 1) {
135             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "新增治疗通知单:更新到访订单状态失败!");
136         }
137
138         //创建治疗项目-----------------------------------------------------------------------------
d6e1c5 139         StringBuffer projectNameSb = new StringBuffer();
F 140         String doctorName = null;
2cfe2a 141         for (TreatProject project : projectList) {
F 142             project.setPositionData("");
143             project.setTreatSingleNo(treatSingle.getTreatSingleNo());
144             project.setTreatSingleId(treatSingle.getId());
145             if (treatProjectMapper.insert(project) != 1) {
146                 throw new PlatTipsException(PlatformCode.ERROR_TIPS, "新增治疗项目失败!");
147             }
148             //新增关联治疗通知单项目的医生
d6e1c5 149             doctorName = addTreatProjectDoctor(project, doctorName);
2cfe2a 150             //新增处理部位信息
F 151             handleBodyPart(project, false);
152             //处理项目适应症
153             handleTreatProjectIndication(project, true);
d6e1c5 154             //拼接项目名
F 155             projectNameSb.append(projectNameSb.length() > 0 ? "+" : "").append(project.getProjectName());
2cfe2a 156         }
F 157
158         //处理治疗单签名-------------------------------------------------------------------------
159         handleSignData(signList, treatSingle.getId(), false);
160
161         //新增治疗单日志
162         StringBuffer logInfo = TreatSingleLogTool.addLogInfo(treatSingle, projectList);
163         TreatSingleLogTool.addLog(commonService, TreatSingleConstants.LOG_TYPE_CREATE, treatSingle, employeeRole, logInfo.toString());
164
1c1cdf 165 //        //所有状态下都发送治疗通知单给MIC
F 166 //        //发送企业微信通知MIC进行分配治疗(正式环境才发)
167 //        if (isMicNotice == BaseEntity.YES  && PlatformPattern.PROD.equals(customParameter.getPlatformPattern())) {
168 //            try{
169 //                String mpToken = WeiXinMpUtil.getAccessToken(commonService, customParameter.getCorpMpAppId());
170 //                String corpMpToken = WeiXinCorpMpUtil.getAccessToken(commonService, customParameter.getCorpMpAppId());
171 //                if(StringUtils.isEmpty(mpToken)){
172 //                    throw new TipsException("获取小程序Token异常!");
173 //                }
174 //                if(StringUtils.isEmpty(corpMpToken)){
175 //                    throw new TipsException("获取企业应用小程序Token异常!");
176 //                }
177 //                TreatUtil.sendTreatNoticeShopMic(commonService, visitOrder, TreatUtil.NOTICE_TYPE_ADD, "1000050", mpToken, corpMpToken, doctorName, projectNameSb.toString());
178 //            }catch (Exception e){
179 //                logger.error("新增治疗单发送企业消息失败:{}", e.getMessage());
180 //                logger.error(GlobalExceptionHandler.getExceptionInformation(e));
181 //            }
d6e1c5 182 //        }
1c1cdf 183
2cfe2a 184     }
F 185
186     /** 编辑治疗通知单 */
187     @Override
188     public void edit(TreatSingle treatSingle, List<TreatProject> projectList, VisitOrder visitOrder
189             , EmployeeRole employeeRole, TreatSingle updateTreat, List<TreatSingleSign> signList) {
190
191         SqlSentence sqlSentence = new SqlSentence();
192         Map<String, Object> values = new HashMap<>();
193         sqlSentence.setM(values);
194         StringBuffer sql = new StringBuffer();
195
196         //判断查询操作员工信息
197         Employee employee;
198 //        employee = employeeMapper.selectOneByKey(treatSingle.getOperatorId());
0ac137 199 //        if (employee == null) {
F 200 //            throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到操作人的信息[02]:" + treatSingle.getOperatorId());
201 //        }
202 //        values.put("operatorName", employee.getCnName());
203 //        values.put("operatorId", employee.getId());
2cfe2a 204
F 205
206         //判断查询操作员工门店信息
207         Shop shop = shopMapper.selectOneByKey(treatSingle.getShopId());
208         if (shop == null) {
209             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到操作人门店信息" + treatSingle.getShopId());
210         }
211         values.put("shopName", shop.getName());
212         values.put("shopId", shop.getId());
213
214         //判断查询开单人
215         employee = employeeMapper.selectOneByKey(treatSingle.getDeveloperId());
216         if (employee == null) {
217             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到开单人的信息" + treatSingle.getDeveloperId());
218         }
219         values.put("developerName", employee.getCnName());
220         values.put("developerId", employee.getId());
221
222         //判断查询开单人门店信息
223         shop = shopMapper.selectOneByKey(treatSingle.getDeveloperShopId());
224         if (shop == null) {
225             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "未找到开单人门店信息" + treatSingle.getDeveloperShopId());
226         }
227         values.put("developerShopName", shop.getName());
228         values.put("developerShopId", shop.getId());
229
230         //更改到访订单中的的是否开治疗单状态
231         sql.delete(0, sql.length());
232         if(visitOrder.getIsTreatOrder() == BaseEntity.NO){
233             sql.append(",isTreatOrder = isTreatOrder + 1 ");
234         }
235
236         if(visitOrder.getStatus() == VisitOrderCommon.WAIT_GT){
237             sql.append(",status = #{m.status} ");
238             values.put("status", VisitOrderCommon.STATUS_DISTRIBUTIVE_TREAT);
239         }
240
241         if(sql.length() > 0){
242             values.put("id", visitOrder.getId());
243             sql.append(" , editTime = now() where id = #{m.id} ");
244             sqlSentence.setSqlSentence( sql.toString().replaceFirst(",", ""));
245             if (visitOrderMapper.updateWhere(sqlSentence) != 1) {
246                 throw new PlatTipsException(PlatformCode.ERROR_TIPS, "修改治疗通知单:更新到访订单状态失败!");
247             }
248         }
249
250         //修改治疗通知单
251         values.put("remarkInfo", treatSingle.getRemarkInfo());
252         values.put("id", treatSingle.getId());
253         values.put("status", treatSingle.getStatus());
254         values.put("orderClassify", treatSingle.getOrderClassify());
31b49a 255         values.put("isWillPhoto", treatSingle.getIsWillPhoto());
F 256         values.put("allergyInfo", treatSingle.getAllergyInfo());
71a9af 257         values.put("isFirstFm", treatSingle.getIsFirstFm());
F 258         values.put("isRd", treatSingle.getIsRd());
37022a 259         values.put("isUserSign", treatSingle.getIsUserSign());
2cfe2a 260         sql.delete(0,sql.length());
F 261         //operatorName = #{m.operatorName},operatorId = #{m.operatorId},
262         sql.append(" developerName = #{m.developerName},developerId = #{m.developerId},remarkInfo = #{m.remarkInfo} ");
d9a6e0 263         sql.append(" ,developerShopName = #{m.developerShopName},developerShopId = #{m.developerShopId} ");
F 264         sql.append(" ,shopName = #{m.shopName},shopId = #{m.shopId},status = #{m.status} ");
31b49a 265         sql.append(" , orderClassify  = #{m.orderClassify}, isWillPhoto  = #{m.isWillPhoto}, allergyInfo  = #{m.allergyInfo} ");
37022a 266         sql.append(" , isFirstFm = #{m.isFirstFm}, isRd = #{m.isRd}, isisUserSign = #{m.isUserSign} ");
2cfe2a 267         sql.append(" where id = #{m.id} ");
F 268         sqlSentence.setSqlSentence(sql.toString());
269         if (treatSingleMapper.updateWhere(sqlSentence) != 1) {
270             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "修改失败!");
271         }
272
273         //获取编辑治疗单信息变化日志信息
274         StringBuffer logInfo = TreatSingleLogTool.editLogInfo(treatSingle, updateTreat);
275
276
277         //出来治疗通知单项目信息 -----------------------------------------------
d6e1c5 278         Map<String, Object> pMap = handleEditProject(treatSingle, projectList, logInfo);
2cfe2a 279
F 280         //处理治疗单签名-------------------------------------------------------------------------
281         handleSignData(signList, treatSingle.getId(), true);
282
283         //新增治疗单日志
284         TreatSingleLogTool.addLog(commonService, TreatSingleConstants.LOG_TYPE_EDIT, treatSingle, employeeRole, logInfo.toString());
285
286         //-----------------------------------------------------------------------
287         //发送企业微信修改之前的医生、护士、MIC企业微信通知
ad0ff1 288         //当状态是已确认或者待确认时 需要通知(正式环境才发)
F 289         if ((treatSingle.getStatus()==TreatSingleConstants.STATUS_WAIT_CONFIRM
290                 || treatSingle.getStatus() == TreatSingleConstants.STATUS_CONFIRM )
291              && PlatformPattern.PROD.equals(customParameter.getPlatformPattern())) {
d6e1c5 292             try{
1fb1a2 293                 String mpToken = WeiXinMpUtil.getAccessToken(commonService, customParameter.getCorpMpAppId());
d6e1c5 294                 String corpMpToken = WeiXinCorpMpUtil.getAccessToken(commonService, customParameter.getCorpMpAppId());
F 295                 if(StringUtils.isEmpty(mpToken)){
296                     throw new TipsException("获取小程序Token异常!");
297                 }
298                 if(StringUtils.isEmpty(corpMpToken)){
299                     throw new TipsException("获取企业应用小程序Token异常!");
300                 }
1c1cdf 301                 TreatUtil.editTreatNotice2(commonService, visitOrder, treatSingle , customParameter.getCorpMpAppId(), corpMpToken);
d6e1c5 302             }catch (Exception e){
F 303                 logger.error("编辑治疗单发送企业消息失败:{}", e.getMessage());
ad0ff1 304                 logger.error(GlobalExceptionHandler.getExceptionInformation(e));
d6e1c5 305             }
2cfe2a 306         }
F 307
308     }
309
310     /** 作废治疗通知单 */
311     public void invalidTreat(TreatSingle treatSingle, VisitOrder visitOrder, EmployeeRole employeeRole) {
312
313         User user = commonDao.selectOneByKey(UserMapper.class, visitOrder.getUserId());
314         if(user == null){
315             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "找不到客户信息!");
316         }
317
318         Shop shop = commonDao.selectOneByKey(ShopMapper.class, visitOrder.getShopId());
319         if(shop == null){
320             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "找不到门店信息!");
321         }
322
323         SqlSentence sqlSentence = new SqlSentence();
324         Map<String, Object> values = new HashMap<>();
325         sqlSentence.setM(values);
326         values.put("treatSingleId", treatSingle.getId());
327
328         //查询治疗通知单项目列表
329         sqlSentence.setSqlSentence(" select id, projectName, hisId from treat_project where isDel = 0 and treatSingleId = #{m.treatSingleId} ");
330         List<Map<String, Object>> tpList = treatProjectMapper.selectListMap(sqlSentence);
331
332         StringBuffer sb = new StringBuffer();
333         if(tpList != null && tpList.size() > 0){
eb8351 334             sqlSentence.setSqlSentence("SELECT e.id, e.cnName, e.userId, d.type from treat_project_doctor d left join employee e on e.id = d.commonId where d.isDel=0 and d.treatSingleId = #{m.treatSingleId} and d.treatProjectId = #{m.treatProjectId} ");
2cfe2a 335             List<Map<String, Object>> treatProjectDoctors ;
3be6a1 336             for(Map<String, Object> m : tpList) {
2cfe2a 337                 sb.append(m.get("projectName")).append(";");
F 338                 //查询项目对应多个医生或护士
eb8351 339                 values.put("treatProjectId", m.get("id"));
2cfe2a 340                 treatProjectDoctors = treatProjectDoctorMapper.selectListMap(sqlSentence);
F 341                 if(treatProjectDoctors != null && treatProjectDoctors.size() > 0){
342                     m.put("eList", treatProjectDoctors);
343                 }
344             }
345
346             //对应作废处理关联划扣数据逻辑
347             invalidTreatUpdateDeductData(tpList);
348         }
349
350         //删除治疗单
3be6a1 351         values.put("status", TreatSingleConstants.STATUS_CANCEL);
F 352         sqlSentence.setSqlSentence(" status = #{m.status}, isDel = 1, editTime = now() where id = #{m.treatSingleId}  ");
2cfe2a 353         if (treatSingleMapper.updateWhere(sqlSentence)!= 1) {
3be6a1 354             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "作废治疗通知单失败!");
2cfe2a 355         }
F 356
3be6a1 357 //        sqlSentence.setSqlSentence(" isDel = 1 where treatSingleId = #{m.treatSingleId}  and isDel = 0 ");
F 358 //        //删除治疗单项目等信息
359 //        treatProjectMapper.updateWhere(sqlSentence);
eb8351 360 //        //删除治疗单医生信息
F 361 //        treatProjectDoctorMapper.updateWhere(sqlSentence);
362 //        //删除治疗通知单项目部分信息
363 //        treatBodyPartMapper.updateWhere(sqlSentence);
2cfe2a 364
F 365         values.put("id", visitOrder.getId());
366         sqlSentence.sqlUpdate(" isTreatOrder = isTreatOrder - 1 WHERE id = #{m.id} ", values);
367         if (visitOrderMapper.updateWhere(sqlSentence) != 1) {
368             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "修改到访订单信息失败!");
369         }
370
eb8351 371         Integer oldStatus = visitOrder.getStatus();
F 372         Integer newStatus = visitOrder.getStatus();
373
374
2cfe2a 375         //新增日志
F 376         VLatelyLogTool.addV3(VisitOrderLogConstants.LOG_TYPE_DEL_TREAT_SINGLE, oldStatus,  newStatus,"作废治疗通知单", "作废治疗通知单(导医)",employeeRole,visitOrder.getId(),visitOrder.getShopId(),visitOrder.getUserId(), commonDao);
377         VGuideLogTool.addV3(VisitOrderLogConstants.LOG_TYPE_DEL_TREAT_SINGLE, oldStatus,  newStatus, visitOrder.getPreStartTime(),new Date(),0,"作废治疗通知单(导医)",employeeRole,visitOrder.getId(),visitOrder.getShopId(),visitOrder.getUserId(),commonDao);
eb8351 378
F 379         //新增治疗单日志
380         TreatSingleLogTool.addLog(commonService, TreatSingleConstants.LOG_TYPE_CANCEL, treatSingle, employeeRole, null);
2cfe2a 381
F 382         try{
383             BaseConfig baseConfig = new BaseConfig();
af7b07 384             baseConfig.setCorpId(customParameter.getCorpMpAppId());
2cfe2a 385             baseConfig.setCorpMpSecret(customParameter.getCorpMpSecret());
F 386             //发送通知
387             TreatNoticeUtil.treatNoticeCancelSendMsg(visitOrder, user, shop, sb.toString(),tpList, baseConfig, commonDao);
751ac6 388         }catch (TipsException|PlatTipsException e){
C 389             logger.error("作废治疗通知单发送企业通知失败:{}", e.getMessage());
2cfe2a 390         }catch (Exception e){
F 391             logger.error("作废治疗通知单发送企业通知失败:{}", GlobalExceptionHandler.getExceptionInformation(e));
392         }
393     }
394
71332a 395     /** 更新治疗单签名 */
F 396     @Override
397     public void updateSign(TreatSingle treatSingle, List<TreatSingleSign> signList, EmployeeRole employeeRole) {
398         SqlSentence sqlSentence = new SqlSentence();
399         Map<String, Object> values = new HashMap<>();
400         sqlSentence.setM(values);
401         values.put("treatSingleId", treatSingle.getId());
402
403
404         TreatSingleSign oldSign;
405         StringBuffer logInfo = new StringBuffer();
406         logInfo.append("更新签名类型:");
37022a 407         Integer isUseSign = treatSingle.getIsUserSign();
71332a 408         for(TreatSingleSign sign : signList){
eb8351 409
F 410             //类型
71332a 411             values.put("signPerType", sign.getSignPerType());
eb8351 412             //删除旧的
F 413             sqlSentence.setSqlSentence(" isDel = 1, editTime = now() where isDel = 0 and treatSingleId = #{m.treatSingleId} and signPerType = #{m.signPerType} ");
414             commonService.updateWhere(TreatSingleSignMapper.class, sqlSentence);
415
416             //新增
417             sign.setTreatSingleId(treatSingle.getId());
418             if(commonService.insert(TreatSingleSignMapper.class, sign) != 1){
419                 throw new TipsException("新增治疗单签名失败!");
71332a 420             }
F 421
37022a 422             //判断用户是否签名
F 423             if(TreatSingleSign.SIGN_PER_TYPE_USER.equals(sign.getSignPerType())){
424                 isUseSign = BaseEntity.YES;
425             }
426
427
71332a 428             //拼接日志
F 429             logInfo.append(TreatSingleSign.signPerTypeCh(sign.getSignPerType())).append("、");
430         }
431         logInfo.delete(logInfo.length() - 1, logInfo.length());
432
37022a 433         //更新治疗单是否用户已签名
F 434         if(treatSingle.getIsUserSign() == BaseEntity.NO && isUseSign == BaseEntity.YES){
435             values.put("id", treatSingle.getId());
436             sqlSentence.setSqlSentence(" isUseSign = 1 where id = #{m.id} ");
437             if(treatSingleMapper.updateWhere(sqlSentence) != 1){
438                 throw new TipsException("更新治疗单签名失败!");
439             }
440         }
441
71332a 442         //新增治疗单日志
F 443         TreatSingleLogTool.addLog(commonService, TreatSingleConstants.LOG_TYPE_EDIT_SIGN, treatSingle, employeeRole, logInfo.toString());
444
445     }
2cfe2a 446
F 447
448     ///////////////////////////////////////////////////////////////////////////////
449
450     //处理编辑治疗通知单时项目变动逻辑
d6e1c5 451     public Map<String, Object> handleEditProject(TreatSingle treatSingle, List<TreatProject> projectList, StringBuffer logInfo){
2cfe2a 452
F 453         SqlSentence sqlSentence = new SqlSentence();
454         Map<String, Object> values = new HashMap<>();
455         sqlSentence.setM(values);
456         values.put("id", treatSingle.getId());
457
458         //逻辑删除原治疗通知单人员
459         sqlSentence.setSqlSentence("isDel = 1 where treatSingleId = #{m.id} and isDel = 0 ");
460         treatProjectDoctorMapper.updateWhere(sqlSentence);
461
462         //查询原治疗通知单项目列表
463         sqlSentence.setSqlSentence(" select * from treat_project where isDel = 0 and treatSingleId = #{m.id}  ");
464         List<TreatProject> tpList = treatProjectMapper.selectList(sqlSentence);
465         Map<String, TreatProject> tpMap = new HashMap<>();
466         if(tpList != null && tpList.size() >= 0){
467             tpMap = tpList.stream().collect(Collectors.toMap(TreatProject::getId, Function.identity()));
468         }
469
470         //创建新的治疗项目
d6e1c5 471         StringBuffer projectNameSb = new StringBuffer();
F 472         String doctorName = null;
2cfe2a 473         TreatProject oldTp = null;
F 474         for (TreatProject project : projectList) {
475             project.setTreatSingleId(treatSingle.getId());
476             oldTp = tpMap.get(project.getId());
477             //原项目
478             if(oldTp != null){
479                 //更新
480                 values.put("num", project.getNum());
481                 values.put("indication", project.getIndication());
482                 values.put("indicationId", project.getIndicationId());
483                 values.put("partType", project.getPartType());
71a9af 484                 values.put("sdRemark", project.getSdRemark());
2cfe2a 485                 values.put("id", oldTp.getId());
F 486                 if (treatProjectMapper.updateEditInfo(values) != 1) {
487                     throw new PlatTipsException(PlatformCode.ERROR_TIPS, "修改治疗项目数量失败!");
488                 }
f771ee 489                 if(oldTp.getNum() != project.getNum()){
F 490                     logInfo.append("修改【").append(oldTp.getProjectName()).append(" x ").append(project.getNum()).append("】").append(";");
491                 }
2cfe2a 492                 //清除更新后的项目
F 493                 tpMap.remove(project.getId());
494             }else{
495                 //新项目
496                 project.setTreatSingleNo(treatSingle.getTreatSingleNo());
497                 if (treatProjectMapper.insert(project) != 1) {
498                     throw new PlatTipsException(PlatformCode.ERROR_TIPS, "修改时新增治疗项目失败!");
499                 }
500                 //添加日志内容
501                 logInfo.append("新增【").append(project.getProjectName()).append(" x ").append(project.getNum()).append("】").append(";");
502             }
503
504             //新增项目人员
d6e1c5 505             doctorName = addTreatProjectDoctor(project, doctorName);
2cfe2a 506             //编辑处理部位信息
F 507             handleBodyPart(project, true);
508             //处理项目适应症
509             handleTreatProjectIndication(project, true);
d6e1c5 510
F 511             //拼接项目名
512             projectNameSb.append(projectNameSb.length() > 0 ? "+" : "").append(project.getProjectName());
2cfe2a 513         }
F 514
515         //删除项目
516         values.clear();
517         List<Map<String, Object>> deductList = null;
518         for(Map.Entry<String, TreatProject> tp : tpMap.entrySet()){
519             oldTp = tp.getValue();
520             values.put("treatProjectId", oldTp.getId());
521             //先查询划扣参数
522             sqlSentence.setSqlSentence(" select dp.id as deductionProjectId, ds.id as deductionSingleId, ds.treatSingleId from deduction_project dp left join deduction_single ds on ds.id = dp.deductionSingleId where dp.treatProjectId = #{m.treatProjectId}  ");
523             deductList = commonDao.selectListMap(DeductionProjectMapper.class, sqlSentence);
524             if(deductList != null && deductList.size() > 0){
525                 //清空划扣记录的关联
526                 for(Map<String, Object> deduct : deductList){
527                     //划扣项目
528                     if(deduct.containsKey("deductionProjectId") && !StringUtils.isEmpty(deduct.get("deductionProjectId").toString())){
529                         values.put("deductionProjectId", deduct.get("deductionProjectId"));
530                         sqlSentence.setSqlSentence(" treatProjectId = null where id = #{m.deductionProjectId} ");
531                         if(commonDao.updateWhere(DeductionProjectMapper.class, sqlSentence) != 1){
532                             throw new TipsException("编辑治疗通知单:清除划扣项目关联id失败!" + deduct.get("deductionProjectId"));
533                         }
534                     }
535
536                     //划扣记录
537                     if(deduct.containsKey("deductionSingleId") && !StringUtils.isEmpty(deduct.get("deductionSingleId").toString())){
538                         values.put("deductionSingleId", deduct.get("deductionSingleId"));
539                         sqlSentence.setSqlSentence(" treatSingleId = null where id = #{m.deductionSingleId} ");
540                         if(commonDao.updateWhere(DeductionSingleMapper.class, sqlSentence) != 1){
541 //                            throw new TipsException("编辑治疗通知单:清除划扣记录关联id失败!");
542                         }
543                     }
544                 }
545             }
546
547             //删除治疗通知单项目
548             sqlSentence.setSqlSentence(" isDel = 1 where id = #{m.treatProjectId} ");
549             if(treatProjectMapper.updateWhere(sqlSentence) != 1){
550                 throw new TipsException("编辑治疗通知单:更新删除治疗通知单项目失败!" + oldTp.getId());
551             }
552             //添加日志内容
553             logInfo.append("将【").append(oldTp.getProjectName()).append("】项目删除");
554         }
d6e1c5 555
F 556         //返回要使用参数
557         values.clear();
558         values.put("projectName", projectNameSb.toString());
559         values.put("doctorName", doctorName);
560         return values;
2cfe2a 561     }
F 562
563     //新增治疗通知单项目人员
d6e1c5 564     public String addTreatProjectDoctor(TreatProject treatProject, String doctorName) {
2cfe2a 565         // 如果项目人员为空
F 566         List<TreatProjectDoctor> treatProjectDoctorList = treatProject.getTreatProjectDoctorList();
567         if (treatProjectDoctorList == null || treatProjectDoctorList.size() < 1) {
d6e1c5 568             return doctorName;
2cfe2a 569         }
d6e1c5 570 //        if (treatProjectDoctorList == null || treatProjectDoctorList.size() < 1) {
F 571 //            //如果新对象字段为空,走旧字段逻辑
572 //            // 如果项目人员为空
573 //            List<String> doctorIds = treatProject.getDoctorIds();
574 //            if(doctorIds == null || doctorIds.size() < 1){
575 //                return null;
576 //            }
577 //
578 //            //获取人员id数组
579 //            TreatProjectDoctor treatProjectDoctor;
580 //            for(String doctorId : doctorIds){
581 //                treatProjectDoctor = new TreatProjectDoctor();
582 //                treatProjectDoctor.setType(RoleType.UNIQUE_STR_DOCTOR);
583 //                treatProjectDoctor.setTreatSingleId(treatProject.getTreatSingleId());
584 //                treatProjectDoctor.setTreatProjectHisId(treatProject.getHisId());
585 //                treatProjectDoctor.setTreatProjectId(treatProject.getId());
586 //                treatProjectDoctor.setCommonId(doctorId);
587 //                treatProjectDoctorMapper.insert(treatProjectDoctor);
588 //            }
589 //            return null;
590 //        }
2cfe2a 591
F 592         // 获取人员对象
593         for (TreatProjectDoctor treatProjectDoctor : treatProjectDoctorList) {
d6e1c5 594             //如果医生名称为空,则查询获取一个
F 595             if(StringUtils.isEmpty(doctorName)){
596                 doctorName = EmployeeTool.getCnName(treatProjectDoctor.getCommonId(), commonService);
597             }
2cfe2a 598             treatProjectDoctor.setType(RoleType.UNIQUE_STR_DOCTOR);
F 599             treatProjectDoctor.setTreatSingleId(treatProject.getTreatSingleId());
600             treatProjectDoctor.setTreatProjectHisId(treatProject.getHisId());
601             treatProjectDoctor.setTreatProjectId(treatProject.getId());
602             // 主诊医生信息处理
603             if (!StringUtils.isEmpty(treatProjectDoctor.getPrimaryDoctorId())) {
604                 Employee employee = employeeMapper.selectOneByKey(treatProjectDoctor.getPrimaryDoctorId());
605                 if (employee == null) {
606                     throw new PlatTipsException(PlatformCode.ERROR_TIPS, "主诊医生信息不存在!");
607                 }
608             }
609             // 治疗医生处理
610             if (!StringUtils.isEmpty(treatProjectDoctor.getCommonId())) {
611                 Employee employee = employeeMapper.selectOneByKey(treatProjectDoctor.getCommonId());
612                 if (employee == null) {
613                     throw new PlatTipsException(PlatformCode.ERROR_TIPS, "治疗医生信息不存在!");
614                 }
615             }
616             treatProjectDoctorMapper.insert(treatProjectDoctor);
617         }
d6e1c5 618         return doctorName;
2cfe2a 619     }
F 620
621     //处理项目选择部位数据
622     public void handleBodyPart(TreatProject treatProject, boolean isEdit){
623         List<TreatBodyPart> bpList = treatProject.getBpList();
624         if(bpList == null ){
625             bpList = new ArrayList<>();
626         }
627
628         Map<String, String> oldMap = new HashMap<>();
629         //编辑时,查询原治疗单项目部分信息
630         if(isEdit){
631             List<TreatBodyPart> oldList = treatBodyPartMapper.selectListByTreatProjectId(treatProject.getId());
632             if(oldList != null && oldList.size() > 0){
633                 oldMap = oldList.stream().collect(Collectors.toMap(TreatBodyPart::getBodyPartItemId, TreatBodyPart::getId));
634             }
635         }
636
637         //遍历处理
638         for(TreatBodyPart treatBodyPart : bpList){
639             if(oldMap.containsKey(treatBodyPart.getBodyPartItemId())){
640                 //更新注射信息
f1015a 641 //                treatBodyPartMapper.updateInjectionInfo(treatBodyPart.getUnit(), treatBodyPart.getInjectionDose(), oldMap.get(treatBodyPart.getBodyPartItemId()));
2cfe2a 642                 oldMap.remove(treatBodyPart.getBodyPartItemId());
F 643                 continue;
644             }
645             //新增
646             treatBodyPart.setTreatSingleId(treatProject.getTreatSingleId());
647             treatBodyPart.setTreatProjectId(treatProject.getId());
648             if(treatBodyPartMapper.insert(treatBodyPart) != 1){
649                 throw new TipsException("新增治疗单项目部位信息失败!");
650             }
651         }
652
653         //删除
654         if(oldMap.size() > 0){
655             for(Map.Entry<String, String> entry : oldMap.entrySet()){
656                 treatBodyPartMapper.updateDelById(entry.getValue());
657             }
658         }
659     }
660
661     //处理项目适应症数据
662     public void handleTreatProjectIndication(TreatProject treatProject, boolean isEdit){
663         List<TreatProjectIndication> indicationList = treatProject.getIndicationList();
664         if(indicationList == null){
665             indicationList = new ArrayList<>();
666         }
667
668         SqlSentence sqlSentence = new SqlSentence();
669         Map<String, Object> values = new HashMap<>();
670         sqlSentence.setM(values);
671
672         Map<String, String> oldMap = new HashMap<>();
673         //编辑时,查询原治疗单项目部分信息
674         if(isEdit){
675             values.put("treatProjectId", treatProject.getId());
676             sqlSentence.setSqlSentence(" select * from treat_project_indication where isDel = 0 and treatProjectId = #{m.treatProjectId} ");
677             List<TreatProjectIndication> oldList = commonService.selectList(TreatProjectIndicationMapper.class, sqlSentence);
678             if(oldList != null && oldList.size() > 0){
679                 oldMap = oldList.stream().collect(Collectors.toMap(TreatProjectIndication::getIndicationId, TreatProjectIndication::getId));
680             }
681         }
682
683         //遍历处理
684         for(TreatProjectIndication treatProjectIndication : indicationList){
685             if(oldMap.containsKey(treatProjectIndication.getIndicationId())){
686                 oldMap.remove(treatProjectIndication.getIndicationId());
687                 continue;
688             }
689             //新增
690             treatProjectIndication.setTreatSingleId(treatProject.getTreatSingleId());
691             treatProjectIndication.setTreatProjectId(treatProject.getId());
692             if(commonService.insert(TreatProjectIndicationMapper.class,treatProjectIndication) != 1){
693                 throw new TipsException("新增治疗单项目适应症失败!");
694             }
695         }
696
697         //删除
698         if(oldMap.size() > 0){
699             values.clear();
700             sqlSentence.setSqlSentence(" isDel = 1, editTime = now() where id = #{m.id} ");
701             for(Map.Entry<String, String> entry : oldMap.entrySet()){
702                 values.put("id", entry.getValue());
703                 commonService.updateWhere(TreatProjectIndicationMapper.class, sqlSentence);
704             }
705         }
706     }
707
708     //处理治疗单签名数据
709     public void handleSignData(List<TreatSingleSign> signList, String treatSingleId, boolean isEdit){
710         if(signList == null){
711             signList = new ArrayList<>();
712         }
713
714         SqlSentence sqlSentence = new SqlSentence();
715         Map<String, Object> values = new HashMap<>();
716         sqlSentence.setM(values);
717
718         //存储删除的信息
719         Map<String, TreatSingleSign> delMap = new HashMap<>();
720         //判断编辑时才查旧数据出来匹配删除
721         if(isEdit){
722             values.put("treatSingleId", treatSingleId);
723             sqlSentence.setSqlSentence(" select * from treat_single_sign where isDel = 0 and treatSingleId = #{m.treatSingleId} ");
724             List<TreatSingleSign> oldList = commonService.selectList(TreatSingleSignMapper.class, sqlSentence);
725             if(oldList != null && oldList.size() > 0){
726                 delMap = oldList.stream().collect(Collectors.toMap(TreatSingleSign::getSignPerType, Function.identity()));
727             }
728         }
729
730         TreatSingleSign oldSign;
731         sqlSentence.setSqlSentence(" signPerName = #{m.signPerName}, signImg = #{m.signImg}, editTime = now() where id = #{m.id} ");
732
733         //遍历
734         for(TreatSingleSign sign : signList){
735             if(delMap.containsKey(sign.getSignPerType())){
736                 oldSign = delMap.get(sign.getSignPerType());
737                 //判断更新
738                 if(!sign.getSignImg().equals(oldSign.getSignImg())){
739                     values.clear();
740                     values.put("signImg", sign.getSignImg());
741                     values.put("signPerName", sign.getSignPerName());
742                     values.put("id", oldSign.getId());
743                     if(commonService.updateWhere(TreatSingleSignMapper.class, sqlSentence) != 1){
744                         throw new TipsException("更新治疗单签名失败!");
745                     }
746                 }
747                 delMap.remove(sign.getSignPerType());
748             }else{
749                 //新增
750                 sign.setTreatSingleId(treatSingleId);
751                 if(commonService.insert(TreatSingleSignMapper.class, sign) != 1){
752                     throw new TipsException("新增治疗单签名失败!");
753                 }
754             }
755         }
756
757         //删除
758         if(delMap.size() > 0){
759             values.clear();
760             sqlSentence.setSqlSentence(" isDel = 1, editTime = now() where id = #{m.id} ");
761             for(Map.Entry<String, TreatSingleSign> entry : delMap.entrySet()){
762                 values.put("id", entry.getValue().getId());
763                 if(commonService.updateWhere(TreatSingleSignMapper.class, sqlSentence) != 1){
764                     throw new TipsException("删除治疗单签名失败!");
765                 }
766             }
767         }
768
769     }
770
771     //作废处理关联划扣数据逻辑
772     private void invalidTreatUpdateDeductData(List<Map<String, Object>> tpList){
773         SqlSentence sqlSentence = new SqlSentence();
774         Map<String, Object> values = new HashMap<>();
775         sqlSentence.setM(values);
776
777         List<Map<String, Object>> deductList = null;
778         for(Map<String, Object> tp : tpList){
779
780             values.put("treatProjectId", tp.get("id"));
781             //先查询划扣参数
782             sqlSentence.setSqlSentence(" select dp.id as deductionProjectId, ds.id as deductionSingleId, ds.treatSingleId from deduction_project dp left join deduction_single ds on ds.id = dp.deductionSingleId where dp.treatProjectId = #{m.treatProjectId}  ");
783             deductList = commonDao.selectListMap(DeductionProjectMapper.class, sqlSentence);
784             if(deductList != null && deductList.size() > 0){
785                 //清空划扣记录的关联
786                 for(Map<String, Object> deduct : deductList){
787                     //划扣项目
788                     if(deduct.containsKey("deductionProjectId") && !StringUtils.isEmpty(deduct.get("deductionProjectId").toString())){
789                         values.put("deductionProjectId", deduct.get("deductionProjectId"));
790                         sqlSentence.setSqlSentence(" treatProjectId = null where id = #{m.deductionProjectId} ");
791                         if(commonDao.updateWhere(DeductionProjectMapper.class, sqlSentence) != 1){
792                             throw new PlatTipsException(PlatformCode.ERROR_TIPS, "清除划扣项目关联id失败!" + deduct.get("deductionProjectId"));
793                         }
794                     }
795
796                     //划扣记录
797                     if(deduct.containsKey("deductionSingleId") && !StringUtils.isEmpty(deduct.get("deductionSingleId").toString())){
798                         values.put("deductionSingleId", deduct.get("deductionSingleId"));
799                         sqlSentence.setSqlSentence(" treatSingleId = null where id = #{m.deductionSingleId} ");
800                         if(commonDao.updateWhere(com.hx.phiappt.dao.mapper.DeductionSingleMapper.class, sqlSentence) != 1){
801 //                            throw new TipsException("清除划扣记录关联id失败!");
802                         }
803                     }
804                 }
805             }
806         }
807     }
808
809
810 }
811