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

/**
 * 死淘处理方式Controller
 * 
 * @author coede
 * @date 2025-03-19
 */
@RestController
@RequestMapping("/app/disposal")
public class DeadDisposalController extends BaseController
{
    @Autowired
    private IDeadDisposalService deadDisposalService;

    /**
     * 查询死淘处理方式列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DeadDisposal deadDisposal)
    {
        startPage();
        List<DeadDisposal> list = deadDisposalService.selectDeadDisposalList(deadDisposal);
        return getDataTable(list);
    }

    /**
     * 导出死淘处理方式列表
     */
    // @PreAuthorize("@ss.hasPermi('app:disposal:export')")
    @Log(title = "死淘处理方式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeadDisposal deadDisposal)
    {
        List<DeadDisposal> list = deadDisposalService.selectDeadDisposalList(deadDisposal);
        ExcelUtil<DeadDisposal> util = new ExcelUtil<DeadDisposal>(DeadDisposal.class);
        util.exportExcel(response, list, "死淘处理方式数据");
    }

    /**
     * 获取死淘处理方式详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deadDisposalService.selectDeadDisposalById(id));
    }

    /**
     * 新增死淘处理方式
     */
    // @PreAuthorize("@ss.hasPermi('app:disposal:add')")
    @Log(title = "死淘处理方式", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DeadDisposal deadDisposal)
    {
        if (!deadDisposalService.checkDealReasonUnique(deadDisposal))
        {
            return error("新增死淘处理方式'" + deadDisposal.getDealReason() + "'失败，处理原因已存在");
        }
        deadDisposal.setCreateBy(getUsername());
        return toAjax(deadDisposalService.insertDeadDisposal(deadDisposal));
    }

    /**
     * 修改死淘处理方式
     */
    // @PreAuthorize("@ss.hasPermi('app:disposal:edit')")
    @Log(title = "死淘处理方式", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DeadDisposal deadDisposal)
    {
        if (!deadDisposalService.checkDealReasonUnique(deadDisposal))
        {
            return error("修改死淘处理方式'" + deadDisposal.getDealReason() + "'失败，处理原因已存在");
        }
        deadDisposal.setUpdateBy(getUsername());
        return toAjax(deadDisposalService.updateDeadDisposal(deadDisposal));
    }

    /**
     * 删除死淘处理方式
     */
    // @PreAuthorize("@ss.hasPermi('app:disposal:remove')")
    @Log(title = "死淘处理方式", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deadDisposalService.deleteDeadDisposalByIds(ids));
    }

    /**
     * 获取检查项目选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        DeadDisposal deadDisposal = new DeadDisposal();
        List<DeadDisposal> list = deadDisposalService.selectDeadDisposalList(deadDisposal);
        return success(list);
    }
}
