package com.xjrsoft.module.organization.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.module.organization.dto.UserStudentAddDto;
import com.xjrsoft.module.organization.dto.UserStudentDeleteDto;
import com.xjrsoft.module.organization.entity.UserStudent;
import com.xjrsoft.module.organization.mapper.UserStudentMapper;
import com.xjrsoft.module.organization.service.IUserStudentService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class UserStudentServiceImpl extends MPJBaseServiceImpl<UserStudentMapper, UserStudent> implements IUserStudentService {
    private final UserStudentMapper userStudentMapper;

    @Override
    public boolean add(UserStudentAddDto dto) {
        long count = count(Wrappers.<UserStudent>query().lambda()
                .eq(UserStudent::getUserId, dto.getUserId())
                .eq(UserStudent::getStudentId, dto.getStudentId()));
        if (count > 0) {
            throw new MyException("已经绑定了学生,不能出重复绑定");
        }
        UserStudent userStudent = BeanUtil.toBean(dto, UserStudent.class);
        userStudentMapper.insert(userStudent);
        return Boolean.TRUE;
    }

    @Override
    public boolean delete(List<Long> ids) {
        userStudentMapper.deleteBatchIds(ids);
        return Boolean.TRUE;
    }

    @Override
    public boolean delete(UserStudentDeleteDto dto) {
        userStudentMapper.delete(Wrappers.<UserStudent>query().lambda()
                .eq(UserStudent::getUserId, dto.getUserId())
                .eq(UserStudent::getStudentId, dto.getStudentId()));
        return Boolean.TRUE;
    }
}
