package com.docus.server.collection.httpservices.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.docus.core.util.Func;
import com.docus.server.collection.dto.TBasicDto;
import com.docus.server.collection.httpservices.HttpTBasicService;
import com.docus.server.collection.rpc.MzZyyRocCurrWebCommonRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: docus-mzzy-collector
 * @BelongsPackage: com.docus.server.collection.httpservices.impl
 * @Author: chierhao
 * @CreateTime: 2023-03-10  11:46
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class HttpTBasicServiceImpl implements HttpTBasicService {
    @Resource
    private MzZyyRocCurrWebCommonRpc mzZyyRocCurrWebCommonRpc;
    @Override
    public List<TBasicDto> getTBasicDto(String url, Map<String,String> headers, Map<String,Object> params) {
        List<TBasicDto> tBasicDtos=null;
        try {
            //发送get请求获取返货值
            log.info("发送请求开始");
            HttpRequest get = HttpUtil.createGet(url);
            for (Map.Entry<String, String> map : headers.entrySet()) {
                get.header(map.getKey(),map.getValue());
            }
            get.form(params);
            get.timeout(60*1000);
            String resultStr = get.execute().body();
//            String resultStr = HttpUtil.get(url, headers, params);

            //解析返货值字符串
            log.info("解析:"+resultStr);
            tBasicDtos=parsing(resultStr);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            log.info("重试");
            return getTBasicDto(url,headers,params);
        }
        return tBasicDtos;
    }

    private List<TBasicDto> parsing(String resultStr){
        if(Func.isEmpty(resultStr)){
            return null;
        }
        Map result = Func.readJson(resultStr, Map.class);
        if(Func.isEmpty(result)){
            return null;
        }
        Map data =(Map) result.get("data");
        if(Func.isEmpty(data)){
            return null;
        }
        List<Map<String,String>> list =(List) data.get("list");
        if(Func.isEmpty(list)){
            return null;
        }

        List<TBasicDto> tBasicDtos=new ArrayList<TBasicDto>();
        for(Map<String,String> map:list){
            //记帐号
            String jzh=map.get("inpatientNo");
            //病案号
            String inpatientNo =map.get("patientNo");
            //患者姓名
            String name=map.get("patientName");
            //住院次数
            String admissTimes=map.get("inTimes");
            //入院时间
            String admissDate=map.get("inDate");
            //出院时间
            String disDate=map.get("outDate");
            //入院科室编号
            String admissDept=map.get("deptCode");
            //入院科室名称
            String admissDeptName=map.get("deptName");
            //出院科室编号
            String disDept=map.get("deptCode");
            //出院科室名称
            String disDeptName=map.get("deptName");
            //主管医生编号
            String attending=map.get("residencyDocCode");
            //主管医生姓名
            String attendingName=map.get("residencyDocName");
            //责任护士编号
            String dutyNurse=map.get("dutyNurseName");
            //年龄
            String age=map.get("age");
            //性别
            String sex=map.get("sexCode");
            //身份证号
            String idCard=map.get("idenno");
            //住院天数
            String admissDays=map.get("inDays");
            //是否死亡
            String isDead=map.get("outType");
            //床位号
            String bedNum=map.get("bedNo");

            String motherInpatientNo=map.get("motherInpatientNo");

            TBasicDto tBasicDto=new TBasicDto();
            tBasicDto.setAdmissDays(admissDays);
            tBasicDto.setIsDead(isDead);
            tBasicDto.setBedNum(bedNum);
            tBasicDto.setDisDept(disDept);
            tBasicDto.setAdmissDept(admissDept);
            tBasicDto.setIdCard(idCard);
            tBasicDto.setSex(sex);
            tBasicDto.setAge(age);
            tBasicDto.setAttendingName(attendingName);
            tBasicDto.setAttending(attending);
            tBasicDto.setAdmissTimes(admissTimes);
            tBasicDto.setDisDate(disDate);
            tBasicDto.setAdmissDate(admissDate);
            tBasicDto.setDisDeptName(disDeptName);
            tBasicDto.setAdmissDeptName(admissDeptName);
            tBasicDto.setJzh(jzh);
            tBasicDto.setName(name);
            tBasicDto.setInpatientNo(inpatientNo);
            tBasicDto.setDutyNurse(dutyNurse);
            tBasicDto.setMotherInpatientNo(motherInpatientNo);
            // 主管医生取另外的接口数据值
            setAttending(tBasicDto);
            tBasicDtos.add(tBasicDto);
        }

        return tBasicDtos;
    }

    /**
     * 设置另外的主管医生为住院医师
     */
    @Override
    public void setAttending(TBasicDto tBasicDto) {
        String jzh = tBasicDto.getJzh();
        JSONObject patientRes = mzZyyRocCurrWebCommonRpc.getInPatient(jzh);
        if (patientRes == null) {
            return;
        }
        boolean success = "200".equals(patientRes.getString("code"));
        JSONObject patientData = patientRes.getJSONObject("data");
        if (!success || patientData == null) {
            return;
        }
//        String houseDocCode = patientData.getString("houseDocCode");
//        String houseDocName = patientData.getString("houseDocName");
        String baInhosDocCode = patientData.getString("baInhosDocCode");
        String baInhosDocName = patientData.getString("baInhosDocName");
        tBasicDto.setAttending(baInhosDocCode);
        tBasicDto.setAttendingName(baInhosDocName);
    }


}
