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

import cn.hutool.core.map.MapUtil;
import com.hotwj.platform.reportStatistics.service.IReportDataProvider;
import com.hotwj.platform.reportStatistics.service.IVehicleFilePrintService;
import com.hotwj.platform.resourceManagement.company.domain.vo.SysCompanyVo;
import com.hotwj.platform.resourceManagement.company.service.ISysCompanyService;
import com.hotwj.platform.resourceManagement.vehicleManagement.domain.vo.HotVehicleVo;
import com.hotwj.platform.resourceManagement.vehicleManagement.service.IHotVehicleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 车辆基本信息记录数据提供者
 *
 * @author shihongwei
 */
@Component
@RequiredArgsConstructor
public class VehicleBaseInfoProvider implements IReportDataProvider {

    private final IHotVehicleService hotVehicleService;
    private final ISysCompanyService sysCompanyService;

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

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

    @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<>();
        }

        HotVehicleVo vehicle = hotVehicleService.queryById(vehicleId);
        if (vehicle == null) {
            return new HashMap<>();
        }

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

        SysCompanyVo company = vehicle.getCompanyId() != null ? sysCompanyService.queryById(vehicle.getCompanyId()) : null;
        if (company != null) {
            ctx.put("company", company);
        }

        // 处理组合字段
        // 外廓尺寸(mm)
        String outerDimensions = String.format("%s*%s*%s",
                vehicle.getVehicleLengthMm() != null ? vehicle.getVehicleLengthMm() : "",
                vehicle.getVehicleWidthMm() != null ? vehicle.getVehicleWidthMm() : "",
                vehicle.getVehicleHeightMm() != null ? vehicle.getVehicleHeightMm() : "");
        ctx.put("outerDimensions", outerDimensions);

        // 货箱内部尺寸(mm)
        String cargoInnerDimensions = String.format("%s*%s*%s",
                vehicle.getCargoInnerLengthMm() != null ? vehicle.getCargoInnerLengthMm() : "",
                vehicle.getCargoInnerWidthMm() != null ? vehicle.getCargoInnerWidthMm() : "",
                vehicle.getCargoInnerHeightMm() != null ? vehicle.getCargoInnerHeightMm() : "");
        ctx.put("cargoInnerDimensions", cargoInnerDimensions);

        return ctx;
    }
}
