package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ActSuppliesRoleDetail;
import com.ruoyi.system.domain.ActSuppliesTemplate;
import com.ruoyi.system.web.ServiceCode;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.system.domain.ActSuppliesRole;
import com.ruoyi.system.service.IActSuppliesRoleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 导入规则Controller
 * 
 * @author dongjg
 * @date 2023-07-12
 */
@RestController
@RequestMapping("/system/suppliesrole")
public class ActSuppliesRoleController extends BaseController
{
    @Autowired
    private IActSuppliesRoleService actSuppliesRoleService;

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

        List<ActSuppliesTemplate> options = actSuppliesRoleService.saveSid();

        return getDataTable(options);

    }

    /**
     * 查询导入规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActSuppliesRole actSuppliesRole)
    {
        startPage();
        List<ActSuppliesRole> list = actSuppliesRoleService.selectActSuppliesRoleList(actSuppliesRole);
        return getDataTable(list);
    }

    /**
     * 查询规则详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:lists')")
    @GetMapping("/lists/{id}")
    public TableDataInfo lists(@PathVariable("id") Integer id)
    {
        startPage();
        List<ActSuppliesRoleDetail> list = actSuppliesRoleService.selectActSuppliesRoleDetailList(id);
        return getDataTable(list);
    }

    /**
     * 导出导入规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:export')")
    @Log(title = "导入规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActSuppliesRole actSuppliesRole)
    {
        List<ActSuppliesRole> list = actSuppliesRoleService.selectActSuppliesRoleList(actSuppliesRole);
        ExcelUtil<ActSuppliesRole> util = new ExcelUtil<ActSuppliesRole>(ActSuppliesRole.class);
        return util.exportExcel(list, "导入规则数据");
    }

    /**
     * 获取导入规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(actSuppliesRoleService.selectActSuppliesRoleById(id));
    }

    /**
     * 新增导入规则
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:add')")
    @Log(title = "导入规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActSuppliesRole actSuppliesRole)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesRole.setCreateBy(user.getUserName());
        /*return toAjax(actSuppliesService.insertActSupplies(actSupplies));*/
        int i = actSuppliesRoleService.insertActSuppliesRole(actSuppliesRole);
        if(i==0){
            return AjaxResult.error("添加名称失败，名称已被占用");
        }
        return  AjaxResult.success("添加成功");
    }



    /**
     * 修改导入规则
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:edit')")
    @Log(title = "导入规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody  ActSuppliesRole actSuppliesRole)
    {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        actSuppliesRole.setUpdateBy(user.getUserName());
        /*return toAjax(actSuppliesRoleService.updateActSuppliesRole(actSuppliesRole));*/
        int i = actSuppliesRoleService.updateActSuppliesRole(actSuppliesRole);
        if(i==0){
            return AjaxResult.error("修改名称失败，名称已被占用");
        }
        return  AjaxResult.success("修改成功");
    }

    /**
     * 修改规则详情
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:edit')")
    @Log(title = "规则详情", businessType = BusinessType.UPDATE)
    @PutMapping("/edit/{id}")
    public AjaxResult edit(@RequestBody ActSuppliesRoleDetail actSuppliesRoleDetail)
    {
        return toAjax(actSuppliesRoleService.updateActSuppliesRoleDetail(actSuppliesRoleDetail));
    }


    /**
     * 删除导入规则
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:remove')")
    @Log(title = "导入规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

//        if (actSuppliesRoleService.findall(ids).size()>0){
//            return AjaxResult.error("该规则下存在关联规则，请先删除关联规则！！！","操作失败");
//        }

        return toAjax(actSuppliesRoleService.deleteActSuppliesRoleByIds(ids));
    }
    /**
     * 启用物料总分类管理
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:enable')")
    @PutMapping("/{id}/enable")
    public AjaxResult setEnable(@PathVariable("id") Long id, @RequestParam("status") Integer status)
    {

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

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

        if (actSuppliesRoleService.find(Math.toIntExact(id)).size()>0){
            return AjaxResult.error("该规则下存在关联规则，请先删除关联规则！！！","操作失败");
        }

        actSuppliesRoleService.setDisable(id,status);

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

    }


    /**
     * 发布Excel模板
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:releaseRS')")
    @Log(title = "Excel模板", businessType = BusinessType.UPDATE)
    @GetMapping("/releaseRS/{ids}/{tempids}")
    public AjaxResult releaseRS(@PathVariable("ids") Long[] ids ,@PathVariable("tempids") Long[] tempids) {

        List<String> longs = actSuppliesRoleService.releaseRS(ids, tempids);
        String s = longs.toString();
        if (longs.size()>0){
            return AjaxResult.success("312",s);
        }else {
            return AjaxResult.success(200);
        }

    }

    /**
     * 下架Excel模板
     */
    @PreAuthorize("@ss.hasPermi('system:suppliesrole:offShelfRS')")
    @Log(title = "Excel模板", businessType = BusinessType.UPDATE)
    @GetMapping("/offShelfRS/{ids}")
    public AjaxResult offShelfRS(@PathVariable("ids") Long[] ids) {

        return toAjax(actSuppliesRoleService.offShelfRS(ids));

    }

}
