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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.StopWatch;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.carapi.car.rpt.entity.RptTendaysSpecialOfflineWeek;
import com.ningdatech.carapi.car.rpt.entity.RptTendaysSpecialOverspeedCompany;
import com.ningdatech.carapi.car.rpt.entity.RptTendaysSpecialOverspeedVehicles;
import com.ningdatech.carapi.car.rpt.model.dto.RptTendaysOfflineWeekSpecialParamDTO;
import com.ningdatech.carapi.car.rpt.model.dto.RptTendaysOverspeedSpecialCompanyParamDTO;
import com.ningdatech.carapi.car.rpt.model.dto.RptTendaysOverspeedSpecialVehiclesParamDTO;
import com.ningdatech.carapi.car.rpt.model.po.RptTendaysSpecialOfflineWeekPO;
import com.ningdatech.carapi.car.rpt.model.po.RptTendaysSpecialOverspeedCompanyPO;
import com.ningdatech.carapi.car.rpt.model.po.RptTendaysSpecialOverspeedVehiclesPO;
import com.ningdatech.carapi.car.rpt.model.vo.RptTendaysSpecialOfflineWeekVO;
import com.ningdatech.carapi.car.rpt.model.vo.RptTendaysSpecialOverspeedCompanyVO;
import com.ningdatech.carapi.car.rpt.model.vo.RptTendaysSpecialOverspeedVehiclesVO;
import com.ningdatech.carapi.car.rpt.service.IRptTendaysSpecialOfflineWeekService;
import com.ningdatech.carapi.car.rpt.service.IRptTendaysSpecialOverspeedCompanyService;
import com.ningdatech.carapi.car.rpt.service.IRptTendaysSpecialOverspeedVehiclesService;
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.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.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Collections;
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 RptTendaysManage {
    private final IRptTendaysSpecialOverspeedCompanyService rptTendaysSpecialOverspeedCompanyService;
    private final IRptTendaysSpecialOverspeedVehiclesService rptTendaysSpecialOverspeedVehiclesService;
    private final IRptTendaysSpecialOfflineWeekService rptTendaysSpecialOfflineWeekService;


    public List<RptTendaysSpecialOverspeedCompanyVO> tendaysSpecialOverspeedCompany(RptTendaysOverspeedSpecialCompanyParamDTO param) {
        //包装本旬和上旬时间
        CodeUtil.buildCurrentTendaysStartEnd(param);
        CodeUtil.buildLastTendaysStartEnd(param);
        List<RptTendaysSpecialOverspeedCompanyPO> list = rptTendaysSpecialOverspeedCompanyService.report(param);
        return CollUtils.convert(list,w -> {
             RptTendaysSpecialOverspeedCompanyVO vo = BeanUtil.copyProperties(w,
                     RptTendaysSpecialOverspeedCompanyVO.class);
             CodeUtil.searchCompanyNameAndRegionName(vo);
             return vo;
        });
    }

    public List<RptTendaysSpecialOverspeedVehiclesVO> tendaysSpecialOverspeedVehicles(RptTendaysOverspeedSpecialVehiclesParamDTO param) {
        //包装本旬和上旬时间
        CodeUtil.buildCurrentTendaysStartEnd(param);
        CodeUtil.buildLastTendaysStartEnd(param);
        List<RptTendaysSpecialOverspeedVehiclesPO> list = rptTendaysSpecialOverspeedVehiclesService.report(param);
        return CollUtils.convert(list,w -> {
            RptTendaysSpecialOverspeedVehiclesVO vo = BeanUtil.copyProperties(w,
                    RptTendaysSpecialOverspeedVehiclesVO.class);
            CodeUtil.searchCompanyNameAndRegionName(vo);
            return vo;
        });
    }

    public List<RptTendaysSpecialOfflineWeekVO> tendaysSpecialOfflineWeek(RptTendaysOfflineWeekSpecialParamDTO param) {
        //包装本旬和上旬时间
        CodeUtil.buildCurrentTendaysStartEnd(param);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        Page<RptTendaysSpecialOfflineWeekPO> page = param.page();
        Page<RptTendaysSpecialOfflineWeekPO> pageList = rptTendaysSpecialOfflineWeekService.reportPage(page,param);
        if(0L == pageList.getTotal()){
            return Collections.emptyList();
        }
        stopWatch.stop();
        log.info("时间1 {}",stopWatch.getTotalTimeSeconds());
        return CollUtils.convert(pageList.getRecords(),w -> {
            RptTendaysSpecialOfflineWeekVO vo = BeanUtil.copyProperties(w,
                    RptTendaysSpecialOfflineWeekVO.class);
            CodeUtil.searchCompanyNameAndRegionName(vo);
            return vo;
        });
    }

    public void specialVehiclesEnterpriseSortingTenDaysReportExport
            (HttpServletResponse response, RptTendaysOverspeedSpecialCompanyParamDTO param) {
        List<RptTendaysSpecialOverspeedCompanyVO> collect = tendaysSpecialOverspeedCompany(param);
        String fileName = "全省专用车辆企业排序表";
        ExcelDownUtil.setFileName(fileName, response);
        // 数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(), RptTendaysSpecialOverspeedCompanyVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public <T> void enterpriseSpecialVehiclesSortingTenDaysReportExport
            (HttpServletResponse response, RptTendaysOverspeedSpecialVehiclesParamDTO param) {
        List<RptTendaysSpecialOverspeedVehiclesVO> collect = tendaysSpecialOverspeedVehicles(param);
        String fileName = "企业专用车辆车辆排序表";
        ExcelDownUtil.setFileName(fileName, response);
        // 数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(), RptTendaysSpecialOverspeedVehiclesVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void specialVehiclesOfflineWeekTenDaysReportExport
            (HttpServletResponse response, RptTendaysOfflineWeekSpecialParamDTO param) {
        List<RptTendaysSpecialOfflineWeekVO> collect = tendaysSpecialOfflineWeek(param);
        String fileName = "全省一周以上不上线的专用车辆排序";
        ExcelDownUtil.setFileName(fileName, response);
        // 数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(), RptTendaysSpecialOfflineWeekVO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

