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.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.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.HashMap;
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;

    /**
     * @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>> monthTomAnnual = DataStatisticsUtil.initNum();
        DataStatisticsUtil.initCommonRequest(annualInspectionDataRequest);
        List<AnnualInspectionDataModel> annualInspectionDataModelList = getAnnualInspectionDataListFromRedis(
                annualInspectionDataRequest);
        if (annualInspectionDataModelList != null && annualInspectionDataModelList.size() > 0) {
            monthTomAnnual.keySet().forEach(month -> {
                Map<Integer, Long> annualStatusToNum = monthTomAnnual.get(month);
                //计划年检设置
                annualStatusToNum.put(0, (long) annualInspectionDataModelList.size());
                //完成年检设置
                List<AnnualInspectionDataModel> finishAnnualNum = annualInspectionDataModelList.stream()
                        .filter(annualInspectionDataModel -> {
                            if (annualInspectionDataModel.getStatus() != null) {
                                return annualInspectionDataModel.getStatus() == 1;
                            }
                            return false;
                        }).collect(Collectors.toList());
                if (finishAnnualNum.size() > 0) {
                    annualStatusToNum.put(1, (long) finishAnnualNum.size());
                }
                monthTomAnnual.put(month, annualStatusToNum);
            });
        }
        return RestResponse.success(ProjectUtils.transReturnMapToStatisticsKVModel(monthTomAnnual),
                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);
        Map<Long, Long> projectIdToLiftNum = projectLiftNum.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())));
            }
        });
        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);
    }

}
