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("回滚状态,执行失败,请联系管理员");
}
}
}
}