package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.entity.Vehicle;
import com.aiotagro.cattletrade.business.service.IVehicleService;
import com.aiotagro.common.core.web.domain.AjaxResult;
import com.aiotagro.common.core.web.domain.PageResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 车辆管理控制器
 *
 * @author System
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController {

    @Autowired
    private IVehicleService vehicleService;

    /**
     * 查询车辆列表（分页+搜索）
     */
    @SaCheckPermission("vehicle:query")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody Map<String, Object> params) {
        try {
            System.out.println("=== 查询车辆列表 ===");
            System.out.println("参数: " + params);

            PageResultResponse<Vehicle> result = vehicleService.pageQuery(params);

            System.out.println("查询成功");

            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询车辆列表失败: " + e.getMessage());
            return AjaxResult.error("查询车辆列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增车辆
     */
    @SaCheckPermission("vehicle:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Vehicle vehicle) {
        try {
            System.out.println("=== 新增车辆 ===");
            System.out.println("车牌号: " + vehicle.getLicensePlate());
            System.out.println("车头照片: " + vehicle.getCarFrontPhoto());
            System.out.println("车尾照片: " + vehicle.getCarRearPhoto());
            System.out.println("行驶证照片: " + vehicle.getDrivingLicensePhoto());
            System.out.println("牧运通备案码: " + vehicle.getRecordCode());
            System.out.println("备注: " + vehicle.getRemark());

            // 参数验证
            if (vehicle.getLicensePlate() == null || vehicle.getLicensePlate().trim().isEmpty()) {
                System.out.println("新增失败: 车牌号不能为空");
                return AjaxResult.error("车牌号不能为空");
            }

            AjaxResult result = vehicleService.addVehicle(vehicle);
            System.out.println("新增操作执行完成");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("新增车辆失败: " + e.getMessage());
            return AjaxResult.error("新增车辆失败：" + e.getMessage());
        }
    }

    /**
     * 编辑车辆
     */
    @SaCheckPermission("vehicle:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Vehicle vehicle) {
        try {
            System.out.println("=== 编辑车辆 ===");
            System.out.println("车辆ID: " + vehicle.getId());
            System.out.println("车牌号: " + vehicle.getLicensePlate());
            System.out.println("车头照片: " + vehicle.getCarFrontPhoto());
            System.out.println("车尾照片: " + vehicle.getCarRearPhoto());
            System.out.println("行驶证照片: " + vehicle.getDrivingLicensePhoto());
            System.out.println("牧运通备案码: " + vehicle.getRecordCode());
            System.out.println("备注: " + vehicle.getRemark());

            // 参数验证
            if (vehicle.getId() == null) {
                System.out.println("编辑失败: 车辆ID不能为空");
                return AjaxResult.error("车辆ID不能为空");
            }

            if (vehicle.getLicensePlate() == null || vehicle.getLicensePlate().trim().isEmpty()) {
                System.out.println("编辑失败: 车牌号不能为空");
                return AjaxResult.error("车牌号不能为空");
            }

            AjaxResult result = vehicleService.updateVehicle(vehicle);
            System.out.println("编辑操作执行完成");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("编辑车辆失败: " + e.getMessage());
            return AjaxResult.error("编辑车辆失败：" + e.getMessage());
        }
    }

    /**
     * 删除车辆（逻辑删除）
     */
    @SaCheckPermission("vehicle:delete")
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Integer id) {
        try {
            System.out.println("=== 删除车辆 ===");
            System.out.println("车辆ID: " + id);

            if (id == null) {
                System.out.println("删除失败: 车辆ID不能为空");
                return AjaxResult.error("车辆ID不能为空");
            }

            AjaxResult result = vehicleService.deleteVehicle(id);
            System.out.println("删除操作执行完成");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除车辆失败: " + e.getMessage());
            return AjaxResult.error("删除车辆失败：" + e.getMessage());
        }
    }
}

