package cn.com.ty.lift.system.homepage.service;

import cn.com.ty.lift.common.constants.ApiConstants;
import cn.com.ty.lift.system.constants.CommonConstants;
import cn.com.ty.lift.system.homepage.dao.dto.request.DataStatisticsRequest;
import cn.com.ty.lift.system.homepage.dao.dto.response.EnterpriseDataStatisticsResponse;
import cn.com.ty.lift.system.homepage.dao.mapper.CommonDataMapper;
import cn.com.ty.lift.system.homepage.dao.model.CountDataModel;
import cn.com.xwy.boot.web.dto.RestResponse;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author huangyuan
 * @date 2020/4/24
 * @description
 */
@Service
public class EnterpriseDataStatisticsService {

    @Resource
    private CommonDataMapper commonDataMapper;

    /**
     * 时间间隔
     */
    private static final int YEAR_INTERVAL = 3;

    /**
     * @param dataStatisticsRequest 请求数据 公司id
     * @return 年份对应业务数据
     * @description 获取年度业务统计数据
     * @date 2020/4/24 10:27 上午
     */
    public RestResponse yearBusinessData(DataStatisticsRequest dataStatisticsRequest) {
        initDataStatisticsRequest(dataStatisticsRequest);
        List<EnterpriseDataStatisticsResponse> enterpriseDataStatisticsResponseList = new LinkedList<>();
        //年份-项目数量
        Map<Integer, Long> projectYearToNum = yearToNum(commonDataMapper
                .getEveryYearProjectCount(dataStatisticsRequest));
        //年份-维保次数
        Map<Integer, Long> maintenanceYearToNum = yearToNum(commonDataMapper
                .getEveryYearMaintenanceCount(dataStatisticsRequest));
        //年份-急修次数
        Map<Integer, Long> emergencyYearToNum = yearToNum(commonDataMapper
                .getEveryYearEmergencyCount(dataStatisticsRequest));
        //年份-年检次数
        Map<Integer, Long> annualYearToNum = yearToNum(commonDataMapper
                .getEveryYearProjectCount(dataStatisticsRequest));
        //年份-大修次数
        Map<Integer, Long> capitalYearToNum = yearToNum(commonDataMapper
                .getEveryYearCapitalCount(dataStatisticsRequest));
        int before = Integer.parseInt(dataStatisticsRequest.getBeforeDateStr().substring(0, 4));
        int nowYear = Integer.parseInt(dataStatisticsRequest.getNowDateStr().substring(0, 4));
        while (before <= nowYear) {
            EnterpriseDataStatisticsResponse enterpriseDataStatisticsResponse = new EnterpriseDataStatisticsResponse();
            enterpriseDataStatisticsResponse.setYear(before);
            enterpriseDataStatisticsResponse.setProjectNum(getLongValue(projectYearToNum.get(before)));
            enterpriseDataStatisticsResponse.setMaintenanceNum(getLongValue(maintenanceYearToNum.get(before)));
            enterpriseDataStatisticsResponse.setEmergencyNum(getLongValue(emergencyYearToNum.get(before)));
            enterpriseDataStatisticsResponse.setAnnualNum(getLongValue(annualYearToNum.get(before)));
            enterpriseDataStatisticsResponse.setCapitalNum(getLongValue(capitalYearToNum.get(before)));
            enterpriseDataStatisticsResponseList.add(enterpriseDataStatisticsResponse);
            before++;
        }
        return RestResponse.success(enterpriseDataStatisticsResponseList, ApiConstants.RESULT_SUCCESS,
                "获取统计信息成功");
    }

    /**
     * @param dataStatisticsRequest 初始化时间数据
     * @description 设置请求的时间数据
     * @date 2020/4/24 11:03 上午
     */
    private void initDataStatisticsRequest(DataStatisticsRequest dataStatisticsRequest) {
        LocalDate now = LocalDate.now();
        LocalDate beforeYear = now.plusYears(-YEAR_INTERVAL);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(CommonConstants.PLAN_DATE_FORMAT);
        dataStatisticsRequest.setNowDateStr(now.format(dateTimeFormatter));
        dataStatisticsRequest.setBeforeDateStr(beforeYear.format(dateTimeFormatter));
    }

    /**
     * @param countDataModelList 年份-数量
     * @return 年份-数量map
     * @description 获取年份-数量map
     * @date 2020/4/24 4:55 下午
     */
    private Map<Integer, Long> yearToNum(List<CountDataModel> countDataModelList) {
        Map<Integer, Long> yearToNum = new HashMap<>();
        if (countDataModelList != null && countDataModelList.size() > 0) {
            countDataModelList.forEach(countDataModel -> {
                yearToNum.put(countDataModel.getYear(), countDataModel.getDataCount());
            });
        }
        return yearToNum;
    }

    /**
     * @param value 值
     * @return long 值
     * @description 去除空值的情况
     * @date 2020/4/24 5:18 下午
     */
    public long getLongValue(Long value) {
        if (value != null) {
            return value;
        }
        return 0L;
    }

}
