package com.qmth.teachcloud.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicUserCourse;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.mapper.BasicUserCourseMapper;
import com.qmth.teachcloud.common.service.BasicClazzService;
import com.qmth.teachcloud.common.service.BasicUserCourseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 用户和课程关联表 服务实现类
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@Service
public class BasicUserCourseServiceImpl extends ServiceImpl<BasicUserCourseMapper, BasicUserCourse> implements BasicUserCourseService {
    @Resource
    private BasicClazzService basicClazzService;

    @Override
    public void removeByUserId(Long userId) {
        UpdateWrapper<BasicUserCourse> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(BasicUserCourse::getUserId, userId);
        this.baseMapper.delete(updateWrapper);
    }

    @Override
    public void saveBatch(SysUser sysUser) {
        Long[] courseIds = sysUser.getCourseIds();
        List<BasicUserCourse> list = new ArrayList<>();
        for (Long courseId : courseIds) {
            BasicUserCourse userCourse = new BasicUserCourse();
            userCourse.setUserId(sysUser.getId());
            userCourse.setCourseId(courseId);
            userCourse.setCreateId(sysUser.getId());
            userCourse.setCreateTime(System.currentTimeMillis());
            list.add(userCourse);
        }
        this.saveBatch(list);
    }

    @Transactional
    @Override
    public void updateByCourseIdAndClazzList(Long courseId, Set<Long> clazzIdSet, SysUser sysUser, Long orgId) {
        List<BasicUserCourse> willAddList = new ArrayList<>();

        // 删除原有的全部
        this.remove(new QueryWrapper<BasicUserCourse>().lambda().eq(BasicUserCourse::getCourseId,courseId).gt(BasicUserCourse::getId,0));

        // 新增
        if (clazzIdSet != null && clazzIdSet.size() > 0){
            for (Long clazzId : clazzIdSet) {
                if (Objects.isNull(basicClazzService.getById(clazzId))){
                    throw ExceptionResultEnum.ERROR.exception("班级主键不存在");
                }
                BasicUserCourse basicUserCourse = new BasicUserCourse();
                basicUserCourse.setId(SystemConstant.getDbUuid());
                basicUserCourse.setSchoolId(sysUser.getSchoolId());
                basicUserCourse.setOrgId(orgId);
                basicUserCourse.setCourseId(courseId);
                basicUserCourse.setClazzId(clazzId);
                basicUserCourse.setUpdateId(sysUser.getId());
                willAddList.add(basicUserCourse);
            }
        }
        this.saveBatch(willAddList);
    }

    @Override
    public List<BasicUserCourse> listBySchoolIdAndCourseCode(Long schoolId, String courseCode) {
        return this.baseMapper.listBySchoolIdAndCourseCode(schoolId, courseCode);
    }
}
