package com.hzs.system.sys.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.sys.SysRoleMenuColumn;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.sys.param.RoleMenuColumnParam;
import com.hzs.system.sys.service.ISysMenuService;
import com.hzs.system.sys.service.ISysRoleMenuColumnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 角色菜单列维护控制器
 * @Author: jiang chao
 * @Time: 2023/6/7 16:46
 * @Classname: SysRoleMenuColumnController
 * @PackageName: com.hzs.system.sys.controller.manage
 */
@RestController
@RequestMapping("/manage/role-menu-column")
public class SysRoleMenuColumnController {

    @Autowired
    private ISysRoleMenuColumnService iSysRoleMenuColumnService;
    @Autowired
    private ISysMenuService iSysMenuService;

    /**
     * 获取菜单以及菜单列
     *
     * @return
     */
    @GetMapping("/all/{roleId}")
    public AjaxResult all(@PathVariable Long roleId) {
        return AjaxResult.success(iSysMenuService.buildMenuTree(iSysMenuService.queryMenuAndColumn(roleId)));
    }

    /**
     * 保存角色菜单列
     *
     * @param param 角色菜单列入参
     * @return
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody RoleMenuColumnParam param) {
        if (CollectionUtil.isEmpty(param.getRoleIdList())) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        iSysRoleMenuColumnService.saveData(param, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 根据菜单权限获取显示列
     *
     * @param perms 菜单权限（与前端确定的菜单控制唯一标识）
     * @return
     */
    @GetMapping("/get-column/{perms}")
    public AjaxResult getColumn(@PathVariable("perms") String perms) {
        List<SysRoleMenuColumn> menuColumnList = iSysRoleMenuColumnService.queryMenuColumn(perms, SecurityUtils.getUserId());
        // 只返回是否显示为是的字段名
        List<String> columnList = menuColumnList.stream().filter((column) -> EYesNo.YES.getIntValue() == column.getShowFlag()).map(SysRoleMenuColumn::getMenuColumn).collect(Collectors.toList());
        return AjaxResult.success(columnList);
    }

}
