package io.hmit.modules.serviceOrder.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.modules.serviceOrder.dao.RoleUserDao;
import io.hmit.modules.serviceOrder.dto.RoleUserDTO;
import io.hmit.modules.serviceOrder.entity.RoleUserEntity;
import io.hmit.modules.serviceOrder.service.RoleUserService;
import io.hmit.modules.sys.entity.SysRoleUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 角色用户关系
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-01-26
 */
@Service
public class RoleUserServiceImpl extends CrudServiceImpl<RoleUserDao, RoleUserEntity, RoleUserDTO> implements RoleUserService {

    @Override
    public QueryWrapper<RoleUserEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<RoleUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        baseDao.deleteByUserIds(new Long[]{userId});

        if (CollUtil.isEmpty(roleIdList)) {
            return;
        }

        //保存角色用户关系
        for (Long roleId : roleIdList) {
            RoleUserEntity roleUserEntity = new RoleUserEntity();
            roleUserEntity.setUserId(userId);
            roleUserEntity.setRoleId(roleId);

            //保存
            insert(roleUserEntity);
        }
    }

    @Override
    public List<Long> getRoleIdList(Long userId) {
        return baseDao.getRoleIdList(userId);
    }
}
