package com.ruoyi.web.controller.app;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.app.model.PigCategory;
import com.ruoyi.app.service.IPigCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生猪分类Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/pigCategory")
public class PigCategoryController extends BaseController
{
    @Autowired
    private IPigCategoryService pigCategoryService;

    /**
     * 查询生猪分类列表
     */
    // @PreAuthorize("@ss.hasPermi('app:pigCategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(PigCategory pigCategory)
    {
        startPage();
        List<PigCategory> list = pigCategoryService.selectPigCategoryList(pigCategory);
        return getDataTable(list);
    }

    /**
     * 导出生猪分类列表
     */
    // @PreAuthorize("@ss.hasPermi('app:pigCategory:export')")
    @Log(title = "生猪分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PigCategory pigCategory)
    {
        List<PigCategory> list = pigCategoryService.selectPigCategoryList(pigCategory);
        ExcelUtil<PigCategory> util = new ExcelUtil<PigCategory>(PigCategory.class);
        util.exportExcel(response, list, "生猪分类数据");
    }

    /**
     * 获取生猪分类详细信息
     */
    // @PreAuthorize("@ss.hasPermi('app:pigCategory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pigCategoryService.selectPigCategoryById(id));
    }

    /**
     * 新增生猪分类
     */
    // @PreAuthorize("@ss.hasPermi('app:pigCategory:add')")
    @Log(title = "生猪分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PigCategory pigCategory)
    {
        if (!pigCategoryService.checkCategoryNameUnique(pigCategory))
        {
            return error("新增生猪品种'" + pigCategory.getCategoryName() + "'失败，列表中存在重复的品种名称");
        }
        pigCategory.setCreateBy(getUsername());
        return toAjax(pigCategoryService.insertPigCategory(pigCategory));
    }

    /**
     * 修改生猪分类
     */
    // @PreAuthorize("@ss.hasPermi('app:pigCategory:edit')")
    @Log(title = "生猪分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PigCategory pigCategory)
    {
        if (!pigCategoryService.checkCategoryNameUnique(pigCategory))
        {
            return error("修改生猪品种'" + pigCategory.getCategoryName() + "'失败，列表中存在重复的品种名称");
        }
        pigCategory.setUpdateBy(getUsername());
        return toAjax(pigCategoryService.updatePigCategory(pigCategory));
    }

    /**
     * 删除生猪分类
     */
    // @PreAuthorize("@ss.hasPermi('app:pigCategory:remove')")
    @Log(title = "生猪分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pigCategoryService.deletePigCategoryByIds(ids));
    }

    /**
     * 获取分类选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect(PigCategory pigCategory)
    {

        List<PigCategory> list = pigCategoryService.selectPigCategoryList(pigCategory);
        return success(list);
    }
}
