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.domain.HarmlessTreatment;
import com.ruoyi.app.service.IHarmlessTreatmentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 无害化处理Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/harmlessTreatment")
public class HarmlessTreatmentController extends BaseController
{
    @Autowired
    private IHarmlessTreatmentService harmlessTreatmentService;

    /**
     * 查询无害化处理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HarmlessTreatment harmlessTreatment)
    {
        startPage();
        List<HarmlessTreatment> list = harmlessTreatmentService.selectHarmlessTreatmentList(harmlessTreatment);
        return getDataTable(list);
    }

    /**
     * 导出无害化处理列表
     */
    @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:export')")
    @Log(title = "无害化处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HarmlessTreatment harmlessTreatment)
    {
        List<HarmlessTreatment> list = harmlessTreatmentService.selectHarmlessTreatmentList(harmlessTreatment);
        ExcelUtil<HarmlessTreatment> util = new ExcelUtil<HarmlessTreatment>(HarmlessTreatment.class);
        util.exportExcel(response, list, "无害化处理数据");
    }

    /**
     * 获取无害化处理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(harmlessTreatmentService.selectHarmlessTreatmentById(id));
    }

    /**
     * 新增无害化处理
     */
    @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:add')")
    @Log(title = "无害化处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated  @RequestBody HarmlessTreatment harmlessTreatment)
    {
        //判断全部数量是否超出了入场数量
        if(!harmlessTreatmentService.checkTotalLessThanAmount(harmlessTreatment)){
            return error("新增无害化处理失败，病死头数、病害头数或急宰头数输入错误");
        }
        harmlessTreatment.setCreateBy(getUsername());
        return toAjax(harmlessTreatmentService.insertHarmlessTreatment(harmlessTreatment));
    }

    /**
     * 修改无害化处理
     */
    @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:edit')")
    @Log(title = "无害化处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody HarmlessTreatment harmlessTreatment)
    {
        //判断全部数量是否超出了入场数量
        if(!harmlessTreatmentService.checkTotalLessThanAmount(harmlessTreatment)){
            return error("修改无害化处理失败，病死头数、病害头数或急宰头数输入错误");
        }
        harmlessTreatment.setUpdateBy(getUsername());
        return toAjax(harmlessTreatmentService.updateHarmlessTreatment(harmlessTreatment));
    }

    /**
     * 删除无害化处理
     */
    @PreAuthorize("@ss.hasPermi('app:harmlessTreatment:remove')")
    @Log(title = "无害化处理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(harmlessTreatmentService.deleteHarmlessTreatmentByIds(ids));
    }
}
