package com.ruoyi.postCheck.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostTeamUserMapper;
import com.ruoyi.postCheck.domain.PostTeamUser;
import com.ruoyi.postCheck.service.IPostTeamUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 岗检小组关联成员Service业务层处理
 *
 * @author csg
 * @date 2023-12-19
 */
@Service
public class PostTeamUserServiceImpl extends ServiceImpl<PostTeamUserMapper, PostTeamUser> implements IPostTeamUserService {
    @Autowired
    private PostTeamUserMapper postTeamUserMapper;

    /**
     * 查询岗检小组关联成员
     *
     * @param id 岗检小组关联成员主键
     * @return 岗检小组关联成员
     */
    @Override
    public PostTeamUser selectPostTeamUserById(Long id) {
        return postTeamUserMapper.selectPostTeamUserById(id);
    }

    /**
     * 查询岗检小组关联成员列表
     *
     * @param postTeamUser 岗检小组关联成员
     * @return 岗检小组关联成员
     */
    @Override
    public List<PostTeamUser> selectPostTeamUserList(PostTeamUser postTeamUser) {
        return postTeamUserMapper.selectPostTeamUserList(postTeamUser);
    }

    /**
     * 新增岗检小组关联成员
     *
     * @param postTeamUserList 岗检小组关联成员数组
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPostTeamUser(List<PostTeamUser> postTeamUserList) {
        if (CollectionUtils.isEmpty(postTeamUserList)) {
            return 0;
        }

        List<Long> postTeamIdList = postTeamUserList.stream().map(t -> t.getPostTeamId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(postTeamIdList)) {
            throw new BaseException("postTeamId为空，请检查！");
        }

        //删除原来的用户
        LambdaQueryWrapper<PostTeamUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PostTeamUser::getPostTeamId, postTeamIdList);
        postTeamUserMapper.delete(wrapper);

        //新增用户
        int count = 0;
        int index = 0;
        Long postTeamIdLast = null;
        for (PostTeamUser postTeamUser : postTeamUserList) {
            //类型（1-组长；2-成员；）
            if (index++ == 0) {
                postTeamUser.setType(1);
            } else {
                postTeamUser.setType(2);
            }
            Long postTeamId = postTeamUser.getPostTeamId();
            if (postTeamId == null) {
                throw new BaseException("postTeamId为空，请检查！");
            }

            if (postTeamIdLast != null && !postTeamIdLast.equals(postTeamId)) {
                throw new BaseException("postTeamId出现多个值，不允许多个小组混合添加成员，请检查！");
            }
            postTeamIdLast = postTeamId;

            Long userId = postTeamUser.getUserId();
            if (userId == null) {
                throw new BaseException("userId为空，请检查！");
            }

            //TODO 自增主键修改为-MyBatis-Plus雪花算法(截取5位解决VUE精度丢失BUG)-系统登录日志
//        if (postTeamUser.getId() == null)
            postTeamUser.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
            postTeamUser.setCreateDefault();
            count += postTeamUserMapper.insertPostTeamUser(postTeamUser);
        }
        return count;
    }

    /**
     * 修改岗检小组关联成员
     *
     * @param postTeamUserList 岗检小组关联成员数组
     * @return 结果
     */
  /*  @Override
    public int updatePostTeamUser(List<PostTeamUser> postTeamUserList) {
        if (CollectionUtils.isEmpty(postTeamUserList)) {
            return 0;
        }

        List<Long> postTeamIdList = postTeamUserList.stream().map(t -> t.getPostTeamId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(postTeamIdList)) {
            throw new BaseException("postTeamId为空，请检查！");
        }
        //删除原来的用户
        LambdaQueryWrapper<PostTeamUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PostTeamUser::getPostTeamId, postTeamIdList);
        postTeamUserMapper.delete(wrapper);

        //新增用户
        int count = 0;
        int index = 0;
        for (PostTeamUser postTeamUser : postTeamUserList) {
            //类型（1-组长；2-成员；）
            if (index++ == 0) {
                postTeamUser.setType(1);
            } else {
                postTeamUser.setType(2);
            }

            Long postTeamId = postTeamUser.getPostTeamId();
            if (postTeamId == null) {
                throw new BaseException("postTeamId为空，请检查！");
            }
            //TODO 自增主键修改为-MyBatis-Plus雪花算法(截取5位解决VUE精度丢失BUG)-系统登录日志
//        if (postTeamUser.getId() == null)
            postTeamUser.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
            postTeamUser.setCreateDefault();
            count += postTeamUserMapper.insertPostTeamUser(postTeamUser);
        }
        return count;

//        postTeamUser.setUpdateDefault();
//        return postTeamUserMapper.updatePostTeamUser(postTeamUser);
    }
*/

    /**
     * 批量删除岗检小组关联成员
     *
     * @param ids 需要删除的岗检小组关联成员主键
     * @return 结果
     */
    @Override
    public int deletePostTeamUserByIds(Long[] ids) {
        return postTeamUserMapper.deletePostTeamUserByIds(ids);
    }

    /**
     * 删除岗检小组关联成员信息
     *
     * @param id 岗检小组关联成员主键
     * @return 结果
     */
    @Override
    public int deletePostTeamUserById(Long id) {
        return postTeamUserMapper.deletePostTeamUserById(id);
    }
}
