package cn.hnthyy.thmz.webservice.impl;


import cn.hnthyy.thmz.Utils.SpringBeanUtils;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.his.zd.ZdDeptAll;
import cn.hnthyy.thmz.mapper.his.zd.ZdDeptAllMapper;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.service.impl.his.mz.EmployeeServiceImpl;
import cn.hnthyy.thmz.service.impl.his.zd.ZdUnitCodeServiceImpl;
import cn.hnthyy.thmz.service.impl.thmz.MessageServiceImpl;
import cn.hnthyy.thmz.service.thmz.MessageService;
import cn.hnthyy.thmz.webservice.CriticalWebService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
//@WebService
@Service
public class CriticalWebServiceImpl implements CriticalWebService {
    private String token = "oI9#ur02cbBHdop87e6Y728Ax30IIlgd";
    MessageService messageService = null;
    EmployeeService employeeService = null;
    ZdUnitCodeService zdUnitCodeService = null;
    @SuppressWarnings("all")
    @Autowired
    private ZdDeptAllMapper zdDeptAllMapper;
    @Override
    public String criticalAccept(String accessToken, String msg) {
        log.info("收到危急值信息:----->" + msg + "<-------");
        if(messageService==null){
            messageService = SpringBeanUtils.getBean(MessageServiceImpl.class);
        }
        if(employeeService==null){
            employeeService= SpringBeanUtils.getBean(EmployeeServiceImpl.class);
        }
        if(zdUnitCodeService==null){
            zdUnitCodeService = SpringBeanUtils.getBean(ZdUnitCodeServiceImpl.class);
        }
        Map<String, String> resultMap = new HashMap<>();
        if (StringUtils.isBlank(accessToken)) {
            resultMap.put("Code", "false");
            resultMap.put("Message", "token不能为空");
            log.error("token不能为空");
            JSONObject jsonObj = new JSONObject(resultMap);
            return jsonObj.toString();
        }
        if (StringUtils.isBlank(msg)) {
            resultMap.put("Code", "false");
            resultMap.put("Message", "msg为空");
            log.error("msg为空");
            JSONObject jsonObj = new JSONObject(resultMap);
            return jsonObj.toString();
        }
        if (!token.equals(accessToken)) {
            resultMap.put("Code", "false");
            resultMap.put("Message", "token校验失败");
            log.error("token校验失败");
            JSONObject jsonObj = new JSONObject(resultMap);
            return jsonObj.toString();
        }
        JSONObject myJsonObject = new JSONObject(msg);
        if (myJsonObject == null) {
            resultMap.put("Code", "false");
            resultMap.put("Message", "消息读取失败");
            log.error("消息读取失败");
            JSONObject jsonObj = new JSONObject(resultMap);
            return jsonObj.toString();
        }
        String targetDeptCode = myJsonObject.getString("TargetDeptCode");
        if (StringUtils.isBlank(targetDeptCode)) {
            resultMap.put("Code", "false");
            resultMap.put("Message", "目标科室为空");
            log.error("目标科室为空");
            JSONObject jsonObj = new JSONObject(resultMap);
            return jsonObj.toString();
        }
        String content = myJsonObject.getString("Content");
        if (StringUtils.isBlank(content)) {
            resultMap.put("Code", "false");
            resultMap.put("Message", "消息内容为空");
            log.error("消息内容为空");
            JSONObject jsonObj = new JSONObject(resultMap);
            return jsonObj.toString();
        }
        //住院科室
        String zkWard = null;
        //管床医生
        String referPhysician = null;
        //主治医师
        String consultPhysician = null;
        //主任医师
        String deptDirector = null;
        if (content.indexOf("住院号") > 0) {
            //住院病人
            String inpatientNo = content.substring(content.indexOf("住院号") + 4, content.indexOf(",姓名"));
            if (StringUtils.isNotBlank(inpatientNo)) {
                Map<String, String> map = zdUnitCodeService.queryZkWardByInpatientNo(inpatientNo);
                if (map != null && map.size() > 0) {
                    zkWard = map.get("zkWard");
                    referPhysician = map.get("refer_physician");
                    consultPhysician = map.get("consult_physician");
                    deptDirector = map.get("dept_director");
                }
            }
        }
        List<Employee> employees = employeeService.queryEmployeeByDept(zkWard != null ? zkWard : targetDeptCode.trim());
        List<ZdDeptAll> zdDeptAlls = zdDeptAllMapper.selectBySmallDept(zkWard != null ? zkWard : targetDeptCode.trim());
        if(zdDeptAlls!=null && zdDeptAlls.size()>0){
            for (ZdDeptAll zdDeptAll:zdDeptAlls){
                if(StringUtils.isBlank(zdDeptAll.getDept())){
                    continue;
                }
                List<Employee> tempList = employeeService.queryEmployeeByDept(zdDeptAll.getDept());
                if(tempList==null || tempList.size()<=0){
                    continue;
                }
                employees.addAll(tempList);
            }
        }
        String codes = null;
        Set<String> tempSet = new HashSet<>();
        if (StringUtils.isNotBlank(referPhysician)) {
            tempSet.add(referPhysician);
        }
        if (StringUtils.isNotBlank(referPhysician)) {
            tempSet.add(consultPhysician);
        }
        if (StringUtils.isNotBlank(deptDirector)) {
            tempSet.add(deptDirector);
        }
        if (tempSet.size() > 0) {
            List<Employee> tempsEmps = employeeService.queryByCodes(new ArrayList<>(tempSet));
            if (tempsEmps != null && tempsEmps.size() > 0) {
                employees.addAll(tempsEmps);
            }
        }
        if (employees != null) {
            Set<String> fullCodes = new HashSet<>();
            for (Employee employee : employees) {
                if(!fullCodes.contains(employee.getCodeRs())){
                    if (codes == null) {
                        codes = employee.getCodeRs();
                    } else {
                        codes += "," + employee.getCodeRs();
                    }
                    fullCodes.add(employee.getCodeRs());
                }
            }
        }
        if (codes != null) {
           String MsgID= (String) myJsonObject.get("MsgID");
            messageService.sendWxMessage(codes, content.replaceAll("=",":").replaceAll(" ",""));
            log.info("消息id=【"+MsgID+"】的危机值消息发布成功,接收人codes=【"+codes+"】");
            //messageService.sendWxMessage("01387,3210", content.replaceAll("=", ":").replaceAll(" ", ""));
        }
        resultMap.put("Code", "true");
        resultMap.put("Message", "");
        JSONObject jsonObj = new JSONObject(resultMap);
        return jsonObj.toString();
    }
}