package com.hotwj.platform.resourceManagement.vehicleRepair.controller;

import com.hotwj.platform.driverManagement.driver.domain.vo.HotDriverVo;
import com.hotwj.platform.driverManagement.driver.service.IHotDriverService;
import com.hotwj.platform.resourceManagement.companySafetyManager.domain.vo.HotCompanySafetyManagerVo;
import com.hotwj.platform.resourceManagement.companySafetyManager.service.IHotCompanySafetyManagerService;
import com.hotwj.platform.resourceManagement.vehicleRepair.domain.bo.HotVehicleRepairBo;
import com.hotwj.platform.resourceManagement.vehicleRepair.domain.vo.HotVehicleRepairVo;
import com.hotwj.platform.resourceManagement.vehicleRepair.service.IHotVehicleRepairService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 车辆维修记录
 *
 * @author shihongwei
 * @date 2025-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/resourceManagement/vehicleRepair")
@Tag(name = "车辆维修记录", description = "车辆维修记录管理")
public class HotVehicleRepairController extends BaseController {

    private final IHotVehicleRepairService hotVehicleRepairService;
    private final IHotCompanySafetyManagerService hotCompanySafetyManagerService;
    private final IHotDriverService hotDriverService;

    /**
     * 查询车辆维修记录列表
     */
    //@SaCheckPermission("resourceManagement:vehicleRepair:list")
    @GetMapping("/list")
    @Operation(summary = "查询车辆维修记录列表")
    public TableDataInfo<HotVehicleRepairVo> list(HotVehicleRepairBo bo, PageQuery pageQuery) {
        return hotVehicleRepairService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出车辆维修记录列表
     */
    //@SaCheckPermission("resourceManagement:vehicleRepair:export")
    @Log(title = "车辆维修记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出车辆维修记录列表")
    public void export(HotVehicleRepairBo bo, HttpServletResponse response) {
        List<HotVehicleRepairVo> list = hotVehicleRepairService.queryList(bo);
        Map<String, String> recorderNameCache = new HashMap<>();
        for (HotVehicleRepairVo item : list) {
            item.setRecorder(resolveRecorderName(item.getRecorder(), recorderNameCache));
        }
        ExcelUtil.exportExcel(list, "车辆维修记录", HotVehicleRepairVo.class, response);
    }

    private String resolveRecorderName(String recorder, Map<String, String> cache) {
        if (recorder == null || recorder.isBlank()) {
            return recorder;
        }
        if (cache.containsKey(recorder)) {
            return cache.get(recorder);
        }
        Long recorderId;
        try {
            recorderId = Long.valueOf(recorder);
        } catch (NumberFormatException e) {
            HotDriverVo driver = hotDriverService.queryById(recorder);
            if (driver != null && driver.getName() != null && !driver.getName().isBlank()) {
                String recorderName = driver.getName();
                cache.put(recorder, recorderName);
                return recorderName;
            }
            return recorder;
        }
        HotCompanySafetyManagerVo manager = hotCompanySafetyManagerService.queryById(recorderId);
        String recorderName = manager != null ? manager.getName() : null;
        if (recorderName == null || recorderName.isBlank()) {
            recorderName = recorder;
        }
        cache.put(recorder, recorderName);
        return recorderName;
    }

    /**
     * 获取车辆维修记录详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("resourceManagement:vehicleRepair:query")
    @GetMapping("/{id}")
    @Operation(summary = "获取车辆维修记录详细信息")
    public R<HotVehicleRepairVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(hotVehicleRepairService.queryById(id));
    }

    /**
     * 新增车辆维修记录
     */
    //@SaCheckPermission("resourceManagement:vehicleRepair:add")
    @Log(title = "车辆维修记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Operation(summary = "新增车辆维修记录")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HotVehicleRepairBo bo) {
        return toAjax(hotVehicleRepairService.insertByBo(bo));
    }

    /**
     * 修改车辆维修记录
     */
    //@SaCheckPermission("resourceManagement:vehicleRepair:edit")
    @Log(title = "车辆维修记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Operation(summary = "修改车辆维修记录")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HotVehicleRepairBo bo) {
        return toAjax(hotVehicleRepairService.updateByBo(bo));
    }

    /**
     * 删除车辆维修记录
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("resourceManagement:vehicleRepair:remove")
    @Log(title = "车辆维修记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除车辆维修记录")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(hotVehicleRepairService.deleteWithValidByIds(List.of(ids), true));
    }
}
