package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.dto.WarehouseCreateDto;
import com.aiotagro.cattletrade.business.dto.WarehouseDto;
import com.aiotagro.cattletrade.business.dto.WarehouseEditDto;
import com.aiotagro.cattletrade.business.entity.Warehouse;
import com.aiotagro.cattletrade.business.service.IWarehouseService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 中转仓管理控制器
 *
 * @author System
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 分页查询中转仓列表
     */
    @SaCheckPermission("warehouse:query")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody WarehouseDto dto) {
        try {
            PageResultResponse<Warehouse> result = warehouseService.pageQuery(dto);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询中转仓列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增中转仓
     */
    @SaCheckPermission("warehouse:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody WarehouseCreateDto dto) {
        try {
            return warehouseService.addWarehouse(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增中转仓失败：" + e.getMessage());
        }
    }

    /**
     * 编辑中转仓
     */
    @SaCheckPermission("warehouse:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody WarehouseEditDto dto) {
        try {
            return warehouseService.updateWarehouse(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("编辑中转仓失败：" + e.getMessage());
        }
    }

    /**
     * 删除中转仓（逻辑删除）
     */
    @SaCheckPermission("warehouse:delete")
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Integer id) {
        try {
            if (id == null) {
                return AjaxResult.error("中转仓ID不能为空");
            }
            return warehouseService.deleteWarehouse(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除中转仓失败：" + e.getMessage());
        }
    }

    /**
     * 获取中转仓详情
     */
    @SaCheckPermission("warehouse:query")
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam Integer id) {
        try {
            if (id == null) {
                return AjaxResult.error("中转仓ID不能为空");
            }
            return warehouseService.getWarehouseDetail(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询中转仓详情失败：" + e.getMessage());
        }
    }

    /**
     * 获取所有启用的中转仓（下拉选择用）
     */
    @GetMapping("/all")
    public AjaxResult getAllEnabled() {
        try {
            List<Warehouse> list = warehouseService.getAllEnabled();
            return AjaxResult.success("查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询中转仓列表失败：" + e.getMessage());
        }
    }
}

