package com.njzscloud.supervisory.sys.role.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.core.utils.GroupUtil;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.sys.resource.pojo.entity.ResourceEntity;
import com.njzscloud.supervisory.sys.resource.service.ResourceService;
import com.njzscloud.supervisory.sys.role.mapper.RoleResMapper;
import com.njzscloud.supervisory.sys.role.pojo.entity.RoleResourceEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 角色-资源关系表
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RoleResService extends ServiceImpl<RoleResMapper, RoleResourceEntity> implements IService<RoleResourceEntity> {
    private final ResourceService resourceService;

    /**
     * 新增
     *
     * @param roleResourceEntity 数据
     */

    public void add(RoleResourceEntity roleResourceEntity) {
        this.save(roleResourceEntity);
    }

    /**
     * 修改
     *
     * @param roleResourceEntity 数据
     */

    public void modify(RoleResourceEntity roleResourceEntity) {
        this.updateById(roleResourceEntity);
    }

    /**
     * 删除
     *
     * @param ids Ids
     */

    @Transactional(rollbackFor = Exception.class)
    public void del(List<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysRoleResourceEntity 结果
     */

    public RoleResourceEntity detail(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * @param roleResourceEntity 筛选条件
     * @param pageParam          分页参数
     * @return PageResult&lt;SysRoleResourceEntity&gt; 分页结果
     */

    public PageResult<RoleResourceEntity> paging(PageParam pageParam, RoleResourceEntity roleResourceEntity) {
        return PageResult.of(this.page(pageParam.toPage(), Wrappers.<RoleResourceEntity>query(roleResourceEntity)));
    }

    /**
     * 为角色绑定资源
     */
    @Transactional(rollbackFor = Exception.class)
    public void bindRes(Long roleId, List<Long> resIds) {
        if (CollUtil.isEmpty(resIds)) return;
        List<ResourceEntity> sysMenuEntities = resourceService.listByIds(resIds);
        Assert.isTrue(sysMenuEntities.size() == resIds.size(), () -> Exceptions.exception("资源不存在"));

        this.remove(Wrappers.<RoleResourceEntity>lambdaQuery().eq(RoleResourceEntity::getRoleId, roleId));

        Map<Long, ResourceEntity> map = GroupUtil.k_o(sysMenuEntities, ResourceEntity::getId);

        List<RoleResourceEntity> list = resIds.stream().map(it -> {
            ResourceEntity resourceEntity = map.get(it);
            return new RoleResourceEntity()
                    .setRoleId(roleId)
                    .setResId(it)
                    .setResSn(resourceEntity.getSn())
                    .setTableName(resourceEntity.getTableName())
                    .setDataId(resourceEntity.getDataId());
        }).collect(Collectors.toList());

        this.saveBatch(list);
    }
}
