package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.dto.WarehouseOutCreateDto;
import com.aiotagro.cattletrade.business.dto.WarehouseOutDto;
import com.aiotagro.cattletrade.business.dto.WarehouseOutEditDto;
import com.aiotagro.cattletrade.business.service.IWarehouseOutService;
import com.aiotagro.cattletrade.business.vo.WarehouseOutVo;
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.*;

/**
 * 出仓管理控制器
 *
 * @author System
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/warehouseOut")
public class WarehouseOutController {

    @Autowired
    private IWarehouseOutService warehouseOutService;

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

    /**
     * 新增出仓记录
     */
    @SaCheckPermission("warehouseout:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody WarehouseOutCreateDto dto) {
        try {
            return warehouseOutService.addWarehouseOut(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增出仓记录失败：" + e.getMessage());
        }
    }

    /**
     * 编辑出仓记录
     */
    @SaCheckPermission("warehouseout:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody WarehouseOutEditDto dto) {
        try {
            return warehouseOutService.updateWarehouseOut(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("编辑出仓记录失败：" + e.getMessage());
        }
    }

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

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

