package com.ruoyi.web.controller.proposal;

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

import com.ruoyi.common.core.domain.entity.CategoryProposal;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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.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.service.ICategoryProposalService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 提案类别Controller
 *
 * @author boman
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/category/proposal")
public class CategoryProposalController extends BaseController
{
    @Autowired
    private ICategoryProposalService categoryProposalService;

/**
 * 查询提案类别列表
 */
@PreAuthorize("@ss.hasPermi('category:proposal:list')")
@GetMapping("/list")
    public TableDataInfo list(CategoryProposal categoryProposal)
    {
        //startPage();
        List<CategoryProposal> list = categoryProposalService.selectCategoryProposalList(categoryProposal);
        return getDataTable(list);
    }

    /**
     * 导出提案类别列表
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:export')")
    @Log(title = "提案类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryProposal categoryProposal)
    {
        List<CategoryProposal> list = categoryProposalService.selectCategoryProposalList(categoryProposal);
        ExcelUtil<CategoryProposal> util = new ExcelUtil<CategoryProposal>(CategoryProposal.class);
        util.exportExcel(response, list, "提案类别数据");
    }

    /**
     * 获取提案类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(categoryProposalService.selectCategoryProposalByCategoryId(categoryId));
    }

    /**
     * 新增提案类别
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:add')")
    @Log(title = "提案类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryProposal categoryProposal)
    {
        return toAjax(categoryProposalService.insertCategoryProposal(categoryProposal));
    }

    /**
     * 修改提案类别
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:edit')")
    @Log(title = "提案类别", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody CategoryProposal categoryProposal)
    {
        return toAjax(categoryProposalService.updateCategoryProposal(categoryProposal));
    }

    /**
     * 删除提案类别
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:remove')")
    @Log(title = "提案类别", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(categoryProposalService.deleteCategoryProposalByCategoryIds(categoryIds));
    }

    /**
     * 获取提案类别树列表
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:treelist')")
    @GetMapping("/categoryTree")
    public AjaxResult categoryTree(CategoryProposal categoryProposal)
    {
        return success(categoryProposalService.selectCategoryTreeList(categoryProposal));
    }

    /**
     * 查询提案类别列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('category:proposal:list')")
    @GetMapping("/list/exclude/{categoryId}")
    public AjaxResult excludeChild(@PathVariable(value = "categoryId", required = false) Long categoryId)
    {
        List<CategoryProposal> list = categoryProposalService.selectCategoryProposalList(new CategoryProposal());
        list.removeIf(d -> d.getCategoryId().intValue() == categoryId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), categoryId + ""));
        return success(list);
    }
}
