package com.diagbot.aggregate;

import com.diagbot.dto.DeptBaseDTO;
import com.diagbot.dto.NumDTO;
import com.diagbot.facade.BasDeptInfoFacade;
import com.diagbot.facade.BehospitalInfoFacade;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.FilterVO;
import io.github.lvyahui8.spring.annotation.DataConsumer;
import io.github.lvyahui8.spring.annotation.DataProvider;
import io.github.lvyahui8.spring.annotation.InvokeParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2020/7/15 16:03
 */
@Component
public class LeaveHosCountByDeptAggregate {
    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private BasDeptInfoFacade basDeptInfoFacade;

    @DataProvider("setAllLeaveHosCount")
    public Map<String, Object> setAllLeaveHosCount(
            @InvokeParameter("filterVO") FilterVO filterVO,
            @DataConsumer("leaveHosCountByDept") List<NumDTO> totleNumList,
            @DataConsumer("deathCountByDept") List<NumDTO> deathNumList,
            @DataConsumer("operationCountByDept") List<NumDTO> operationNumList) {
        Map<String, Object> retMap = new LinkedHashMap<>();
        //关联科室
        Map<String, Object> deptMap = getDeptByUser(filterVO);
        if (deptMap == null) {
            return retMap;
        }
        Map<String, NumDTO> totleMap = ListUtil.isEmpty(totleNumList)
                ? new HashMap<>()
                : EntityUtil.makeEntityMap(totleNumList, "name");

        Map<String, NumDTO> deathMap = new HashMap<>();
        Map<String, NumDTO> operationMap = new HashMap<>();
        deathMap = ListUtil.isEmpty(deathNumList)
                ? new HashMap<>()
                : EntityUtil.makeEntityMap(deathNumList, "name");
        operationMap = ListUtil.isEmpty(operationNumList)
                ? new HashMap<>()
                : EntityUtil.makeEntityMap(operationNumList, "name");


        for (String deptName : deptMap.keySet()) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("总人数", 0);
            map.put("死亡人数", 0);
            map.put("手术病人数", 0);
            if (totleMap.containsKey(deptName)) {
                map.put("总人数", totleMap.get(deptName).getNum());
            }
            if (deathMap.containsKey(deptName)) {
                map.put("死亡人数", deathMap.get(deptName).getNum());
            }
            if (operationMap.containsKey(deptName)) {
                map.put("手术病人数", operationMap.get(deptName).getNum());
            }
            retMap.put(deptName, map);
        }
        return retMap;
    }

    /**
     * 出院总人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("leaveHosCountByDept")
    public List<NumDTO> leaveHosCountByDept(@InvokeParameter("filterVO") FilterVO filterVO) {
        List<NumDTO> numDTOS = behospitalInfoFacade.leaveHosCountByDept(filterVO);
        return numDTOS;
    }

    /**
     * 死亡人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("deathCountByDept")
    public List<NumDTO> deathCountByDept(@InvokeParameter("filterVO") FilterVO filterVO) {
        List<NumDTO> numDTOS = behospitalInfoFacade.deathCountByDept(filterVO);
        return numDTOS;

    }

    /**
     * 手术人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("operationCountByDept")
    public List<NumDTO> operationCountByDept(@InvokeParameter("filterVO") FilterVO filterVO) {
        List<NumDTO> numDTOS = behospitalInfoFacade.operationCountByDept(filterVO);
        return numDTOS;
    }

    /**
     * 用户关联科室
     *
     * @param filterVO
     * @return
     */
    public Map<String, Object> getDeptByUser(FilterVO filterVO) {
        List<DeptBaseDTO> deptList = basDeptInfoFacade.getDeptByUser(filterVO);
        if (ListUtil.isNotEmpty(deptList)) {
            return EntityUtil.makeMapWithKeyValue(deptList, "deptName", "deptId");
        } else {
            return null;
        }
    }
}
