package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.dto.SlaughterEntryCreateDto;
import com.aiotagro.cattletrade.business.dto.SlaughterEntryDto;
import com.aiotagro.cattletrade.business.dto.SlaughterEntryEditDto;
import com.aiotagro.cattletrade.business.entity.SlaughterEntry;
import com.aiotagro.cattletrade.business.service.ISlaughterEntryService;
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-12-09
 */
@RestController
@RequestMapping("/slaughter/entry")
public class SlaughterEntryController {

    @Autowired
    private ISlaughterEntryService slaughterEntryService;

    /**
     * 分页查询进场记录
     */
    @SaCheckPermission("slaughterEntry:query")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SlaughterEntryDto dto) {
        try {
            PageResultResponse<SlaughterEntry> result = slaughterEntryService.pageQuery(dto);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询进场记录失败：" + e.getMessage());
        }
    }

    /**
     * 新增进场记录
     */
    @SaCheckPermission("slaughterEntry:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SlaughterEntryCreateDto dto) {
        try {
            return slaughterEntryService.addEntry(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增进场记录失败：" + e.getMessage());
        }
    }

    /**
     * 编辑进场记录
     */
    @SaCheckPermission("slaughterEntry:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SlaughterEntryEditDto dto) {
        try {
            return slaughterEntryService.updateEntry(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("编辑进场记录失败：" + e.getMessage());
        }
    }

    /**
     * 删除进场记录
     */
    @SaCheckPermission("slaughterEntry:delete")
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Integer id) {
        try {
            return slaughterEntryService.deleteEntry(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除进场记录失败：" + e.getMessage());
        }
    }

    /**
     * 详情
     */
    @SaCheckPermission("slaughterEntry:query")
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam Integer id) {
        try {
            return slaughterEntryService.detail(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询进场记录详情失败：" + e.getMessage());
        }
    }
}

