package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.dto.SlaughterHouseCreateDto;
import com.aiotagro.cattletrade.business.dto.SlaughterHouseDto;
import com.aiotagro.cattletrade.business.dto.SlaughterHouseEditDto;
import com.aiotagro.cattletrade.business.entity.SlaughterHouse;
import com.aiotagro.cattletrade.business.service.ISlaughterHouseService;
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-12-09
 */
@RestController
@RequestMapping("/slaughter/house")
public class SlaughterHouseController {

    @Autowired
    private ISlaughterHouseService slaughterHouseService;

    /**
     * 分页查询屠宰场
     */
    @SaCheckPermission("slaughterHouse:query")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SlaughterHouseDto dto) {
        try {
            PageResultResponse<SlaughterHouse> result = slaughterHouseService.pageQuery(dto);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询屠宰场列表失败：" + e.getMessage());
        }
    }

    /**
     * 新增屠宰场
     */
    @SaCheckPermission("slaughterHouse:add")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SlaughterHouseCreateDto dto) {
        try {
            return slaughterHouseService.addHouse(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增屠宰场失败：" + e.getMessage());
        }
    }

    /**
     * 编辑屠宰场
     */
    @SaCheckPermission("slaughterHouse:edit")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SlaughterHouseEditDto dto) {
        try {
            return slaughterHouseService.updateHouse(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("编辑屠宰场失败：" + e.getMessage());
        }
    }

    /**
     * 删除屠宰场
     */
    @SaCheckPermission("slaughterHouse:delete")
    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam Integer id) {
        try {
            return slaughterHouseService.deleteHouse(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除屠宰场失败：" + e.getMessage());
        }
    }

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

    /**
     * 启用列表（下拉）
     */
    @GetMapping("/all")
    public AjaxResult allEnabled() {
        try {
            List<SlaughterHouse> list = slaughterHouseService.getAllEnabled();
            return AjaxResult.success("查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询屠宰场列表失败：" + e.getMessage());
        }
    }
}

