package thyyxxk.webserver.service.medicalinsurance.costcontrol;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.medicalinsurance.costcontrol.YbCssBzDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.costcontrol.YbkfShareDto;
import thyyxxk.webserver.utils.DecimalUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 长沙市病种统计
 * @Author: hsh
 * @CreateTime: 2022-09-20 10:26
 * @Version: 1.0
 */
@Service
public class YbCssBzService {

    private final YbCssBzDao dao;

    @Autowired
    public YbCssBzService(YbCssBzDao dao) {
        this.dao = dao;
    }

    /**
     * @description: 查询长沙市病种
     * @author: hsh
     * @date: 2022/9/20 10:38
     * @param: [dto]
     * @return: ResultVo<List<Map<String,Object>>>
     **/
    public ResultVo<List<Map<String, Object>>> selectCssYbBzTj(YbkfShareDto dto){
        String beginTime = dto.getStartTime();
        String endTime = dto.getEndTime();
        String dept = dto.getDept();
        String insurName = dto.getInsurName();
        String bzmc = dto.getBzmc();
        String doctor = dto.getDoctor();
        String yk = dto.getYk();
        List<Map<String, Object>> result = dao.selectCssYbBzTj(beginTime, endTime, dept, insurName, bzmc, doctor);
        List<Map<String, Object>> bzTjmx = dao.selectCssYbBzTjMx(beginTime, endTime, dept, insurName, bzmc, doctor);

        Map<String, List<Map<String, Object>>> bzMap = bzTjmx.stream().collect(Collectors.groupingBy(f -> String.valueOf(f.get("zdmc")), Collectors.toList()));

        if(StringUtil.isBlank(yk)){

            callBzMxInfo(result, bzMap);

            return ResultVoUtil.success(result);
        } else {
            List<Map<String, Object>> ls = new ArrayList<>();
            if("y".equals(yk)){
                for(Map<String, Object> map : result){
                    double bal = Double.parseDouble(map.get("bal") + "");
                    if(bal >= 0d){
                        ls.add(map);
                    }
                }
            } else {
                for(Map<String, Object> map : result){
                    double bal = Double.parseDouble(map.get("bal") + "");
                    if(bal < 0d){
                        ls.add(map);
                    }
                }
                ls = ls.stream().sorted((e1,e2) -> -Double.compare(Double.valueOf(e1.get("bal") + ""), Double.valueOf(e2.get("bal") + ""))).collect(Collectors.toList());
            }

            callBzMxInfo(ls, bzMap);

            return ResultVoUtil.success(ls);

        }

    }

    /**
     * @description: 病种添加明细信息
     * @author: hsh
     * @date: 2022/9/21 8:44
     * @param: [list, bzMap]
     * @return: void
     **/
    private void callBzMxInfo(List<Map<String, Object>> list, Map<String, List<Map<String, Object>>> bzMap) {
        if(null != bzMap || bzMap.size() != 0){
            for(Map<String, Object> map : list){
                String zdbm = map.get("zdbm") + "";
                String zdmc = map.get("zdmc") + "";
                List<Map<String, Object>> bzmx = bzMap.get(zdmc);
                List<Map<String, Object>> childrenList = new ArrayList<>();
                if(bzmx.size() > 0){
                    Map<String, List<Map<String, Object>>> m2 = bzmx.stream().collect(Collectors.groupingBy(f -> formatString(f.get("zlfs"), f.get("sblx"))));
                    for(Map.Entry<String, List<Map<String, Object>>> entry : m2.entrySet()){
                        Map<String, Object> m1 = new HashMap<>();
                        String key = entry.getKey();
                        String[] gb = key.split("\\|");
                        List<Map<String, Object>> l = entry.getValue();
                        String yjzf = "0";
                        String tczf = "0";
                        int rs = 0;
                        String fz = "0";
                        String bal = "0";
                        for(Map<String, Object> m3 : l){
                            String o1 = m3.get("yjzf") + "";
                            String o2 = m3.get("tczf") + "";
                            String o4 = m3.get("fz") + "";
                            String o5 = m3.get("bal") + "";
                            yjzf = DecimalUtil.add(yjzf, o1);
                            tczf = DecimalUtil.add(tczf, o2);
                            fz = DecimalUtil.add(fz, o4);
                            bal = DecimalUtil.add(bal, o5);
                            rs ++;
                        }
                        m1.put("zdmc", zdmc);
                        m1.put("zdbm", zdbm);
                        m1.put("yjzf", yjzf);
                        m1.put("tczf", tczf);
                        m1.put("rs", rs);
                        m1.put("cjyjzf", DecimalUtil.divide(yjzf, String.valueOf(rs), 2));
                        m1.put("cjtczf", DecimalUtil.divide(tczf, String.valueOf(rs), 2));
                        m1.put("zlfs", gb[0]);
                        m1.put("sblx", gb[1]);
                        m1.put("fz", fz);
                        m1.put("bal", bal);
                        childrenList.add(m1);
                    }
                }
                if(childrenList.size() > 0){
                    map.put("children", childrenList);
                    map.put("childKey", zdmc);
                    map.put("isChildren", true);
                }
            }
        }
    }

    /**
     * @description: 病种对比信息查询
     * @author: hsh
     * @date: 2022/9/21 11:20
     * @param: [dto]
     * @return: ResultVo<List<Map<String,Object>>>
     **/
    public ResultVo<List<Map<String, Object>>> selectBzDbTj(YbkfShareDto dto){
        String beginTime = dto.getStartTime();
        String endTime = dto.getEndTime();
        String dept = dto.getDept();
        String insurName = dto.getInsurName();
        String bzmc = dto.getBzmc();
        String doctor = dto.getDoctor();
        String zlfs = dto.getZlfs();
        String sblx = dto.getSblx();

        List<Map<String, Object>> list = dao.selectBzDbTj(beginTime, endTime, dept, insurName, bzmc, doctor, zlfs, sblx);
        return ResultVoUtil.success(list);
    }

    /**
     * @description: 病种对比病人信息查询
     * @author: hsh
     * @date: 2022/9/21 15:30
     * @param: [dto]
     * @return: ResultVo<List<Map<String,Object>>>
     **/
    public ResultVo<List<Map<String, Object>>> selectBzDbBrTj(YbkfShareDto dto){
        String beginTime = dto.getStartTime();
        String endTime = dto.getEndTime();
        String dept = dto.getDept();
        String insurName = dto.getInsurName();
        String bzmc = dto.getBzmc();
        String doctor = dto.getDoctor();
        String zlfs = dto.getZlfs();
        String sblx = dto.getSblx();

        List<Map<String, Object>> list = dao.selectBzDbBrTj(beginTime, endTime, dept, insurName, bzmc, doctor, zlfs, sblx);
        return ResultVoUtil.success(list);
    }

    private String formatString(Object... paras) {
        return MessageFormat.format("{0}|{1}", paras);
    }

}
