package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.system.domain.ActClassification;
import com.ruoyi.system.service.IActClassificationService;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分配信息Controller
 * 
 * @author ruoyi
 * @date 2023-08-11
 */
@RestController
@RequestMapping("/ActClassification/ActClassification")
public class ActClassificationController extends BaseController
{
    @Autowired
    private IActClassificationService actClassificationService;

    /**
     * 查询上级分类
     * */
    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:savepid')")
    @GetMapping("/savepid/{sskId}")
    @ResponseBody
    public TableDataInfo saveSid(@PathVariable("sskId") Long sskId){

        List<ActClassification> options = actClassificationService.savePid(sskId);
        return getDataTable(options);

    }

    /**
     * 查询分配信息列表
     */
    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActClassification actClassification)
    {
        startPage();
        List<ActClassification> list = actClassificationService.selectActClassificationList(actClassification);
        return getDataTable(list);
    }

    /**
     * 导出分配信息列表
     */
    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:export')")
    @Log(title = "分配信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActClassification actClassification)
    {
        List<ActClassification> list = actClassificationService.selectActClassificationList(actClassification);
        ExcelUtil<ActClassification> util = new ExcelUtil<ActClassification>(ActClassification.class);
        return util.exportExcel(list, "分配信息数据");
    }

    /**
     * 获取分配信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(actClassificationService.selectActClassificationById(id));
    }

    /**
     * 新增分配信息
     */
    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:add')")
    @Log(title = "分配信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActClassification actClassification)
    {
        return toAjax(actClassificationService.insertActClassification(actClassification));
    }

    /**
     * 修改分配信息
     */
    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:edit')")
    @Log(title = "分配信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActClassification actClassification)
    {
        return toAjax(actClassificationService.updateActClassification(actClassification));
    }

    /**
     * 删除分配信息
     */
    @PreAuthorize("@ss.hasPermi('ActClassification :ActClassification:remove')")
    @Log(title = "分配信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(actClassificationService.deleteActClassificationByIds(ids));
    }

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

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

        if (actClassificationService.findId(id).size()>0){

            return AjaxResult.success("该物料下存在关联物料，请先删除关联物料！！！","操作失败");

        }

        actClassificationService.setDisable(id,status);

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

    }

//    @PreAuthorize("@ss.hasPermission('ActClassification:ActClassification:GetAll')")
//    @GetMapping("/all")
//    public List<ActClassification> getAllActClassifications() {
//        return actClassificationService.getAllActClassifications();
//    }
//
//    @PreAuthorize("@ss.hasPermission('ActClassification:ActClassification:GetSub')")
//    @GetMapping("/sub/{parentId}")
//    public List<ActClassification> getSubActClassifications(@PathVariable("parentId") int parentId) {
//        return actClassificationService.getSubActClassifications(parentId);
//    }

    @PreAuthorize("@ss.hasPermi('ActClassification:ActClassification:SubActDy')")
    @GetMapping("/SunDy/{sskId}")
    public List<ActClassification> getSubActClassDy(@PathVariable("sskId") int sskId) {

        return actClassificationService.getSubActClassDy(sskId);

    }

}
