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.domain.ActSuppliesDetails;
import com.ruoyi.system.service.IActSuppliesDetailsService;
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("/ActSuppliesDetails/ActSuppliesDetails")
public class ActSuppliesDetailsController extends BaseController
{
    @Autowired
    private IActSuppliesDetailsService actSuppliesDetailsService;

    /**
     * 查询上级分类
     * */
    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:savesid')")
    @GetMapping("/savesid")
    @ResponseBody
    public TableDataInfo saveSid(){

        List<ActSupplies> options = actSuppliesDetailsService.saveSid();

        return getDataTable(options);

    }

    /**
     * 查询上级分类
     * */
    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:getLevel')")
    @GetMapping("/getLevel")
    @ResponseBody
    public TableDataInfo getLevel(){

        List<ActSuppliesDetails> options = actSuppliesDetailsService.getLevel();

        return getDataTable(options);

    }

    /**
     * 查询物料细分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActSuppliesDetails actSuppliesDetails)
    {
        startPage();
        List<ActSuppliesDetails> list = actSuppliesDetailsService.selectActSuppliesDetailsList(actSuppliesDetails);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:materialList')")
    @GetMapping("/materialList")
    public TableDataInfo list2(ActSuppliesDetails actSuppliesDetails)
    {
        List<ActSuppliesDetails> list = actSuppliesDetailsService.selectActSuppliesDetailsMaterialList(actSuppliesDetails);
        return getDataTable(list);
    }


    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:materList')")
    @GetMapping("/materialListPZ")
    public TableDataInfo materialListPZ(ActSuppliesDetails actSuppliesDetails)
    {
        List<ActSuppliesDetails> list = actSuppliesDetailsService.selectActSuppliesDetailsMaterialListPZ(actSuppliesDetails);
        return getDataTable(list);
    }

    /**
     * 导出物料细分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:export')")
    @Log(title = "物料细分类管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActSuppliesDetails actSuppliesDetails)
    {
        List<ActSuppliesDetails> list = actSuppliesDetailsService.selectActSuppliesDetailsList(actSuppliesDetails);
        ExcelUtil<ActSuppliesDetails> util = new ExcelUtil<ActSuppliesDetails>(ActSuppliesDetails.class);
        return util.exportExcel(list, "六矿物料");
    }

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

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

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

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

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

        List<Long> ids1 =  actSuppliesDetailsService.deleteActSuppliesDetailsByIds(ids);

        if (ids1.size()>0){
            String s = ids1.toString();
            return AjaxResult.success("312",s);
        }

        return AjaxResult.success(200);

    }

    /**
     * 启用物料细分类管理
     */
    @PreAuthorize("@ss.hasPermi('ActSuppliesDetails:ActSuppliesDetails:enable')")
    @PutMapping("/{id}/enable")
    public AjaxResult setEnable(@PathVariable("id") Long id, @RequestParam("status") Integer status)
    {

        actSuppliesDetailsService.setEnable(id,status);
        if (status < 0){
            return AjaxResult.error(ServiceCode.ERR_CODE+"","启用失败");
        }else {
            return AjaxResult.success(ServiceCode.OK+"","启用成功");
        }
    }

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

        actSuppliesDetailsService.setDisable(id,status);

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

    }

}
