/**
 * Copyright (C), 2015-2019
 * Author:   HJL
 * Date:     2019/7/19 15:39
 * Description:患者住院信息主索引管理
 */
package com.emr.service.ipml;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.emr.dao.ArchivePrintinfoMapper;
import com.emr.dao.Archive_MasterMapper;
import com.emr.dao.DocusRequestInfoMapper;
import com.emr.dao.MedicalRecordHomepageCacheMapper;
import com.emr.util.HttpClientTool;
import com.emr.util.IDHelper;
import com.emr.vo.Power_User_Dto;
import com.emr.entity.*;
import com.emr.service.Archive_MasterService;
import com.emr.service.PowerUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Function;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.google.common.collect.Maps;
import org.springframework.util.CollectionUtils;

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

@Slf4j
@Service
public class Archive_MasterServiceImpl implements Archive_MasterService {
    @Value("${returnUrl}")
    private String returnUrl;
    @Value("${returnParam}")
    private String returnParam;
    @Value("${POWER_JSP}")
    private String POWER_JSP;
    @Autowired
    private Archive_MasterMapper archiveMasterMapper;

    @Autowired
    private PowerUserService powerUserService;

    @Autowired
    private MedicalRecordHomepageCacheMapper medicalRecordHomepageCacheMapper;

    @Autowired
    private ArchivePrintinfoMapper archivePrintinfoMapper;

    @Value("${observationRecordRequestUrl}")
    private String observationRecordRequestUrl;

    @Autowired
    private DocusRequestInfoMapper docusRequestInfoMapper;

    @Override
    public Archive_Master selectById(String id) {
        return archiveMasterMapper.selectById(id);
    }

    @Override
    public List<Archive_Master> selByCol(Archive_Master archiveMaster) {
        return archiveMasterMapper.selByCol(archiveMaster);
    }

    @Override
    public OffsetLimitPage selectByCol(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master> list= selectByCol(archiveMasterVo);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master> selectByCol(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master> list=archiveMasterMapper.selectByCol(archiveMasterVo);
        if(list!=null && list.size()>0) {
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<JSONObject> powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), JSONObject.class);
            if (powerUserList != null) {
                Map<String, JSONObject> mappedMovies = Maps.uniqueIndex(powerUserList, new Function<JSONObject, String>() {
                    @Override
                    public String apply(JSONObject temp) {
                        return temp.getString("userName");
                    }
                });
                for (Archive_Master archiveMaster : list) {
                    JSONObject userInfo = mappedMovies.get(archiveMaster.getDoctorName());
                    if (userInfo == null) {
                        archiveMaster.setDoctorName(archiveMaster.getDoctorName());
                    } else {
                        archiveMaster.setDoctorName(userInfo.getString("name"));
                    }

                    userInfo = mappedMovies.get(archiveMaster.getNurseName());
                    if (userInfo == null) {
                        archiveMaster.setNurseName(archiveMaster.getNurseName());
                    } else {
                        archiveMaster.setNurseName(userInfo.getString("name"));
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<Archive_Master_Vo> selectByColumn(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list=archiveMasterMapper.selectByColumn(archiveMasterVo);
        setDoctorAndNurseName(list);
        return list;
    }

   /*   @Override
    public List<Archive_Master> selectByUnfile2(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByUnfile2(archiveMasterVo);
    }*/

   /* @Override
    public List<Archive_Master> sel7DayByCol(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.sel7DayByCol(archiveMasterVo);
    }*/

    @Override
    public List<Archive_Master_Vo> selectByLastByColumn(Archive_Master_Vo archiveMasterVo,Integer offset, Integer limit) {
        //判断并设置查询是否打印权限
        //setIdByPrintNum(archiveMasterVo);
        //根据条件查询记录
        if(null != offset && null != limit) {
            PageHelper.offsetPage(offset, limit);
        }
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByLast(archiveMasterVo);
        //设置医生、护士名称和打印标识
        setNameAndPrintFlag(archiveMasterVo, list);
        return list;
    }

    @Override
    public OffsetLimitPage selectByColumn(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        List<Archive_Master_Vo> list = selectBeHospitalByColumn(archiveMasterVo,offset,limit);
        return new OffsetLimitPage((Page) list);
    }

    /**
     * 判断并设置查询是否打印权限
     * @param archiveMasterVo
     */
    private void setIdByPrintNum(Archive_Master_Vo archiveMasterVo){
        //是否查询打印情况
        if(StringUtils.isNotBlank(archiveMasterVo.getPrintNum())){
            List<ArchivePrintinfo> archivePrintinfos = archivePrintinfoMapper.selectPrintCountByMasterId(null);
            //定义masterIds
            StringBuilder masterIds = new StringBuilder();
            if(!CollectionUtils.isEmpty(archivePrintinfos)){
                for(ArchivePrintinfo archivePrintinfo : archivePrintinfos){
                    if(StringUtils.isNotBlank(masterIds)){
                        masterIds.append(",");
                    }
                    masterIds.append(archivePrintinfo.getMasterid());
                }
            }else{
                masterIds = new StringBuilder("notAll");
            }
            archiveMasterVo.setId(masterIds.toString());
        }
    }

    @Override
    public List<Archive_Master_Vo> selectBeHospitalByColumn(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        //判断并设置查询是否打印权限
        //setIdByPrintNum(archiveMasterVo);
        //根据条件查询记录
        if(null != offset && null != limit) {
            PageHelper.offsetPage(offset, limit);
        }
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByColumn(archiveMasterVo);
        //设置医生、护士名称和打印标识
        setNameAndPrintFlag(archiveMasterVo, list);
        return list;
    }

    /**
     * 设置医生、护士名称和打印标识
     * @param archiveMasterVo
     * @param list
     */
    private void setNameAndPrintFlag(Archive_Master_Vo archiveMasterVo, List<Archive_Master_Vo> list) {
        if(!CollectionUtils.isEmpty(list)) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if(!CollectionUtils.isEmpty(powerUsers)) {
                 //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //定义查询的masterId集合
            StringBuilder masterIds = new StringBuilder();
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if(null != powerUserList) {
                    //获取医生工号
                    String doctorName = archiveMaster.getDoctorName();
                    //获取护士工号
                    String nurseName = archiveMaster.getNurseName();
                    //获取初审人工号
                    String firstInstance = archiveMaster.getFirstInstance();
                    //遍历匹配工号赋值姓名
                    for(Power_User_Dto powerUserDto : powerUserList){
                        String name = powerUserDto.getName();
                        if(StringUtils.isNotBlank(doctorName) && powerUserDto.getUserName().equals(doctorName)){
                            archiveMaster.setDoctorName(name);
                        }
                        if(StringUtils.isNotBlank(nurseName) && powerUserDto.getUserName().equals(nurseName)){
                            archiveMaster.setNurseName(name);
                        }
                        if(StringUtils.isNotBlank(firstInstance) && powerUserDto.getUserName().equals(firstInstance)){
                            archiveMaster.setFirstInstance(name);
                        }
                    }
                }
                String printNum = archiveMaster.getPrintNum();
                if(StringUtils.isNotBlank(printNum) && "1".equals(printNum)){
                    archiveMaster.setPrintNum("已打印");
                }else{
                    archiveMaster.setPrintNum("未打印");
                }
                //是否打印条件不为空，直接赋值打印标识
                if(StringUtils.isNotBlank(archiveMasterVo.getPrintNum())){
                    String printFlag = "未打印";
                    if("1".equals(archiveMasterVo.getPrintNum())){
                        printFlag = "已打印";
                    }
                    archiveMaster.setPrintNum(printFlag);
                }else{
                    if(StringUtils.isNotBlank(masterIds)){
                        masterIds.append(",");
                    }
                    masterIds.append(archiveMaster.getId());
                }
            }
            //查询打印情况
            /*if(StringUtils.isNotBlank(masterIds)){
                List<ArchivePrintinfo> archivePrintinfos = archivePrintinfoMapper.selectPrintCountByMasterId(masterIds.toString());
                //定义初始化打印状态
                String printFlag = "未打印";
                for (Archive_Master_Vo archiveMaster : list) {
                    if(!CollectionUtils.isEmpty(archivePrintinfos)){
                        for(ArchivePrintinfo archivePrintinfo : archivePrintinfos){
                            if(archivePrintinfo.getMasterid().equals(archiveMaster.getId())){
                                printFlag = "已打印";
                                break;
                            }
                        }
                    }
                    archiveMaster.setPrintNum(printFlag);
                }
            }*/
        }
    }

    /**
     * 设置医生和护士姓名
     * @param list
     */
    private void setDoctorAndNurseName(List<Archive_Master_Vo> list){
        if(!CollectionUtils.isEmpty(list)) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取医生工号
                    String doctorName = archiveMaster.getDoctorName();
                    //获取护士工号
                    String nurseName = archiveMaster.getNurseName();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorName) && powerUserDto.getUserName().equals(doctorName)) {
                            archiveMaster.setDoctorName(name);
                        }
                        if (StringUtils.isNotBlank(nurseName) && powerUserDto.getUserName().equals(nurseName)) {
                            archiveMaster.setNurseName(name);
                        }
                    }
                }
            }
        }
    }

    @Override
    public OffsetLimitPage selectByUnfile(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        List<Archive_Master_Vo> list = selectByUnfileAndColumn(archiveMasterVo,offset,limit);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectByUnfileAndColumn(Archive_Master_Vo archiveMasterVo,Integer offset, Integer limit) {
        //判断并设置查询是否打印权限
        //setIdByPrintNum(archiveMasterVo);
        //根据条件查询记录
        if(null != offset && null != limit) {
            PageHelper.offsetPage(offset, limit);
        }
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByUnfileNew(archiveMasterVo);
        //设置医生、护士名称和打印标识
        setNameAndPrintFlag(archiveMasterVo, list);
        return list;
    }


    @Override
    public OffsetLimitPage selectStorageByCol(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectStorageByCol(archiveMasterVo);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectStorageByCol(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectStorageByCol(archiveMasterVo);
    }

    @Override
    public OffsetLimitPage selectByLast(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        List<Archive_Master_Vo> list = selectByLastByColumn(archiveMasterVo, offset, limit);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public OffsetLimitPage selectByCmtCol(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        List<Archive_Master_Vo> list = selectByCmtByColumn(archiveMasterVo, offset, limit);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public int updateByClo(Archive_Master record) {
        return archiveMasterMapper.updateByClo(record);
    }

    @Override
    public int updateById(Archive_Master record) {
        return archiveMasterMapper.updateById(record);
    }

    @Override
    public List<Archive_Master> getDoctorInCharge(Archive_Master record) {
        return archiveMasterMapper.getDoctorInCharge(record);
    }

    @Override
    public List<Archive_Master> bbSel(Archive_Master_Vo record) {
        return archiveMasterMapper.bbSel(record);
    }

    @Override
    public OffsetLimitPage bbSel(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master> list = archiveMasterMapper.bbSel(archiveMasterVo);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master> bbSel2(Archive_Master_Vo record) {
        return archiveMasterMapper.bbSel2(record);
    }

    @Override
    public OffsetLimitPage bbSel2(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master> list = archiveMasterMapper.bbSel2(archiveMasterVo);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectHandleDetail(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.selectHandleDetail(archiveMasterVo);
        return list;
    }

    @Override
    public OffsetLimitPage selectHandleDetail(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectHandleDetail(archiveMasterVo);
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectOverdue(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.selectOverdue(archiveMasterVo);
        if(list!=null && list.size()>0){
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取主管医生
                    String doctorInCharge = archiveMaster.getDoctorInCharge();
                    //获取 初审人
                    String firstInstance = archiveMaster.getFirstInstance();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorInCharge) && powerUserDto.getUserName().equals(doctorInCharge)) {
                            archiveMaster.setDoctorInCharge(name);
                        }
                        if (StringUtils.isNotBlank(firstInstance) && powerUserDto.getUserName().equals(firstInstance)) {
                            archiveMaster.setFirstInstance(name);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public OffsetLimitPage selectOverdue(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectOverdue(archiveMasterVo);
        if(list!=null && list.size()>0) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取主管医生
                    String doctorInCharge = archiveMaster.getDoctorInCharge();
                    //获取 初审人
                    String firstInstance = archiveMaster.getFirstInstance();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorInCharge) && powerUserDto.getUserName().equals(doctorInCharge)) {
                            archiveMaster.setDoctorInCharge(name);
                        }
                        if (StringUtils.isNotBlank(firstInstance) && powerUserDto.getUserName().equals(firstInstance)) {
                            archiveMaster.setFirstInstance(name);
                        }
                    }
                }
            }
        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectOverdue2(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.selectOverdue2(archiveMasterVo);
        if(list!=null && list.size()>0) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取主管医生
                    String doctorInCharge = archiveMaster.getDoctorInCharge();
                    //获取 初审人
                    String firstInstance = archiveMaster.getFirstInstance();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorInCharge) && powerUserDto.getUserName().equals(doctorInCharge)) {
                            archiveMaster.setDoctorInCharge(name);
                        }
                        if (StringUtils.isNotBlank(firstInstance) && powerUserDto.getUserName().equals(firstInstance)) {
                            archiveMaster.setFirstInstance(name);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public OffsetLimitPage selectOverdue2(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectOverdue2(archiveMasterVo);
        if(list!=null && list.size()>0) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取主管医生
                    String doctorInCharge = archiveMaster.getDoctorInCharge();
                    //获取 初审人
                    String firstInstance = archiveMaster.getFirstInstance();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorInCharge) && powerUserDto.getUserName().equals(doctorInCharge)) {
                            archiveMaster.setDoctorInCharge(name);
                        }
                        if (StringUtils.isNotBlank(firstInstance) && powerUserDto.getUserName().equals(firstInstance)) {
                            archiveMaster.setFirstInstance(name);
                        }
                    }
                }
            }
        }
        return new OffsetLimitPage((Page) list);
    }

    @Override
    public OffsetLimitPage selectByFollowinglog(Archive_Master_Vo record, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByFollowinglog(record);

        return  new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> selectByFollowinglog(Archive_Master_Vo archiveMasterVo) {
        return archiveMasterMapper.selectByFollowinglog(archiveMasterVo);
    }

    @Override
    public List<Archive_Master> selectByObject(String patientId,String visitId) {
        return archiveMasterMapper.selectByObject(patientId,visitId);
    }

    @Override
    public void requestObservationRecord(String inpatientNo, String status,String reason) {
        //留观推送第三方 http://10.162.0.51:8091/sanshuyun-ems-function/file/away/updateStatus
        Map<String,String> params = new HashMap<>();
        //留观号
        params.put("watchNo",inpatientNo);
        //status状态 AUDITED审核，RETURNED 退回
        params.put("status",status);
        if (StringUtils.isNotBlank(reason)) {
            params.put("reason",reason);
        }
        try {
            HttpClientTool.doPost(observationRecordRequestUrl,params);
        } catch (IOException e) {
            e.printStackTrace();
            String errorStr = "留观病历操作推送第三方失败,inpatientNo:" + inpatientNo + ";status:" + status;
            System.out.println(errorStr);
            log.error(errorStr);
        }
    }

    @Override
    public List<Archive_Master_Vo> selectByCmtByColumn(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        //判断并设置查询是否打印权限
        //setIdByPrintNum(archiveMasterVo);
        //根据条件查询记录
        if(null != offset && null != limit) {
            PageHelper.offsetPage(offset, limit);
        }
        List<Archive_Master_Vo> list = archiveMasterMapper.selectByCmtCol(archiveMasterVo);
        //设置医生、护士名称和打印标识
        setNameAndPrintFlag(archiveMasterVo, list);
        return list;
    }

    /**
     * 退回电子病历
     * @param emrFaultDetail
     * @param archiveMaster
     * @param request
     */
    @Override
    public String returnEmr(List<Emr_Fault_Detail> list,Emr_Fault_Detail emrFaultDetail, Archive_Master archiveMaster, HttpServletRequest request) {
        String returnMsg = null;
        //获取退回标识，0打回首页；1打回医生；2打回护士
        String returnFlag = emrFaultDetail.getDealFlag();
        if(StringUtils.isNotBlank(returnFlag)) {
            //获取打回原因
            StringBuilder returnContent = new StringBuilder();
            if(!CollectionUtils.isEmpty(list)){
                for(Emr_Fault_Detail obj : list){
                    if(StringUtils.isNotBlank(returnContent)){
                        returnContent.append(",");
                    }
                    returnContent.append(obj.getBackContent());
                    String content = obj.getContent();
                    if(StringUtils.isBlank(content)){
                        content = "无";
                    }
                    returnContent.append(":").append(content);
                }
            }
            //组织打回参数值
            /*{
                "INP_NO": "1356601",
                "VISIT_ID": "31",
                "DISCHARGE_DATE_TIME": "2021/3/8 9:54:00",
                "FLAG": "0,2",
                "HOSPITAL_NO": "12440000455858169P",
                "userid": "CSCS",
                "username": "CSCS",
                "code": "1340100",
                "REASON": "第三方打回"
            }*/
            Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
            Map<String,Object> map = new HashMap<>();
            map.put("INP_NO",archiveMaster.getInpNo());
            map.put("VISIT_ID",archiveMaster.getVisitId());
            map.put("DISCHARGE_DATE_TIME",archiveMaster.getDischargeDateTime());
            map.put("FLAG",returnFlag);
            map.put("HOSPITAL_NO","12440000455858169P");
            map.put("userid",user.getUserId().toString());
            map.put("username",user.getUserName());
            map.put("code",archiveMaster.getDeptName());
            map.put("REASON",returnContent.toString());
            //定义请求信息记录表
            DocusRequestInfo docusRequestInfo = new DocusRequestInfo();
            docusRequestInfo.setCode("return");
            docusRequestInfo.setCreateUsername(user.getUserName());
            docusRequestInfo.setCreateTime(new Date());
            docusRequestInfo.setJzh(archiveMaster.getPatientId());
            docusRequestInfo.setPrimaryKey(archiveMaster.getInpNo());
            docusRequestInfo.setMasterId(archiveMaster.getId());
            docusRequestInfo.setParamPrimaryMsg(returnFlag);
            docusRequestInfo.setResultState(0);
            try {
                long id = IDHelper.NewID();
                docusRequestInfo.setId(id);
                //map转json字符串
                Map<String,String> paramMap = new HashMap<>();
                JSONObject object = new JSONObject(map);
                String json = object.toString();
                docusRequestInfo.setParamInfo(json);
                paramMap.put(returnParam,json);
                String returnStr = HttpClientTool.doPost(returnUrl, paramMap);
                if(StringUtils.isNotBlank(returnStr) && returnStr.contains("成功")){
                    //成功
                    docusRequestInfo.setResultState(1);
                }else{
                    returnMsg = "提示错误信息（请与V6系统工程师联系）无法退回";
                }
                docusRequestInfo.setResultMsg(returnStr);
            } catch (Exception e) {
                e.printStackTrace();
                returnMsg = "调用退回电子病历失败";
            }
            docusRequestInfoMapper.insert(docusRequestInfo);
        }
        return returnMsg;
    }

    @Override
    public List<Archive_Master_Vo> copyDetailList(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.copyDetailList(archiveMasterVo);
        if(list!=null && list.size()>0) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取主管医生
                    String doctorInCharge = archiveMaster.getDoctorInCharge();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorInCharge) && powerUserDto.getUserName().equals(doctorInCharge)) {
                            archiveMaster.setDoctorInCharge(name);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public OffsetLimitPage copyDetailList(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.copyDetailList(archiveMasterVo);
        if(list!=null && list.size()>0) {
            //查询用户集合
            JSONArray powerUsers = powerUserService.getPowerUserList("1");
            List<Power_User_Dto> powerUserList = null;
            if (!CollectionUtils.isEmpty(powerUsers)) {
                //转换对象
                powerUserList = JSONArray.parseArray(powerUsers.toJSONString(), Power_User_Dto.class);
            }
            //遍历赋值
            for (Archive_Master_Vo archiveMaster : list) {
                if (null != powerUserList) {
                    //获取主管医生
                    String doctorInCharge = archiveMaster.getDoctorInCharge();
                    //遍历匹配工号赋值姓名
                    for (Power_User_Dto powerUserDto : powerUserList) {
                        String name = powerUserDto.getName();
                        if (StringUtils.isNotBlank(doctorInCharge) && powerUserDto.getUserName().equals(doctorInCharge)) {
                            archiveMaster.setDoctorInCharge(name);
                        }
                    }
                }
            }
        }
        return  new OffsetLimitPage((Page) list);
    }

    @Override
    public List<Archive_Master_Vo> copyCountList(Archive_Master_Vo archiveMasterVo) {
        List<Archive_Master_Vo> list = archiveMasterMapper.copyCountList(archiveMasterVo);
        return list;
    }

    @Override
    public OffsetLimitPage copyCountList(Archive_Master_Vo archiveMasterVo, Integer offset, Integer limit) {
        PageHelper.offsetPage(offset, limit);
        List<Archive_Master_Vo> list = archiveMasterMapper.copyCountList(archiveMasterVo);
        return  new OffsetLimitPage((Page) list);
    }
}
 

