package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.MzyReqrec;
import cn.hnthyy.thmz.enums.PayMarkEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.pageDto.MzyReqrecPageDto;
import cn.hnthyy.thmz.service.his.mz.MzDepositFileService;
import cn.hnthyy.thmz.service.his.mz.MzyReqrecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 首页控制层
 */
@Slf4j
@RestController
public class IndexController {
    @Autowired
    private MzyReqrecService mzyReqrecService;
    @Autowired
    private MzDepositFileService mzDepositFileService;

    /**
     * 查询首页统计数据
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getIndexCount", method = {RequestMethod.GET})
    public Map<String, Object> getIndexCount() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            MzyReqrecPageDto mzyReqrecPageDto = new MzyReqrecPageDto();
            Date now = new Date();
            mzyReqrecPageDto.setBeginTime(DateUtil.getFirstSecond(now));
            mzyReqrecPageDto.setEndTime(DateUtil.getLastSecond(now));
            MzyReqrec mzyReqrec = new MzyReqrec();
            mzyReqrecPageDto.setMzyReqrec(mzyReqrec);
            mzyReqrec.setVisitedMark(YesNoEnum.NO.code);
            mzyReqrec.setCancelMark(YesNoEnum.NO.code);
            Integer registrationNum = 0;
            Double amountD = 0D;
            List<Map<String, Object>> list = mzyReqrecService.countAndAmountMzyReqrec(mzyReqrecPageDto);
            if (list != null && list.size()>0) {
                Map<String, Object> map = list.get(0);
                registrationNum = (Integer) map.get("count");
                amountD = (Double) map.get("amount");
            }
            BigDecimal amount = BigDecimal.ZERO;
            if (amountD != null) {
                amount = BigDecimal.valueOf(amountD);
            }
            mzyReqrec.setVisitedMark(YesNoEnum.YES.code);
            list = mzyReqrecService.countAndAmountMzyReqrec(mzyReqrecPageDto);
            Integer haveClinicalNum = 0;
            if (list != null && list.size()>0) {
                Map<String, Object> map = list.get(0);
                haveClinicalNum = (Integer) map.get("count");
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询首页统计数据成功");
            resultMap.put("registrationNum", registrationNum == null ? 0 : registrationNum);
            resultMap.put("haveClinicalNum", haveClinicalNum);
            //总的有效挂号金额
            resultMap.put("ghAmount", amount);
            //总收费金额
            resultMap.put("sfAmount", mzDepositFileService.queryAmount(mzyReqrecPageDto.getBeginTime(), mzyReqrecPageDto.getEndTime()));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询首页统计数据失败，系统出错，请联系管理员");
            log.error("查询首页统计数据失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }



    /**
     * 门诊运营趋势
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getMzyytsqs", method = {RequestMethod.POST})
    public Map<String, Object> getMzyytsqs(@RequestBody MzyReqrecPageDto mzyReqrecPageDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzyReqrecPageDto==null){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊运营趋势失败，参数为空");
                return resultMap;
            }
            if (mzyReqrecPageDto.getBeginTime()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊运营趋势失败，开始时间参数为空");
                return resultMap;
            }
            if (mzyReqrecPageDto.getEndTime()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "门诊运营趋势失败，结束时间参数为空");
                return resultMap;
            }
            mzyReqrecPageDto.setMzyReqrec(new MzyReqrec());
            List<Map<String, Object>> list = mzyReqrecService.countAndAmountMzyReqrec(mzyReqrecPageDto);
            Map<String,Map<String,Object>> fullMap = new HashMap<>();
            if (list != null && list.size()>0) {
                for (Map<String, Object> map:list){
                    String requestDay = (String) map.get("request_day");
                    map.put("ghAmount",map.get("amount"));
                    map.put("thAmount",map.get("thAmount"));
                    fullMap.put(requestDay,map);
                }
            }
            list= mzDepositFileService.countAndAmount(mzyReqrecPageDto.getBeginTime(),mzyReqrecPageDto.getEndTime(),PayMarkEnum.CHARGED.code);
            if (list != null && list.size()>0) {
                for (Map<String, Object> map:list){
                    String chargeDate = (String) map.get("charge_date");
                    Map<String, Object> temp= fullMap.get(chargeDate);
                    if(temp==null){
                        fullMap.put(chargeDate,map);
                        temp=map;
                    }
                    temp.put("sfAmount",map.get("amount"));
                }
            }
            list= mzDepositFileService.countAndAmount(mzyReqrecPageDto.getBeginTime(),mzyReqrecPageDto.getEndTime(),PayMarkEnum.RETURN_PREMIUM.code);
            if (list != null && list.size()>0) {
                for (Map<String, Object> map:list){
                    String chargeDate = (String) map.get("charge_date");
                    Map<String, Object> temp= fullMap.get(chargeDate);
                    if(temp==null){
                        fullMap.put(chargeDate,map);
                    }
                    temp.put("tfAmount",map.get("amount"));
                }
            }
            //获取时间区间内的日期集合
            mzyReqrecPageDto.setEndTime(DateUtil.getFirstSecond(mzyReqrecPageDto.getEndTime()));
            List<Date> days = DateUtil.getBetweenDates(mzyReqrecPageDto.getBeginTime(), mzyReqrecPageDto.getEndTime());
//            //如果新号表日期集合不包含当前日期，加入当前日期
//            if (!days.contains(mzyReqrecPageDto.getBeginTime())) {
//                List<Date> tempDays = new ArrayList<>(days.size()+1);
//                tempDays.add(mzyReqrecPageDto.getBeginTime());
//                tempDays.addAll(days);
//                days=tempDays;
//            }
            List<String> keyList =days.stream().map(d-> DateUtil.paseToString(d,"yyyy-MM-dd")).collect(Collectors.toList());
            List<Map<String, Object>> resultList = new ArrayList<>();
            keyList.stream().forEach(k->{
                Map<String, Object> temp= fullMap.get(k);
                if(temp==null){
                    temp=new HashMap<>();
                    temp.put("ghAmount",0);
                    temp.put("thAmount",0);
                    temp.put("sfAmount",0);
                    temp.put("tfAmount",0);
                }else {
                    if(temp.get("ghAmount")==null){
                        temp.put("ghAmount",0);
                    }
                    if(temp.get("thAmount")==null){
                        temp.put("thAmount",0);
                    }
                    if(temp.get("sfAmount")==null){
                        temp.put("sfAmount",0);
                    }
                    if(temp.get("tfAmount")==null){
                        temp.put("tfAmount",0);
                    }
                }
                resultList.add(temp);
            });
            resultMap.put("code", 0);
            resultMap.put("message", "门诊运营趋势成功");
            resultMap.put("keyList", keyList);
            resultMap.put("data", resultList);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "门诊运营趋势失败，系统出错，请联系管理员");
            log.error("门诊运营趋势失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }
}


