package com.xhpc.system.service.impl;

import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.domain.SysUserPost;
import com.xhpc.system.domain.SysUserRole;
import com.xhpc.system.mapper.SysUserPostMapper;
import com.xhpc.system.mapper.SysUserRoleMapper;
import com.xhpc.system.service.SysUserPostRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserPostRoleServiceImpl implements SysUserPostRoleService {

    private static final Logger log = LoggerFactory.getLogger(SysUserPostRoleServiceImpl.class);

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;
    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    @Override
    @Transactional
    public void insertUserPost(SysUser user)
    {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotNull(posts))
        {
            // 新增用户与岗位管理
            List<SysUserPost> list = new ArrayList<SysUserPost>();
            for (Long postId : posts)
            {
                SysUserPost up = new SysUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0)
            {
                userPostMapper.batchUserPost(list);
            }
        }
    }

    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    @Override
    @Transactional
    public void insertUserRole(SysUser user)
    {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roles)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0)
            {
                userRoleMapper.batchUserRole(list);
            }
        }
    }

}
