package com.sw.mes.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.auth.domain.AuthRole;
import com.sw.mes.auth.domain.AuthRoleFactory;
import com.sw.mes.auth.domain.AuthRolePermission;
import com.sw.mes.auth.domain.AuthRoleWarehouse;
import com.sw.mes.auth.service.AuthRoleFactoryService;
import com.sw.mes.auth.service.AuthRolePermissionService;
import com.sw.mes.auth.service.AuthRoleService;
import com.sw.mes.auth.mapper.AuthRoleMapper;
import com.sw.mes.auth.service.AuthRoleWarehouseService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author JS111816
 * @description 针对表【auth_role(【系统域—系统管理】角色表)】的数据库操作Service实现
 * @createDate 2022-05-19 14:21:42
 */
@Service
public class AuthRoleServiceImpl extends ServiceImpl<AuthRoleMapper, AuthRole>
        implements AuthRoleService {
    @Autowired
    private AuthRoleMapper authRoleMapper;

    @Autowired
    private AuthRolePermissionService authRolePermissionService;

    @Autowired
    private AuthRoleFactoryService authRoleFactoryService;

    @Autowired
    private AuthRoleWarehouseService authRoleWarehouseService;

    @Override
    public List<AuthRole> getByUserId(Integer userId) {
        return authRoleMapper.getByUserId(userId);
    }

    @Override
    public void updatePermission(Integer roleId, String permissionIds) {
        //查找出角色所有的RolePermission
        List<AuthRolePermission> rolePermissions = authRolePermissionService.findByRoleId(roleId);
        List<String> idList = new ArrayList<>(Arrays.asList(permissionIds.split(",")));
        for (int i = rolePermissions.size() - 1; i >= 0; i--) {
            AuthRolePermission rolePermission = rolePermissions.get(i);
            if (idList.contains(rolePermission.getPermissionId().toString())) {
                rolePermissions.remove(i);
                String idStr = rolePermission.getPermissionId().toString();
                idList.remove(idStr);
            }
        }
        //先把旧的关联关系全部删除
        List<Integer> authrolePermissionIds = rolePermissions.stream().map(t -> t.getId()).collect(Collectors.toList());
        authRolePermissionService.removeByIds(authrolePermissionIds);
        //逐个添加新的关联关系
        List<AuthRolePermission> newRps = new ArrayList<>();
        for (String idstr : idList) {
            if (!StringUtils.isBlank(idstr)) {
                Integer pid = Integer.parseInt(idstr);
                AuthRolePermission rolePermission = new AuthRolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(pid);
                newRps.add(rolePermission);
            }
        }
        //保存新的
        if (newRps.size() > 0) {
            authRolePermissionService.saveOrUpdateBatch(newRps);
        }
    }

    @Override
    public List<AuthRole> findByids(List<String> ids) {
        LambdaQueryWrapper<AuthRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AuthRole::getId, ids);
        List<AuthRole> authRoles = getBaseMapper().selectList(lambdaQueryWrapper);
        return authRoles;
    }

    @Override
    public void updateDataPermission(Integer roleId, String factoryModelIds, String warehouseIds) {
//        查询出角色关联的所有工厂模型权限
        List<AuthRoleFactory> authRoleFactoryList = authRoleFactoryService.findAuthRoleFactoryByRoleId(roleId);
//        查询出角色关联的所有仓库权限
        List<AuthRoleWarehouse> authRoleWarehouseList = authRoleWarehouseService.findAuthRoleWarehouseByRoleId(roleId);

        ArrayList<AuthRoleFactory> factoryList = new ArrayList<>();
        ArrayList<AuthRoleWarehouse> warehouseList = new ArrayList<>();

//        工厂模型权限
        if (StringUtils.isNotEmpty(factoryModelIds)) {
            String[] split = factoryModelIds.split(",");
            for (String factoryModelId : split) {
                AuthRoleFactory authRoleFactory = new AuthRoleFactory();
                authRoleFactory.setRoleId(roleId);
                authRoleFactory.setFactoryModelId(Integer.parseInt(factoryModelId));
                factoryList.add(authRoleFactory);
            }
            List<Integer> authRoleFactoryWarehouseIds = authRoleFactoryList.stream().map(AuthRoleFactory::getId).collect(Collectors.toList());
            //            删除旧的权限
            authRoleFactoryService.removeByIds(authRoleFactoryWarehouseIds);
        }
//            仓库权限
        if (StringUtils.isNotEmpty(warehouseIds)) {
            String[] split = warehouseIds.split(",");
            for (String warehouseId : split) {
                AuthRoleWarehouse authRoleWarehouse = new AuthRoleWarehouse();
                authRoleWarehouse.setRoleId(roleId);
                authRoleWarehouse.setWarehouseId(Integer.parseInt(warehouseId));
                warehouseList.add(authRoleWarehouse);
            }
            List<Integer> authRoleFactoryWarehouseIds = authRoleWarehouseList.stream().map(AuthRoleWarehouse::getId).collect(Collectors.toList());
//            删除旧的权限
            authRoleWarehouseService.removeByIds(authRoleFactoryWarehouseIds);
        }

//          保存新的工厂权限
        if (CollectionUtils.isNotEmpty(factoryList)) {
            authRoleFactoryService.saveOrUpdateBatch(factoryList);
        }
//        保存新的仓库权限
        if (CollectionUtils.isNotEmpty(warehouseList)) {
            authRoleWarehouseService.saveOrUpdateBatch(warehouseList);
        }
    }
}




