package com.yunfeiyun.agmp.common.web.system.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yunfeiyun.agmp.common.web.system.domain.SysConfig;
import com.yunfeiyun.agmp.common.web.system.domain.SysMenuConfig;
import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseMenu;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.utils.ConfigUtils;
import com.yunfeiyun.agmp.common.web.system.service.ISysConfigService;
import com.yunfeiyun.agmp.common.web.system.service.ISysMenuConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 菜单配置信息
 *
 * @author 杨晓辉
 */
@RestController
@RequestMapping("/system/menuconfig")
public class SysMenuConfigController extends BaseController
{

    @Autowired
    private ISysMenuConfigService menuConfigService;

    @Autowired
    private ISysConfigService configService;

    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysMenuConfig menuConfig)
    {
        SysConfig menuCfgYml = configService.selectConfigByKey(ConfigUtils.ConfigKeyEnum.INIT_SYSMENU.getKey(),1);
        List<SysMenuConfig> menuCfgYmlJsonList = new Gson().fromJson(menuCfgYml.getConfigValue() , new TypeToken<List<SysMenuConfig>>(){}.getType());
        List<SysMenuConfig> menuConfigList = menuConfigService.selectMenuConfigList(menuConfig);
        List<SysMenuConfig> menuConfigsNew = menuCfgYmlJsonList.stream().map(ymlCfg -> {
            ymlCfg.setMenuId( ymlCfg.getMenucfgId());
            ymlCfg.setMenuName( ymlCfg.getMenucfgSys());
            ymlCfg.setSysName( ymlCfg.getMenucfgSys() );
            ymlCfg.setMenuParentid( "-1");
            for(SysMenuConfig cfgItem : menuConfigList ) {

                if ( ymlCfg.isMenucfgEnabled() && ymlCfg.getMenucfgId().equals(cfgItem.getMenucfgSys())) {
                    cfgItem.setMenuParentid( ymlCfg.getMenucfgId());
                }
            }
            return ymlCfg;
        }).collect(Collectors.toList());
        menuConfigList.addAll(menuCfgYmlJsonList);
        return success(menuConfigList);
    }


    /**
     * 获取子系统列表
     */
    @PreAuthorize("@ss.hasPermi('system:menuconfig:select')")
    @GetMapping("/sys/list")
    public AjaxResult systemList()
    {
        List<IBaseMenu> systemConfigs = menuConfigService.selectMenuConfig();
        return success(systemConfigs);
    }

    /**
     * 获取菜单列表
     */
    @PreAuthorize("@ss.hasPermi('system:menuconfig:tree')")
    @GetMapping("/tree")
    public AjaxResult tree(String menucfgSys)
    {
        List<SysMenuConfig> menuConfigs = menuConfigService.selectMenuConfigByCfgId(menucfgSys);
        return success(menuConfigs);
    }


    /**
     * 新增菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menuconfig:add')")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysMenuConfig menuConfig)
    {
        return toAjax(menuConfigService.insertMenuConfig(menuConfig));
    }


    /**
     * 删除菜单
     */
    @PreAuthorize("@ss.hasPermi('system:menuconfig:remove')")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{menucfgId}")
    public AjaxResult remove(@PathVariable("menucfgId") String menucfgId)
    {
        return toAjax(menuConfigService.deleteMenuConfigById(menucfgId));
    }
}