package com.ruoyi.web.controller.system;

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

import com.ruoyi.common.constant.UserConstants;
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.common.core.domain.entity.ColumnNavigationBar;
import com.ruoyi.system.service.IColumnNavigationBarService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 栏目导航Controller
 * 
 * @author boman
 * @date 2021-12-24
 */
@RestController
@RequestMapping("/system/column")
public class ColumnNavigationBarController extends BaseController
{
    @Autowired
    private IColumnNavigationBarService columnNavigationBarService;

    /**
     * 查询栏目导航列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:list')")
    @GetMapping("/list")
    public AjaxResult list(ColumnNavigationBar columnNavigationBar)
    {
        List<ColumnNavigationBar> list = columnNavigationBarService.selectColumnNavigationBarList(columnNavigationBar);
        return AjaxResult.success(list);
    }


    /**
     * 查询栏目导航列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/list/exclude/{columnId}")
    public AjaxResult excludeChild(@PathVariable(value = "columnId", required = false) Long columnId)
    {
        List<ColumnNavigationBar> columnNavigationBars = columnNavigationBarService.selectColumnNavigationBarList(new ColumnNavigationBar());
        Iterator<ColumnNavigationBar> it = columnNavigationBars.iterator();
        while (it.hasNext())
        {
            ColumnNavigationBar d = (ColumnNavigationBar) it.next();
            if (d.getColumnId().intValue() == columnId
                    || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), columnId + ""))
            {
                it.remove();
            }
        }
        return AjaxResult.success(columnNavigationBars);
    }

    /**
     * 导出栏目导航列表
     */
    @PreAuthorize("@ss.hasPermi('system:column:export')")
    @Log(title = "栏目导航", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ColumnNavigationBar columnNavigationBar)
    {
        List<ColumnNavigationBar> list = columnNavigationBarService.selectColumnNavigationBarList(columnNavigationBar);
        ExcelUtil<ColumnNavigationBar> util = new ExcelUtil<ColumnNavigationBar>(ColumnNavigationBar.class);
        util.exportExcel(response, list, "栏目导航数据");
    }

    /**
     * 获取栏目导航详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:column:query')")
    @GetMapping(value = "/{columnId}")
    public AjaxResult getInfo(@PathVariable("columnId") Long columnId)
    {
        ColumnNavigationBar columnNavigationBar = columnNavigationBarService.selectColumnNavigationBarByColumnId(columnId);
        return AjaxResult.success(columnNavigationBar);
    }

    /**
     * 获取栏目导航下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(ColumnNavigationBar columnNavigationBar)
    {
        List<ColumnNavigationBar> columnNavigationBars = columnNavigationBarService.selectColumnNavigationBarList(columnNavigationBar);
        return AjaxResult.success(columnNavigationBarService.buildColumnTreeSelect(columnNavigationBars));
    }
    /**
     * 新增栏目导航
     */
    @PreAuthorize("@ss.hasPermi('system:column:add')")
    @Log(title = "栏目导航", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ColumnNavigationBar columnNavigationBar)
    {
        if (UserConstants.NOT_UNIQUE.equals(columnNavigationBarService.checkColumnNameUnique(columnNavigationBar)))
        {
            return AjaxResult.error("新增栏目导航'" + columnNavigationBar.getColumnName() + "'失败，栏目导航名称已存在");
        }
        columnNavigationBar.setCreateBy(getUsername());
        return toAjax(columnNavigationBarService.insertColumnNavigationBar(columnNavigationBar));
    }

    /**
     * 修改栏目导航
     */
    @PreAuthorize("@ss.hasPermi('system:column:edit')")
    @Log(title = "栏目导航", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ColumnNavigationBar columnNavigationBar)
    {
        if (UserConstants.NOT_UNIQUE.equals(columnNavigationBarService.checkColumnNameUnique(columnNavigationBar)))
        {
            return AjaxResult.error("修改栏目导航'" + columnNavigationBar.getColumnName() + "'失败，栏目导航名称已存在");
        }
        else if (columnNavigationBar.getParentId() != null && columnNavigationBar.getParentId().equals(columnNavigationBar.getColumnId()))
        {
            return AjaxResult.error("修改栏目导航'" + columnNavigationBar.getColumnName() + "'失败，上级栏目导航不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, columnNavigationBar.getStatus())
                && columnNavigationBarService.selectNormalChildrenColumnById(columnNavigationBar.getColumnId()) > 0)
        {
            return AjaxResult.error("该栏目导航包含未停用的栏目导航！");
        }
        columnNavigationBar.setUpdateBy(getUsername());
        return toAjax(columnNavigationBarService.updateColumnNavigationBar(columnNavigationBar));
    }

    /**
     * 删除栏目导航
     */
    @PreAuthorize("@ss.hasPermi('system:column:remove')")
    @Log(title = "栏目导航", businessType = BusinessType.DELETE)
	@DeleteMapping("/{columnId}")
    public AjaxResult remove(@PathVariable Long columnId)
    {
        if (columnNavigationBarService.hasChildByColumnId(columnId))
        {
            return AjaxResult.error("存在下级部门,不允许删除");
        }
        return toAjax(columnNavigationBarService.deleteColumnNavigationBarByColumnId(columnId));
    }
}
