package com.dcsoft.system.controller;

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

import com.dcsoft.common.core.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.system.domain.SysSpace;
import com.dcsoft.system.service.ISysSpaceService;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.utils.poi.ExcelUtil;

/**
 * 空间管理Controller
 *
 * @author nichun
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/space")
public class SysSpaceController extends BaseController
{
    @Autowired
    private ISysSpaceService sysSpaceService;

    /**
     * 查询空间管理列表
     */
    @RequiresPermissions("system:space:list")
    @GetMapping("/list")
    public AjaxResult list(SysSpace sysSpace)
    {
        List<SysSpace> list = sysSpaceService.selectSysSpaceList(sysSpace);
        return success(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @RequiresPermissions("system:space:list")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
    {
        List<SysSpace> depts = sysSpaceService.selectSysSpaceList(new SysSpace());
        depts.removeIf(d -> d.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""));
        return success(depts);
    }


    /**
     * 导出空间管理列表
     */
    @RequiresPermissions("system:space:export")
    @Log(title = "空间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSpace sysSpace)
    {
        List<SysSpace> list = sysSpaceService.selectSysSpaceList(sysSpace);
        ExcelUtil<SysSpace> util = new ExcelUtil<SysSpace>(SysSpace.class);
        util.exportExcel(response, list, "空间管理数据");
    }

    /**
     * 获取空间管理详细信息
     */
    @RequiresPermissions("system:space:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysSpaceService.selectSysSpaceById(id));
    }

    /**
     * 新增空间管理
     */
    @RequiresPermissions("system:space:add")
    @Log(title = "空间管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSpace sysSpace)
    {
        return toAjax(sysSpaceService.insertSysSpace(sysSpace));
    }

    /**
     * 修改空间管理
     */
    @RequiresPermissions("system:space:edit")
    @Log(title = "空间管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSpace sysSpace)
    {

        return toAjax(sysSpaceService.updateSysSpace(sysSpace));
    }

    /**
     * 获取空间树列表
     */
//    @RequiresPermissions("system:space:list")
    @GetMapping("/spaceTree")
    public AjaxResult spaceTree(SysSpace space)
    {
        return success(sysSpaceService.selectSpaceTreeList(space));
    }

    /**
     * 删除空间
     */
    @RequiresPermissions("system:space:remove")
    @Log(title = "空间管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysSpaceService.deleteSysSpaceByIds(ids));
    }


}
