package com.hotwj.platform.reportStatistics.provider.vehicle;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.reportStatistics.service.IVehicleFilePrintService;
import com.hotwj.platform.resourceManagement.vehicleManagement.domain.vo.HotVehicleVo;
import com.hotwj.platform.resourceManagement.vehicleManagement.service.IHotVehicleService;
import com.hotwj.platform.resourceManagement.vehicleMileage.domain.bo.HotVehicleMileageBo;
import com.hotwj.platform.resourceManagement.vehicleMileage.domain.vo.HotVehicleMileageVo;
import com.hotwj.platform.resourceManagement.vehicleMileage.service.IHotVehicleMileageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆行驶里程记录数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class VehicleMileageRecordProvider implements IReportDataProvider {

    private final IHotVehicleMileageService mileageService;
    private final IHotVehicleService vehicleService;

    @Override
    public String getTemplateCode() {
        return "templates/vehicle/mileageRecord.html.vm";
    }

    @Override
    public String getReportType() {
        return IVehicleFilePrintService.TYPE_MILEAGE_RECORD;
    }

    @Override
    public String getReportTitle() {
        return "车辆行驶里程记录";
    }

    @Override
    public Map<String, Object> prepareData(Map<String, Object> params) {
        Long vehicleId = MapUtil.getLong(params, "vehicleId");
        if (vehicleId == null) {
            return new HashMap<>();
        }

        String beginTime = MapUtil.getStr(params, "beginTime");
        String endTime = MapUtil.getStr(params, "endTime");
        Integer printEmptyData = MapUtil.getInt(params, "printEmptyData");
        boolean allowEmpty = printEmptyData == null || printEmptyData == 1;
        Date startDate = null;
        Date endDate = null;
        if (StrUtil.isNotBlank(beginTime)) {
            startDate = DateUtil.beginOfDay(DateUtil.parse(beginTime, "yyyy-MM-dd"));
        }
        if (StrUtil.isNotBlank(endTime)) {
            endDate = DateUtil.endOfDay(DateUtil.parse(endTime, "yyyy-MM-dd"));
        }

        HotVehicleMileageBo bo = new HotVehicleMileageBo();
        bo.setVehicleId(vehicleId);
        // 去掉时间限制，查询所有
        // 增加 isDeleted=0 条件
        bo.setIsDeleted(0L);

        // 获取里程记录列表
        List<HotVehicleMileageVo> list = mileageService.queryList(bo);

        if (list != null && (startDate != null || endDate != null)) {
            Date finalStartDate = startDate;
            Date finalEndDate = endDate;
            list = list.stream()
                    .filter(item -> {
                        Date recordStart = item.getStartDate();
                        Date recordEnd = item.getEndDate();
                        if (recordStart == null || recordEnd == null) {
                            return false;
                        }
                        if (finalStartDate != null && recordStart.before(finalStartDate)) {
                            return false;
                        }
                        return finalEndDate == null || !recordEnd.after(finalEndDate);
                    })
                    .toList();
        }

        if (!allowEmpty && (list == null || list.isEmpty())) {
            return null;
        }

        // 单独查询车辆信息
        HotVehicleVo vehicle = vehicleService.queryById(vehicleId);

        Map<String, Object> ctx = new HashMap<>();
        ctx.put("mileageList", list);
        ctx.put("vehicle", vehicle);

        return ctx;
    }
}
