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

import cn.com.ty.lift.batch.applet.constants.DataStatisticsConstants;
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.dao.model.request.CommonRequest;
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 org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @Resource
    private AnnualInspectionDataMapper annualInspectionDataMapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * @param commonRequest 起始时间 终止时间
     * @return
     * @description 将年检数据放入redis中
     * @date 2020/2/4 11:52 上午
     */
    public void putAnnualInspectionDataToRedis(CommonRequest commonRequest) {
        List<AnnualInspectionDataModel> annualInspectionDataModelList =
                annualInspectionDataMapper.getAnnualInspectionDataList(commonRequest);
        //将树通过公司id分组后方法redis中
        DataStatisticsUtil.putStatisticsDataToRedis(annualInspectionDataModelList, redisTemplate,
                DataStatisticsConstants.ANNUAL_INSPECTION_DATA_FIELD);
    }

    /**
     * @param annualInspectionDataRequest
     * @return
     * @description 从redis中获取年检信息
     * @date 2020/2/4 1:18 下午
     */
    public List<AnnualInspectionDataModel> getAnnualInspectionDataListFromRedis(
            AnnualInspectionDataRequest annualInspectionDataRequest) {
        //从redis中获取当前公司的年检信息
        List<AnnualInspectionDataModel> annualInspectionDataModelList = (List<AnnualInspectionDataModel>)
                DataStatisticsUtil.getStatisticsDataFromRedis(redisTemplate,
                        DataStatisticsConstants.ANNUAL_INSPECTION_DATA_FIELD, annualInspectionDataRequest.getMtCompanyId());
        if (annualInspectionDataModelList != null && annualInspectionDataModelList.size() > 0) {
            if (annualInspectionDataRequest.getStartTimeDate() != null) {
                //通过起始时间过滤数据
                annualInspectionDataModelList = annualInspectionDataModelList.stream()
                        .filter(a -> {
                            if (a.getPlanDate() != null) {
                                return a.getPlanDate().isAfter(annualInspectionDataRequest.getStartTimeDate().toLocalDate());
                            }
                            return false;
                        })
                        .collect(Collectors.toList());
            }
            if (annualInspectionDataRequest.getEndTimeDate() != null) {
                //通过终止时间过滤数据
                annualInspectionDataModelList = annualInspectionDataModelList.stream()
                        .filter(a -> {
                            if (a.getPlanDate() != null) {
                                a.getPlanDate().isBefore(annualInspectionDataRequest.getEndTimeDate().toLocalDate());
                            }
                            return false;
                        })
                        .collect(Collectors.toList());
            }
        }
        return annualInspectionDataModelList;
    }

    /**
     * @param
     * @return
     * @description 年检总览情况
     * @date 2020/2/5 4:05 下午
     */
    public RestResponse annualOverView(AnnualInspectionDataRequest annualInspectionDataRequest) {
        List<AnnualInspectionDataModel> annualInspectionDataModelList = getAnnualInspectionDataListFromRedis(
                annualInspectionDataRequest);
        Map<Integer, Long> annualStatusToNum = DataStatisticsConstants.ANNUAL_STATUS_TO_NUM;
        if (annualInspectionDataModelList != null && annualInspectionDataModelList.size() > 0) {
            //设置计划年检条数
            annualStatusToNum.put(1, annualInspectionDataModelList.stream().count());
            //设置完成年检条数
            long finishAnnualCount = annualInspectionDataModelList.stream()
                    .filter(a -> a.getStatus() == 1).count();
            annualStatusToNum.put(2, finishAnnualCount);
            //设置超期未检条数
            long beyondAnnualCount = annualInspectionDataModelList.stream()
                    .filter(a -> a.getStatus() == 2).count();
            annualStatusToNum.put(3, beyondAnnualCount);
        }
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(annualStatusToNum),
                ApiConstants.RESULT_SUCCESS, "获取统计数据成功");
    }

    /**
     * @param
     * @return
     * @description 年检进度
     * @date 2020/2/5 4:11 下午
     */
    public RestResponse annualProgress(AnnualInspectionDataRequest annualInspectionDataRequest) {
        List<AnnualInspectionDataModel> annualInspectionDataModelList = getProjectAnnualProgress(
                annualInspectionDataRequest, "top5");
        return RestResponse.success(annualInspectionDataModelList, ApiConstants.RESULT_SUCCESS, "获取统计数据成功");
    }

    /**
     * @param
     * @return
     * @description 获取所有项目的年检进度
     * @date 2020/2/5 4:14 下午
     */
    public RestResponse allProjectAnnualProgress(AnnualInspectionDataRequest annualInspectionDataRequest) {
        List<AnnualInspectionDataModel> annualInspectionDataModelList = getProjectAnnualProgress(
                annualInspectionDataRequest, "all");
        return RestResponse.success(annualInspectionDataModelList, ApiConstants.RESULT_SUCCESS, "获取统计数据成功");
    }

    /**
     * @param
     * @return
     * @description 获取项目的年检进度
     * @date 2020/2/5 4:16 下午
     */
    private List<AnnualInspectionDataModel> getProjectAnnualProgress(
            AnnualInspectionDataRequest annualInspectionDataRequest, String countFlag) {
        //从redis中获取当前公司年检数据
        List<AnnualInspectionDataModel> annualInspectionDataModelList = getAnnualInspectionDataListFromRedis(
                annualInspectionDataRequest);
        if (annualInspectionDataModelList != null && annualInspectionDataModelList.size() > 0) {
            //获取全部项目的年检进度
            if ("all".equals(countFlag)) {
                if (StringUtils.isNotBlank(annualInspectionDataRequest.getProjectName())) {
                    //通过项目名称模糊查询
                    annualInspectionDataModelList = annualInspectionDataModelList.stream()
                            //通过名称模糊匹配
                            .filter(a -> DataStatisticsUtil.strMatch(
                                    annualInspectionDataRequest.getProjectName(), a.getProjectName()))
                            .collect(Collectors.toList());
                }
                //获取所有项目的年检进度，按照计划年检时间顺序排列
                annualInspectionDataModelList = annualInspectionDataModelList.stream()
                        .sorted(Comparator.comparing(AnnualInspectionDataModel::getPlanDate))
                        .collect(Collectors.toList());
            } else {
                //获取计划年检时间前五的项目的年检进度，按照计划年检时间顺序排列
                annualInspectionDataModelList = annualInspectionDataModelList.stream()
                        .sorted(Comparator.comparing(AnnualInspectionDataModel::getPlanDate)).limit(5)
                        .collect(Collectors.toList());
            }
        }
        return annualInspectionDataModelList;
    }
}
