package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.dto.WarehouseInCreateDto;
import com.aiotagro.cattletrade.business.dto.WarehouseInDto;
import com.aiotagro.cattletrade.business.dto.WarehouseInEditDto;
import com.aiotagro.cattletrade.business.service.IWarehouseInService;
import com.aiotagro.cattletrade.business.vo.WarehouseInVo;
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("/warehouseIn")
public class WarehouseInController {

    @Autowired
    private IWarehouseInService warehouseInService;

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

    /**
     * 新增进仓记录
     */
    @SaCheckPermission("warehousein:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody WarehouseInCreateDto dto) {
        try {
            return warehouseInService.addWarehouseIn(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增进仓记录失败：" + e.getMessage());
        }
    }

    /**
     * 编辑进仓记录
     */
    @SaCheckPermission("warehousein:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody WarehouseInEditDto dto) {
        try {
            return warehouseInService.updateWarehouseIn(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("编辑进仓记录失败：" + e.getMessage());
        }
    }

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

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

