package com.lightinit.hsdatashow.controller.admin;

import com.lightinit.hsdatashow.common.RoleModulesUtilsPro;
import com.lightinit.hsdatashow.entity.SentryRole;
import com.lightinit.hsdatashow.model.admin.*;
import com.lightinit.hsdatashow.service.IRoleService;
import com.lightinit.hsdatashow.service.admin.ICommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通用
 */
@Controller
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
    private ICommonService commonService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private RoleModulesUtilsPro roleModulesUtilsPro;

    @RequestMapping(value = "role_tree_data.action")
    @ResponseBody
    public List<TreeModel> getRoleTreeData(Long selectedId){
        return commonService.queryRoleTreeData(selectedId);
    }

    @RequestMapping(value = "module_tree_data.action")
    @ResponseBody
//    public List<TreeModel> getModuleTreeData(@RequestParam(required = false, value = "selectedIds[]") Long[] selectedIds, @RequestParam Long roleId){
    public List<TreeModel> getModuleTreeData(@RequestBody RoleMgrModuleDataModel inputModel){
        List<Long> moduleIds = null;
        SentryRole sentryRole= roleService.QueryOne(inputModel.getRoleId());
        if(sentryRole.getParentid() != null && sentryRole.getParentid() > 0) {
            moduleIds = roleModulesUtilsPro.RoleHasModules(sentryRole.getParentid());
        }
        return commonService.queryModuleTreeData(moduleIds, inputModel.getSelectedModuleIds(), inputModel.getSelectedActionIds());
    }

}
