package com.ruoyi.web.controller.category;

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

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

/**
 * 行业类别Controller
 *
 * @author ruoyi
 * @date 2024-04-23
 */
@RestController
@RequestMapping("/system/category")
public class IndustryCategoryController extends BaseController
{
    @Autowired
    private IIndustryCategoryService industryCategoryService;

    /**
     * 查询行业类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(IndustryCategory industryCategory)
    {
        //startPage();
        List<IndustryCategory> list = industryCategoryService.selectIndustryCategoryList(industryCategory);
        return getDataTable(list);
    }

    /**
     * 导出行业类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:category:export')")
    @Log(title = "行业类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IndustryCategory industryCategory)
    {
        List<IndustryCategory> list = industryCategoryService.selectIndustryCategoryList(industryCategory);
        ExcelUtil<IndustryCategory> util = new ExcelUtil<IndustryCategory>(IndustryCategory.class);
        util.exportExcel(response, list, "行业类别数据");
    }

    /**
     * 获取行业类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(industryCategoryService.selectIndustryCategoryByCategoryId(categoryId));
    }

    /**
     * 新增行业类别
     */
    @PreAuthorize("@ss.hasPermi('system:category:add')")
    @Log(title = "行业类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IndustryCategory industryCategory)
    {
        return toAjax(industryCategoryService.insertIndustryCategory(industryCategory));
    }

    /**
     * 修改行业类别
     */
    @PreAuthorize("@ss.hasPermi('system:category:edit')")
    @Log(title = "行业类别", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody IndustryCategory industryCategory)
    {
        return toAjax(industryCategoryService.updateIndustryCategory(industryCategory));
    }

    /**
     * 删除行业类别
     */
    @PreAuthorize("@ss.hasPermi('system:category:remove')")
    @Log(title = "行业类别", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(industryCategoryService.deleteIndustryCategoryByCategoryIds(categoryIds));
    }

    /**
     * 获取行业类别树列表
     */
    @GetMapping("/categoryTree")
    public AjaxResult categoryTree(IndustryCategory industryCategory)
    {
        return success(industryCategoryService.selectCategoryTreeList(industryCategory));
    }

    /**
     * 查询行业类别列表（排除节点）
     */
    @GetMapping("/list/exclude/{categoryId}")
    public AjaxResult excludeChild(@PathVariable(value = "categoryId", required = false) Long categoryId)
    {
        List<IndustryCategory> list = industryCategoryService.selectIndustryCategoryList(new IndustryCategory());
        list.removeIf(d -> d.getCategoryId().intValue() == categoryId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), categoryId + ""));
        return success(list);
    }
}
