package com.kiisoo.ic.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kiisoo.ic.system.entity.PrivilageUserRoleDO;
import com.kiisoo.ic.system.mapper.PrivilageUserRoleDOMapper;
import com.kiisoo.ic.system.service.PrivilageUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户角色  服务实现类
 * @author jinchaofan
 * @since 2020-02-19
 */
@Service
public class PrivilageUserRoleServiceImpl extends ServiceImpl<PrivilageUserRoleDOMapper, PrivilageUserRoleDO> implements PrivilageUserRoleService {

    /**
     * 用户角色关系 mapper
     */
    private final PrivilageUserRoleDOMapper privilageUserRoleDOMapper;

    @Autowired
    public PrivilageUserRoleServiceImpl(PrivilageUserRoleDOMapper privilageUserRoleDOMapper) {
        this.privilageUserRoleDOMapper = privilageUserRoleDOMapper;
    }


    @Override
    public Long insertUserRoleRelation(Long userId, Long roleId) {
        PrivilageUserRoleDO privilageUserRoleDO = new PrivilageUserRoleDO();
        privilageUserRoleDO.setRoleId(roleId);
        privilageUserRoleDO.setUserId(userId);
        privilageUserRoleDO.setCreateTime(new Date());
        privilageUserRoleDOMapper.insert(privilageUserRoleDO);
        return privilageUserRoleDO.getId();
    }

    @Override
    public void removeUserRoleRelation(Long userId) {
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("user_id", userId);
        privilageUserRoleDOMapper.deleteByMap(deleteMap);
    }

    @Override
    public void saveUserRole(Long userId, Long roleId) {
        //删除用户角色关系
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("user_id", userId);
        privilageUserRoleDOMapper.deleteByMap(deleteMap);

        //更改用户角色关系
        PrivilageUserRoleDO privilageUserRoleDO = new PrivilageUserRoleDO();
        privilageUserRoleDO.setUserId(userId);
        privilageUserRoleDO.setRoleId(roleId);
        privilageUserRoleDO.setCreateTime(new Date());
        privilageUserRoleDOMapper.insert(privilageUserRoleDO);
    }
}
