package com.bonus.material.scrap.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.scrap.domain.ScrapReason;
import com.bonus.material.scrap.service.IScrapReasonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.bonus.common.log.annotation.SysLog;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.warehouse.domain.WhHouseInfo;
import com.bonus.material.warehouse.service.IWhHouseInfoService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

/**
 * 报废原因管理Controller
 *
 * @author hongchao
 * @date 2025-01-07
 */
@Api(tags = "报废原因管理接口")
@RestController
@RequestMapping("/scrap_reason")
public class ScrapReasonController extends BaseController {

    @Autowired
    private IScrapReasonService scrapReasonService;

    /**
     * 查询二级类型下拉框列表
     */
    @ApiOperation(value = "查询二级类型列表")
    @RequiresPermissions("scrap:reason:list")
    @GetMapping("/levelTwolist")
    public AjaxResult levelTwolist(ScrapReason scrapReason) {
        List<ScrapReason> list = scrapReasonService.levelTwolist(scrapReason);
        return success(list);
    }

    /**
     * 查询三级类型下拉框列表
     */
    @ApiOperation(value = "查询三级类型列表")
    @RequiresPermissions("scrap:reason:list")
    @GetMapping("/levelThreelist")
    public AjaxResult levelThreelist(ScrapReason scrapReason) {
        List<ScrapReason> list = scrapReasonService.levelThreelist(scrapReason);
        return success(list);
    }

    /**
     * 查询报废原因下拉列表 app用
     * @param scrapReason
     * @return
     */
    @ApiOperation(value = "查询报废原因下拉列表 app用")
    //@RequiresPermissions("scrap:reason:list")
    @GetMapping("/getList")
    public AjaxResult getList(ScrapReason scrapReason) {
        try {
            List<ScrapReason> list = scrapReasonService.getList(scrapReason);
            return success(list);
        } catch (Exception e) {
            return AjaxResult.error("报废原因数据查询失败");
        }
    }

    /**
     * 查询报废原因列表
     */
    @ApiOperation(value = "查询报废原因列表")
    @RequiresPermissions("scrap:reason:list")
    @GetMapping("/list")
    public TableDataInfo list(ScrapReason scrapReason) {
        startPage();
        List<ScrapReason> list = scrapReasonService.selectScrapReasonList(scrapReason);
        return getDataTable(list);
    }


    /**
     * 导出报废原因管理列表
     */
    @ApiOperation(value = "导出报废原因管理列表")
    @PreventRepeatSubmit
    @RequiresPermissions("scrap:reason:export")
    @SysLog(title = "报废原因管理", businessType = OperaType.EXPORT, module = "报废原因管理->导出报废原因管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrapReason scrapReason) {
        List<ScrapReason> list = scrapReasonService.selectScrapReasonList(scrapReason);
        ExcelUtil<ScrapReason> util = new ExcelUtil<ScrapReason>(ScrapReason.class);
        util.exportExcel(response, list, "报废原因数据");
    }

    /**
     * 获取报废原因管理详细信息
     */
    @ApiOperation(value = "获取报废原因管理详细信息")
    @RequiresPermissions("scrap:reason:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(scrapReasonService.selectScrapReasonById(id));
    }

    /**
     * 新增报废原因
     */
    @ApiOperation(value = "新增报废原因")
    @PreventRepeatSubmit
    @RequiresPermissions("scrap:reason:add")
    @SysLog(title = "报废原因管理", businessType = OperaType.INSERT, logType = 1,module = "报废原因管理->新增报废原因")
    @PostMapping
    public AjaxResult add(@RequestBody ScrapReason scrapReason) {
        try {
            return toAjax(scrapReasonService.insertScrapReasonInfo(scrapReason));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改报废原因
     */
    @ApiOperation(value = "修改报废原因")
    @PreventRepeatSubmit
    @RequiresPermissions("scrap:reason:edit")
    @SysLog(title = "报废原因管理", businessType = OperaType.UPDATE,module = "报废原因管理->修改报废原因")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ScrapReason scrapReason) {
        try {
            return toAjax(scrapReasonService.updateScrapReasonInfo(scrapReason));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除报废原因
     */
    @ApiOperation(value = "删除报废原因")
    @PreventRepeatSubmit
    @RequiresPermissions("scrap:reason:remove")
    @SysLog(title = "报废原因管理", businessType = OperaType.DELETE, module = "报废原因管理->删除报废原因")
    @PostMapping("/delete")
    public AjaxResult remove(@RequestBody ScrapReason scrapReason) {
        return toAjax(scrapReasonService.deleteScrapReasonInfo(scrapReason));
    }
}
