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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.module.textbook.dto.AddCourseToSubjectGroupDto;
import com.xjrsoft.module.textbook.dto.AddSubjectGroupDto;
import com.xjrsoft.module.textbook.dto.UpdateSubjectGroupDto;
import com.xjrsoft.module.textbook.entity.SubjectGroup;
import com.xjrsoft.module.textbook.entity.SubjectGroupCourse;
import com.xjrsoft.module.textbook.mapper.SubjectGroupMapper;
import com.xjrsoft.module.textbook.service.ISubjectGroupCourseService;
import com.xjrsoft.module.textbook.service.ISubjectGroupService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @title: 学科组管理
* @Author szs
* @Date: 2023-12-25
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class SubjectGroupServiceImpl extends MPJBaseServiceImpl<SubjectGroupMapper, SubjectGroup> implements ISubjectGroupService {

    private final ISubjectGroupCourseService subjectGroupCourseService;

    @Override
    @Transactional
    public Boolean add(AddSubjectGroupDto dto) {
        SubjectGroup subjectGroup = BeanUtil.toBean(dto, SubjectGroup.class);
        subjectGroup.setCreateDate(new Date());
        subjectGroup.setCreateUserId(StpUtil.getLoginIdAsLong());
        boolean isSuccess = this.save(subjectGroup);

        LambdaQueryWrapper<SubjectGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(SubjectGroup::getId)
                .eq(SubjectGroup::getGroupName,dto.getGroupName())
                .eq(SubjectGroup::getLeaderUserId,dto.getLeaderUserId());
        SubjectGroup result = this.getOne(queryWrapper);
        //新增学科组的时候同时添加了学科课程
        if(ObjectUtil.isNotNull(dto.getSubjectGroupCourseIds()) && dto.getSubjectGroupCourseIds().size() > 0 && ObjectUtil.isNotNull(result)){
            for (Long subjectGroupCourseId : dto.getSubjectGroupCourseIds()) {
                SubjectGroupCourse subjectGroupCourse = new SubjectGroupCourse();
                subjectGroupCourse.setSubjectGroupId(result.getId());
                subjectGroupCourse.setCourseSubjectId(subjectGroupCourseId);
                subjectGroupCourse.setCreateDate(new Date());
                subjectGroupCourse.setCreateUserId(StpUtil.getLoginIdAsLong());
                isSuccess = subjectGroupCourseService.save(subjectGroupCourse);
                if(!isSuccess){
                    throw new MyException("出现未知错误，联系管理员");
                }
            }
        }
        return isSuccess;
    }

    @Override
    @Transactional
    public Boolean addCourse(AddCourseToSubjectGroupDto dto) {
        boolean isSuccess = false;
        if(ObjectUtil.isNotNull(dto.getCourseSubjectIds()) && dto.getCourseSubjectIds().size() > 0){
            for (Long courseSubjectId:dto.getCourseSubjectIds()) {
                SubjectGroupCourse subjectGroupCourse = new SubjectGroupCourse();
                subjectGroupCourse.setSubjectGroupId(dto.getSubjectGroupId());
                subjectGroupCourse.setCourseSubjectId(courseSubjectId);
                isSuccess = subjectGroupCourseService.save(subjectGroupCourse);
                if(!isSuccess){
                    throw new MyException("出现未知错误，联系管理员");
                }
            }
        }
        return isSuccess;
    }

    @Override
    @Transactional
    public Boolean updateCourse(UpdateSubjectGroupDto dto) {
        //修改该课程组下的课程
        if(ObjectUtil.isNotNull(dto.getSubjectGroupCourseIds()) && dto.getSubjectGroupCourseIds().size() > 0){
            LambdaQueryWrapper<SubjectGroupCourse> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(SubjectGroupCourse::getSubjectGroupId,dto.getId());
            subjectGroupCourseService.remove(queryWrapper);
            for (Long id : dto.getSubjectGroupCourseIds()) {
                SubjectGroupCourse subjectGroupCourse = new SubjectGroupCourse();
                subjectGroupCourse.setCourseSubjectId(id);
                subjectGroupCourse.setSubjectGroupId(dto.getId());
                subjectGroupCourseService.save(subjectGroupCourse);
            }
        }
        SubjectGroup subjectGroup = BeanUtil.toBean(dto, SubjectGroup.class);
        return this.updateById(subjectGroup);
    }

    @Override
    @Transactional
    public Boolean delete(List<Long> ids) {
        //删除课程组下面的所有课程
        for(Long id : ids){
            LambdaQueryWrapper<SubjectGroupCourse> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(SubjectGroupCourse::getSubjectGroupId,id);
            subjectGroupCourseService.remove(queryWrapper);
        }
        return this.removeBatchByIds(ids);
    }
}
