package com.keyware.shandan.bianmu.controller;

import com.keyware.shandan.bianmu.entity.DirectoryVo;
import com.keyware.shandan.bianmu.service.DirPermissionService;
import com.keyware.shandan.bianmu.service.DirectoryService;
import com.keyware.shandan.bianmu.service.DirectoryTreeService;
import com.keyware.shandan.bianmu.utils.DirectoryUtil;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.entity.TreeVo;
import com.keyware.shandan.common.enums.SystemTypes;
import com.keyware.shandan.common.util.StreamUtil;
import com.keyware.shandan.common.util.TreeUtil;
import com.keyware.shandan.frame.config.security.SecurityUtil;
import com.keyware.shandan.system.entity.SysSetting;
import com.keyware.shandan.system.entity.SysUser;
import com.keyware.shandan.system.utils.SysSettingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资源目录共用控制器
 *
 * @author GuoXin
 * @since 2021/12/15
 */
@RestController
@RequestMapping("/business/directory")
public class DirectoryCommonController {

    @Autowired
    private DirectoryService directoryService;
    @Autowired
    private DirectoryTreeService directoryTreeService;

    @Autowired
    private DirPermissionService dirPermissionService;

    /**
     * 获取树形结构的资源目录数据
     *
     * @param id           父级目录ID
     * @param reviewStatus 审核状态
     * @param all          是否包含数据资源
     * @return 结果集
     */
    @GetMapping("/tree")
    public Result<List<TreeVo>> tree(String id, String reviewStatus, boolean all, String permis, boolean allChild, int busType) {
        return Result.of(directoryTreeService.tree(id, reviewStatus, all, permis, allChild, busType));
    }

    @GetMapping("/details/{id}")
    public ModelAndView details(ModelAndView mav, @PathVariable String id) {
        mav.setViewName("business/directory/details");

        DirectoryVo dir = directoryService.getById(id);
        mav.addObject("directory", dir);

        SysSetting bianmuSetting = SysSettingUtil.getSysSetting(SystemTypes.BIANMU.name());
        mav.addObject("bianmuAddress", bianmuSetting.getSysAddress());

        // 获取父级目录
        List<DirectoryVo> dirList = directoryService.parentLists(dir);
        dirList.addAll(directoryService.childrenLists(dir));
        dirList.add(dir);

        List<TreeVo> treeVoList = StreamUtil.as(dirList).map(DirectoryUtil::dir2Tree).toList();
        mav.addObject("treeData", TreeUtil.buildDirTree(treeVoList, dir.getParentId()));
        return mav;
    }

    @GetMapping("/permis")
    public Result<Object> getUserPermisDirs(){
        dirPermissionService.refreshCache();
        SysUser user = SecurityUtil.getLoginSysUser();
        Map<String, Object> data = new HashMap<>();
        data.put("readIds", dirPermissionService.getByReadPermis(user));
        data.put("writeIds", dirPermissionService.getByWritePermis(user));
        return Result.of(data);
    }
}
