package com.ssyf.module.auth.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssyf.module.auth.domain.entity.MenuPermission;
import com.ssyf.module.auth.domain.entity.UserPermissionRelation;
import com.ssyf.module.auth.mapper.UserPermissionRelationMapper;
import com.ssyf.module.auth.service.UserPermissionRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author auth
 * @since 2023-01-12
 */
@Service
public class UserPermissionRelationServiceImpl extends ServiceImpl<UserPermissionRelationMapper, UserPermissionRelation> implements UserPermissionRelationService {

    @Override
    public void removeByPermissionIds(List<String> permissionUids) {
        remove(new LambdaQueryWrapper<UserPermissionRelation>().in(UserPermissionRelation::getPermissionUid ,permissionUids));
    }

    @Override
    public void removeByRoleUids(List<String> roleUids) {
        remove(new LambdaQueryWrapper<UserPermissionRelation>().in(UserPermissionRelation::getRoleUid ,roleUids));
    }

    @Override
    public void removeByUserUids(List<String> userUids) {
        remove(new LambdaQueryWrapper<UserPermissionRelation>().in(UserPermissionRelation::getUserUid ,userUids));
    }

    @Override
    public List<UserPermissionRelation> getListByUserUids(List<String> userUids) {
        if(CollectionUtil.isEmpty(userUids)){
            return Collections.emptyList();
        }
        return list(new LambdaQueryWrapper<UserPermissionRelation>().in(UserPermissionRelation::getUserUid ,userUids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignOrRemoveOldByRoleUid(String roleUid, List<String> userUids, List<MenuPermission> menuPermissionList) {
        //删除旧数据
        this.removeByRoleUid(roleUid);

        //重建关联数据
        if(CollectionUtil.isEmpty(userUids) || CollectionUtil.isEmpty(menuPermissionList)){
            return;
        }
        List<UserPermissionRelation> relationList = createRelation(userUids ,Arrays.asList(roleUid) ,menuPermissionList);
        if(CollectionUtil.isEmpty(relationList)){
            return;
        }
        List<List<UserPermissionRelation>> pages = CollectionUtil.split(relationList ,1000);
        for (List<UserPermissionRelation> page : pages) {
            this.baseMapper.insertBatch(page);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void assignOrRemoveOldByUserUid(String userUid, List<String> roleUids, List<MenuPermission> menuPermissionList) {
        //删除旧有数据
        this.removeByUserUid(userUid);

        //重建关联
        if(CollectionUtil.isEmpty(roleUids) || CollectionUtil.isEmpty(menuPermissionList)){
            return;
        }
        List<UserPermissionRelation> relationList = createRelation(Arrays.asList(userUid) ,roleUids ,menuPermissionList);

        if(CollectionUtil.isEmpty(relationList)){
            return;
        }
        //TODO 需要优化
        this.saveBatch(relationList);
    }

    @Override
    public void updateByPermission(String oldPermission, String permission) {
        UserPermissionRelation relation = new UserPermissionRelation();
        relation.setPermission(permission);
        LambdaQueryWrapper<UserPermissionRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPermissionRelation::getPermission ,oldPermission);
        this.update(relation ,queryWrapper);
    }

    private List<UserPermissionRelation> createRelation(List<String> userUids, List<String> roleUids, List<MenuPermission> menuPermissionList){
        List<UserPermissionRelation> relationList = new ArrayList<>();
        userUids.forEach(userUid->{
            roleUids.forEach(roleUid->{
                menuPermissionList.forEach(menuPermission -> {
                    if(StrUtil.isBlank(menuPermission.getPermission())){
                        return;
                    }
                    UserPermissionRelation relation = new UserPermissionRelation();
                    relation.setPermissionUid(menuPermission.getUid()).setPermission(menuPermission.getPermission()).setUserUid(userUid).setRoleUid(roleUid);
                    relationList.add(relation);
                });
            });
        });
        return relationList;
    }

}
