package com.usky.system.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessErrorCode;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.system.domain.SysMenu;
import com.usky.system.domain.SysMobileMenu;
import com.usky.system.domain.SysMobilePlatformMenu;
import com.usky.system.domain.constants.UserConstants;
import com.usky.system.mapper.SysTenantPlatformMapper;
import com.usky.system.service.ISysMenuService;
import com.usky.system.service.SysPlatformService;
import com.usky.system.service.vo.PlatformMenuVo;
import com.usky.system.service.vo.MobilePlatformMenuVO;
import com.usky.system.service.vo.SysPlatformVo;
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-18
 */
@RestController
@RequestMapping("/sysPlatform")
public class SysPlatformController {
    @Autowired
    private SysPlatformService sysPlatformService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysTenantPlatformMapper tenantPlatformMapper;

    /**
     * 平台挂管理-列表查询
     *
     * @param platformName 平台名称
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param page         当前页
     * @param size         每页条数
     * @param id           平台ID
     * @return
     */
    @GetMapping("/getPlatformList")
    public ApiResult<CommonPage<SysPlatformVo>> getPlatformList(@RequestParam(value = "platformName", required = false) String platformName,
                                                                @RequestParam(value = "startTime", required = false) String startTime,
                                                                @RequestParam(value = "endTime", required = false) String endTime,
                                                                @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                                                                @RequestParam(value = "size", required = false, defaultValue = "0") Integer size,
                                                                @RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        return ApiResult.success(sysPlatformService.getPlatformList(platformName, startTime, endTime, page, size, id));
    }

    /**
     * PC平台管理-菜单查询
     *
     * @param platformId 平台ID
     * @return
     */
    @GetMapping("/getPlatformMenu")
    public ApiResult<Map<String, Object>> getPlatformMenu(@RequestParam Long platformId) {

        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuListOne(userId);
        Map<String,Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", sysPlatformService.getPlatformMenu(platformId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ApiResult.success(ajax);
    }

    /**
     * 移动端平台管理-菜单查询
     *
     * @param platformId 平台ID
     * @return
     */
    @GetMapping("/getMobilePlatformMenu")
    public ApiResult<Map<String, Object>> getMobilePlatformMenu(@RequestParam Long platformId) {

        Long userId = SecurityUtils.getUserId();
        List<SysMobileMenu> menus = menuService.selectMobileMenuListOne(userId);
        Map<String,Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", sysPlatformService.getMobilePlatformMenu(platformId));
        ajax.put("menus", menuService.buildMobileMenuTreeSelect(menus));
        return ApiResult.success(ajax);
    }


    /**
     * PC平台管理-菜单编辑
     *
     * @param platformMenuVo
     * @return
     */
    @Log(title = "PC端应用菜单编辑", businessType = BusinessType.INSERT)
    @PostMapping("updatePlatformMenu")
    public ApiResult<Void> updatePlatformMenu(@RequestBody PlatformMenuVo platformMenuVo) {
        sysPlatformService.updatePlatformMenu(platformMenuVo);
        return ApiResult.success();
    }


    /**
     * 移动端平台管理-菜单编辑
     *
     * @param mobilePlatformMenuVO
     * @return
     */
    @Log(title = "移动端应用菜单编辑", businessType = BusinessType.INSERT)
    @PostMapping("updateMobilePlatformMenu")
    public ApiResult<Void> updateMobilePlatformMenu(@RequestBody MobilePlatformMenuVO mobilePlatformMenuVO) {
        sysPlatformService.updateMobilePlatformMenu(mobilePlatformMenuVO);
        return ApiResult.success();
    }

    /**
     * 删除应用
     */
    @Log(title = "删除应用", businessType = BusinessType.DELETE)
    @DeleteMapping("/{platformId}")
    public ApiResult<Void> remove(@PathVariable Integer platformId)
    {
        if (tenantPlatformMapper.checkPlatformUnique(platformId)>0)
        {
            return ApiResult.error("500", "删除应用失败，存在在用的租户单位");
        }
        sysPlatformService.deletePlatformById(platformId);
        return ApiResult.success();
    }
}

