package org.jeecg.modules.roleMaptool.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.roleMaptool.entity.RoleMaptool;
import org.jeecg.modules.roleMaptool.mapper.RoleMaptoolMapper;
import org.jeecg.modules.roleMaptool.service.IRoleMaptoolService;
import org.springframework.stereotype.Service;

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

import java.util.*;

/**
 * @Description: 角色地图工具表
 * @Author: jeecg-boot
 * @Date:   2022-07-30
 * @Version: V1.0
 */
@Service
public class RoleMaptoolServiceImpl extends ServiceImpl<RoleMaptoolMapper, RoleMaptool> implements IRoleMaptoolService {
    @Override
    public void saveRolePermission(String roleId, String maptoolIds) {
        LambdaQueryWrapper<RoleMaptool> query = new QueryWrapper<RoleMaptool>().lambda().eq(RoleMaptool::getRoleId, roleId);
        this.remove(query);
        List<RoleMaptool> list = new ArrayList<RoleMaptool>();
        String[] arr = maptoolIds.split(",");
        for (String maptoolId : arr) {
            if (oConvertUtils.isNotEmpty(maptoolId)) {
                RoleMaptool roleMaptool = new RoleMaptool();
                roleMaptool.setRoleId(roleId);
                roleMaptool.setMaptoolId(maptoolId);
                this.save(roleMaptool);
                list.add(roleMaptool);
            }
        }
        this.saveBatch(list);
    }

    @Override
    public void saveRolePermission(String roleId, String maptoolIds, String lastMaptoolIds) {

        List<String> addList = getDiff(lastMaptoolIds, maptoolIds);
        if (addList != null && addList.size() > 0) {
            List<RoleMaptool> list = new ArrayList<RoleMaptool>();
            for (String maptoolId : addList) {
                if (oConvertUtils.isNotEmpty(maptoolId)) {
                    RoleMaptool roleMaptool = new RoleMaptool();
                    roleMaptool.setRoleId(roleId);
                    roleMaptool.setMaptoolId(maptoolId);
                    list.add(roleMaptool);
                }
            }
            this.saveBatch(list);
        }

        List<String> delete = getDiff(maptoolIds, lastMaptoolIds);
        if (delete != null && delete.size() > 0) {
            for (String catalogId : delete) {
                this.remove(new QueryWrapper<RoleMaptool>().lambda().eq(RoleMaptool::getRoleId, roleId).eq(RoleMaptool::getMaptoolId, 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;
    }
}
