package thyyxxk.webserver.service.medicalinsurance.costcontrol;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.YbTjConstant;
import thyyxxk.webserver.dao.his.medicalinsurance.costcontrol.OutpatientCoordinationDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

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-10-19  10:29
 * @Version: 1.0
 */
@Service
public class OutpatientCoordinationService {

    private final OutpatientCoordinationDao dao;

    @Autowired
    public OutpatientCoordinationService(OutpatientCoordinationDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> selectOutpatientCoordination(String month) {
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> headTitle = new ArrayList<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        // 挂号人数（不包含驾驶员体检人员）
        List<Map<String, String>> register = dao.selectRegisterPeople(month);
        // 挂号可以使用门诊统筹的
        List<Map<String, String>> oc = dao.selectRegisterOutpatientCoordinationPeople(month);
        // 挂号已经使用门诊统筹的
        List<Map<String, String>> usedOc = dao.selectUsedOutpatientCoordinationPeople(month);
        // 挂号科室已经使用门诊统筹的
        List<Map<String, String>> deptList = dao.selectUsedOutpatientCoordinationDeptPeople(month);
        Map<String, List<Map<String, String>>> deptMap = deptList.stream().collect(Collectors.groupingBy(map -> map.get("dept")));

        // 分别是医保办，信息中心，财务部统计的门诊统筹病人
        List<Map<String, String>> ybbList = dao.selectUsedOutpatientCoordinationDeptPeopleForDept(month, YbTjConstant.MEDICAL_INSURANCE_DEPT_NAME);
        List<Map<String, String>> xxkList = dao.selectUsedOutpatientCoordinationDeptPeopleForDept(month, YbTjConstant.INFO_CENTER_DEPT_NAME);
        List<Map<String, String>> cwbList = dao.selectUsedOutpatientCoordinationDeptPeopleForDept(month, YbTjConstant.REGISTER_DEPT_NAME);

        List<String> list = DateUtil.getDayListOfMonth(month);
        assert list != null;
        Map<String, Object> head = new HashMap<>(list.size() + 2);
        head.put("prop", "item");
        head.put("label", "科室");
        headTitle.add(head);
        head = new HashMap<>();
        head.put("prop", "total");
        head.put("label", "合计");
        headTitle.add(head);
        for(String yf : list){
            head = new HashMap<>();
            head.put("prop", yf);
            head.put("label", yf);
            headTitle.add(head);
        }

        Map<String, Object> regMap = new HashMap<>(list.size() + 2);
        Map<String, Object> ocMap = new HashMap<>(list.size() + 2);
        Map<String, Object> uocMap = new HashMap<>(list.size() + 2);
        int regRs = 0;
        int ocRs = 0;
        int uocRs = 0;
        regMap.put("item", "门诊挂号人数");
        for(Map<String, String> m : register){
            regMap.put(m.get("rq"), m.get("rs"));
            regRs = regRs + Integer.parseInt(m.get("rs"));
        }
        regMap.put("total", regRs);
        resultList.add(regMap);
        ocMap.put("item", "可以使用门诊统筹人数");
        for(Map<String, String> m : oc){
            ocMap.put(m.get("rq"), m.get("rs"));
            ocRs = ocRs + Integer.parseInt(m.get("rs"));
        }
        ocMap.put("total", ocRs);
        resultList.add(ocMap);
        uocMap.put("item", "已经使用门诊统筹人数");
        for(Map<String, String> m : usedOc){
            uocMap.put(m.get("rq"), m.get("rs"));
            uocRs = uocRs + Integer.parseInt(m.get("rs"));
        }
        uocMap.put("total", uocRs);
        resultList.add(uocMap);

        Map<String, List<Map<String, String>>> yfMap = deptList.stream().collect(Collectors.groupingBy(map -> map.get("rq")));
        Map<String, Object> ksMap = new HashMap<>(list.size() + 2);
        ksMap.put("item", "门诊科室");
        int ksRs = 0;
        for(Map.Entry<String, List<Map<String, String>>> entry : yfMap.entrySet()){
            String rq = entry.getKey();
            int rs = 0;
            for(Map<String, String> m : entry.getValue()){
                if(!(YbTjConstant.REGISTER_DEPT_NAME.equals((m.get("dept").trim()))
                        || YbTjConstant.INFO_CENTER_DEPT_NAME.equals((m.get("dept").trim()))
                        || YbTjConstant.MEDICAL_INSURANCE_DEPT_NAME.equals((m.get("dept").trim())))){
                    rs = rs + Integer.parseInt(m.get("rs"));
                }
            }
            ksMap.put(rq, rs);
            ksRs = ksRs + rs;
        }
        ksMap.put("total", ksRs);


        List<Map<String, Object>> ksList = new ArrayList<>();
        for(Map.Entry<String, List<Map<String, String>>> entry : deptMap.entrySet()){
            Map<String, Object> deptR = new HashMap<>(list.size() + 2);
            String dept = entry.getKey().trim();
            deptR.put("item", dept);
            int rs = 0;
            for(Map<String, String> m : entry.getValue()){
                deptR.put(m.get("rq"), m.get("rs"));
                rs = rs + Integer.parseInt(m.get("rs"));
            }
            deptR.put("total", rs);
            if(YbTjConstant.REGISTER_DEPT_NAME.equals(dept)
                    || YbTjConstant.INFO_CENTER_DEPT_NAME.equals(dept)
                    || YbTjConstant.MEDICAL_INSURANCE_DEPT_NAME.equals(dept)){
                resultList.add(deptR);
            } else {
                ksList.add(deptR);
            }
        }

        if(ksList.size() > 0){
            ksMap.put("children", ksList);
            ksMap.put("childKey", "门诊科室");
            ksMap.put("isChildren", true);
        }
        resultList.add(ksMap);

        // 组装科室数据
        for(Map<String, Object> map : resultList){
            String dept = String.valueOf(map.get("item"));
            if(YbTjConstant.MEDICAL_INSURANCE_DEPT_NAME.equals(dept)){
                if(ybbList.size() > 0){
                    map.put("children", ybbList);
                    map.put("childKey", "医保办");
                    map.put("isDept", true);
                }
            } else if(YbTjConstant.INFO_CENTER_DEPT_NAME.equals(dept)){
                if(xxkList.size() > 0){
                    map.put("children", xxkList);
                    map.put("childKey", "信息中心");
                    map.put("isDept", true);
                }
            } else if(YbTjConstant.REGISTER_DEPT_NAME.equals(dept)){
                if(cwbList.size() > 0){
                    map.put("children", cwbList);
                    map.put("childKey", "财务部");
                    map.put("isDept", true);
                }
            }

        }

        resultMap.put("headTitle", headTitle);
        resultMap.put("resultData", resultList);
        resultMap.put("deptData", ksList);
        return ResultVoUtil.success(resultMap);
    }

}
