package com.mosty.common.core.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.common.base.constant.enums.DeletedEnum;
import com.mosty.common.base.exception.Asserts;
import com.mosty.common.core.business.entity.SysRole;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.entity.SysUserRole;
import com.mosty.common.core.business.entity.vo.RoleUserPage;
import com.mosty.common.core.business.entity.vo.RoleUserVO;
import com.mosty.common.core.business.entity.vo.SysUserVO;
import com.mosty.common.core.business.entity.vo.UserRoleVO;
import com.mosty.common.core.business.mapper.SysUserRoleMapper;
import com.mosty.common.core.business.service.LoginService;
import com.mosty.common.core.business.service.SysRoleService;
import com.mosty.common.core.business.service.SysUserRoleService;
import com.mosty.common.core.business.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 * @description 针对表【sys_user_role(用户和角色关联表)】的数据库操作Service实现
 * @createDate 2022-02-12 16:53:24
 */
@Slf4j
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private LoginService loginService;

    @Override
    public IPage<SysUserVO> roleUserList(String rootPath, RoleUserPage roleUserPage) {

        List<SysUserVO> dataList = sysUserRoleMapper.roleUserList(rootPath, roleUserPage);
        Page<SysUserVO> page = BeanUtil.copyProperties(dataList, Page.class);
        page.setRecords(dataList);
        return page;
    }

    @Override
    public List<SysUserRole> listByUserId(Long id) {
        return this.list(new LambdaQueryWrapper<SysUserRole>()
                .eq(SysUserRole::getUserId, id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean grantRoleToUser(UserRoleVO userRoleVo) {
        // 先删除所有的权限信息
        this.sysUserRoleMapper.delete(
                new LambdaQueryWrapper<SysUserRole>()
                        .eq(SysUserRole::getUserId, userRoleVo.getUserId())
        );
        SysUser sysUser = sysUserService.getById(userRoleVo.getUserId());
        Asserts.check(sysUser == null || DeletedEnum.DELETED.code.equals(sysUser.getXtZxbz()), "用户不存在或已删除");
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        List<Long> roleIds = userRoleVo.getRoleIds();
        roleIds.forEach(roleId -> {
            SysRole sysRole = sysRoleService.getById(roleId);
            Asserts.check(sysRole == null || DeletedEnum.DELETED.code.equals(sysRole.getXtZxbz()), "角色不存在或已删除");
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(roleId);
            sysUserRole.setUserId(userRoleVo.getUserId());
            sysUserRoles.add(sysUserRole);
        });
        // 修改redis中用户缓存的信息
        return this.saveBatch(sysUserRoles);
    }

    @Override
    public boolean grantUserToRole(RoleUserVO roleUserVO) {
        SysRole role = sysRoleService.getById(roleUserVO.getRoleId());
        Asserts.check(role == null || DeletedEnum.DELETED.code.equals(role.getXtZxbz()), "角色不存在或已删除");
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        List<Long> userIds = roleUserVO.getUserIds();
        userIds.forEach(userId -> {
            SysUser user = sysUserService.getById(userId);
            Asserts.check(user == null || DeletedEnum.DELETED.code.equals(user.getXtZxbz()), "用户不存在或已删除");
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(role.getId());
            sysUserRole.setUserId(userId);
            sysUserRoles.add(sysUserRole);
        });
        return this.saveBatch(sysUserRoles);
    }


}




