package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.cache.CommonCacheService;
import com.qmth.sop.business.entity.SysPrivilege;
import com.qmth.sop.business.entity.SysRole;
import com.qmth.sop.business.entity.SysRolePrivilege;
import com.qmth.sop.business.mapper.SysRolePrivilegeMapper;
import com.qmth.sop.business.service.SysPrivilegeService;
import com.qmth.sop.business.service.SysRolePrivilegeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-07-17
 */
@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService {

    @Resource
    CommonCacheService commonCacheService;

    @Resource
    SysPrivilegeService sysPrivilegeService;

    @Resource
    SysRolePrivilegeService sysRolePrivilegeService;

    /**
     * 批量保存角色权限
     *
     * @param role
     * @return
     */
    @Override
    @Transactional
    public Boolean saveBatch(SysRole role) {
        List<SysRolePrivilege> sysRolePrivilegeList = commonCacheService.rolePrivilegeCache(role.getId());
        List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(new QueryWrapper<SysPrivilege>().lambda().in(SysPrivilege::getId, role.getPrivilegeIds()));

        Set<String> relatedSet = sysPrivilegeList.stream().filter(s -> Objects.nonNull(s.getRelated())).map(s -> s.getRelated()).collect(Collectors.toSet());
        Set<Long> finalRelatedSet = new HashSet<>(role.getPrivilegeIds());
        for (String s : relatedSet) {
            if (s.contains(",")) {
                String[] arrays = s.split(",");
                for (int i = 0; i < arrays.length; i++) {
                    finalRelatedSet.add(Long.parseLong(arrays[i].trim()));
                }
            } else {
                finalRelatedSet.add(Long.parseLong(s));
            }
        }
        // 修改后权限集合
        List<Long> relatedList = new ArrayList<>(finalRelatedSet);
        List<Long> finalRelatedList = relatedList;
        int count = (int) sysRolePrivilegeList.stream().filter(s -> finalRelatedList.contains(s.getPrivilegeId())).count();
        if (count != sysRolePrivilegeList.size() || count != finalRelatedSet.size()) {
            sysRolePrivilegeService.remove(new QueryWrapper<SysRolePrivilege>().lambda().eq(SysRolePrivilege::getRoleId, role.getId()));
            commonCacheService.removeRolePrivilegeCache(role.getId());
            List<SysRolePrivilege> list = new ArrayList<>();
            for (Long privilegeId : relatedList) {
                list.add(new SysRolePrivilege(role.getId(), privilegeId));
            }
            this.saveBatch(list);
            return true;
        }
        return false;
    }
}
