package com.ym.mec.biz.service.impl;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.ReturnFeeDto;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.*;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.util.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author Joburgess
 * @Date 2020/2/17
 */
@Service
public class GroupClassServiceImpl implements GroupClassService {

    @Autowired
    private GroupDao groupDao;
    @Autowired
    private ClassGroupDao classGroupDao;
    @Autowired
    private ClassGroupTeacherMapperDao classGroupTeacherMapperDao;
    @Autowired
    private ClassGroupTeacherSalaryDao classGroupTeacherSalaryDao;
    @Autowired
    private ClassGroupStudentMapperDao classGroupStudentMapperDao;
    @Autowired
    private CourseScheduleDao courseScheduleDao;
    @Autowired
    private CourseScheduleTeacherSalaryDao courseScheduleTeacherSalaryDao;
    @Autowired
    private CourseScheduleStudentPaymentDao courseScheduleStudentPaymentDao;
    @Autowired
    private TeacherAttendanceDao teacherAttendanceDao;
    @Autowired
    private StudentAttendanceDao studentAttendanceDao;
    @Autowired
    private PracticeGroupDao practiceGroupDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private SysUserCashAccountLogDao sysUserCashAccountLogDao;
    @Autowired
    private CoursesGroupDao coursesGroupDao;
    @Autowired
    private SysTenantAccountService sysTenantAccountService;
    @Autowired
    private ActivityUserMapperService activityUserMapperService;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private PracticeGroupService practiceGroupService;

    private final static Logger LOGGER = LoggerFactory.getLogger(GroupClassServiceImpl.class);

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAllGroupInfo(String groupId, GroupType groupType) {
        groupDao.deleteGroup(groupId, groupType.getCode());
        classGroupDao.deleteByGroup(groupId, groupType);
        classGroupTeacherMapperDao.deleteByGroup(groupId, groupType);
        classGroupTeacherSalaryDao.deleteByGroup(groupId, groupType);
        classGroupStudentMapperDao.deleteByGroup(groupId, groupType);
        courseScheduleDao.deleteByGroup(groupId, groupType);
        courseScheduleTeacherSalaryDao.deleteByGroup(groupId, groupType);
        courseScheduleStudentPaymentDao.deleteByGroup(groupId, groupType);
        teacherAttendanceDao.deleteByGroup(groupId, groupType);
        studentAttendanceDao.deleteByGroup(groupId, groupType);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteGroupOtherInfo(String groupId, GroupType groupType) {
        classGroupDao.deleteByGroup(groupId, groupType);
        classGroupTeacherMapperDao.deleteByGroup(groupId, groupType);
        classGroupTeacherSalaryDao.deleteByGroup(groupId, groupType);
        classGroupStudentMapperDao.deleteByGroup(groupId, groupType);
        courseScheduleDao.deleteByGroup(groupId, groupType);
        courseScheduleTeacherSalaryDao.deleteByGroup(groupId, groupType);
        courseScheduleStudentPaymentDao.deleteByGroup(groupId, groupType);
        teacherAttendanceDao.deleteByGroup(groupId, groupType);
        studentAttendanceDao.deleteByGroup(groupId, groupType);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean cleanGroupInfo(String groupId, GroupType groupType) {
        classGroupDao.deleteByGroupWithLogic(groupId, groupType);
        classGroupStudentMapperDao.deleteByGroupWithLogic(groupId, groupType);
        List<CourseSchedule> groupNotStartCourses = courseScheduleDao.findGroupNotStartCourses(groupId, groupType);
        if (CollectionUtils.isEmpty(groupNotStartCourses)) {
            return true;
        }
        List<Long> courseIds = groupNotStartCourses.stream().map(CourseSchedule::getId).collect(Collectors.toList());
        courseScheduleDao.batchDeleteCourseSchedules(courseIds);
        courseScheduleTeacherSalaryDao.batchDeleteByCourseScheduleIds(courseIds);
        courseScheduleStudentPaymentDao.deleteByCourseSchedule(courseIds);
        teacherAttendanceDao.batchDeleteByCourseSchedules(courseIds);
        studentAttendanceDao.deleteByCourseSchedules(courseIds);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean hideGroupInfo(String groupId, GroupType groupType) {
        classGroupDao.deleteByGroupWithLogic(groupId, groupType);
        classGroupStudentMapperDao.deleteByGroupWithLogic(groupId, groupType);
        List<CourseSchedule> groupNotStartCourses = courseScheduleDao.findGroupNotStartCourses(groupId, groupType);
        if (CollectionUtils.isEmpty(groupNotStartCourses)) {
            return true;
        }
        List<Long> courseIds = groupNotStartCourses.stream().map(CourseSchedule::getId).collect(Collectors.toList());
        courseScheduleDao.batchDeleteCourseSchedulesWithLogic(courseIds);
        courseScheduleTeacherSalaryDao.batchDeleteByCourseScheduleIds(courseIds);
        courseScheduleStudentPaymentDao.deleteByCourseSchedule(courseIds);
        teacherAttendanceDao.batchDeleteByCourseSchedules(courseIds);
        studentAttendanceDao.deleteByCourseSchedules(courseIds);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public HttpResponseResult cancelGroup(ReturnFeeDto returnFeeDto) {
        Long groupId = returnFeeDto.getGroupId();
        if (Objects.isNull(groupId)) {
            throw new BizException("请指定课程组");
        }
        GroupType groupType = returnFeeDto.getGroupType();
        if (Objects.isNull(groupType)) {
            throw new BizException("请指定课程组类型");
        }
        Date now = new Date();
        if (groupType.equals(GroupType.PRACTICE)) {
            PracticeGroup practiceGroup = practiceGroupDao.get(groupId);
            if (Objects.isNull(practiceGroup)) {
                throw new BizException("指定的课程组不存在");
            }
            if (!practiceGroup.getGroupStatus().equals(GroupStatusEnum.NORMAL) || practiceGroup.getCoursesExpireDate().before(now)) {
                throw new BizException("当前课程组不可关闭");
            }
            if (Objects.nonNull(returnFeeDto.getAmount()) && returnFeeDto.getAmount().compareTo(BigDecimal.ZERO) > 0) {
                Map<String, BigDecimal> studentSurplusCourseFee = practiceGroupService.getStudentSurplusCourseFee(groupId);
                BigDecimal suplusCourseFee = studentSurplusCourseFee.get("suplusCourseOriginalFee");
                if(returnFeeDto.getAmount().compareTo(suplusCourseFee) > 0){
                    throw new BizException("学员最大可退费金额为{}元", suplusCourseFee.toString());
                }
                SysUserCashAccountLog sysUserCashAccountLog = new SysUserCashAccountLog();
                sysUserCashAccountLog.setGroupType(GroupType.PRACTICE);
                sysUserCashAccountLog.setUserId(practiceGroup.getStudentId());
                sysUserCashAccountLog.setOrganId(practiceGroup.getOrganId());
                sysUserCashAccountLog.setGroupId(practiceGroup.getId().toString());
                sysUserCashAccountLog.setAmount(returnFeeDto.getAmount());
                sysUserCashAccountLog.setReturnFeeType(ReturnFeeEnum.PRACTICE);
                sysUserCashAccountLog.setComment("后台关闭网管课");
                sysUserCashAccountLogDao.insert(sysUserCashAccountLog);
            }
            cleanGroupInfo(groupId.toString(), GroupType.PRACTICE);
            practiceGroup.setMemo("后台关闭网管课");
            practiceGroup.setGroupStatus(GroupStatusEnum.CANCEL);
            practiceGroup.setUpdateTime(now);
            practiceGroupDao.update(practiceGroup);
        } else if (groupType.equals(GroupType.COMM)) {
            CoursesGroup coursesGroup = coursesGroupDao.get(groupId);
            if (Objects.isNull(coursesGroup)) {
                throw new BizException("指定的课程组不存在");
            }
            if (coursesGroup.getStatus().equals(GroupStatusEnum.CANCEL)) {
                throw new BizException("此课程组已关闭");
            }
            List<CourseSchedule> groupNotStartCourses = courseScheduleDao.findGroupNotStartCourses(groupId.toString(), groupType);
            if (!CollectionUtils.isEmpty(groupNotStartCourses)) {
                //剩余课程时长
                Integer totalCourseTime = groupNotStartCourses.size() * coursesGroup.getSingleClassMinutes();
                sysTenantAccountService.update(coursesGroup.getTeacherId(), totalCourseTime, "", SysTenantAccountDetail.TransType.RETURN, "", BigDecimal.ZERO, "关闭课程组");
            } else {
                throw new BizException("此课程组已结束");
            }
            cleanGroupInfo(groupId.toString(), GroupType.COMM);
            coursesGroup.setMemo("主动关闭课程组");
            coursesGroup.setStatus(GroupStatusEnum.CANCEL);
            coursesGroupDao.update(coursesGroup);
        }
        return BaseController.succeed();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelGroupOa(ReturnFeeDto returnFeeDto) {
        Long groupId = returnFeeDto.getGroupId();
        if (Objects.isNull(groupId)) {
            LOGGER.error("请指定课程组");
            return;
        }
        GroupType groupType = returnFeeDto.getGroupType();
        if (Objects.isNull(groupType)) {
            LOGGER.error("请指定课程组类型");
            return;
        }
        Date now = new Date();
        if (groupType.equals(GroupType.PRACTICE)) {
            PracticeGroup practiceGroup = practiceGroupDao.get(groupId);
            if (Objects.isNull(practiceGroup)) {
                LOGGER.error("课程组 {} 不存在",groupId);
                return;
            }
            if (!practiceGroup.getGroupStatus().equals(GroupStatusEnum.NORMAL) || practiceGroup.getCoursesExpireDate().before(now)) {
                LOGGER.error("课程组 {} {} 不可关闭",groupId,practiceGroup.getName());
                return;
            }
            if (Objects.nonNull(returnFeeDto.getAmount()) && returnFeeDto.getAmount().compareTo(BigDecimal.ZERO) > 0) {
                SysUserCashAccountLog sysUserCashAccountLog = new SysUserCashAccountLog();
                sysUserCashAccountLog.setGroupType(GroupType.PRACTICE);
                sysUserCashAccountLog.setUserId(practiceGroup.getStudentId());
                sysUserCashAccountLog.setOrganId(practiceGroup.getOrganId());
                sysUserCashAccountLog.setGroupId(practiceGroup.getId().toString());
                sysUserCashAccountLog.setAmount(returnFeeDto.getAmount());
                sysUserCashAccountLog.setReturnFeeType(ReturnFeeEnum.PRACTICE);
                sysUserCashAccountLog.setComment("OA审批关闭网管课");
                sysUserCashAccountLogDao.insert(sysUserCashAccountLog);
            }
//            ActivityUserMapper activityUserMapper = activityUserMapperDao.findVipUserMapper(practiceGroup.getId(),"PRACTICE",null);
//            quitActivityGive(activityUserMapper, returnFeeDto.getConfirmReturnActivityGive(), practiceGroup.getId(),GroupType.PRACTICE);
            cleanGroupInfo(groupId.toString(), GroupType.PRACTICE);
            practiceGroup.setMemo("OA审批关闭网管课");
            practiceGroup.setGroupStatus(GroupStatusEnum.CANCEL);
            practiceGroup.setUpdateTime(now);
            practiceGroupDao.update(practiceGroup);
        } else if (groupType.equals(GroupType.COMM)) {
            CoursesGroup coursesGroup = coursesGroupDao.get(groupId);
            if (Objects.isNull(coursesGroup)) {
                LOGGER.error("课程组 {} 不存在",groupId);
                return;
            }
            if (coursesGroup.getStatus().equals(GroupStatusEnum.CANCEL)) {
                LOGGER.error("课程组 {} {} 已关闭",groupId,coursesGroup.getName());
                return;
            }
            List<CourseSchedule> groupNotStartCourses = courseScheduleDao.findGroupNotStartCourses(groupId.toString(), groupType);
            if (!CollectionUtils.isEmpty(groupNotStartCourses)) {
                //剩余课程时长
                Integer totalCourseTime = groupNotStartCourses.size() * coursesGroup.getSingleClassMinutes();
                sysTenantAccountService.update(coursesGroup.getTeacherId(), totalCourseTime, "", SysTenantAccountDetail.TransType.RETURN, "", BigDecimal.ZERO, "关闭课程组");
            } else {
                LOGGER.error("课程组 {} {} 已结束",groupId,coursesGroup.getName());
                return;
            }
            cleanGroupInfo(groupId.toString(), GroupType.COMM);
            coursesGroup.setMemo("OA审批关闭课程组");
            coursesGroup.setStatus(GroupStatusEnum.CANCEL);
            coursesGroupDao.update(coursesGroup);
        }
    }

    @Override
    public void checkCancelGroupOa(ReturnFeeDto returnFeeDto) {
        Long groupId = returnFeeDto.getGroupId();
        Integer studentId = returnFeeDto.getStudentId();
        if (Objects.isNull(groupId)) {
            throw new BizException("请指定课程组");
        }
        if (Objects.isNull(studentId)) {
            throw new BizException("请指定学员");
        }
        GroupType groupType = returnFeeDto.getGroupType();
        if (Objects.isNull(groupType)) {
            throw new BizException("请指定课程组类型");
        }
        SysUser user = teacherDao.getUser(studentId);
        if(user == null){
            throw new BizException("用户 {} 信息不存在",studentId);
        }
        Student student = studentDao.get(studentId);
        if(student == null){
            throw new BizException("学员 {} 信息不存在",studentId);
        }
        Date now = new Date();
        if (groupType.equals(GroupType.PRACTICE)) {
            PracticeGroup practiceGroup = practiceGroupDao.get(groupId);
            if (Objects.isNull(practiceGroup)) {
                throw new BizException("课程组 {} 不存在",groupId);
            }
            if (!returnFeeDto.getStudentId().equals(practiceGroup.getStudentId())) {
                throw new BizException("学员 {} 不在 {} 课程组内",user.getUsername(),practiceGroup.getName());
            }
            if (!practiceGroup.getGroupStatus().equals(GroupStatusEnum.NORMAL) || practiceGroup.getCoursesExpireDate().before(now)) {
                throw new BizException("课程组 {} {} 不可关闭",groupId,practiceGroup.getName());
            }
            if (Objects.nonNull(returnFeeDto.getAmount()) && returnFeeDto.getAmount().compareTo(BigDecimal.ZERO) > 0) {
                Map<String, BigDecimal> studentSurplusCourseFee = practiceGroupService.getStudentSurplusCourseFee(groupId);
                BigDecimal suplusCourseFee = studentSurplusCourseFee.get("suplusCourseOriginalFee");
                if(returnFeeDto.getAmount().compareTo(suplusCourseFee) > 0){
                    throw new BizException("学员 {} {} 最大可退费金额为{}元",studentId,user.getUsername(),suplusCourseFee.toString());
                }
            }
        } else if (groupType.equals(GroupType.COMM)) {
            CoursesGroup coursesGroup = coursesGroupDao.get(groupId);
            if (Objects.isNull(coursesGroup)) {
                throw new BizException("课程组 {} 不存在",groupId);
            }
            if (coursesGroup.getStatus().equals(GroupStatusEnum.CANCEL)) {
                throw new BizException("课程组 {} {} 已关闭",groupId,coursesGroup.getName());
            }
            List<CourseSchedule> groupNotStartCourses = courseScheduleDao.findGroupNotStartCourses(groupId.toString(), groupType);
            if (CollectionUtils.isEmpty(groupNotStartCourses)) {
                throw new BizException("课程组 {} {} 已结束",groupId,coursesGroup.getName());
            }
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public HttpResponseResult quitActivityGive(ActivityUserMapper activityUserMapper, boolean confirmReturnActivityGive,Long groupId,GroupType groupType){
        /*if(activityUserMapper != null){
            if(activityUserMapper.getReturnFee()){
                return BaseController.succeed();
            }
            VipGroupActivity activity = vipGroupActivityDao.get(activityUserMapper.getActivityId());
            Boolean masterGroup;
            if(groupType == GroupType.VIP){
                //付费vip课
                if(groupId.equals(activityUserMapper.getVipGroupId())){
                    masterGroup = true;
                }else {
                    masterGroup = false;
                }
            }else {
                //付费网管课
                if(groupId.equals(activityUserMapper.getPracticeGroupId())){
                    masterGroup = true;
                }else {
                    masterGroup = false;
                }
            }
            if(!confirmReturnActivityGive){
                if(masterGroup){
                    if(activityUserMapper.getGiveMemberFlag() != 0){
                        String format = String.format("该课程组通过 %s 创建，含赠送会员，确认退学(关闭)后将扣减学员%s%s会员使用期限", activity.getName(), activity.getGiveMemberTime(), activity.getGivePeriodEnum().getMsg());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                    if(activityUserMapper.getGiveVipFlag() == 1 || activityUserMapper.getGivePracticeFlag() == 1){
                        String format = String.format("该课程组通过 %s 创建，学员赠课尚未排课，赠课排课资格将清除", activity.getName());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                    if(activityUserMapper.getGiveVipFlag() == 2){
                        VipGroup vipGroup = vipGroupDao.get(activityUserMapper.getGiveVipGroupId());
                        String format = String.format("该课程组通过 %s 创建，含赠课课程组%s %s，确认退学(关闭)后赠课课程组也将退学", activity.getName(),vipGroup.getId(),vipGroup.getName());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                    if(activityUserMapper.getGivePracticeFlag() == 2){
                        PracticeGroup practiceGroup = practiceGroupDao.get(activityUserMapper.getGivePracticeGroupId());
                        String format = String.format("该课程组通过 %s 创建，含赠课课程组%s %s，确认退学(关闭)后赠课课程组也将退学", activity.getName(),practiceGroup.getId(),practiceGroup.getName());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                }else {
                    if(activityUserMapper.getMemberFlag() != 0){
                        String format = String.format("该课程组通过 %s 赠送创建，含付费会员，确认退学(关闭)后将扣减学员%s%s会员使用期限", activity.getName(), activity.getGiveMemberTime(), activity.getGivePeriodEnum().getMsg());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                    if(activityUserMapper.getVipFlag() == 1 || activityUserMapper.getPracticeFlag() == 1){
                        String format = String.format("该课程组通过 %s 赠送创建，学员付费课尚未排课，付费课排课资格将清除", activity.getName());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                    if(activityUserMapper.getVipFlag() == 2){
                        VipGroup vipGroup = vipGroupDao.get(activityUserMapper.getVipGroupId());
                        String format = String.format("该课程组通过 %s 赠送创建，含付费课课程组%s %s，确认退学(关闭)后付费课课程组也将退学", activity.getName(),vipGroup.getId(),vipGroup.getName());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                    if(activityUserMapper.getPracticeFlag() == 2){
                        PracticeGroup practiceGroup = practiceGroupDao.get(activityUserMapper.getPracticeGroupId());
                        String format = String.format("该课程组通过 %s 赠送创建，含付费课课程组%s %s，确认退学(关闭)后付费课课程组也将退学", activity.getName(),practiceGroup.getId(),practiceGroup.getName());
                        return BaseController.failed(HttpStatus.PARTIAL_CONTENT,format);
                    }
                }
            }
            Integer studentId = activityUserMapper.getUserId();
            activityUserMapper.setReturnFee(true);
            Date now = new Date();
            if(activityUserMapper.getGiveMemberFlag() == 2 || activityUserMapper.getMemberFlag() == 2 ){
                //只退还赠送的时间
                Integer giveMemberTime = activity.getGiveMemberTime();
                PeriodEnum givePeriodEnum = activity.getGivePeriodEnum();
                Long giveMemberOrderId = activityUserMapper.getGiveMemberOrderId();
                if(activityUserMapper.getMemberFlag() == 2){
                    giveMemberTime = activity.getMemberTime();
                    givePeriodEnum = activity.getPeriodEnum();
                    giveMemberOrderId = activityUserMapper.getMemberOrderId();
                }
                switch (givePeriodEnum){
                    case DAY:
                        break;
                    case MONTH:
                        giveMemberTime = giveMemberTime * 30;
                        break;
                    case QUARTERLY:
                        giveMemberTime = giveMemberTime * 30 * 3;
                        break;
                    case YEAR_HALF:
                        giveMemberTime = giveMemberTime * 30 * 6;
                        break;
                    case YEAR:
                        giveMemberTime = giveMemberTime * 30 * 12;
                        break;
                    default:
                        throw new BizException("错误的会员周期类型");
                }
                Student student = studentDao.get(studentId);
                Date membershipEndTime = student.getMembershipEndTime();
                if(membershipEndTime != null){
                    if(membershipEndTime.compareTo(now) > 0){
                        membershipEndTime = DateUtil.addDays(membershipEndTime, -giveMemberTime);
                        student.setMembershipEndTime(membershipEndTime);
                        if(membershipEndTime.compareTo(now) < 0){
                            student.setMemberRankSettingId(null);
                            student.setExperienceMemberRankSettingId(null);
                        }
                        student.setUpdateTime(now);
                        studentDao.updateStudentMember(student);
                    }
                }

                CloudTeacherOrder cloudTeacherOrder = cloudTeacherOrderDao.get(giveMemberOrderId);
                cloudTeacherOrder.setStatus(3);
                cloudTeacherOrder.setRemark("退还活动关联会员时长");
                cloudTeacherOrderDao.update(cloudTeacherOrder);
            }
            if((masterGroup && activityUserMapper.getGiveVipFlag() == 2) || (!masterGroup && activityUserMapper.getVipFlag() == 2)){
                Long giveVipGroupId = activityUserMapper.getGiveVipGroupId();
                if(!masterGroup){
                    giveVipGroupId = activityUserMapper.getVipGroupId();
                }
                VipGroup giveVipGroup = vipGroupDao.get(giveVipGroupId);
                ClassGroup giveClassGroup = classGroupDao.findByVipGroup(giveVipGroupId, null);
                ClassGroupStudentMapper giveClassGroupStudentMapper = classGroupStudentMapperDao.findClassGroupStudent(giveClassGroup.getId(),studentId);
                giveClassGroupStudentMapper.setStatus(ClassGroupStudentStatusEnum.QUIT);
                classGroupStudentMapperDao.update(giveClassGroupStudentMapper);

                courseScheduleService.batchDeleteMusicGroupCourseWithStudent(giveVipGroupId.toString(),studentId,GroupType.VIP);

                List<Integer> studentPaymentIds = courseScheduleStudentPaymentDao.findNotStartCourseStudentPaymentIdsWithClassGroupAndStudent(giveClassGroup.getId(), studentId);
                if(!CollectionUtils.isEmpty(studentPaymentIds)){
                    courseScheduleStudentPaymentDao.batchDeleteWithID(studentPaymentIds);
                }
                VipGroupCategory giveVipGroupCategory = vipGroupCategoryDao.get(giveVipGroup.getVipGroupCategoryId());

                if(!giveVipGroup.getStatus().equals(VipGroupStatusEnum.APPLYING) || (Objects.nonNull(giveVipGroupCategory) && giveVipGroupCategory.getMusicTheory())){
                    BigDecimal offlineTeacherSalary=BigDecimal.ZERO;

                    List<CourseScheduleTeacherSalary> courseScheduleTeacherSalaries = courseScheduleTeacherSalaryDao.findByGroupWithNotStart(giveVipGroupId.toString(), GroupType.VIP.getCode());
                    if(!CollectionUtils.isEmpty(courseScheduleTeacherSalaries)){
                        for(int i=0;i<courseScheduleTeacherSalaries.size();i++){
                            if(!courseScheduleTeacherSalaries.get(i).getEnableChangeSalary()){
                                continue;
                            }
                            Map<String, BigDecimal> salary = vipGroupService.countVipGroupPredictFee1(giveVipGroup, giveVipGroup.getUserId(), courseScheduleTeacherSalaries.get(i).getCourseScheduleId());
                            if(!masterGroup){
                                offlineTeacherSalary = salary.get("offlineTeacherSalary");
                            }else {
                                offlineTeacherSalary = salary.get("giveOfflineTeacherSalary");
                            }
                            courseScheduleTeacherSalaries.get(i).setExpectSalary(offlineTeacherSalary);
                        }
                        courseScheduleTeacherSalaryDao.batchUpdateTeacherExpectSalarys(courseScheduleTeacherSalaries);
                    }
                }

                giveClassGroup.setStudentNum(giveClassGroup.getStudentNum() - 1);
                classGroupDao.update(giveClassGroup);

                //学员退出班级群
                imGroupMemberService.quit(giveClassGroup.getId().toString(), studentId);
            }
            if((masterGroup && activityUserMapper.getGivePracticeFlag() == 2) || (!masterGroup && activityUserMapper.getPracticeFlag() == 2)){
                Long givePracticeGroupId = activityUserMapper.getGivePracticeGroupId();
                if(!masterGroup){
                    givePracticeGroupId = activityUserMapper.getPracticeGroupId();
                }
                PracticeGroup practiceGroup = practiceGroupService.get(givePracticeGroupId);
                cleanGroupInfo(givePracticeGroupId.toString(), GroupType.PRACTICE);
                practiceGroup.setMemo("关闭活动关联网管课");
                practiceGroup.setGroupStatus(GroupStatusEnum.CANCEL);
                practiceGroup.setUpdateTime(now);
                practiceGroupService.update(practiceGroup);
            }
            activityUserMapperDao.update(activityUserMapper);
        }*/
        return BaseController.succeed();
    }

}
