package com.yunfeiyun.agmp.common.web.system.service.impl;

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.web.system.mapper.SysMenuConfigMapper;
import com.yunfeiyun.agmp.common.web.system.service.ISysConfigService;
import com.yunfeiyun.agmp.common.web.system.service.ISysMenuConfigService;
import com.yunfeiyun.agmp.common.core.domain.entity.SysMenu;
import com.yunfeiyun.agmp.common.core.domain.model.IBaseMenu;
import com.yunfeiyun.agmp.common.utils.ConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单 业务层处理
 *
 * @author 杨晓辉
 */
@Service
public class SysMenuConfigServiceImpl implements ISysMenuConfigService
{

    @Autowired
    private SysMenuConfigMapper menuConfigMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 根据用户查询系统菜单列表
     *
     * @param menuConfig
     * @return 菜单列表
     */
    @Override
    public List<SysMenuConfig> selectMenuConfigList(SysMenuConfig menuConfig)
    {
        return menuConfigMapper.selectMenuConfigList(menuConfig);
    }

    @Override
    public List<SysMenuConfig> selectMenuConfigByCfgId(String menucfgSys) {
        return menuConfigMapper.selectMenuConfigByCfgId(menucfgSys);
    }

    @Override
    public List<IBaseMenu> selectMenuConfig() {
        SysConfig menuCfgYml = configService.selectConfigByKey(ConfigUtils.ConfigKeyEnum.INIT_SYSMENU.getKey(),1);
        List<SysMenuConfig> menuCfgYmlJsonList = new Gson().fromJson(menuCfgYml.getConfigValue() , new TypeToken<List<SysMenuConfig>>(){}.getType());
        return menuCfgYmlJsonList.stream().filter(m-> m.isMenucfgEnabled()).map(SysMenu::new).collect(Collectors.toList());
    }

    @Override
    public List<String> selectMenuConfigIds() {
        List<IBaseMenu> menuCfgYml = selectMenuConfig();
        return menuCfgYml.stream().map(IBaseMenu::getMenuId).collect(Collectors.toList());
    }

    @Override
    public SysMenuConfig selectMenuConfigByMenuId(String menuId) {
        SysMenuConfig menuConfig = menuConfigMapper.selectMenuConfigByMenuId(menuId);
        if(menuConfig != null){
            SysConfig menuCfgYml = configService.selectConfigByKey(ConfigUtils.ConfigKeyEnum.INIT_SYSMENU.getKey(),1);
            List<SysMenuConfig> menuCfgYmlJsonList = new Gson().fromJson(menuCfgYml.getConfigValue() , new TypeToken<List<SysMenuConfig>>(){}.getType());
            SysMenuConfig cfg = menuCfgYmlJsonList.stream().filter(cfgItem -> cfgItem.getMenucfgId().equals(menuConfig.getMenucfgSys())).findAny().orElse(null);
            if(cfg != null){
                menuConfig.setSysName( cfg.getMenucfgSys() );
            }
        }
        return menuConfig;
    }


    @Override
    public List<SysMenuConfig> selectMenuConfigList() {
        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> cfglist = menuConfigMapper.selectMenuConfigList(null);
        return cfglist.stream().map(cfgItem -> {
            SysMenuConfig cfg = menuCfgYmlJsonList.stream().filter(ymlItem -> ymlItem.getMenucfgId().equals(cfgItem.getMenucfgSys())).findAny().orElse(null);
            if(cfg != null){
                cfgItem.setSysName( cfg.getMenucfgSys() );
            }
            return cfgItem;
        }).collect(Collectors.toList());
    }


    @Override
    public List<IBaseMenu> queryMenuBy(List<IBaseMenu> menus, String menuId) {
        List<SysMenuConfig> systemConfigList = selectMenuConfigList();
        if (menuId == null) {
            return menus.stream().peek(menuItem -> {
                SysMenuConfig cfg = systemConfigList.stream().filter(cfgItem -> cfgItem.getMenuId().equals(menuItem.getMenuId())).findAny().orElse(null);
                if(cfg != null) {
                    menuItem.setMenuSystemId( cfg.getSysName() );
                }
            }).collect(Collectors.toList());
        }else{
            SysMenuConfig cfg = systemConfigList.stream().filter(cfgItem -> cfgItem.getMenuId().equals(menuId)).findAny().orElse(null);
            if(cfg != null) {
                SysMenu  sysMenu = new SysMenu();
                sysMenu.setMenuSystemId(cfg.getMenucfgSys());
                return new ArrayList<IBaseMenu>(){{
                   add(sysMenu);
                }};
            }
            return null;
        }

    }

    /**
     * 新增保存菜单信息
     *
     * @param menuConfig 菜单信息
     * @return 结果
     */
    @Override
    public int insertMenuConfig(SysMenuConfig menuConfig)
    {
        return menuConfigMapper.insertMenuConfig(menuConfig);
    }

    /**
     * 修改保存菜单信息
     *
     * @param menuConfig 菜单信息
     * @return 结果
     */
    @Override
    public int updateMenuConfig(SysMenuConfig menuConfig)
    {
        return menuConfigMapper.updateMenuConfig(menuConfig);
    }

    /**
     * 删除菜单管理信息
     *
     * @param menucfgId 菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuConfigById(String menucfgId)
    {
        return menuConfigMapper.deleteMenuConfigById(menucfgId);
    }

    /**
     * 删除菜单管理信息
     *
     * @param menuId 菜单ID
     * @return 结果
     */
    @Override
    public int deleteMenuConfigByMenuId(String menuId){
        return menuConfigMapper.deleteMenuConfigByMenuId(menuId);
    }



}
