package com.usky.system.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.system.domain.SysMenu;
import com.usky.system.domain.SysMobileMenu;
import com.usky.system.domain.SysTenant;
import com.usky.system.service.ISysMenuService;
import com.usky.system.service.SysPlatformMenuService;
import com.usky.system.service.SysTenantService;
import com.usky.system.service.vo.TenantMenuVo;
import com.usky.system.service.vo.MobileTenantMenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 租户管理-权限配置
 *
 * @author ya
 * @since 2022-05-05
 */
@RestController
@RequestMapping("/authorityConfig")
public class AuthorityConfigController {

    @Autowired
    private SysPlatformMenuService sysPlatformMenuService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysTenantService sysTenantService;


    /**
     * 租户管理-权限配置-目录查询
     *
     * @param platformId 平台ID
     * @param tenantId   租户ID
     * @return
     */
    @GetMapping("getMenuList")
    public ApiResult<Map<String, Object>> getMenuList(@RequestParam(value = "platformId") Integer platformId,
                                                      @RequestParam(value = "tenantId") Integer tenantId) {
        return ApiResult.success(sysPlatformMenuService.getMenuList(platformId, tenantId));
    }


    /**
     * PC租户管理-权限配置-目录查询
     *
     * @param platformId 平台ID
     * @param tenantId   租户ID
     * @return
     */
    @GetMapping("getAuthorityConfigMenuList")
    public ApiResult<Map<String, Object>> getAuthorityConfigMenuList(@RequestParam(value = "platformId") Integer platformId,
                                                                     @RequestParam(value = "tenantId") Integer tenantId) {
        List<SysMenu> menus =sysPlatformMenuService.getPlatformMenuList(platformId);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", sysPlatformMenuService.selectPlatformMenu(tenantId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ApiResult.success(ajax);
    }

    /**
     * 移动端租户管理-权限配置-目录查询
     *
     * @param platformId 平台ID
     * @param tenantId   租户ID
     * @return
     */
    @GetMapping("getMobileConfigMenuList")
    public ApiResult<Map<String, Object>> getMobileConfigMenuList(@RequestParam(value = "platformId") Integer platformId,
                                                                     @RequestParam(value = "tenantId") Integer tenantId) {
        List<SysMobileMenu> menus =sysPlatformMenuService.getMobilePlatformMenuList(tenantId);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", sysPlatformMenuService.selectMobilePlatformMenu(tenantId));
        ajax.put("menus", menuService.buildMobileMenuTreeSelect(menus));
        return ApiResult.success(ajax);
    }

    /**
     * 手机端APP租户管理-权限配置-目录查询
     *
     * @param domain 域名
     * @return
     */
    @GetMapping("getAppConfigMenuList")
    public ApiResult<Map<String, Object>> getAppConfigMenuList(@RequestParam(value = "domain") String domain) {
        List<SysTenant> list = sysTenantService.getAppData(domain);

        Integer tenantId = list.get(0).getId();
        String platformId = list.get(0).getSystemName();
        List<SysMobileMenu> menus =sysPlatformMenuService.getMobilePlatformMenuList(Integer.parseInt(platformId));
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", sysPlatformMenuService.selectMobilePlatformMenu(tenantId));
        ajax.put("menus", menuService.buildMobileMenuTreeSelect(menus));
        return ApiResult.success(ajax);
    }

    /**
     * 租户管理-权限配置-目录保存
     *
     * @param tenantMenuVo
     * @return
     */
//    @Log(title = "权限配置", businessType = BusinessType.UPDATE)
    @PostMapping("updateMenu")
    public ApiResult<Void> updateMenu(@RequestBody TenantMenuVo tenantMenuVo) {
        sysPlatformMenuService.updateMenu(tenantMenuVo);
        return ApiResult.success();
    }

    /**
     * 移动端租户管理-权限配置-目录保存
     *
     * @param mobileTenantMenuVO
     * @return
     */
//    @Log(title = "权限配置", businessType = BusinessType.UPDATE)
    @PostMapping("updateMobileMenu")
    public ApiResult<Void> updateMobileMenu(@RequestBody MobileTenantMenuVO mobileTenantMenuVO) {
        sysPlatformMenuService.updateMobileMenu(mobileTenantMenuVO);
        return ApiResult.success();
    }


}

