package com.yunzhi.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.service.ITaRoleService;
import com.yunzhi.marketing.entity.*;
import com.yunzhi.marketing.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 角色表 角色表，每个org的角色可以单独设置。但是都共有一个admin角色。这个角色在创建用户的时候创建好了 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@Service
public class TaRoleServiceImpl extends ServiceImpl<TaRoleMapper, TaRole> implements ITaRoleService {
    @Autowired
    TaRoleMapper taRoleMapper;

    @Autowired
    TaRoleMenuMapper taRoleMenuMapper;

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Autowired
    SysButtonInMenuMapper sysButtonInMenuMapper;

    @Autowired
    TaRoleButtonMapper taRoleButtonMapper;

    @Autowired
    private TaAdvancedControllerMapper taAdvancedControllerMapper;

    @Autowired
    private SysAdvancedConfigRelationMapper sysAdvancedConfigRelationMapper;

    @Override
    public List<TaRole> getRolesByUser(Integer userId) {
        return taRoleMapper.getRolesByUser(userId);
    }

    @Override
    public TaRole taRoleDetails(Integer id,Integer orgId) {
        TaRole taRole = taRoleMapper.selectById(id);
        // 查询当前角色的所有菜单
        QueryWrapper<TaRoleMenu> taRoleMenuQuery= new QueryWrapper<>();
        taRoleMenuQuery.eq("role_id",id);
       List<TaRoleMenu> taRoleMenusList = taRoleMenuMapper.selectList(taRoleMenuQuery);

        //添加高级配置的过滤条件
        List<TaAdvancedController> taAdvancedControllerList = taAdvancedControllerMapper.selectList(new QueryWrapper<TaAdvancedController>().eq("org_id", orgId));

        //需要过滤的菜单
        QueryWrapper<SysAdvancedConfigRelation> sysAdvancedConfigRelationQueryWrapper = new QueryWrapper<>();
        sysAdvancedConfigRelationQueryWrapper.eq("target_type", CommConstant.ADVANCED_TYPE_MENU);
        sysAdvancedConfigRelationQueryWrapper.notIn(taAdvancedControllerList.size() > 0,"advanced_config_id",taAdvancedControllerList.stream().map(TaAdvancedController::getAdvancedConfigId).collect(Collectors.toList()));
        List<SysAdvancedConfigRelation> sysAdvancedConfigRelations = sysAdvancedConfigRelationMapper.selectList(sysAdvancedConfigRelationQueryWrapper);

       // 同过menu_id查询当前菜单
         List<SysMenu> sysMenuList = new ArrayList<>();
        for (TaRoleMenu taRoleMenu :taRoleMenusList){
            SysMenu sysMenu = sysMenuMapper.selectById(taRoleMenu.getMenuId());
            sysMenuList.add(sysMenu);
            for (SysAdvancedConfigRelation sysAdvancedConfigRelation : sysAdvancedConfigRelations){
                if (taRoleMenu.getMenuId().equals(Integer.valueOf(sysAdvancedConfigRelation.getTargetId()))){
                    sysMenuList.remove(sysMenu);
                }
            }
        }
        // 查询按钮
        QueryWrapper<TaRoleButton> taRoleButtonQuery= new QueryWrapper<>();
        taRoleButtonQuery.eq("role_id",id);
        List<TaRoleButton> taRoleButtonList= taRoleButtonMapper.selectList(taRoleButtonQuery);
        List<SysButtonInMenu> sysButtonInArr = new ArrayList<>();
        for(TaRoleButton taRoleButton :taRoleButtonList){
            QueryWrapper<SysButtonInMenu> sysButtonInMenuQuery = new QueryWrapper<>();
            sysButtonInMenuQuery.eq("btn_id",taRoleButton.getBtnId());
            SysButtonInMenu sysButtonInMenuList= sysButtonInMenuMapper.selectOne(sysButtonInMenuQuery);
            sysButtonInArr.add(sysButtonInMenuList);
        }
        taRole.setSysButtonInMenu(sysButtonInArr);
        taRole.setSysMenuList(sysMenuList);
        return taRole;
    }
}