package com.diagbot.aggregate;

import com.diagbot.dto.ReBeHosDTO;
import com.diagbot.facade.BehospitalInfoFacade;
import com.diagbot.facade.ConsoleFacade;
import com.diagbot.util.BeanUtil;
import com.diagbot.vo.FilterVO;
import com.diagbot.vo.ReBeHosPageVO;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2024/7/15 16:03
 */
@Component
public class LeaveHosStatisticsAggregate {
    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private ConsoleFacade consoleFacade;

    @DataProvider("setAllLeaveHos")
    public Map<String, Object> setAllLeaveHos(
            @InvokeParameter("filterVO") FilterVO filterVO,
            @DataConsumer("getTotleCount") Integer totleNum,
            @DataConsumer("getDeathCount") Integer deathNum,
            @DataConsumer("getNewBornCount") Integer newBornNum,
            @DataConsumer("getOperationCount") Integer operationNum,
            @DataConsumer("getNonAdviceCount") Integer nonAdviceNum,
            @DataConsumer("get31DaysBehospitalCount") Integer reBehospitalNum) {

        Map<String, Object> retMap = new LinkedHashMap<>();
        retMap.put("总人数", totleNum);
        retMap.put("死亡人数", deathNum);
        retMap.put("新生儿人数", newBornNum);
        retMap.put("手术病人数", operationNum);
        retMap.put("非医嘱离院病人数", nonAdviceNum);
        retMap.put("31日再入院病历数", reBehospitalNum);
        return retMap;

    }

    /**
     * 总人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("getTotleCount")
    public Integer getTotleCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        return behospitalInfoFacade.leaveHosCount(filterVO);
    }

    /**
     * 死亡人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("getDeathCount")
    public Integer getDeathCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        return behospitalInfoFacade.deathCount(filterVO);
    }

    /**
     * 新生儿患者人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("getNewBornCount")
    public Integer getNewBornCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        return behospitalInfoFacade.newBornCount(filterVO);
    }

    /**
     * 手术人数
     *
     * @param filterVO
     * @return
     */
    @DataProvider("getOperationCount")
    public Integer getOperationCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        return behospitalInfoFacade.operationCount(filterVO);
    }

    /**
     * 非医嘱离院
     *
     * @param filterVO
     * @return
     */
    @DataProvider("getNonAdviceCount")
    public Integer getNonAdviceCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        return behospitalInfoFacade.nonAdviceCount(filterVO);
    }

    /**
     * 31天重复入院
     *
     * @param filterVO
     * @return
     */
    @DataProvider("get31DaysBehospitalCount")
    public Integer get31DaysBehospitalCount(@InvokeParameter("filterVO") FilterVO filterVO) {
        ReBeHosPageVO reBeHosPageVO = new ReBeHosPageVO();
        BeanUtil.copyProperties(filterVO,reBeHosPageVO);
        reBeHosPageVO.setStartDate(timeTrans(filterVO.getStartDate()));
        reBeHosPageVO.setEndDate(timeTrans(filterVO.getEndDate()));
        reBeHosPageVO.setFlag(31);
        List<ReBeHosDTO> records = consoleFacade.reHos31DaysPage(reBeHosPageVO).getRecords();
        Set<String> codeList = new HashSet<>();
        records.forEach(obj->{
            obj.getDetails().forEach(opj->{
                codeList.add(opj.getBehospitalCode());
            });
        });
        return codeList.size();
    }

    public static Date timeTrans(String time){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = format.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();

    }
}
