ChatGroupController.java 31.4 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
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
package com.lyms.talkonlineweb.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyms.talkonlineweb.annotation.TokenRequired;
import com.lyms.talkonlineweb.constants.ErrorCodeConstants;
import com.lyms.talkonlineweb.domain.*;
import com.lyms.talkonlineweb.result.BaseResponse;
import com.lyms.talkonlineweb.service.*;
import com.lyms.talkonlineweb.util.Constant;
import com.lyms.talkonlineweb.util.DateUtil;
import com.lyms.talkonlineweb.util.HXService;
import com.lyms.talkonlineweb.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;

@RestController
@RequestMapping("chat")
@Log4j2
public class ChatGroupController {
@Value("${hx.hxuser}")
private String hxuser;
@Autowired
private LymsChatgroupService lymsChatgroupService;

@Autowired
private LymsMessageService lymsMessageService;

@Autowired
private PatientInfoService patientInfoService;

@Autowired
private LymsDoctorService lymsDoctorService;
@Autowired
private LymsHdepartService lymsHdepartService;
@Autowired
private LymsDictService lymsDictService;
@Autowired
private HXService hxService;

@Autowired
private LymsTcardService lymsTcardService;

@Autowired
private LymsPatientService lymsPatientService;

@Autowired
private LymsTkrecordService lymsTkrecordService;

@Autowired
private AppgetdoctorlistInfoService appgetdoctorlistInfoService;
@Autowired
private LymsChatInfoService lymsChatInfoService;
@Autowired
private LymsReturnVisitRecordService lymsReturnVisitRecordService;

/**
* 添加或删除聊天组
*
* @param group
* @return
*/
@PostMapping("saveChatGroup")
@TokenRequired
public BaseResponse saveChatGroup(@RequestBody LymsChatgroup group) {
BaseResponse baseResponse = new BaseResponse();
String retDlogin=group.getRetDlogin();
List<LymsChatgroup> gLst = lymsChatgroupService.list(Wrappers.query(group).orderByDesc("id"));
if (group.getOwnerk() == null) {
group.setOwnerk(hxuser);
}
LymsPatient patient= lymsPatientService.getOne(new QueryWrapper<LymsPatient>()
.lambda().eq(LymsPatient::getIdno, group.getFromp()));
LymsDoctor doctor= lymsDoctorService.getOne(new QueryWrapper<LymsDoctor>()
.lambda().eq(LymsDoctor::getDlogin, group.getTarget()));
if (gLst.size() > 0) {
LymsChatgroup group2 = gLst.get(0);
group = group2;
//回访的状态才去执行(上次关闭了再次点击会开启)
if (1==group.getType()&&group.getStat()==1) {
group.setStat(0);
//修改群组状态
lymsChatgroupService.updateById(group);
//添加回访记录
LymsReturnVisitRecord returnVisitRecord=new LymsReturnVisitRecord();
returnVisitRecord.setDlogin(retDlogin);//前端传的当前登录医生账号
returnVisitRecord.setDpid(doctor.getDpid());
returnVisitRecord.setIdno(patient.getIdno());
returnVisitRecord.setType(1);
returnVisitRecord.setPcid(group.getPcid());
returnVisitRecord.setHxgroupid(group.getHxgroupid());
lymsReturnVisitRecordService.save(returnVisitRecord);
}
//患者消费问诊卡 咨询结束后
if(0==group.getType()){

group.setType(1);
lymsChatgroupService.updateById(group);
//添加回访记录
LymsReturnVisitRecord returnVisitRecord2 = new LymsReturnVisitRecord();
returnVisitRecord2.setDlogin(retDlogin);//前端传的当前登录医生账号
returnVisitRecord2.setDpid(doctor.getDpid());
returnVisitRecord2.setIdno(patient.getIdno());
returnVisitRecord2.setType(1);
returnVisitRecord2.setPcid(group.getPcid());
returnVisitRecord2.setHxgroupid(group.getHxgroupid());
lymsReturnVisitRecordService.save(returnVisitRecord2);
}
} else {
List<String> adminDlogins=new ArrayList<>();
adminDlogins.add(group.getFromp());
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"));
lymsPatientService.updateById(patient);
}
}
//查询值班医生
List<LymsDoctor> doctorAminList= lymsDoctorService.list(new QueryWrapper<LymsDoctor>()
.lambda().eq(LymsDoctor::getDpid, doctor.getDpid())
.in(LymsDoctor::getAdminType, new int[]{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"));
lymsDoctorService.updateById(lymsDoctor);
}
}
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"));
lymsDoctorService.updateById(doctor);
}
adminDlogins.add(doctor.getDlogin());
groupnames.add(doctor.getDname());
}
}

log.info("创建环信组:{} description:{}", group.getOwnerk(), StringUtils.join(adminDlogins.toArray(), ","));
JSONObject rJson = hxService.addChatGroups(StringUtils.join(adminDlogins.toArray(), ","), group.getOwnerk(), adminDlogins.toArray(new String[adminDlogins.size()]));
log.info("rJson:" + rJson);
group.setHxgroupid(rJson.getJSONObject("data").getString("groupid"));
group.setDescription(StringUtils.join(adminDlogins.toArray(), ","));
//环信群组名称更改为姓名串-用于PC端问诊展示
group.setGroupname(StringUtils.join(groupnames.toArray(), ","));
group.setCtime(new Date());
lymsChatgroupService.saveOrUpdate(group);
}
baseResponse.setObject(group);
return baseResponse;
}

/**
* 保存消息记录
*
* @param message
* @return
*/
@PostMapping("saveMsg")
@TokenRequired
public BaseResponse saveMsg(@RequestBody LymsMessage message) {
BaseResponse baseResponse = new BaseResponse();
message.setSendtime(new Date());
boolean f = lymsMessageService.saveOrUpdate(message);
baseResponse.setErrorcode(f == true ? 0 : 1);
return baseResponse;
}

/**
* 根据群组ID获取患者和医生信息
* @param chatgroup
* @return
*/
@GetMapping("getPatDoc")
@TokenRequired
public BaseResponse getPatDoc(LymsChatgroup chatgroup) {
log.info(">>>>>>>>>>chatgroup:" + chatgroup);
BaseResponse baseResponse = new BaseResponse();
Map<String, Object> rs = new HashMap<>();
//不作为条件查询
Integer type=chatgroup.getType();
chatgroup.setType(null);
List<LymsChatgroup> gLst = lymsChatgroupService.list(Wrappers.query(chatgroup).orderByDesc("id"));

if (gLst.size()>0){
chatgroup = gLst.get(0);
}
LymsDoctor doctor = new LymsDoctor();
doctor = lymsDoctorService.getOne(Wrappers.query(doctor).eq("dlogin", chatgroup.getTarget()));

LymsPatient patient = new LymsPatient();
patient.setIdno(chatgroup.getFromp());
List<LymsPatient> pLst = lymsPatientService.list(Wrappers.query(patient));
conDocCode(doctor);

List<Map<String, Object>> tmpList = lymsChatgroupService.getPinfoBySesson(null, doctor.getDid().toString(), chatgroup.getHxgroupid());
if (tmpList.size() > 0) {
if(null==type) {
rs.put("sess", tmpList.get(0));
}else {
//修改为恢复聊天状态
LymsTkrecord tkrecord=new LymsTkrecord();
tkrecord.setId((Integer) tmpList.get(0).get("rid"));
tkrecord.setStat(0);
lymsTkrecordService.updateById(tkrecord);
tmpList.get(0).put("stat", 0);
rs.put("sess", tmpList.get(0));
}
}else {
//防止重复创建组
if(null!=type && 1==type && CollectionUtils.isNotEmpty(gLst)){
Map<String, Object> map=new HashMap<>();
map.put("stat", 0);
rs.put("sess", tmpList.add(map));
}
}

rs.put("doctor", doctor);
rs.put("patient", pLst);

baseResponse.setObject(rs);

return baseResponse;
}

public void conDocCode(LymsDoctor d) {
Map param = new HashMap();
param.put("vtype", 4);
List<LymsDict> dLst = lymsDictService.listByMap(param);
param.clear();
param.put("did", d.getDpid());
List<LymsHdepart> departLst = lymsHdepartService.listByMap(param);
if (departLst.size() > 0) {
LymsHdepart depart = departLst.get(0);
d.setHname(depart.getHname());
d.setDdname(depart.getDname());
dLst.forEach(dd -> {
if (dd.getCode() == d.getLvl()) {
d.setLname(dd.getValue());
}
});
}

}

/**
* 医生端--回话会话列表获取患者信息
*
* @param froms 发送人员ID
* @param did 问诊医生ID
* @param groupid 群组ID
* @return
*/
@PostMapping("getPinfoBySession")
@TokenRequired
public BaseResponse getPinfoBySesson(String froms, String did, String groupid) {
BaseResponse baseResponse = new BaseResponse();
List<Map<String, Object>> psList = new ArrayList<>();

List<Map<String, Object>> tmpList = lymsChatgroupService.getPinfoBySesson(null, did, groupid);
if (tmpList.size() > 0) {
psList.add(tmpList.get(0));
}

baseResponse.setObject(psList);
return baseResponse;
}

/**
* 根据环信传入的会话,获取用户信息
* @param request
* @param type 医生回访停止回话用
* @return
*/
@PostMapping("getSessionListInfo")
@TokenRequired
public BaseResponse getSessionListInfo(HttpServletRequest request) {
BaseResponse baseResponse = new BaseResponse();
log.info(">>>>>>>>> getSessionListInfo 根据环信传入的会话,获取用户信息");
JSONArray rs=new JSONArray();
try {
request.setCharacterEncoding("utf-8");
String res=IOUtils.toString(request.getInputStream(),Charset.forName("UTF-8"));
log.info(res);
JSONObject jsonObject= JSON.parseObject(res);
JSONArray channel_infos=jsonObject.getJSONArray("channel_infos");
if(channel_infos.size()>0) {
for (int i = 0; i < channel_infos.size(); i++) {
JSONObject row = channel_infos.getJSONObject(i);

if (row.getJSONObject("meta").size() > 0) {
JSONObject payload = row.getJSONObject("meta").getJSONObject("payload");
System.out.println(payload);

payload.put("unread_num", row.get("unread_num"));

String to = payload.getString("to");
LymsTkrecord tkrecord = new LymsTkrecord();
tkrecord.setHxgroupid(to);
List<LymsTkrecord> rLst = lymsTkrecordService.list(Wrappers.query(tkrecord));
payload.put("stat", 0);
if (rLst.size() > 0) {
payload.put("stat", rLst.get(0).getStat());
}
LymsChatgroup chatgroup = new LymsChatgroup();
chatgroup.setHxgroupid(to);
List<LymsChatgroup> cLst = lymsChatgroupService.list(Wrappers.query(chatgroup));

if (cLst.size() > 0) {
chatgroup = cLst.get(0);
Map<String, Object> param = new HashMap<>();
param.put("idno", chatgroup.getFromp());
List<LymsPatient> pLst = lymsPatientService.listByMap(param);
if (pLst.size() > 0) {
payload.put("pat", pLst.get(0));
}
param.clear();
param.put("dlogin", chatgroup.getTarget());
List<AppgetdoctorlistInfo> dLst = appgetdoctorlistInfoService.listByMap(param);
if (dLst.size() > 0) {
payload.put("doc", dLst.get(0));
}
if (CollectionUtils.isEmpty(rLst)) {
if (1 == cLst.get(0).getType()) {
payload.put("stat", cLst.get(0).getStat());
}
}
}

payload.put("timestamp", row.getJSONObject("meta").get("timestamp"));
rs.add(payload);
}
}
}
//系统自动发回访消息时获取聊天组(前端获取不到聊天组这里后端自己查)
getChatRecord(rs, jsonObject);

} catch (IOException e) {
e.printStackTrace();
}
baseResponse.setObject(rs);
return baseResponse;
}

/**
* 获取专家组\客服人员信息
* @param request
* @return
*/
@GetMapping("getPreMsg")
@TokenRequired
public BaseResponse getPreMsg(HttpServletRequest request){
BaseResponse baseResponse =new BaseResponse();
Map<String,Object> param=new HashMap<>();
param.put("vtype",998);
List<LymsDict> dLst=lymsDictService.listByMap(param);
if(dLst.size()>0){
baseResponse.setObject(dLst.get(0));
}
return baseResponse;
}
/**
* 医生端获取自己的患者列表
* @param doctorId 医生id
* @param current 页数
* @param size 条数
* @param synthesisQuery 综合筛选条件
* @param did 科室id 值班医生/值班护士 时需要传。doctorId 就不能传
* @return
*/
@GetMapping("getDoctorPatient")
@TokenRequired
public BaseResponse getDoctorPatient(Integer doctorId,Integer current, Integer size,String synthesisQuery,
Integer did){
BaseResponse baseResponse =new BaseResponse();
if((null==doctorId && null==did)||(null!=doctorId && null!=did)){
baseResponse.setErrorcode(ErrorCodeConstants.PARAMETER_ERROR);
baseResponse.setErrormsg("参数错误");
return baseResponse;
}
Page<Map<String,Object>> page=new Page<>(current,size);
QueryWrapper queryWrapper=new QueryWrapper<>();
if(null!=did){
//科室下的患者
queryWrapper.eq("pc.did", did);
}else {
//医生下的患者
queryWrapper.eq("pc.dtid", doctorId);
}
if (StringUtil.isNotEmpty(synthesisQuery)){
queryWrapper.like("p.pname", synthesisQuery);
}
List<Map<String,Object>> list=lymsChatgroupService.getDoctorPatient(page,queryWrapper);
for (Map<String, Object> map : list) {
if(map.get("sex")!=null) {
map.put("sex",(int)map.get("sex")==1?"男":"女");
}
if(map.get("birth")!=null){
map.put("birth", DateUtil.getAge(DateUtil.parseYMD(map.get("birth").toString())));
}
}
page.setRecords(list);
baseResponse.setObject(page);
return baseResponse;
}

/**
* 前端3秒执行一次调用。聊天室状态(患者、医生分别查询)
* @param groupId
* @return
*/
@GetMapping("getChatStat")
@TokenRequired
public BaseResponse getChatStat(String groupId){
BaseResponse baseResponse =new BaseResponse();
try {
final LymsChatgroup chatgroup = lymsChatgroupService.getOne(new QueryWrapper<LymsChatgroup>()
.lambda().eq(LymsChatgroup::getHxgroupid, groupId));
final LymsTkrecord tkrecord = lymsTkrecordService.getOne(new QueryWrapper<LymsTkrecord>()
.lambda().eq(LymsTkrecord::getHxgroupid, groupId)
.groupBy(LymsTkrecord::getHxgroupid));
Map<String,Object> map= new LinkedHashMap<>();
map.put("groupStat",null==chatgroup?"":chatgroup.getStat());
map.put("tkrecordStat",null==tkrecord?"":tkrecord.getStat());
baseResponse.setObject(map);
} catch (Exception e) {
e.printStackTrace();
}
return baseResponse;
}
/**
* 添加/修改 回访聊天信息自动回复话术
* @param chatInfo
* @return
*/
@PostMapping("addOrUpdateLymsChatInfo")
@TokenRequired
public BaseResponse addOrUpdateLymsChatInfo(@RequestBody LymsChatInfo chatInfo){
BaseResponse baseResponse =new BaseResponse();
try {
final boolean b = lymsChatInfoService.saveOrUpdate(chatInfo);
baseResponse.setErrorcode(b?0:1);
baseResponse.setErrormsg(b?"成功":"失败");
} catch (Exception e) {
e.printStackTrace();
}
return baseResponse;
}
/**
* 删除 回访聊天信息自动回复话术
* @param id
* @return
*/
@DeleteMapping("delLymsChatInfo")
@TokenRequired
public BaseResponse delLymsChatInfo(Integer id){
BaseResponse baseResponse =new BaseResponse();
try {
final boolean b = lymsChatInfoService.removeById(id);
baseResponse.setErrorcode(b?0:1);
baseResponse.setErrormsg(b?"成功":"失败");
} catch (Exception e) {
e.printStackTrace();
}
return baseResponse;
}
/**
* 查询列表 回访聊天信息自动回复话术
* @param chatInfo
* @param current 页数
* @param size 条数
* @return
*/
@GetMapping("getLymsChatInfo")
@TokenRequired
public BaseResponse getLymsChatInfo(LymsChatInfo chatInfo, int current, int size){
BaseResponse baseResponse =new BaseResponse();
try {
QueryWrapper<LymsChatInfo> queryWrapper = Wrappers.query(chatInfo);
Page<LymsChatInfo> page = new Page<>(current, size);
Page<LymsChatInfo> patientPagePage = lymsChatInfoService.page(page,queryWrapper);
baseResponse.setObject(patientPagePage);
baseResponse.setErrorcode(0);
baseResponse.setErrormsg("成功");
} catch (Exception e) {
e.printStackTrace();
}
return baseResponse;
}
/**
* 患者点咨询。查询是否有回访。有就直接进入聊天。不消耗问诊卡
* @param chatgroup
* @return
*/
@GetMapping("getYnReturnVisit")
@TokenRequired
public BaseResponse getYnReturnVisit(LymsChatgroup chatgroup){
BaseResponse baseResponse =new BaseResponse();
try {
final LymsChatgroup lymsChatgroup = lymsChatgroupService.getOne(new QueryWrapper<LymsChatgroup>()
.lambda().eq(LymsChatgroup::getFromp, chatgroup.getFromp())
.eq(LymsChatgroup::getTarget, chatgroup.getTarget())
.eq(LymsChatgroup::getPcid, chatgroup.getPcid()));
if (null!=lymsChatgroup) {
baseResponse.setObject(lymsChatgroup);
}
baseResponse.setErrorcode(0);
baseResponse.setErrormsg("成功");
} catch (Exception e) {
e.printStackTrace();
}
return baseResponse;
}
/**
* 有回访记录时。患者消耗问诊卡修改环信组状态 并加问诊记录,开启聊天
* @param chatgroup
* @return
*/
@PostMapping("updateGroupStat")
@TokenRequired
@Transactional(rollbackFor = Exception.class)
public BaseResponse updateGroupStat(@RequestBody LymsChatgroup chatgroup){
BaseResponse baseResponse =new BaseResponse();
try {
final boolean b = lymsChatgroupService.updateById(chatgroup);
if(!b){
baseResponse.setErrorcode(1);
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
final PatientInfo patientInfo = patientInfoService.getOne(new QueryWrapper<PatientInfo>()
.lambda().eq(PatientInfo::getCid, chatgroup.getPcid()));
if (null==patientInfo) {
baseResponse.setErrorcode(1);
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
LymsTkrecord tkrecord=new LymsTkrecord();
LymsTcard tcard = new LymsTcard();
tcard.setPid(patientInfo.getId());
tcard.setPcid(patientInfo.getCid());
// 获取问诊卡按照先从医院购买的问诊卡消费
List<LymsTcard> rLst = lymsTcardService.list(Wrappers.query(tcard).notInSql("id", "SELECT r.cid FROM lyms_tkrecord r").orderByDesc("fid"));

// 小程序购买的问诊卡
tcard.setPcid(null);
tcard.setFid(1);
List<LymsTcard> xLst = lymsTcardService.list(Wrappers.query(tcard).notInSql("id", "SELECT r.cid FROM lyms_tkrecord r").orderByDesc("fid"));

if (rLst.size() > 0 || xLst.size() > 0) {
if (rLst.size() > 0) {
tkrecord.setCid(rLst.get(0).getId());
tkrecord.setFid((byte) 2);
} else {
tkrecord.setCid(xLst.get(0).getId());
tkrecord.setFid((byte) 1);
}

} else {
baseResponse.setErrorcode(1);
throw new RuntimeException("已经没有咨询卡,请联系客服购买!");
}

tkrecord.setPid(patientInfo.getId());
tkrecord.setPcid(patientInfo.getCid());
tkrecord.setDid(patientInfo.getDtid());
tkrecord.setDid(patientInfo.getDtid());
tkrecord.setHxgroupid(chatgroup.getHxgroupid());
tkrecord.setCreatedtime(new Date());
final boolean save = lymsTkrecordService.save(tkrecord);
if (!save) {
baseResponse.setErrorcode(1);
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) {
baseResponse.setErrorcode(1);
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
}
baseResponse.setErrorcode(0);
baseResponse.setErrormsg("成功");
} catch (Exception e) {
baseResponse.setErrorcode(1);
e.printStackTrace();
//这里想触发事务回滚需抛运行时异常
throw new RuntimeException("回滚状态,执行失败,请联系管理员");
}
return baseResponse;
}
/**
* 添加自动回复信息-查重
* @param type 自动回复类型
* @param illid 疾病id
* @param id 修改时用
* @return true 可以使用,false已经存在
*/
@GetMapping("getChatInfoRepetition")
@TokenRequired
public BaseResponse getChatInfoRepetition(Integer type, Integer illid, @RequestParam(required = false) Integer id){
BaseResponse baseResponse=new BaseResponse();
try {
Map<String, Boolean> map= new HashMap<>();
map.put("type", true);map.put("illid", true);
QueryWrapper<LymsChatInfo> queryWrapper=new QueryWrapper<>();
queryWrapper.eq("type", type);
queryWrapper.eq("illid", illid);
if(null!=id){//修改的时候判断是否重复
QueryWrapper<LymsChatInfo> queryWrapper2=new QueryWrapper<>();
queryWrapper2.eq("id", id);
LymsChatInfo chatInfo2=lymsChatInfoService.getOne(queryWrapper2);
List<LymsChatInfo> chatInfoList=lymsChatInfoService.list(queryWrapper);
if(CollectionUtils.isNotEmpty(chatInfoList)){
if(null!=type && !chatInfo2.getType().equals(type)){
for (LymsChatInfo chatInfo : chatInfoList) {
if(chatInfo.getType().equals(type)){
map.put("type", false);
}
}
}
if(null!=illid && !chatInfo2.getIllid().equals(illid)){
for (LymsChatInfo chatInfo : chatInfoList) {
if(chatInfo.getIllid().equals(illid)){
map.put("illid", false);
}
}
}
}
baseResponse.setObject(map);
}else {
LymsChatInfo chatInfo=lymsChatInfoService.getOne(queryWrapper);
if (null!=chatInfo) {
if(null!=type && chatInfo.getType().equals(type)){
map.put("type", false);
}
if(null!=illid && illid.equals(chatInfo.getIllid())){
map.put("illid", false);
}
}
baseResponse.setObject(map);
}
baseResponse.setErrormsg("成功");
} catch (Exception e) {
baseResponse.setErrorcode(1);
baseResponse.setErrormsg("失败");
e.printStackTrace();
}
return baseResponse;
}

/**
* 获取聊天群组,组装数据
* @param rs 最后的结果
* @param jsonObject 参数集合
*/
public void getChatRecord(JSONArray rs,JSONObject jsonObject){
JSONArray rsrjson=new JSONArray();
//登录账号
String user_name=jsonObject.get("user_name").toString();
//1 环信管理员 2 医生 3 患者
String type=jsonObject.get("type").toString();
LymsChatgroup queryChatgroup=new LymsChatgroup();
switch (type){
case "1":
queryChatgroup.setOwnerk(hxuser);
break;
case "2":
queryChatgroup.setTarget(user_name);
break;
case "3":
queryChatgroup.setFromp(user_name);
break;
}
List<LymsChatgroup> chatgroupList=lymsChatgroupService.list(Wrappers.query(queryChatgroup));
for (LymsChatgroup lymsChatgroup : chatgroupList) {
//组装结果
JSONObject reusltJsonObject = new JSONObject();
reusltJsonObject.put("unread_num",0);
reusltJsonObject.put("stat",lymsChatgroup.getStat());
//患者信息
LambdaQueryWrapper<LymsPatient> patientQueryWrapper=new QueryWrapper().lambda();
patientQueryWrapper.eq(LymsPatient::getIdno, lymsChatgroup.getFromp());
LymsPatient patient = lymsPatientService.getOne(patientQueryWrapper);
if (null==patient) {
continue;
}
reusltJsonObject.put("pat",patient);
//聊天记录
LambdaQueryWrapper<LymsMessage> messageQueryWrapper=new QueryWrapper().lambda();
messageQueryWrapper.eq(LymsMessage::getTargetid, lymsChatgroup.getHxgroupid());
messageQueryWrapper.orderByDesc(LymsMessage::getSendtime);
final List<LymsMessage> lymsMessageList = lymsMessageService.list(messageQueryWrapper);
Map map=new HashMap();
map.put("msg",lymsMessageList.size()>0?lymsMessageList.get(0).getContent():"");
map.put("type","txt");
reusltJsonObject.put("bodies",Arrays.asList(map));
//医生信息
LambdaQueryWrapper<AppgetdoctorlistInfo> appgetdoctorlistInfoQueryWrapper=new QueryWrapper().lambda();
appgetdoctorlistInfoQueryWrapper.eq(AppgetdoctorlistInfo::getDlogin, lymsChatgroup.getTarget());
List<AppgetdoctorlistInfo> dLst = appgetdoctorlistInfoService.list(appgetdoctorlistInfoQueryWrapper);
if (dLst.size()==0) {
continue;
}
reusltJsonObject.put("doc",dLst.get(0));
reusltJsonObject.put("from",lymsChatgroup.getFromp());
reusltJsonObject.put("to",lymsChatgroup.getHxgroupid());
reusltJsonObject.put("type","groupchat");
reusltJsonObject.put("timestamp",lymsMessageList.get(0).getSendtime());
for (Object r : rs) {
JSONObject rjson= (JSONObject) JSON.toJSON(r);
if (! rjson.get("to").toString().equals(lymsChatgroup.getHxgroupid())) {
rsrjson.add(reusltJsonObject);
}
}
}
//结果集中添加
for (Object o : rsrjson) {
rs.add(o);
}
}
}