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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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.google.common.collect.Lists;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.carapi.car.datascreen.model.dto.DataScreenParam;
import com.ningdatech.carapi.car.monitor.entity.AlarmDataLoad;
import com.ningdatech.carapi.car.monitor.entity.SecurityMonitorJoin;
import com.ningdatech.carapi.car.monitor.model.dto.AlarmDataLoadDTO;
import com.ningdatech.carapi.car.monitor.model.dto.LoadMonitorPageParamDTO;
import com.ningdatech.carapi.car.monitor.model.dto.SecurityMonitorPageParamDTO;
import com.ningdatech.carapi.car.monitor.model.vo.AlarmDataLoadVO;
import com.ningdatech.carapi.car.monitor.model.vo.SecurityMonitorVO;
import com.ningdatech.carapi.car.monitor.service.IAlarmDataLoadService;
import com.ningdatech.carapi.car.monitor.service.SecurityMonitorService;
import com.ningdatech.carapi.car.trips.entity.VehicleTripsCity;
import com.ningdatech.carapi.car.trips.entity.VehicleTripsCompany;
import com.ningdatech.carapi.car.trips.model.dto.VehicleTripsCityExportDTO;
import com.ningdatech.carapi.car.trips.model.dto.VehicleTripsParamDTO;
import com.ningdatech.carapi.car.trips.model.vo.VehicleTripsCityVO;
import com.ningdatech.carapi.car.trips.model.vo.VehicleTripsCompanyVO;
import com.ningdatech.carapi.car.trips.service.IVehicleTripsCityService;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionEntity;
import com.ningdatech.carapi.common.constant.AlarmTypeEnum;
import com.ningdatech.carapi.common.constant.CommonConstant;
import com.ningdatech.carapi.common.contants.DefValConstants;
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.entity.dto.CascadeCodeDTO;
import com.ningdatech.carapi.sys.entity.dto.RegionDTO;
import com.ningdatech.carapi.sys.helper.RegionsCacheHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Classname VehicleTripsManage
 * @Description
 * @Date 2022/11/4 16:30
 * @Created by PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class VehicleTripsManage {

    private final IVehicleTripsCityService vehicleTripsCityService;

    //逻辑修改为 从上车记录里取
    public PageVo<VehicleTripsCityVO> lictGroupByCity(VehicleTripsParamDTO param) {
        Page<VehicleTripsCity> page = param.page();
        ((VehicleTripsManage)AopContext.currentProxy()).search(page,param);
        if(CollUtil.isEmpty(page.getRecords())){
            return PageVo.of(Collections.emptyList(), page.getTotal());
        }
        List<VehicleTripsCityVO> data = page.getRecords().stream().map(w -> BeanUtil.copyProperties(w, VehicleTripsCityVO.class)).collect(Collectors.toList());
        return PageVo.of(data, page.getTotal());
    }

    public PageVo<VehicleTripsCompanyVO> lictGroupByCompany(VehicleTripsParamDTO param) {
        Page<VehicleTripsCompany> page = param.page();
        ((VehicleTripsManage)AopContext.currentProxy()).searchGroupCompany(page,param);
        if(0L == page.getTotal()){
            return PageVo.empty();
        }
        List<VehicleTripsCompanyVO> data = page.getRecords().stream().map(w -> BeanUtil.copyProperties(w, VehicleTripsCompanyVO.class)).collect(Collectors.toList());
        return PageVo.of(data, page.getTotal());
    }

    @XmlDataScope(entityIndex = 1)
    @BuildChildrenRegionEntity(entityIndex = 1)
    public void search(Page<VehicleTripsCity> page, VehicleTripsParamDTO param) {
        CodeUtil.buildRegionLevel(param);
        vehicleTripsCityService.pageCityGetOn(page, param);
    }

    @XmlDataScope(entityIndex = 1)
    @BuildChildrenRegionEntity(entityIndex = 1)
    public void searchGroupCompany(Page<VehicleTripsCompany> page, VehicleTripsParamDTO param) {
        vehicleTripsCityService.pageCityGetOnCompany(page, param);
    }

    public <T> void exportGroupByCity(HttpServletResponse response, VehicleTripsParamDTO param) {
        param.page(CommonConstant.EXPORT_PAGE_NUMBER, CommonConstant.EXPORT_PAGE_SIZE);
        PageVo<VehicleTripsCityVO> page = lictGroupByCity(param);
        List<VehicleTripsCityVO> collect = page.getRecords().stream().map(w -> BeanUtil.copyProperties(w, VehicleTripsCityVO.class)).collect(Collectors.toList());
        String fileName = "车辆趟次列表";
        ExcelDownUtil.setFileName(fileName, response);
        // 数据导出处理函数
        try {
            EasyExcel.write(response.getOutputStream(), VehicleTripsCityExportDTO.class)
                    .autoCloseStream(false)
                    .registerWriteHandler(ExcelExportStyle.formalStyle())
                    .sheet(fileName)
                    .doWrite(collect);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
