/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.base.dto.RoleMenuDTO;
import com.tofly.base.entity.Menu;
import com.tofly.base.entity.Role;
import com.tofly.base.entity.RoleMenu;
import com.tofly.base.mapper.RoleMenuMapper;
import com.tofly.base.service.RoleMenuService;
import com.tofly.base.service.RoleService;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 角色菜单表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@Service("roleMenuService")
@Slf4j
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private RoleService roleService;
    @Override
    @Transactional
    public boolean updateBatch(RoleMenu[] roleMenus) {
        List<RoleMenu> list= Arrays.asList(roleMenus);
        boolean b=this.remove(Wrappers.<RoleMenu>lambdaQuery()
        .eq(RoleMenu::getRoleId,roleMenus[0].getRoleId()));
        list.forEach(roleMenu -> {
            this.save(roleMenu);
        });
        return true;
    }

    @Override
    public ResultRespone roleMenuBound(RoleMenuDTO roleMenuDTO) {
        log.info("更新角色id{}",roleMenuDTO.getId());
        // 删除对应角色菜单关联信息
        roleMenuService.remove(new QueryWrapper<RoleMenu>().lambda().eq(RoleMenu::getRoleId,roleMenuDTO.getId()));
        //判断当前角色是否已经有绑定的菜单，如果有那么需要删除
//        List<RoleMenu> roleMenus = roleMenuService.getRoleMenu(roleMenuDTO.getId());
//        if (!CollectionUtils.isEmpty(roleMenus)){
//            List<Long> ids = roleMenus.stream().map(RoleMenu::getId).collect(Collectors.toList());
//            //向下取整
//            int down = ids.size()/1000;
//            //向下取整
//            //向上取整 , 获得次数
////            int count = (int) Math.ceil(Double.valueOf(ids.size())/1000d);
//            if (down<1){
//                roleMenuService.removeByIds(ids);
//            }
//            for (int i=1;i<down;i++){
//                    List<Long> two = ids.subList(i*1000,(i+1)*1000);
//                    roleMenuService.removeByIds(two);
//            }
//            if (ids.size()<1000){
//                roleMenuService.delete(ids);
//            }else if (ids.size()>1000&&ids.size()<2000){
//                List<Long> one = ids.subList(0,1000);
//                roleMenuService.delete(one);
//                List<Long> two = ids.subList(1001,ids.size());
//                roleMenuService.delete(two);
//            }else if (ids.size()>2000&&ids.size()<3000){
//                List<Long> one = ids.subList(0,1000);
//                roleMenuService.delete(one);
//                List<Long> two = ids.subList(1001,2000);
//                roleMenuService.delete(two);
//                List<Long> three = ids.subList(2001,ids.size());
//                roleMenuService.delete(three);
//            }
//            }
        //需要判断角色是否重复，重复的角色不能重建
        if (roleMenuDTO.getRoleName()!=null) {
            Boolean b = roleService.selectByRoleName(roleMenuDTO.getRoleName());
            if (!b) {
                return ResultRespone.failed("输入的角色名称已存在");
            }
            Role role = new Role();
            role.setRoleName(roleMenuDTO.getRoleName());
            if (roleMenuDTO.getRoleDesc()!=null){
                role.setRoleDesc(roleMenuDTO.getRoleDesc());
            }
            role.setId(roleMenuDTO.getId());
            role.setTreeValue(roleMenuDTO.getMenuIds());
            roleService.updateById(role);
        }else {
            Role role = new Role();
            if (roleMenuDTO.getRoleDesc()!=null){
                role.setRoleDesc(roleMenuDTO.getRoleDesc());
            }
            role.setId(roleMenuDTO.getId());
            role.setTreeValue(roleMenuDTO.getMenuIds());
            roleService.updateById(role);
        }
        if(!StringUtil.isEmpty(roleMenuDTO.getMenuIds())){
            String[] menu=roleMenuDTO.getMenuIds().split(",");
            RoleMenu roleMenu;
            for(String s:menu){
                roleMenu = new RoleMenu();
                roleMenu.setMenuId(Long.valueOf(s));
                roleMenu.setRoleId(roleMenuDTO.getId());
                roleMenuService.save(roleMenu);
            }
        }
        return ResultRespone.success();
    }

    @Override
    public List<Long> getMenuIds(List<Long> roles) {
        List<RoleMenu> roleMenus = new ArrayList<>();
        for (Long l :roles){
            QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("ROLE_ID",l);
            List<RoleMenu> roleMenuList = baseMapper.selectList(wrapper);
            roleMenus.addAll(roleMenuList);
        }
        List<Long> menu = roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
        return menu;
    }

    @Override
    public List<RoleMenu> getRoleMenu(Long id) {
        QueryWrapper<RoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ROLE_ID",id);
        List<RoleMenu> roleMenus = baseMapper.selectList(queryWrapper);
        return roleMenus;
    }

    @Override
    public void delete(List<Long> ids ) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public List<RoleMenu> getAllRoleId(Long id) {
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("MENU_ID",id);
        List<RoleMenu> list = baseMapper.selectList(wrapper);
        return list;
    }
}
