PushChatInfoTask.java 14.2 KB
   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
package com.lyms.talkonlineweb.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lyms.talkonlineweb.domain.*;
import com.lyms.talkonlineweb.service.*;
import com.lyms.talkonlineweb.util.*;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
* 回访信息回复
*/

@Component
@Log4j2
public class PushChatInfoTask {
@Value("${hx.hxuser}")
private String hxuser;
@Autowired
private LymsArticleService lymsArticleService;
@Autowired
private PatientInfoService patientInfoService;
@Autowired
private LymsPushedartService lymsPushedartService;//推送的历史记录
@Autowired
private PushArticleService pushArticleService;//推送文章新逻辑shiy改
@Autowired
private LymsPatientService lymsPatientService;
@Autowired
private LymsPushMessagesService lymsPushMessagesService;
@Autowired
private LymsPcaseService lymsPcaseService;//病例
@Autowired
private LymsDoctorService lymsDoctorService;
@Autowired
public LymsPushAttentionRecordService lymsPushAttentionRecordService;
@Autowired
private LymsChatgroupService lymsChatgroupService;
@Autowired
private LymsReturnVisitRecordService lymsReturnVisitRecordService;
@Autowired
private HXService hxService;
@Autowired
private LymsIllnessService lymsIllnessService;
@Autowired
private LymsChatInfoService lymsChatInfoService;
@Autowired
private LymsMessageService lymsMessageService;

@Autowired
private LymsTkrecordService lymsTkrecordService;

/**
* 每天19点执行自动回访功能
* 第二天:就诊医生
* 第五天:值班护士
* 第十天:值班医生
* 第二十天:就诊医生
* 第三十天:值班医生
*/
@Scheduled(cron = "0 0 19 * * ?")
public void pushChatInfo() {
//用户下的病例信息。每个环信群组代表一个病例,一个病例下有多个疾病种类。
List<Map<String,Object>> PcInfoList=lymsPatientService.getPcInfoList();
for (Map<String, Object> map : PcInfoList) {

try {
//患者信息
LymsPatient patient= lymsPatientService.getOne(new QueryWrapper<LymsPatient>()
.lambda().eq(LymsPatient::getIdno, map.get("idno").toString()));
//医生信息
LymsDoctor doctor= lymsDoctorService.getOne(new QueryWrapper<LymsDoctor>()
.lambda().eq(LymsDoctor::getDlogin, map.get("dlogin").toString()));
//回访记录
final List<LymsReturnVisitRecord> returnVisitRecords = lymsReturnVisitRecordService.list(new QueryWrapper<LymsReturnVisitRecord>()
.lambda().eq(LymsReturnVisitRecord::getPcid, (Integer)map.get("pcid"))
.eq(LymsReturnVisitRecord::getType, 0));
//计算(自动回复周期上传病例后- 1:第二天2:第五天3:第十天4:第二十天5:第三十天)
int day = DateUtil.daysBetween(DateUtil.parseYMD(map.get("createdtime").toString()),new Date());
switch (returnVisitRecords.size()) {
case 0:
if (day==1) {
getEverydayInfo(map,patient,doctor,0,1);
}
break;
case 1:
if (day==4) {
getEverydayInfo(map,patient,doctor,2,2);
}
break;
case 2:
if (day==9) {
getEverydayInfo(map,patient,doctor,1,3);
}
break;
case 3:
if (day==19) {
getEverydayInfo(map,patient,doctor,0,4);
}
break;
case 4:
if (day==29) {
getEverydayInfo(map,patient,doctor,1,5);
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

/**
* 每天推送逻辑
* @param map
* @param patient
* @param doctor
* @param adminType 0:就诊医生 1:值班医生 2:值班护士
* @param ChatInfoType 自动回复周期上传病例后- 1:第二天2:第五天3:第十天4:第二十天5:第三十天
*/
@Transactional(rollbackFor = Exception.class)
public void getEverydayInfo(Map<String,Object> map,LymsPatient patient,LymsDoctor doctor,int adminType,int ChatInfoType){
//值班医生/值班护士信息
List<LymsDoctor> doctorAminList= lymsDoctorService.list(new QueryWrapper<LymsDoctor>()
.lambda().eq(LymsDoctor::getDpid, doctor.getDpid())
.in(LymsDoctor::getAdminType, Arrays.asList(1,2))
.orderByAsc(LymsDoctor::getAdminType));
//环信群组信息
LymsChatgroup chatgroup = lymsChatgroupService.getOne(new QueryWrapper<LymsChatgroup>()
.lambda().eq(LymsChatgroup::getPcid, (Integer)map.get("pcid")));
if(null==chatgroup){
//创建聊天群组
chatgroup=addChatGroup(map,patient,doctor);
}
//获取 就诊医生/值班医生/值班护士账号
String dlogin="";
if(adminType!=0) {
for (LymsDoctor lymsDoctor : doctorAminList) {
if (lymsDoctor.getAdminType() == adminType) {
dlogin = lymsDoctor.getDlogin();
break;
}
}
}else {
dlogin=doctor.getDlogin();
}
//病例下的疾病
final List<LymsIllness> lymsIllnessList = lymsIllnessService.list(new QueryWrapper<LymsIllness>()
.lambda().eq(LymsIllness::getPcid, (Integer) map.get("pcid")));
for (LymsIllness lymsIllness : lymsIllnessList) {
//要推送的疾病对应的话术
final LymsChatInfo chatInfo = lymsChatInfoService.getOne(new QueryWrapper<LymsChatInfo>()
.lambda().eq(LymsChatInfo::getIllid, lymsIllness.getIid())
.eq(LymsChatInfo::getType, ChatInfoType));
///推送回访
if (null!=chatInfo && StringUtil.isNotEmpty(chatgroup.getHxgroupid()) && null!=doctor) {
//推送回访消息和保存发送记录
pushOrMessage(dlogin,chatgroup,chatInfo,doctor);
}else {
throw new RuntimeException("参数不符合");
}
}
//增加回访记录,修改环信群组状态,修改问诊记录状态
addReturnVisitRecordOrUpdate(dlogin,chatgroup ,patient,doctor);
}

/**
* 创建环信群组
* @param map
* @param patient
* @param doctor
* @return
*/
@Transactional(rollbackFor = Exception.class)
public LymsChatgroup addChatGroup(Map<String,Object> map,LymsPatient patient,LymsDoctor doctor){
List<String> adminDlogins=new ArrayList<>();
adminDlogins.add(patient.getIdno());
List<String> groupnames=new ArrayList<>();
groupnames.add(patient.getPname());
//患者注册环信
if (StringUtil.isEmpty(patient.getHxid())) {
JSONObject json = hxService.addUser(patient.getIdno(), Constant.COMMON_PASSWD, patient.getPname());
JSONArray rArr = json.getJSONArray("entities");
if (rArr.size() > 0) {
patient.setHxid(rArr.getJSONObject(0).getString("uuid"));
final boolean b = lymsPatientService.updateById(patient);
if (!b) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}else {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}
//查询值班医生/值班护士
List<LymsDoctor> doctorAminList= lymsDoctorService.list(new QueryWrapper<LymsDoctor>()
.lambda().eq(LymsDoctor::getDpid, doctor.getDpid())
.in(LymsDoctor::getAdminType, Arrays.asList(1,2))
.orderByAsc(LymsDoctor::getAdminType));
//科室值班医生注册环信
for (LymsDoctor lymsDoctor : doctorAminList) {
if(StringUtil.isEmpty(lymsDoctor.getHxid())) {
JSONObject json = hxService.addUser(lymsDoctor.getDlogin(), Constant.COMMON_PASSWD, lymsDoctor.getDname());
JSONArray rArr = json.getJSONArray("entities");
if (rArr.size() > 0) {
lymsDoctor.setHxid(rArr.getJSONObject(0).getString("uuid"));
final boolean b = lymsDoctorService.updateById(lymsDoctor);
if (!b) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}
}
adminDlogins.add(lymsDoctor.getDlogin());
groupnames.add(lymsDoctor.getDname());
}
if ( ! adminDlogins.contains(doctor.getDlogin())) {
//医生注册环信
if (StringUtil.isEmpty(doctor.getHxid())) {
JSONObject json = hxService.addUser(doctor.getDlogin(), Constant.COMMON_PASSWD, doctor.getDname());
JSONArray rArr = json.getJSONArray("entities");
if (rArr.size() > 0) {
doctor.setHxid(rArr.getJSONObject(0).getString("uuid"));
final boolean b = lymsDoctorService.updateById(doctor);
if (!b) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}
}
adminDlogins.add(doctor.getDlogin());
groupnames.add(doctor.getDname());
}
JSONObject rJson = hxService.addChatGroups(StringUtils.join(adminDlogins.toArray(), ","), hxuser, adminDlogins.toArray(new String[adminDlogins.size()]));
LymsChatgroup group =new LymsChatgroup();
group.setGroupname(StringUtils.join(groupnames.toArray(), ","));
group.setDescription(StringUtils.join(adminDlogins.toArray(), ","));
group.setOwnerk(hxuser);
group.setFromp(patient.getIdno());
group.setHxgroupid(rJson.getJSONObject("data").getString("groupid"));
group.setTarget(doctor.getDlogin());
group.setType(1);
group.setStat(0);
group.setPcid((Integer)map.get("pcid"));
final boolean save = lymsChatgroupService.save(group);
if (!save) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
return group;
}

/**
* 推送回访消息和保存发送记录
* @param dlogin 发送医生
* @param chatgroup
* @param chatInfo
* @param doctor
*/
@Transactional(rollbackFor = Exception.class)
public void pushOrMessage(String dlogin,LymsChatgroup chatgroup,LymsChatInfo chatInfo,LymsDoctor doctor) {
JSONObject jsonObject = hxService.sendGroupMsg(new String[]{chatgroup.getHxgroupid()}, chatInfo.getConten(), StringUtil.isEmpty(dlogin)? doctor.getDlogin():dlogin);
if (null!=jsonObject) {
//保存发送消息记录
LymsMessage message=new LymsMessage();
message.setContent(chatInfo.getConten());
message.setFromid(dlogin);
message.setTargetid(chatgroup.getHxgroupid());
message.setMtype("TEXT");
message.setSendtime(new Date());
message.setType(1);
final boolean save = lymsMessageService.save(message);
if (! save) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}else {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}

/**
* 增加回访记录,修改环信群组状态,修改问诊记录状态
* @param dlogin
* @param chatgroup
* @param patient
* @param doctor
*/
@Transactional(rollbackFor = Exception.class)
public void addReturnVisitRecordOrUpdate(String dlogin,LymsChatgroup chatgroup ,LymsPatient patient,LymsDoctor doctor) {
//增加回访记录
LymsReturnVisitRecord returnVisitRecord=new LymsReturnVisitRecord();
returnVisitRecord.setDlogin(dlogin);
returnVisitRecord.setDpid(doctor.getDpid());
returnVisitRecord.setIdno(patient.getIdno());
returnVisitRecord.setType(0);
returnVisitRecord.setHxgroupid(chatgroup.getHxgroupid());
returnVisitRecord.setPcid(chatgroup.getPcid());
final boolean save = lymsReturnVisitRecordService.save(returnVisitRecord);
if (!save) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
chatgroup.setType(1);
chatgroup.setStat(0);
final boolean b = lymsChatgroupService.updateById(chatgroup);
if (!b) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
//修改同一群组记录的状态
final List<LymsTkrecord> tkrecordList = lymsTkrecordService.list(new QueryWrapper<LymsTkrecord>()
.lambda().eq(LymsTkrecord::getHxgroupid, chatgroup.getHxgroupid()));
for (LymsTkrecord lymsTkrecord : tkrecordList) {
lymsTkrecord.setStat(0);
final boolean saveOrUpdate = lymsTkrecordService.updateById(lymsTkrecord);
if (!saveOrUpdate) {
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}
}
}