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

import com.docus.core.util.Func;
import com.docus.server.collection.dto.TBasicDto;
import com.docus.server.collection.httpservices.HttpTBasicService;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.http.util.HttpUtil;
import org.springframework.stereotype.Service;

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 {
    @Override
    public List<TBasicDto> getTBasicDto(String url, Map<String,String> headers, Map<String,Object> params) {
        List<TBasicDto> tBasicDtos=null;
        try {
            //发送get请求获取返货值
            log.info("发送请求开始");
            String resultStr = HttpUtil.get(url, headers, params);

            //解析返货值字符串
            log.info("解析:"+resultStr);
            tBasicDtos=parsing(resultStr);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        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("directorDocCode");
            //主管医生姓名
            String attendingName=map.get("directorDocName");
            //责任护士编号
            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("");
            //床位号
            String bedNum=map.get("bedNo");

            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);

            tBasicDtos.add(tBasicDto);
        }

        return tBasicDtos;
    }
}
