package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ActSupplies;
import com.ruoyi.system.service.IActSuppliesService;
import com.ruoyi.system.web.ServiceCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料总分类管理Controller
 *
 * @author ruoyi
 * @date 2023-07-11
 */
@RestController
@RequestMapping("/Actsupplies/Actsupplies")
public class ActSuppliesController extends BaseController
{
    @Autowired
    private IActSuppliesService actSuppliesService;

    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:options')")
    @GetMapping("/options")
    @ResponseBody
    public TableDataInfo options(){

        List<ActSupplies> options = actSuppliesService.options();

        return getDataTable(options);

    }

    /**
     * 查询物料总分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActSupplies actSupplies)
    {
        startPage();
        List<ActSupplies> list = actSuppliesService.selectActSuppliesList(actSupplies);;
        return getDataTable(list);
    }

    /**
     * 导出物料总分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:export')")
    @Log(title = "用友物料", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActSupplies actSupplies)
    {

        List<ActSupplies> list = actSuppliesService.selectActSuppliesList(actSupplies);

        ExcelUtil<ActSupplies> util = new ExcelUtil<ActSupplies>(ActSupplies.class);
        return util.exportExcel(list, "二级分类数据");
    }

    /**
     * 获取物料总分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(actSuppliesService.selectActSuppliesById(id));
    }

    /**
     * 新增物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:add')")
    @Log(title = "物料总分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActSupplies actSupplies)
    {

        int i = actSuppliesService.insertActSupplies(actSupplies);
        if(i == 0){
            return AjaxResult.error("添加名称失败，名称已被占用");
        }else if (i==-1){
            return AjaxResult.error("添加名称失败，物资编码已被占用");
        }
        return  AjaxResult.success("添加成功");
    }

    /**
     * 修改物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:edit')")
    @Log(title = "物料总分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActSupplies actSupplies)
    {
        int i = actSuppliesService.updateActSupplies(actSupplies);
        if(i==0){
            return AjaxResult.error("修改名称失败.名称已被占用");
        }
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:remove')")
    @Log(title = "物料总分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i = actSuppliesService.deleteActSuppliesByIds(ids);

        if (i==0){
            return AjaxResult.error("该物料下存在关联物料，请先删除关联物料！！！","操作失败");
        }

        return toAjax(i);
    }

    /** 启用物料总分类管理
      */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:enable')")
    @PutMapping("/{id}/enable")
    public AjaxResult setEnable(@PathVariable("id") Long id, @RequestParam("status") Integer status)
    {
        actSuppliesService.setEnable(id,status);
        if (status < 0){
            return AjaxResult.error(ServiceCode.ERR_CODE+"","启用失败");
        }else {
            return AjaxResult.success(ServiceCode.OK+"","启用成功");
        }
    }

    /**
     * 禁用物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('Actsupplies:Actsupplies:Disable')")
    @PutMapping("/{id}/Disable")
    public AjaxResult setDisable(@PathVariable("id") Long id, @RequestParam("status") Integer status) {

        if (actSuppliesService.find(id).size() > 0 || actSuppliesService.findId(id).size() > 0) {

        }
            actSuppliesService.setDisable(id, status);
        if (status > 1){
            return AjaxResult.error(ServiceCode.ERR_CODE+"","禁用失败");
        }else {
            return AjaxResult.success(ServiceCode.OK+"","禁用成功");
        }

    }

}
