package com.ruoyi.cms.controller.cms;

import java.util.List;

import com.ruoyi.common.core.domain.entity.Industry;
import com.ruoyi.common.utils.poi.ExcelUtil;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.cms.service.IIndustryService;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 行业Controller
 * 
 * @author LishunDong
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/cms/industry")
@Api(tags = "后台：行业管理")
public class IndustryController extends BaseController
{
    @Autowired
    private IIndustryService industryService;

    /**
     * 查询行业列表
     */
    @ApiOperation("查询行业列表")
    @PreAuthorize("@ss.hasPermi('cms:industry:list')")
    @GetMapping("/list")
    public TableDataInfo list(Industry industry)
    {
        startPage();
        List<Industry> list = industryService.selectIndustryList(industry);
        return getDataTable(list);
    }
    @ApiOperation("行业树结构")
    @PreAuthorize("@ss.hasPermi('cms:industry:list')")
    @GetMapping("/treeselect")
    public AjaxResult treeselect(Industry industry)
    {
        List<Industry> industryList = industryService.selectIndustryList(industry);
        return success(industryService.buildIndustryTreeSelect(industryList));
    }
    /**
     * 导出行业列表
     */
    @ApiOperation("导出行业列表")
    @PreAuthorize("@ss.hasPermi('cms:industry:export')")
    @Log(title = "行业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Industry industry)
    {
        List<Industry> list = industryService.selectIndustryList(industry);
        ExcelUtil<Industry> util = new ExcelUtil<Industry>(Industry.class);
        util.exportExcel(response, list, "行业数据");
    }

    /**
     * 获取行业详细信息
     */
    @ApiOperation("获取行业详细信息")
    @PreAuthorize("@ss.hasPermi('cms:industry:query')")
    @GetMapping(value = "/{industryId}")
    public AjaxResult getInfo(@PathVariable("industryId") Long industryId)
    {
        return success(industryService.selectIndustryByIndustryId(industryId));
    }

    /**
     * 新增行业
     */
    @ApiOperation("新增行业")
    @PreAuthorize("@ss.hasPermi('cms:industry:add')")
    @Log(title = "行业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Industry industry)
    {
        return toAjax(industryService.insertIndustry(industry));
    }

    /**
     * 修改行业
     */
    @ApiOperation("修改行业")
    @PreAuthorize("@ss.hasPermi('cms:industry:edit')")
    @Log(title = "行业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Industry industry)
    {
        return toAjax(industryService.updateIndustry(industry));
    }

    /**
     * 删除行业
     */
    @ApiOperation("删除行业")
    @PreAuthorize("@ss.hasPermi('cms:industry:remove')")
    @Log(title = "行业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{industryId}")
    public AjaxResult remove(@PathVariable Long industryId)
    {
        if (industryService.hasChildByIndustryId(industryId))
        {
            return warn("存在子行业,不允许删除");
        }
        Long[] industryIds = {industryId};
        return toAjax(industryService.deleteIndustryByIndustryIds(industryIds));
    }
    @GetMapping("/import")
    public AjaxResult importData()
    {
        industryService.importData();
        return success();
    }

}
