package cn.com.ty.lift.batch.applet.service;

import cn.com.ty.lift.batch.applet.dao.mapper.AnnualInspectionDataMapper;
import cn.com.ty.lift.batch.applet.dao.model.AnnualInspectionDataModel;
import cn.com.ty.lift.batch.applet.dao.model.request.AnnualInspectionDataRequest;
import cn.com.ty.lift.batch.applet.util.DataStatisticsUtil;
import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.common.utils.ProjectUtils;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.hutool.core.date.DatePattern;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

/**
 * @author huangyuan
 * @date 2020/2/3
 * @description
 */
@Service
public class AnnualInspectionDataService {

    @Resource
    private AnnualInspectionDataMapper annualInspectionDataMapper;

    /**
     * @param annualInspectionDataRequest
     * @return
     * @description 从redis中获取年检信息
     * @date 2020/2/4 1:18 下午
     */
    public List<AnnualInspectionDataModel> getAnnualInspectionDataListFromRedis(
            AnnualInspectionDataRequest annualInspectionDataRequest) {
        //从redis中获取当前公司的年检信息
        return annualInspectionDataMapper.getAnnualInspectionDataList(annualInspectionDataRequest);
    }

    /**
     * @param
     * @return
     * @description 年检总览情况
     * @date 2020/2/5 4:05 下午
     */
    public RestResponse annualOverView(AnnualInspectionDataRequest annualInspectionDataRequest) {
        Map<Integer, Map<Integer, Long>> monthToAnnual = DataStatisticsUtil.initAnnualNum();
        //设置时间 年初-年末
        annualInspectionDataRequest.setStartTimeDateStr(DataStatisticsUtil.getYearFirstTime()
                .format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
        annualInspectionDataRequest.setEndTimeDateStr(DataStatisticsUtil.getYearLastTime()
                .format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN)));
        List<AnnualInspectionDataModel> annualInspectionDataModelList = getAnnualInspectionDataListFromRedis(
                annualInspectionDataRequest);
        if (annualInspectionDataModelList != null && annualInspectionDataModelList.size() > 0) {
            //计划年检总数
            AtomicLong planAnnualTotalNum = new AtomicLong(0L);
            //完成年检总数
            AtomicLong finishAnnualTotalNum = new AtomicLong(0L);
            monthToAnnual.keySet().forEach(month -> {
                Map<Integer, Long> annualStatusToNum = monthToAnnual.get(month);
                long monthAnnual = annualInspectionDataModelList.stream()
                        .filter(annualInspectionDataModel -> {
                            if (annualInspectionDataModel.getPlanDate() != null) {
                                return DataStatisticsUtil.getMonth(annualInspectionDataModel.getPlanDate()) == month;
                            }
                            return false;
                        }).count();
                //计划年检设置
                planAnnualTotalNum.addAndGet(monthAnnual);
                annualStatusToNum.put(0, planAnnualTotalNum.longValue());
                //完成年检设置
                List<AnnualInspectionDataModel> finishAnnual = annualInspectionDataModelList.stream()
                        .filter(annualInspectionDataModel -> {
                            if (annualInspectionDataModel.getStatus() != null) {
                                return annualInspectionDataModel.getStatus() == 1;
                            }
                            return false;
                        }).collect(Collectors.toList());
                long finishAnnualNum = 0L;
                //获取本月完成年检时间
                if (finishAnnual.size() > 0) {
                    finishAnnualNum = finishAnnual.stream().filter(annualInspectionDataModel -> {
                        if (annualInspectionDataModel.getFinishTime() != null) {
                            return DataStatisticsUtil.getMonth(annualInspectionDataModel.getFinishTime()) == month;
                        }
                        return false;
                    }).count();
                    annualStatusToNum.put(1, finishAnnualNum);
                }
                //完成年检累计
                finishAnnualTotalNum.addAndGet(finishAnnualNum);
                annualStatusToNum.put(2, finishAnnualTotalNum.longValue());
                monthToAnnual.put(month, annualStatusToNum);
            });
        }
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(monthToAnnual),
                ApiConstants.RESULT_SUCCESS, "获取统计数据成功");
    }

    /**
     * @param
     * @return
     * @description 年检进度
     * @date 2020/2/5 4:11 下午
     */
    public RestResponse annualProgress(AnnualInspectionDataRequest annualInspectionDataRequest) {
        DataStatisticsUtil.initCommonRequest(annualInspectionDataRequest);
        if (StringUtils.isNotBlank(annualInspectionDataRequest.getProjectName())) {
            annualInspectionDataRequest.setProjectName("%" + annualInspectionDataRequest.getProjectName() + "%");
        }
        long count = annualInspectionDataMapper.getProjectAnnualCount(annualInspectionDataRequest);
        List<AnnualInspectionDataModel> projectAnnualList = annualInspectionDataMapper.getProjectAnnual(annualInspectionDataRequest);
        //总台量
        List<AnnualInspectionDataModel> projectLiftNum = annualInspectionDataMapper.projectLiftNum(annualInspectionDataRequest);
        //开启年检的台量
        List<AnnualInspectionDataModel> projectAnnualLiftNum = annualInspectionDataMapper.projectAnnualLiftNum(annualInspectionDataRequest);
        Map<Long, Long> projectIdToLiftNum = projectLiftNum.stream().collect(Collectors.groupingBy(
                AnnualInspectionDataModel::getProjectId, Collectors.counting()));
        Map<Long, Long> projectIdToAnnalLiftNum = projectAnnualLiftNum.stream().collect(Collectors.groupingBy(
                AnnualInspectionDataModel::getProjectId, Collectors.counting()));
        //封装台量
        projectAnnualList.forEach(annualInspectionDataModel -> {
            //总台量
            if (projectIdToLiftNum != null && projectIdToLiftNum.size() > 0) {
                annualInspectionDataModel.setUnitNum(DataStatisticsUtil.getLongValue(
                        projectIdToLiftNum.get(annualInspectionDataModel.getProjectId())));
                annualInspectionDataModel.setAnnualUnitNum(DataStatisticsUtil.getLongValue(
                        projectIdToAnnalLiftNum.get(annualInspectionDataModel.getProjectId())));
            }
        });
        Map<String, Object> keyToAnnual = new HashMap<>();
        keyToAnnual.put("count", DataStatisticsUtil.getLongValue(count));
        keyToAnnual.put("list", projectAnnualList);
        return RestResponse.success(keyToAnnual, ApiConstants.RESULT_SUCCESS, "获取统计数据成功");
    }

    /**
     * @param annualInspectionDataRequest 年检数据
     * @return
     * @description
     * @date 2020/5/23 10:02 上午
     */
    public RestResponse liftAnnualInfo(AnnualInspectionDataRequest annualInspectionDataRequest) {
        DataStatisticsUtil.initCommonRequest(annualInspectionDataRequest);
        List<AnnualInspectionDataModel> liftAnnualList = annualInspectionDataMapper
                .getProjectLiftAnnualInfo(annualInspectionDataRequest);
        return RestResponse.success(liftAnnualList);
    }

}
