package com.ningdatech.carapi.car.rpt.manage;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.car.rpt.entity.*;
import com.ningdatech.carapi.car.rpt.model.dto.*;
import com.ningdatech.carapi.car.rpt.model.po.*;
import com.ningdatech.carapi.car.rpt.model.vo.*;
import com.ningdatech.carapi.car.rpt.service.*;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionEntity;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.common.util.ExcelDownUtil;
import com.ningdatech.carapi.common.util.ExcelExportStyle;
import com.ningdatech.carapi.datascope.aop.XmlDataScope;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Classname RptManage
 * @Description
 * @Date 2022/11/8 17:03
 * @Created by PoffyZhang
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class RptDailyManage {
    private final IRptDailySpecialVehiclesLaunchRateService rptDailySpecialVehiclesLaunchRateService;
    private final IRptDailyOperatorVehiclesLaunchRateService rptDailyOperatorVehiclesLaunchRateService;
    private final IRptDailySpecialVehiclesCompanyLaunchRate60Service rptDailySpecialVehiclesCompanyLaunchRate60Service;
    private final IRptDailySpecialVehiclesOverspeedService rptDailySpecialVehiclesOverspeedService;
    private final IRptDailyOverspeedCompanyService rptDailyOverspeedCompanyService;
    private final IRptDailyOverspeedSpecialVehiclesService rptDailyOverspeedSpecialVehiclesService;

    @XmlDataScope
    @BuildChildrenRegionEntity
    public List<RptDailySpecialVehiclesLaunchRateVO> dailySpecialVehiclesLaunchRate(
            RptDailySpecialVehiclesLaunchRateParamDTO param) {
        List<RptDailySpecialVehiclesLaunchRatePO> list = rptDailySpecialVehiclesLaunchRateService.report(param);
        return CollUtils.convert(list,w -> BeanUtil.copyProperties(w,RptDailySpecialVehiclesLaunchRateVO.class));
    }

    @XmlDataScope
    public List<RptDailyOperatorVehiclesLaunchRateVO> dailyOperatorVehiclesLaunchRate(
            RptDailyOperatorVehiclesLaunchRateParamDTO param) {
        List<RptDailyOperatorVehiclesLaunchRatePO> list = rptDailyOperatorVehiclesLaunchRateService.report(param);
        return CollUtils.convert(list,w -> BeanUtil.copyProperties(w,RptDailyOperatorVehiclesLaunchRateVO.class));
    }

    public List<RptDailySpecialVehiclesCompanyLaunchRate60VO> dailySpecialVehiclesCompanyLaunchRate(
            RptDailySpecialVehiclesCompanyLaunchRateParamDTO param) {
        List<RptDailySpecialVehiclesCompanyLaunchRate60PO> list = rptDailySpecialVehiclesCompanyLaunchRate60Service.report(param);
        return CollUtils.convert(list,w -> {
            RptDailySpecialVehiclesCompanyLaunchRate60VO vo = BeanUtil.copyProperties(w, RptDailySpecialVehiclesCompanyLaunchRate60VO.class);
            CodeUtil.searchCompanyNameAndRegionName(vo);
            return vo;
        });
    }

    public List<RptDailySpecialVehiclesOverspeedVO> dailySpecialVehiclesOverspeed(
            RptDailySpecialVehiclesOverspeedParamDTO param) {
        List<RptDailySpecialVehiclesOverspeedPO> list = rptDailySpecialVehiclesOverspeedService.report(param);
        return CollUtils.convert(list,w -> BeanUtil.copyProperties(w,RptDailySpecialVehiclesOverspeedVO.class));
    }

    public List<RptDailyOverspeedCompanyVO> dailyOverspeedCompany(
            RptDailyOverspeedCompanyParamDTO param) {
        List<RptDailyOverspeedCompanyPO> list = rptDailyOverspeedCompanyService.report(param);
        return CollUtils.convert(list,w -> {
             RptDailyOverspeedCompanyVO vo = BeanUtil.copyProperties(w, RptDailyOverspeedCompanyVO.class);
             CodeUtil.searchRegionName(vo);
             return vo;
        });
    }

    public List<RptDailyOverspeedSpecialVehiclesVO> dailyOverspeedSpecialVehicles(RptDailyOverspeedSpecialVehiclesParamDTO param) {
        List<RptDailyOverspeedSpecialVehiclesPO> list = rptDailyOverspeedSpecialVehiclesService.report(param);
        return CollUtils.convert(list,w -> {
            RptDailyOverspeedSpecialVehiclesVO vo = BeanUtil.copyProperties(w, RptDailyOverspeedSpecialVehiclesVO.class);
            CodeUtil.searchRegionName(vo);
            return vo;
        });
    }

    public void specialVehiclesOnlineRateDailyReportExport(HttpServletResponse response,RptDailySpecialVehiclesLaunchRateParamDTO param) {
        List<RptDailySpecialVehiclesLaunchRateVO> collect = dailySpecialVehiclesLaunchRate(param);
        String fileName = "浙江省专用车辆上线率日报表";
        ExcelDownUtil.setFileName(fileName,response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(),RptDailySpecialVehiclesLaunchRateVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public void operatorsVehiclesOnlineRateDailyReportExport(HttpServletResponse response, RptDailyOperatorVehiclesLaunchRateParamDTO param) {
        List<RptDailyOperatorVehiclesLaunchRateVO> collect = dailyOperatorVehiclesLaunchRate(param);
        String fileName = "运营商车辆上线率日报表";
        ExcelDownUtil.setFileName(fileName,response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(),RptDailyOperatorVehiclesLaunchRateVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void specialVehiclesOnlineRateEnterpriseDailyReportExport60(HttpServletResponse response, RptDailySpecialVehiclesCompanyLaunchRateParamDTO param) {
        List<RptDailySpecialVehiclesCompanyLaunchRate60VO> collect = dailySpecialVehiclesCompanyLaunchRate(param);
        String fileName = "浙江省专用车辆上线率低于60%的企业日报表";
        ExcelDownUtil.setFileName(fileName,response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(),RptDailySpecialVehiclesCompanyLaunchRate60VO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void specialVehiclesOverSpeedDailyReportExport(HttpServletResponse response, RptDailySpecialVehiclesOverspeedParamDTO param) {
        List<RptDailySpecialVehiclesOverspeedVO> collect = dailySpecialVehiclesOverspeed(param);
        String fileName = "浙江省专用车辆超速情况日报表";
        ExcelDownUtil.setFileName(fileName,response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(),RptDailySpecialVehiclesOverspeedVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void overSpeedEnterpriseRankDailyReportExport(HttpServletResponse response, RptDailyOverspeedCompanyParamDTO param) {
        List<RptDailyOverspeedCompanyVO> collect = dailyOverspeedCompany(param);
        String fileName = "浙江省超速企业排名日报表";
        ExcelDownUtil.setFileName(fileName,response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(),RptDailyOverspeedCompanyVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void overSpeedSpecialVehiclesRankDailyReportExport(HttpServletResponse response, RptDailyOverspeedSpecialVehiclesParamDTO param) {
        List<RptDailyOverspeedSpecialVehiclesVO> collect = dailyOverspeedSpecialVehicles(param);
        String fileName = "浙江省超速专用车辆排名日报表";
        ExcelDownUtil.setFileName(fileName,response);
        //数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(),RptDailyOverspeedSpecialVehiclesVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
