package org.jeecg.modules.resManager.roleRescatalog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.resManager.roleRescatalog.entity.RoleRescatalog;
import org.jeecg.modules.resManager.roleRescatalog.mapper.RoleRescatalogMapper;
import org.jeecg.modules.resManager.roleRescatalog.service.IRoleRescatalogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 角色资源表
 * @Author: jeecg-boot
 * @Date: 2022-07-20
 * @Version: V1.0
 */
@Service
public class RoleRescatalogServiceImpl extends ServiceImpl<RoleRescatalogMapper, RoleRescatalog> implements IRoleRescatalogService {

    @Override
    public void saveRolePermission(String roleId, String rescatalogIds) {
        LambdaQueryWrapper<RoleRescatalog> query = new QueryWrapper<RoleRescatalog>().lambda().eq(RoleRescatalog::getRoleId, roleId);
        this.remove(query);
        List<RoleRescatalog> list = new ArrayList<RoleRescatalog>();
        String[] arr = rescatalogIds.split(",");
        for (String resId : arr) {
            if (oConvertUtils.isNotEmpty(resId)) {
                RoleRescatalog roleRescatalog = new RoleRescatalog();
                roleRescatalog.setRoleId(roleId);
                roleRescatalog.setRescatalogId(resId);
                this.save(roleRescatalog);
                list.add(roleRescatalog);
            }
        }
        this.saveBatch(list);
    }

    @Override
    public void saveRolePermission(String roleId, String rescatalogIds, String lastRescatalogIds) {

        List<String> addList = getDiff(lastRescatalogIds, rescatalogIds);
        if (addList != null && addList.size() > 0) {
            List<RoleRescatalog> list = new ArrayList<RoleRescatalog>();
            for (String resId : addList) {
                if (oConvertUtils.isNotEmpty(resId)) {
                    RoleRescatalog roleRescatalog = new RoleRescatalog();
                    roleRescatalog.setRoleId(roleId);
                    roleRescatalog.setRescatalogId(resId);
                    list.add(roleRescatalog);
                }
            }
            this.saveBatch(list);
        }

        List<String> delete = getDiff(rescatalogIds, lastRescatalogIds);
        if (delete != null && delete.size() > 0) {
            for (String catalogId : delete) {
                this.remove(new QueryWrapper<RoleRescatalog>().lambda().eq(RoleRescatalog::getRoleId, roleId).eq(RoleRescatalog::getRescatalogId, catalogId));
            }
        }
    }

    /**
     * 从diff中找出main中没有的元素
     *
     * @param main
     * @param diff
     * @return
     */
    private List<String> getDiff(String main, String diff) {
        if (oConvertUtils.isEmpty(diff)) {
            return null;
        }
        if (oConvertUtils.isEmpty(main)) {
            return Arrays.asList(diff.split(","));
        }

        String[] mainArr = main.split(",");
        String[] diffArr = diff.split(",");
        Map<String, Integer> map = new HashMap<>();
        for (String string : mainArr) {
            map.put(string, 1);
        }
        List<String> res = new ArrayList<String>();
        for (String key : diffArr) {
            if (oConvertUtils.isNotEmpty(key) && !map.containsKey(key)) {
                res.add(key);
            }
        }
        return res;
    }
}
