package com.ruoyi.web.controller.business;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Industry;
import com.ruoyi.business.service.IIndustryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 行业Controller
 * 
 * @author xiatao
 * @date 2022-02-08
 */
@Controller
@RequestMapping("/business/industry")
public class IndustryController extends BaseController
{
    private String prefix = "business/industry";

    @Autowired
    private IIndustryService industryService;

    @RequiresPermissions("business:industry:view")
    @GetMapping()
    public String industry()
    {
        return prefix + "/industry";
    }

    /**
     * 查询行业树列表
     */
    @RequiresPermissions("business:industry:list")
    @PostMapping("/list")
    @ResponseBody
    public List<Industry> list(Industry industry)
    {
        List<Industry> list = industryService.selectIndustryList(industry);
        return list;
    }

    /**
     * 导出行业列表
     */
    @RequiresPermissions("business:industry:export")
    @Log(title = "行业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Industry industry)
    {
        List<Industry> list = industryService.selectIndustryList(industry);
        ExcelUtil<Industry> util = new ExcelUtil<Industry>(Industry.class);
        return util.exportExcel(list, "行业数据");
    }

    /**
     * 新增行业
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("industry", industryService.selectIndustryById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存行业
     */
    @RequiresPermissions("business:industry:add")
    @Log(title = "行业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Industry industry)
    {
        if(industry.getStatus() == null){
            industry.setStatus(0);
        }
        return toAjax(industryService.insertIndustry(industry));
    }

    /**
     * 修改行业
     */
    @RequiresPermissions("business:industry:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Industry industry = industryService.selectIndustryById(id);
        mmap.put("industry", industry);
        return prefix + "/edit";
    }

    /**
     * 修改保存行业
     */
    @RequiresPermissions("business:industry:edit")
    @Log(title = "行业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Industry industry)
    {
        if(industry.getStatus() == null){
            industry.setStatus(0);
        }
        return toAjax(industryService.updateIndustry(industry));
    }

    /**
     * 删除
     */
    @RequiresPermissions("business:industry:remove")
    @Log(title = "行业", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(industryService.deleteIndustryById(id));
    }

    /**
     * 选择行业树
     */
    @GetMapping(value = { "/selectIndustryTree/{id}", "/selectIndustryTree/" })
    public String selectIndustryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("industry", industryService.selectIndustryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载行业树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = industryService.selectIndustryTree();
        return ztrees;
    }
}
