package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.dto.SlaughterRecordCreateDto;
import com.aiotagro.cattletrade.business.dto.SlaughterRecordDto;
import com.aiotagro.cattletrade.business.dto.SlaughterRecordEditDto;
import com.aiotagro.cattletrade.business.entity.SlaughterRecord;
import com.aiotagro.cattletrade.business.service.ISlaughterRecordService;
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-10
 */
@RestController
@RequestMapping("/slaughter/record")
public class SlaughterRecordController {

    @Autowired
    private ISlaughterRecordService slaughterRecordService;

    /**
     * 分页查询屠宰记录
     */
    @SaCheckPermission("slaughterRecord:query")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SlaughterRecordDto dto) {
        try {
            PageResultResponse<SlaughterRecord> result = slaughterRecordService.pageQuery(dto);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询屠宰记录失败：" + e.getMessage());
        }
    }

    /**
     * 新增屠宰记录
     */
    @SaCheckPermission("slaughterRecord:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SlaughterRecordCreateDto dto) {
        try {
            return slaughterRecordService.addRecord(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增屠宰记录失败：" + e.getMessage());
        }
    }

    /**
     * 编辑屠宰记录
     */
    @SaCheckPermission("slaughterRecord:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SlaughterRecordEditDto dto) {
        try {
            return slaughterRecordService.updateRecord(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("编辑屠宰记录失败：" + e.getMessage());
        }
    }

    /**
     * 删除屠宰记录
     */
    @SaCheckPermission("slaughterRecord:delete")
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Integer id) {
        try {
            return slaughterRecordService.deleteRecord(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除屠宰记录失败：" + e.getMessage());
        }
    }

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


