package com.casic.missiles.modular.group.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.casic.missiles.modular.system.model.GroupUser;
import com.casic.missiles.modular.system.dao.GroupUserMapper;
import com.casic.missiles.modular.group.service.IGroupUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lwh123
 * @since 2020-03-20
 */
@Service
public class GroupUserServiceImpl extends ServiceImpl<GroupUserMapper, GroupUser> implements IGroupUserService {

    @Override
    public void addUsers(String id, List<String> userIds) {
        try {
            //先删除重复的绑定关系
            EntityWrapper<GroupUser> groupUserEntityWrapper = new EntityWrapper<>();
            groupUserEntityWrapper.eq("GROUP_ID",id);
            groupUserEntityWrapper.in("USER_ID",userIds);
            delete(groupUserEntityWrapper);
            List<GroupUser> groupUsers = new ArrayList<>();
            for (String str : userIds) {
                GroupUser groupUser = new GroupUser();
                groupUser.setGroupId(Long.valueOf(id));
                groupUser.setUserId(Long.valueOf(str));
                groupUsers.add(groupUser);
            }
            this.insertBatch(groupUsers);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<String> findUserByGroupid(String id) {
        return this.baseMapper.findUserByGroupid(id);
    }
}
