package com.ho.user.service.impl;

import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.user.api.entity.SysUser;
import com.ho.user.api.vo.req.OrderUserSelectReqVo;
import com.ho.user.entity.SysPosition;
import com.ho.user.entity.SysUserPosition;
import com.ho.user.mapper.SysPositionMapper;
import com.ho.user.mapper.SysUserPositionMapper;
import com.ho.user.service.UserPostService;
import com.ho.user.vo.req.UserPostOperationReqVO;
import lombok.extern.slf4j.Slf4j;
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.Date;
import java.util.List;

/**
 * @author fancl
 * @desc: 用户岗位关联
 * @date 2022/12/6
 */
@Service
@Slf4j
public class UserPostServiceImpl implements UserPostService {

    @Autowired
    SysUserPositionMapper userPositionMapper;

    @Autowired
    SysPositionMapper positionMapper;

    @Override
    public int removeByPostId(Integer postId) {
        int i = userPositionMapper.removeByPostId(postId);
        return i;
    }

    @Override
    public List<Integer> getPostIdsByUserId(String userId) {
        List<Integer> postIds = userPositionMapper.selectPostIdsByUserId(userId);
        return postIds;
    }

    //新增人员岗位关联
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addUserPostInfo(UserPostOperationReqVO vo) {
        if (vo.getPostIds() == null || vo.getPostIds().isEmpty()) {
            return;
        }
        Date createTime = new Date();
        List<SysUserPosition> list = new ArrayList<>();
        for (Integer postId : vo.getPostIds()) {
            SysUserPosition sysUserPosition = new SysUserPosition();
            sysUserPosition.setCreateTime(createTime);
            sysUserPosition.setUserId(vo.getUserId());
            sysUserPosition.setPositionId(postId);
            list.add(sysUserPosition);
        }
        userPositionMapper.removeByUserId(vo.getUserId());
        int count = userPositionMapper.batchUserPosition(list);
        if (count == 0) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERRO);
        }
    }

    @Override
    public int removeByUserId(String userId) {
        int i = userPositionMapper.removeByUserId(userId);
        return i;
    }

    @Override
    public int removeByUserIds(List<String> userIds) {
        int i = userPositionMapper.removeByUserIds(userIds);
        return i;
    }

    //暂时应该用不到
    @Override
    public List<String> getUserIdsByPostIds(List<Integer> postIds) {
        List<String> postUserIds = userPositionMapper.getUserIdsByPostIds(postIds);
        return postUserIds;
    }

    //用户岗位列表
    @Override
    public List<SysPosition> getPostListByUserId(String userid) {
        //用户岗位ids
        List<Integer> postIds = userPositionMapper.selectPostIdsByUserId(userid);
        if (!postIds.isEmpty()) {
            List<SysPosition> sysPositions = positionMapper.selectPostListByPostIds(postIds);
            return sysPositions;
        }
        return new ArrayList<>();
    }

    @Override
    public List<SysUser> selectUserByPostIds(OrderUserSelectReqVo vo, List<Integer> postIds) {
        List<SysUser> sysUsers = positionMapper.selectUserByPostIds(vo, postIds);
        return sysUsers;
    }

    @Override
    public List<SysUserPosition> getByPostIds(List<Integer> postIds) {
        return  userPositionMapper.selectByPostIds(postIds);
    }
}
