package com.tofly.watermeter.custom.service.statisticanalysis.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.util.DateUtil;
import com.tofly.watermeter.config.ComConstans;
import com.tofly.watermeter.custom.mapper.statisticanalysis.ReportManagementMapper;
import com.tofly.watermeter.custom.service.statisticanalysis.IReportManagementService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class ReportManagementServiceImpl implements IReportManagementService {

    private final ReportManagementMapper mapper;

    @Override
    public List getEconomicBenefit(String startDate, String endDate) {
        if(StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
            return null;
        }
        List<Map<String,Object>> res = mapper.getEconomicBenefitCountAndAmount(startDate,endDate);
//        Map<String,Object> amount = mapper.EconomicBenefitAmount(startDate,endDate);
        return res;
    }

    @Override
    public Page<HashMap<String, Object>> getEconomicBenefitDetail(Page page, String startDate, String endDate, String rectifyType) {
        if(StringUtils.isBlank(rectifyType) || StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
            return null;
        }
        return mapper.getEconomicBenefitDetail(page,startDate,endDate,rectifyType);
    }

    @Override
    public List getQualityStatistics(String startDate, String endDate,String queryType) {
        List res = null;
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate) ) {
            return res;
        }
        YearMonth start = YearMonth.parse(startDate, DateTimeFormatter.ofPattern(DateUtil.DATE_MONTH_FORMAT));
        YearMonth end = YearMonth.parse(endDate, DateTimeFormatter.ofPattern(DateUtil.DATE_MONTH_FORMAT));
        if (end.isBefore(start)) {
            return res;
        } else {
            List<String> types = Arrays.asList(queryType.split(ComConstans.CSV));
            res = new ArrayList<>();
            HashMap<String,Object> map = null;
            if (types.size() > 0) {
                for (String type : types) {
                    List<HashMap<String,String>> parameter  =  mapper.getQualityStatisticsByCertificationProject(startDate,endDate,type);
                    map = new HashMap<>();
                    map.put("parameter",parameter);
                    map.put("certificationProject",type);
                    res.add(map);
                }
            }
            return res;
        }
    }

    @Override
    public Page getQualityStatisticsDetail(Page page,String yearMonth, String zglx) {
        if (StringUtils.isNotBlank(zglx)) {
            return mapper.getQualityStatisticsDetail(page,yearMonth,zglx);
        }
        return null;
    }

    @Override
    public List getManagementBenefit(String queryType, String startDate, String endDate, String cbpq) {
        List<HashMap<String,Object>> res = null;
        YearMonth start = YearMonth.parse(startDate, DateTimeFormatter.ofPattern(DateUtil.DATE_MONTH_FORMAT));
        YearMonth end = YearMonth.parse(endDate, DateTimeFormatter.ofPattern(DateUtil.DATE_MONTH_FORMAT));
        if (end.isBefore(start)) {
            return res;
        }

        List<String> cbpqList = Arrays.asList(cbpq.split(ComConstans.CSV));
        if (cbpqList.size() > 0) {
            res = new ArrayList<>();
            HashMap<String,Object> parameter = null;
            //  0为合格 1为不合格
            if ("bhgl".equals(queryType)) {
                for (String pq : cbpqList) {
                    List<HashMap<String,String>> monthlyData  = mapper.getUnqualifiedQuantity(startDate,endDate,pq);
                    parameter = new HashMap<>();
                    parameter.put("monthlyData",monthlyData);
                    parameter.put("cbpq",pq);
                    res.add(parameter);
                }
            // 合格率
            } else if ("hgl".equals(queryType)) {
                for (String pq : cbpqList) {
                    YearMonth queryDate = start;
                    YearMonth queryEnd = end.plusMonths(1L);
                    parameter = new HashMap<>();
                    ArrayList<HashMap<String,String>> monthlyData = new ArrayList<>();
                    while (!queryDate.equals(queryEnd)){
                        HashMap<String,String> monthlyPass  = mapper.getMonthlyPassRate(queryDate.toString(),pq);
                        if (monthlyPass != null && !monthlyPass.isEmpty()) {
                            monthlyData.add(monthlyPass);
                        }
                        queryDate = queryDate.plusMonths(1L);
                    }
                    parameter.put("monthlyData",monthlyData);
                    parameter.put("cbpq",pq);
                    res.add(parameter);
                }
            }
            return res;
        }
        return res;
    }

    @Override
    public Page getUnqualifiedQuantityDetail(Page page,String queryDate, String cbpq) {
        Page res = null;
        if (StringUtils.isBlank(queryDate) || StringUtils.isBlank(cbpq)) {
            return res;
        }
        return mapper.getUnqualifiedQuantityDetail(page,queryDate,cbpq);
    }

    @Override
    public List getPassRateDetail(String startDate, String endDate, String cbpq) {
        List res = null;
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate) || StringUtils.isBlank(cbpq)) {
            return res;
        }
        YearMonth start = YearMonth.parse(startDate, DateTimeFormatter.ofPattern(DateUtil.DATE_MONTH_FORMAT));
        YearMonth end = YearMonth.parse(endDate, DateTimeFormatter.ofPattern(DateUtil.DATE_MONTH_FORMAT));
        if (end.isBefore(start)) {
            return res;
        } else {
            res = new ArrayList();
            YearMonth queryDate = start;
            YearMonth endTime = end.plusMonths(1l);
            while (!queryDate.equals(endTime)) {
                List<HashMap<String, String>> monthData = mapper.getPassRateDetailDetail(queryDate.toString(), cbpq);
                long rzbh = monthData.stream().filter(x -> StringUtils.isNotBlank(x.get("rzbh"))).count();
                long noPass = monthData.stream().filter(x -> "0".equals(x.get("pass"))).count();
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("sjny",queryDate.toString());
                hashMap.put("cbzl",String.valueOf(monthData.size()));
                hashMap.put("rwzl",String.valueOf(rzbh));
                hashMap.put("bhgl",String.valueOf(noPass));
                res.add(hashMap);
                queryDate = queryDate.plusMonths(1L);
            }
            return res;
        }
    }

    @Override
    public HashMap<String, String> getRectifyByNumber(String rectifyNumber) {
        if (StringUtils.isNotBlank(rectifyNumber)) {
            return mapper.getRectifyByNumber(rectifyNumber);
        }
        return null;
    }
}
