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

import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PayUserType.SCHOOL;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PayUserType.STUDENT;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum.*;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentType.ADD_COURSE;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentType.ADD_STUDENT;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentType.MUSIC_APPLY;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentType.MUSIC_RENEW;
import static com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentType.SPAN_GROUP_CLASS_ADJUST;
import static com.ym.mec.biz.dal.enums.PaymentStatusEnum.YES;

import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.*;
import com.ym.mec.biz.event.source.GroupEventSource;
import com.ym.mec.biz.service.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.auth.api.entity.SysUserRole;
import com.ym.mec.biz.dal.dto.MusicGroupPaymentCalenderDto.MusicGroupPaymentDateRange;
import com.ym.mec.biz.dal.entity.CourseSchedule.CourseScheduleType;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PayUserType;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentCalenderStatusEnum;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender.PaymentType;
import com.ym.mec.biz.dal.entity.MusicGroupStudentFee.PaymentStatus;
import com.ym.mec.biz.dal.page.MusicGroupPaymentCalenderQueryInfo;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.IdGeneratorService;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
import com.ym.mec.util.collection.MapUtil;
import com.ym.mec.util.date.DateUtil;

@Service
public class MusicGroupPaymentCalenderServiceImpl extends BaseServiceImpl<Long, MusicGroupPaymentCalender> implements MusicGroupPaymentCalenderService {

	@Autowired
	private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;
	@Autowired
	private MusicGroupPaymentCalenderDetailDao musicGroupPaymentCalenderDetailDao;
	@Autowired
	private MusicGroupStudentFeeDao musicGroupStudentFeeDao;
	
	@Autowired
	private MusicGroupPaymentCalenderCourseSettingsDao musicGroupPaymentCalenderCourseSettingsDao;
	
	@Autowired
	private MusicGroupOrganizationCourseSettingsDetailDao musicGroupOrganizationCourseSettingsDetailDao;

	@Autowired
	private MusicGroupOrganizationCourseSettingsDao musicGroupOrganizationCourseSettingsDao;
	
	@Autowired
	private OrganizationCourseUnitPriceSettingsDao organizationCourseUnitPriceSettingsDao;
	
	@Autowired
	private MusicGroupPaymentStudentCourseDetailDao musicGroupPaymentStudentCourseDetailDao;

	@Autowired
	private MusicGroupSchoolTermCourseDetailDao musicGroupSchoolTermCourseDetailDao;

	@Autowired
	private MusicGroupSchoolTermStudentCourseDetailDao musicGroupSchoolTermStudentCourseDetailDao;
	
	@Autowired
	private IdGeneratorService idGeneratorService;
	
	@Autowired
	private ClassGroupService classGroupService;
	
	@Autowired
	private MusicGroupDao musicGroupDao;

	@Autowired
	private ClassGroupDao classGroupDao;

	@Autowired
	private CourseScheduleDao courseScheduleDao;

	@Autowired
	private CourseScheduleTeacherSalaryDao courseScheduleTeacherSalaryDao;

	@Autowired
	private TeacherAttendanceDao teacherAttendanceDao;
	@Autowired
	private MusicGroupBuildLogDao musicGroupBuildLogDao;
	@Autowired
	private SysConfigDao sysConfigDao;
	@Autowired
	private OrganizationDao organizationDao;
	@Autowired
	private SysMessageService sysMessageService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private ImGroupMemberService imGroupMemberService;
	@Autowired
	private ImUserFriendService imUserFriendService;
	@Autowired
	private ClassGroupStudentMapperDao classGroupStudentMapperDao;
	@Autowired
	private MusicGroupStudentClassAdjustDao musicGroupStudentClassAdjustDao;
	@Autowired
	private MusicGroupPaymentCalenderDetailService musicGroupPaymentCalenderDetailService;
	@Autowired
	private MusicGroupPaymentCalenderStudentDetailDao musicGroupPaymentCalenderStudentDetailDao;
	@Autowired
	private StudentRegistrationDao studentRegistrationDao;
	@Autowired
	private GroupEventSource groupEventSource;

	@Autowired
	private StudentPaymentOrderDao studentPaymentOrderDao;
	@Autowired
	private MemberFeeSettingDao memberFeeSettingDao;
	@Autowired
	private MemberRankSettingDao memberRankSettingDao;
	@Autowired
	private StudentService studentService;
	@Autowired
	private CloudTeacherOrderDao cloudTeacherOrderDao;

	@Override
	public BaseDAO<Long, MusicGroupPaymentCalender> getDAO() {
		return musicGroupPaymentCalenderDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String create(MusicGroupPaymentCalenderDto musicGroupPaymentCalenderDto) {

		String musicGroupId = musicGroupPaymentCalenderDto.getMusicGroupId();

		PaymentType paymentType = musicGroupPaymentCalenderDto.getPaymentType();

		PayUserType payUserType = musicGroupPaymentCalenderDto.getPayUserType();

		List<MusicGroupPaymentDateRange> musicGroupPaymentDateRangeList = musicGroupPaymentCalenderDto.getMusicGroupPaymentDateRangeList();

		if(musicGroupPaymentCalenderDto.getMemberRankSettingId() != null){
			if(paymentType != ADD_STUDENT && paymentType != MUSIC_APPLY){
				throw new BizException("操作失败：{} 不支持会员收费",paymentType.getDesc());
			}
		}

		if ((paymentType == PaymentType.ADD_COURSE)) {
			if(musicGroupPaymentDateRangeList.size() > 1){
				throw new BizException("[临时加课]不支持多周期缴费");
			}
		}
		MusicGroup musicGroup = musicGroupDao.getLocked(musicGroupId);
		if (musicGroup == null) {
			throw new BizException("乐团查询失败，请检查参数");
		}
		if (paymentType == PaymentType.ADD_STUDENT) {
			//获取缴费状态在审核中或者已拒绝的缴费项目的学员
			Integer userId = Integer.parseInt(musicGroupPaymentCalenderDto.getStudentIds());
			if(musicGroup.getCourseViewType() == CourseViewTypeEnum.MEMBER_FEE){
				//该学员是否在其他的会员团
				boolean hasMemberGroup = studentRegistrationDao.checkHasMemberGroup(musicGroupId,userId);
				if(hasMemberGroup){
					throw new BizException("操作失败：学员已在其他系统收费乐团中，不可报名该乐团请联系教务老师");
				}
			}

			String studentIds = musicGroupPaymentCalenderDao.queryCalenderStudentIds(musicGroupId,null);
			if(StringUtils.isNotEmpty(studentIds)){
				if(studentIds.contains(userId.toString())){
					throw new BizException("创建缴费失败：所选学员有待审核或已拒绝的缴费项目");
				}
			}
			List<MusicGroupPaymentCalenderDetail> musicGroupPaymentCalenderDetailList = musicGroupPaymentCalenderDetailDao.queryNotPaymentStudentByUserIdAndMusicGroupId(userId, musicGroupId);
			if(musicGroupPaymentCalenderDetailList != null && musicGroupPaymentCalenderDetailList.size() > 0){
				throw new BizException("创建缴费失败：已存在缴费项目");
			}
		}

		if (musicGroup.getStatus() != MusicGroupStatusEnum.PROGRESS && musicGroup.getStatus() != MusicGroupStatusEnum.PRE_BUILD_FEE) {
			throw new BizException("创建缴费失败：已存在缴费项目");
		}
		
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("请登录");
		}
		if (musicGroup.getCourseViewType() == CourseViewTypeEnum.MEMBER_FEE && musicGroupPaymentDateRangeList.size() > 1) {
			throw new BizException("创建缴费失败：会员收费乐团不支持多周期缴费");
		}
		// 所有缴费项目已完成排课才能创建下一个缴费项目
		String orignBatchNo = musicGroupPaymentStudentCourseDetailDao.getUnUseBatchNoWithStudentAndCourseTypeAndCourseMinutes(musicGroupId, null, null,musicGroupPaymentCalenderDto.getIgnoreBatchNoList());
		if (StringUtils.isNoneBlank(orignBatchNo)) {
			if(StringUtils.isEmpty(musicGroupPaymentCalenderDto.getBatchNo()) || !musicGroupPaymentCalenderDto.getBatchNo().equals(orignBatchNo)){
				throw new BizException("当前乐团存在未排课的缴费项目，请先完成排课再操作");
			}
		}

		// 不是进行中，只能创建一次缴费
		if (musicGroup.getStatus() != MusicGroupStatusEnum.PROGRESS) {
			List<MusicGroupPaymentCalender> list = musicGroupPaymentCalenderDao.findByMusicGroupId(musicGroupId);
			if (list.size() > 0) {
				throw new BizException("创建失败，已经存在缴费信息");
			}
		}

		PaymentCalenderStatusEnum status = PaymentCalenderStatusEnum.NO;

		Date date = new Date();
		String batchNo = idGeneratorService.generatorId() + "";

		// 获取设置的课程收费标准
		List<MusicGroupPaymentCalenderCourseSettings> musicGroupPaymentCalenderCourseSettingsList = musicGroupPaymentCalenderDto
				.getMusicGroupPaymentCalenderCourseSettingsList();

		if(musicGroupPaymentCalenderCourseSettingsList != null){
			// 必须课程缴费金额为0时，报名、续费不能建多期
			if(paymentType == MUSIC_APPLY || paymentType == MUSIC_RENEW){
				BigDecimal noOptionalCoursePrice = musicGroupPaymentCalenderCourseSettingsList.stream()
						.filter(e -> !e.getIsStudentOptional()).map(MusicGroupPaymentCalenderCourseSettings::getCourseCurrentPrice)
						.reduce(BigDecimal.ZERO, BigDecimal::add);
				if(noOptionalCoursePrice.compareTo(BigDecimal.ZERO) <= 0 && musicGroupPaymentDateRangeList.size() > 1){
					throw new BizException("必选课程缴费金额为0时不支持多周期缴费");
				}
			}
		}
		//会员原价
		BigDecimal memberPaymentAmount = BigDecimal.ZERO;
		BigDecimal originalMemberPaymentAmount = BigDecimal.ZERO;
		if (payUserType == SCHOOL) {
			status = AUDITING;
		} else {
			if(musicGroupPaymentCalenderCourseSettingsList != null){
				if (paymentType == MUSIC_APPLY || paymentType == MUSIC_RENEW) {
					// 当前缴费的课程费用
					Map<CourseScheduleType, BigDecimal> currentCoursePrice = musicGroupPaymentCalenderCourseSettingsList.stream().collect(
							Collectors
									.toMap(MusicGroupPaymentCalenderCourseSettings::getCourseType, MusicGroupPaymentCalenderCourseSettings::getCourseCurrentPrice));

					// 查询默认课程费用
					Integer musicGroupOrganizationCourseSettingId = musicGroupPaymentCalenderDto.getMusicGroupOrganizationCourseSettingId();
					Map<CourseScheduleType, BigDecimal> defaultCoursePrice = musicGroupOrganizationCourseSettingsDetailDao
							.queryByMusicGroupOrganizationCourseSettingsId(musicGroupOrganizationCourseSettingId)
							.stream()
							.collect(
									Collectors.toMap(MusicGroupOrganizationCourseSettingsDetail::getCourseType,
											MusicGroupOrganizationCourseSettingsDetail::getCourseCurrentPrice));

					// 相同类型的课程如果修改了课程费用，需要走审批
					for (Entry<CourseScheduleType, BigDecimal> entry : currentCoursePrice.entrySet()) {
						if (defaultCoursePrice.get(entry.getKey()).compareTo(entry.getValue()) != 0) {
							status = AUDITING;
							break;
						}
					}
				} else if (paymentType == PaymentType.ADD_COURSE || paymentType == PaymentType.ADD_STUDENT) {
					PaymentCalenderStatusEnum dtoStatus = musicGroupPaymentCalenderDto.getStatus();
					if(dtoStatus != null && dtoStatus == AUDITING){
						status = AUDITING;
					}else {
						// 如果是课程收费，判断是否审核
						for (MusicGroupPaymentCalenderCourseSettings courseSettings : musicGroupPaymentCalenderCourseSettingsList) {
							OrganizationCourseUnitPriceSettings defaultUnitPrice = organizationCourseUnitPriceSettingsDao.queryByOrganIdAndCourseTypeAndChargeType(
									musicGroup.getOrganId(), courseSettings.getCourseType(), musicGroup.getChargeTypeId());
							if (defaultUnitPrice == null) {
								throw new BizException("请先设置分部课程类型单价");
							}
							if (courseSettings.getCourseTotalMinuties() != 0) {

								if (defaultUnitPrice.getUnitPrice().multiply(new BigDecimal(courseSettings.getCourseTotalMinuties())).setScale(0, BigDecimal.ROUND_HALF_UP)
										.compareTo(courseSettings.getCourseCurrentPrice()) != 0) {

									status = AUDITING;
									break;
								}
							} else {
								status = AUDITING;
								break;
							}
						}
					}
				}else if(paymentType == SPAN_GROUP_CLASS_ADJUST){
					status = musicGroupPaymentCalenderDto.getStatus();
				}
			}
			if(musicGroupPaymentCalenderDto.getMemberRankSettingId() != null){
				//会员价格是否变动
				MemberFeeSetting memberFee = memberFeeSettingDao.findByRankIdAndOrganId(musicGroup.getOrganId(), musicGroupPaymentCalenderDto.getMemberRankSettingId());
				if(memberFee == null){
					throw new BizException("操作失败：请配置当前分部会员收费标准");
				}
				switch (musicGroupPaymentCalenderDto.getMemberValidDate()){
					case 1 :
						memberPaymentAmount = memberFee.getCurrentMonthFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						originalMemberPaymentAmount = memberFee.getCurrentMonthFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						break;
					case 6 :
						memberPaymentAmount = memberFee.getCurrentHalfYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						originalMemberPaymentAmount = memberFee.getOriginalHalfYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						break;
					case 12 :
						memberPaymentAmount = memberFee.getCurrentYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						originalMemberPaymentAmount = memberFee.getOriginalYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						break;
					default:
						throw new BizException("请选择正确的会员有效期");
				}
				if(memberPaymentAmount.compareTo(musicGroupPaymentCalenderDto.getMemberPaymentAmount()) != 0){
					status = AUDITING;
				}
			}
		}

		int times = musicGroupPaymentDateRangeList.size();

		// 排序
		Collections.sort(musicGroupPaymentDateRangeList, new Comparator<MusicGroupPaymentDateRange>() {

			@Override
			public int compare(MusicGroupPaymentDateRange o1, MusicGroupPaymentDateRange o2) {
				return o1.getPaymentValidStartDate().compareTo(o2.getPaymentValidStartDate());
			}
		});

		for (int i = 0; i < musicGroupPaymentDateRangeList.size(); i++) {

			MusicGroupPaymentDateRange musicGroupPaymentDateRange = musicGroupPaymentDateRangeList.get(i);

			MusicGroupPaymentCalender musicGroupPaymentCalender = new MusicGroupPaymentCalender();
			musicGroupPaymentCalender.setAttribute1(musicGroupPaymentCalenderDto.getAttribute1());
			musicGroupPaymentCalender.setOriginalMemberPaymentAmount(originalMemberPaymentAmount);
			musicGroupPaymentCalender.setAttribute2(musicGroupPaymentCalenderDto.getAttribute2());
			musicGroupPaymentCalender.setDeadlinePaymentDate(musicGroupPaymentDateRange.getDeadlinePaymentDate());
			musicGroupPaymentCalender.setIsGiveMusicNetwork(musicGroupPaymentCalenderDto.getIsGiveMusicNetwork());
			musicGroupPaymentCalender.setMemo(musicGroupPaymentCalenderDto.getMemo());
			musicGroupPaymentCalender.setMusicGroupId(musicGroupId);
			musicGroupPaymentCalender.setMusicGroupOrganizationCourseSettingId(musicGroupPaymentCalenderDto.getMusicGroupOrganizationCourseSettingId());

			BigDecimal totalPaymentAmount = BigDecimal.ZERO;
			if(musicGroupPaymentCalenderCourseSettingsList != null){
				List<MusicGroupPaymentCalenderCourseSettings> newCSList = new ArrayList<MusicGroupPaymentCalenderCourseSettings>(
						musicGroupPaymentCalenderCourseSettingsList.size());
				for (MusicGroupPaymentCalenderCourseSettings pccs : musicGroupPaymentCalenderCourseSettingsList) {

					MusicGroupPaymentCalenderCourseSettings tempPccs = new MusicGroupPaymentCalenderCourseSettings();
					try {
						BeanUtils.copyProperties(tempPccs, pccs);
					} catch (Exception e) {
						throw new BizException("克隆对象出错", e);
					}
					if (i == 0) {
						if (pccs.getIsStudentOptional()) {
							tempPccs.setCourseCurrentPrice(pccs.getCourseCurrentPrice());
							tempPccs.setCourseOriginalPrice(pccs.getCourseOriginalPrice());
							tempPccs.setCourseTotalMinuties(pccs.getCourseTotalMinuties());
						} else {
							tempPccs.setCourseCurrentPrice(pccs.getCourseCurrentPrice().subtract(
									pccs.getCourseCurrentPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(times - 1))
											.setScale(0, BigDecimal.ROUND_DOWN)));
							tempPccs.setCourseOriginalPrice(pccs.getCourseOriginalPrice().subtract(
									pccs.getCourseOriginalPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(times - 1))
											.setScale(0, BigDecimal.ROUND_DOWN)));
							tempPccs.setCourseTotalMinuties(pccs.getCourseTotalMinuties() - pccs.getCourseTotalMinuties() / times * (times - 1));
						}
					} else {
						if (!pccs.getIsStudentOptional()) {
							tempPccs.setCourseCurrentPrice(pccs.getCourseCurrentPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN));
							tempPccs.setCourseOriginalPrice(pccs.getCourseOriginalPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN));
							tempPccs.setCourseTotalMinuties(pccs.getCourseTotalMinuties() / times);
						} else {
							continue;
						}
					}
					totalPaymentAmount = totalPaymentAmount.add(tempPccs.getCourseCurrentPrice());
					newCSList.add(tempPccs);
				}
				musicGroupPaymentCalender.setMusicGroupPaymentCalenderCourseSettingsList(newCSList);
			}
			musicGroupPaymentCalender.setPaymentAmount(totalPaymentAmount);
			musicGroupPaymentCalender.setPaymentPattern(musicGroupPaymentCalenderDto.getPaymentPattern());
			musicGroupPaymentCalender.setPaymentValidEndDate(musicGroupPaymentDateRange.getPaymentValidEndDate());
			musicGroupPaymentCalender.setPaymentValidStartDate(musicGroupPaymentDateRange.getPaymentValidStartDate());
			
			if (paymentType == PaymentType.MUSIC_APPLY) {
				if (i == 0) {
					musicGroupPaymentCalender.setPaymentType(paymentType);
				} else {
					musicGroupPaymentCalender.setPaymentType(PaymentType.MUSIC_RENEW);
				}
			} else {
				musicGroupPaymentCalender.setPaymentType(paymentType);
			}
			musicGroupPaymentCalender.setPayUserType(musicGroupPaymentCalenderDto.getPayUserType());
			musicGroupPaymentCalender.setStartPaymentDate(musicGroupPaymentDateRange.getStartPaymentDate());
			musicGroupPaymentCalender.setStudentIds(musicGroupPaymentCalenderDto.getStudentIds());
			musicGroupPaymentCalender.setOperator(sysUser.getId());
			musicGroupPaymentCalender.setCreateTime(date);
			musicGroupPaymentCalender.setUpdateTime(date);
			musicGroupPaymentCalender.setStatus(status);

			//缴费截止日期默认三天后
			if (musicGroupPaymentCalender.getDeadlinePaymentDate() == null && payUserType == PayUserType.STUDENT) {
				musicGroupPaymentCalender.setDeadlinePaymentDate(DateUtil.addDays(musicGroupPaymentCalender.getStartPaymentDate(), 3));
			}
			//校验缴费有效期冲突
			if (paymentType != ADD_STUDENT && paymentType != ADD_COURSE && paymentType != SPAN_GROUP_CLASS_ADJUST) {
				if(musicGroupPaymentCalender.getPaymentValidStartDate() != null){
					int count = musicGroupPaymentCalenderDao.queryIntersectionByValidDate(musicGroupId, musicGroupPaymentCalender.getPayUserType(),
							musicGroupPaymentCalender.getPaymentValidStartDate(), musicGroupPaymentCalender.getPaymentValidEndDate(), null);
					if (count > 0) {
						throw new BizException("缴费有效期存在冲突，请修改缴费有效期");
					}
				}
			}

			if (paymentType == PaymentType.ADD_STUDENT) {
				musicGroupPaymentCalender.setPayUserType(STUDENT);
				musicGroupPaymentCalender.setIsGiveMusicNetwork(false);
				if (musicGroupPaymentCalender.getStatus() != AUDITING) {
					musicGroupPaymentCalender.setExpectNum(1);
				}
			}
			if (musicGroupPaymentCalender.getStatus() != AUDITING) {
				if (date.after(musicGroupPaymentCalender.getStartPaymentDate())) {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OPEN);
				} else if (date.after(musicGroupPaymentCalender.getDeadlinePaymentDate())) {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OVER);
				} else {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.NO);
				}
			}

			// 设置批次号
			musicGroupPaymentCalender.setBatchNo(batchNo);
			//设置会员缴费金额、级别以及有效期
			musicGroupPaymentCalender.setMemberPaymentAmount(musicGroupPaymentCalenderDto.getMemberPaymentAmount());
			musicGroupPaymentCalender.setMemberRankSettingId(musicGroupPaymentCalenderDto.getMemberRankSettingId());
			musicGroupPaymentCalender.setMemberValidDate(musicGroupPaymentCalenderDto.getMemberValidDate());
			musicGroupPaymentCalenderDto.setBatchNo(batchNo);
			musicGroupPaymentCalenderDao.insert(musicGroupPaymentCalender);

			//课程费用列表
			List<MusicGroupPaymentCalenderCourseSettings> currentMusicGroupPaymentCalenderCourseSettings = musicGroupPaymentCalender
					.getMusicGroupPaymentCalenderCourseSettingsList();

			if (currentMusicGroupPaymentCalenderCourseSettings != null && currentMusicGroupPaymentCalenderCourseSettings.size() > 0) {
				MusicGroupOrganizationCourseSettings courseSettings = musicGroupOrganizationCourseSettingsDao.get(musicGroupPaymentCalender
						.getMusicGroupOrganizationCourseSettingId());
				for (MusicGroupPaymentCalenderCourseSettings musicGroupPaymentCalenderCourseSettings : currentMusicGroupPaymentCalenderCourseSettings) {
					musicGroupPaymentCalenderCourseSettings.setMusicGroupPaymentCalenderId(musicGroupPaymentCalender.getId());
					if (courseSettings != null) {
						musicGroupPaymentCalenderCourseSettings.setName(courseSettings.getName());
					}
				}
				musicGroupPaymentCalenderCourseSettingsDao.batchInsert(currentMusicGroupPaymentCalenderCourseSettings);
			}

			//如果是跨团班级合并,保存用户缴费详情
			List<MusicGroupPaymentCalenderStudentDetail> musicGroupPaymentCalenderStudentDetails = null;
			if (musicGroupPaymentCalender.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
				musicGroupPaymentCalenderStudentDetails = musicGroupPaymentCalenderDto.getMusicGroupPaymentCalenderStudentDetails();
				//记录学员缴费详情
				musicGroupPaymentCalenderStudentDetailDao.batchInsert(musicGroupPaymentCalenderStudentDetails,musicGroupPaymentCalender.getBatchNo());
			}

			if (musicGroupPaymentCalender.getStatus() != AUDITING) {
				// 如果是进行中加学生
				if (musicGroupPaymentCalender.getPaymentType() == PaymentType.ADD_STUDENT) {
					addStudent(musicGroupPaymentCalender, currentMusicGroupPaymentCalenderCourseSettings,musicGroup);
				}else if(musicGroupPaymentCalender.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
					//如果是跨团班级合并,添加学员
					musicGroupPaymentCalenderDetailService.batchAdd(musicGroupPaymentCalender,musicGroupPaymentCalenderStudentDetails);
				}else if(musicGroupPaymentCalenderDto.getMergeClassFlag()){
					//缴费项目添加学员
					int[] ints = Arrays.stream(musicGroupPaymentCalenderDto.getStudentIds().split(",")).mapToInt(Integer::parseInt).toArray();
					Set<Integer> list2 = Arrays.stream(ints).boxed().collect(Collectors.toSet());
					musicGroupPaymentCalenderDetailService.batchAdd(musicGroupPaymentCalender.getId(),list2,musicGroup);
				}
				//将0元未缴费学员缴费状态更新为已缴费
				int j = musicGroupPaymentCalenderDetailDao.updateNoPaymentAndZeroPaymentStatus(musicGroupPaymentCalender.getId());
				musicGroupPaymentCalender.setActualNum((musicGroupPaymentCalender.getActualNum()==null?0:musicGroupPaymentCalender.getActualNum()) + j);
				musicGroupPaymentCalenderDao.update(musicGroupPaymentCalender);
			}
		}

		// 如果是报名，需要修改乐团状态
		if (paymentType == MUSIC_APPLY) {
			if (status != AUDITING) {
				musicGroup.setStatus(MusicGroupStatusEnum.APPLY);
				// 记录操作日志
				musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroupId, "报名缴费项目创建成功（创建缴费 -> 报名中）", sysUser.getId(), ""));
			} else {
				musicGroup.setStatus(MusicGroupStatusEnum.FEE_AUDIT);
				// 记录操作日志
				musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroupId, "报名缴费项目创建成功（创建缴费 -> 费用审核中）", sysUser.getId(), ""));
			}
			musicGroup.setUpdateTime(date);
			musicGroupDao.update(musicGroup);
		} else if (paymentType == ADD_STUDENT) {
			if (status != AUDITING) {
				// 学生加到班级
				String classGroupIdStr = musicGroupPaymentCalenderDto.getAttribute1();
				classGroupService.addStudentToClassGroupAndCourseArranging(Integer.parseInt(musicGroupPaymentCalenderDto.getStudentIds()), classGroupIdStr,
						batchNo, musicGroupPaymentCalenderCourseSettingsList,musicGroup);
			}
		}

		if (status == AUDITING) {
			// 如果是审核中
			Set<Integer> roleIds = new HashSet<>(1);
			roleIds.add(SysUserRole.ADMINISTRATOR);
			Organization organization = organizationDao.get(musicGroup.getOrganId());
			sysMessageService.batchSeoMessage(musicGroupDao.queryUserIdByRoleId(roleIds, musicGroup.getOrganId()),
					MessageTypeEnum.BACKSTAGE_PAYMENT_CALENDER_AUDIT, "", organization.getName(), musicGroup.getName());
		}else {
			imUserFriendService.refreshGroupImUserFriend(musicGroupId, GroupType.MUSIC);
		}

		return batchNo;
	}

	@Override
	public Object getDetail(Long id) {
		MusicGroupPaymentCalender calender = musicGroupPaymentCalenderDao.get(id);
		Map<Long, Long> expectNumMap = MapUtil.convertIntegerMap(musicGroupPaymentCalenderDao.countExpectNum(id));
		Map<Long, Long> actualNumMap = MapUtil.convertIntegerMap(musicGroupPaymentCalenderDao.countActualNum(id));
		calender.setActualNum(actualNumMap.get(id) == null ? 0 : actualNumMap.get(id).intValue());
		calender.setExpectNum(expectNumMap.get(id) == null ? 0 : expectNumMap.get(id).intValue());
		BigDecimal sumActualAmount = musicGroupPaymentCalenderDetailDao.sumActualAmount(id);
		//收费标准名称
		String name = musicGroupPaymentCalenderDao.getCalenderSettingsName(id);
		List<MusicGroupPaymentCalenderCourseSettings> musicGroupPaymentCalenderCourseSettings = musicGroupPaymentCalenderCourseSettingsDao.queryCalenderCourseSettings(id);
		Map<String, Object> result = new HashMap<>(4);
		result.put("musicGroupPaymentCalenderCourseSettings", musicGroupPaymentCalenderCourseSettings);
		if(calender.getMemberRankSettingId() != null){
			MemberRankSetting memberRankSetting = memberRankSettingDao.get(calender.getMemberRankSettingId());
			calender.setMemberRankSettingName(memberRankSetting.getName());
		}
		result.put("calender", calender);
		result.put("calenderSettingsName", name);
		result.put("sumActualAmount", sumActualAmount);
		return result;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String merge(MusicGroupPaymentCalenderDto musicGroupPaymentCalenderDto) {
		
		String batchNo = musicGroupPaymentCalenderDto.getBatchNo();
		
		List<MusicGroupPaymentCalender> musicGroupPaymentCalenderList = musicGroupPaymentCalenderDao.findByBatchNo(batchNo);
		if(musicGroupPaymentCalenderList == null || musicGroupPaymentCalenderList.size() == 0){
			throw new BizException("[批次号]参数错误");
		}
		
		PaymentCalenderStatusEnum status = musicGroupPaymentCalenderList.get(0).getStatus();
		if(status != PaymentCalenderStatusEnum.REJECT && status != PaymentCalenderStatusEnum.DRAFT){
			throw new BizException("{}的缴费状态不允许修改",status.getDesc());
		}
		String musicGroupId = musicGroupPaymentCalenderDto.getMusicGroupId();

		PaymentType paymentType = musicGroupPaymentCalenderDto.getPaymentType();

		PayUserType payUserType = musicGroupPaymentCalenderDto.getPayUserType();

		List<MusicGroupPaymentDateRange> musicGroupPaymentDateRangeList = musicGroupPaymentCalenderDto.getMusicGroupPaymentDateRangeList();

		if(musicGroupPaymentCalenderDto.getMemberRankSettingId() != null){
			if(paymentType != ADD_STUDENT && paymentType != MUSIC_APPLY){
				throw new BizException("操作失败：{} 不支持会员收费",paymentType.getDesc());
			}
		}

		if ((paymentType == PaymentType.ADD_COURSE) && musicGroupPaymentDateRangeList.size() > 1) {
			throw new BizException("[临时加课]不支持多周期缴费");
		}

		MusicGroup musicGroup = musicGroupDao.getLocked(musicGroupId);

		if (musicGroup == null) {
			throw new BizException("乐团查询失败，请检查参数");
		}
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("请登录");
		}
		if (musicGroup.getCourseViewType() == CourseViewTypeEnum.MEMBER_FEE && musicGroupPaymentDateRangeList.size() > 1) {
			throw new BizException("创建缴费失败：会员收费乐团不支持多周期缴费");
		}

		List<Long> calenderIds = musicGroupPaymentCalenderList.stream().map(MusicGroupPaymentCalender :: getId).collect(Collectors.toList());
		//删除原来数据
		musicGroupPaymentCalenderDao.delByIds(calenderIds);
		musicGroupPaymentCalenderCourseSettingsDao.deleteByMusicGroupPaymentCalenderId(calenderIds);

		status = PaymentCalenderStatusEnum.NO;

		Date date = new Date();

		// 获取设置的课程
		List<MusicGroupPaymentCalenderCourseSettings> musicGroupPaymentCalenderCourseSettingsList = musicGroupPaymentCalenderDto
				.getMusicGroupPaymentCalenderCourseSettingsList();
//		if (musicGroupPaymentCalenderCourseSettingsList == null) {
//			musicGroupPaymentCalenderCourseSettingsList = new ArrayList<>();
//		}

		//会员原价
		BigDecimal memberPaymentAmount = BigDecimal.ZERO;
		BigDecimal originalMemberPaymentAmount = BigDecimal.ZERO;
		if (payUserType == SCHOOL) {
			status = AUDITING;
		} else {
			if(musicGroupPaymentCalenderCourseSettingsList != null){
				if (paymentType == MUSIC_APPLY || paymentType == MUSIC_RENEW) {
					// 当前缴费的课程费用
					Map<CourseScheduleType, BigDecimal> currentCoursePrice = musicGroupPaymentCalenderCourseSettingsList.stream().collect(
							Collectors
									.toMap(MusicGroupPaymentCalenderCourseSettings::getCourseType, MusicGroupPaymentCalenderCourseSettings::getCourseCurrentPrice));

					// 查询默认课程费用
					Integer musicGroupOrganizationCourseSettingId = musicGroupPaymentCalenderDto.getMusicGroupOrganizationCourseSettingId();
					Map<CourseScheduleType, BigDecimal> defaultCoursePrice = musicGroupOrganizationCourseSettingsDetailDao
							.queryByMusicGroupOrganizationCourseSettingsId(musicGroupOrganizationCourseSettingId)
							.stream()
							.collect(
									Collectors.toMap(MusicGroupOrganizationCourseSettingsDetail::getCourseType,
											MusicGroupOrganizationCourseSettingsDetail::getCourseCurrentPrice));

					// 相同类型的课程如果修改了课程费用，需要走审批
					for (Entry<CourseScheduleType, BigDecimal> entry : currentCoursePrice.entrySet()) {
						if (defaultCoursePrice.get(entry.getKey()).compareTo(entry.getValue()) != 0) {
							status = AUDITING;
							break;
						}
					}
				} else if (paymentType == PaymentType.ADD_COURSE || paymentType == PaymentType.ADD_STUDENT) {
					// 如果是临时加课，判断是否审核
					for (MusicGroupPaymentCalenderCourseSettings courseSettings : musicGroupPaymentCalenderCourseSettingsList) {
						OrganizationCourseUnitPriceSettings defaultUnitPrice = organizationCourseUnitPriceSettingsDao.queryByOrganIdAndCourseTypeAndChargeType(
								musicGroup.getOrganId(), courseSettings.getCourseType(), musicGroup.getChargeTypeId());
						if (defaultUnitPrice == null) {
							throw new BizException("请先设置分部课程类型单价");
						}
						if (courseSettings.getCourseTotalMinuties() != 0) {
							if (defaultUnitPrice.getUnitPrice().multiply(new BigDecimal(courseSettings.getCourseTotalMinuties())).setScale(0, BigDecimal.ROUND_HALF_UP)
									.compareTo(courseSettings.getCourseCurrentPrice()) != 0) {

								status = AUDITING;
								break;
							}
						} else {
							status = AUDITING;
							break;
						}
					}
				}else if(paymentType == SPAN_GROUP_CLASS_ADJUST){
					MusicGroupStudentClassAdjust adjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
					//获取默认的学员缴费详情
					List<Integer> studentIdList = JSON.parseArray(adjust.getStudentIds(), Integer.class);
					//获取欠费学员列表
					List<Integer> noPaymentUserIds = musicGroupPaymentCalenderDetailDao.queryNoPaymentUserIds(musicGroup.getId(), studentIdList);
					if(noPaymentUserIds.size() > 0){
						throw new BizException("操作失败：有欠费的学员不允许创建缴费");
					}
					//获取缴费状态在审核中或者已拒绝的缴费项目的学员
					String studentIds = musicGroupPaymentCalenderDao.queryCalenderStudentIds(musicGroupId,batchNo);
					if(StringUtils.isNotEmpty(studentIds)){
						for (Integer integer : studentIdList) {
							if(studentIds.contains(integer.toString())){
								throw new BizException("操作失败：所选学员有待审核或已拒绝的缴费项目");
							}
						}
					}

					List<MusicGroupPaymentCalenderStudentDetail> musicGroupPaymentCalenderStudentDetails = classGroupService.queryStudentPaymentCalenders(adjust.getMasterClassGroupId(),
							adjust.getClassGroupStudents(),studentIdList);
					List<MusicGroupPaymentCalenderStudentDetail> calenderStudentDetails = musicGroupPaymentCalenderDto.getMusicGroupPaymentCalenderStudentDetails();

					boolean containsAll = musicGroupPaymentCalenderStudentDetails.containsAll(calenderStudentDetails);
					if(!containsAll){
						status = AUDITING;
						//保存新的学员详情
					}
					//删除之前的记录
					musicGroupPaymentCalenderStudentDetailDao.delByBatchNo(batchNo);
					//保存现有记录
					musicGroupPaymentCalenderStudentDetailDao.batchInsert(calenderStudentDetails,batchNo);
				}
			}
			if(musicGroupPaymentCalenderDto.getMemberRankSettingId() != null){
				//会员价格是否变动
				MemberFeeSetting memberFee = memberFeeSettingDao.findByRankIdAndOrganId(musicGroup.getOrganId(), musicGroupPaymentCalenderDto.getMemberRankSettingId());
				if(memberFee == null){
					throw new BizException("操作失败：请配置当前分部会员收费标准");
				}
				switch (musicGroupPaymentCalenderDto.getMemberValidDate()){
					case 1 :
						memberPaymentAmount = memberFee.getCurrentMonthFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						originalMemberPaymentAmount = memberFee.getOriginalMonthFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						break;
					case 6 :
						memberPaymentAmount = memberFee.getCurrentHalfYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						originalMemberPaymentAmount = memberFee.getOriginalHalfYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						break;
					case 12 :
						memberPaymentAmount = memberFee.getCurrentYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						originalMemberPaymentAmount = memberFee.getOriginalYearFee().setScale(0, BigDecimal.ROUND_HALF_UP);
						break;
					default:
						throw new BizException("请选择正确的会员有效期");
				}
				if(memberPaymentAmount.compareTo(musicGroupPaymentCalenderDto.getMemberPaymentAmount()) != 0){
					status = AUDITING;
				}
			}
		}

		int times = musicGroupPaymentDateRangeList.size();

		// 排序
		Collections.sort(musicGroupPaymentDateRangeList, new Comparator<MusicGroupPaymentDateRange>() {

			@Override
			public int compare(MusicGroupPaymentDateRange o1, MusicGroupPaymentDateRange o2) {
				return o1.getPaymentValidStartDate().compareTo(o2.getPaymentValidStartDate());
			}
		});

		for (int i = 0; i < musicGroupPaymentDateRangeList.size(); i++) {

			MusicGroupPaymentDateRange musicGroupPaymentDateRange = musicGroupPaymentDateRangeList.get(i);

			MusicGroupPaymentCalender musicGroupPaymentCalender = new MusicGroupPaymentCalender();
			musicGroupPaymentCalender.setAttribute1(musicGroupPaymentCalenderDto.getAttribute1());
			musicGroupPaymentCalender.setAttribute2(musicGroupPaymentCalenderDto.getAttribute2());
			musicGroupPaymentCalender.setOriginalMemberPaymentAmount(originalMemberPaymentAmount);
			musicGroupPaymentCalender.setDeadlinePaymentDate(musicGroupPaymentDateRange.getDeadlinePaymentDate());
			musicGroupPaymentCalender.setIsGiveMusicNetwork(musicGroupPaymentCalenderDto.getIsGiveMusicNetwork());
			musicGroupPaymentCalender.setMemo(musicGroupPaymentCalenderDto.getMemo());
			musicGroupPaymentCalender.setMusicGroupId(musicGroupId);
			musicGroupPaymentCalender.setMusicGroupOrganizationCourseSettingId(musicGroupPaymentCalenderDto.getMusicGroupOrganizationCourseSettingId());
			BigDecimal totalPaymentAmount = BigDecimal.ZERO;
			if(musicGroupPaymentCalenderCourseSettingsList != null){
				List<MusicGroupPaymentCalenderCourseSettings> newCSList = new ArrayList<MusicGroupPaymentCalenderCourseSettings>(
						musicGroupPaymentCalenderCourseSettingsList.size());

				if(musicGroupPaymentCalenderDto.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
					totalPaymentAmount = musicGroupPaymentDateRange.getPaymentAmount();
				}
				for (MusicGroupPaymentCalenderCourseSettings pccs : musicGroupPaymentCalenderCourseSettingsList) {

					MusicGroupPaymentCalenderCourseSettings tempPccs = new MusicGroupPaymentCalenderCourseSettings();
					try {
						BeanUtils.copyProperties(tempPccs, pccs);
					} catch (Exception e) {
						throw new BizException("克隆对象出错", e);
					}
					if (i == 0) {
						if (pccs.getIsStudentOptional()) {
							tempPccs.setCourseCurrentPrice(pccs.getCourseCurrentPrice());
							tempPccs.setCourseOriginalPrice(pccs.getCourseOriginalPrice());
							tempPccs.setCourseTotalMinuties(pccs.getCourseTotalMinuties());
						} else {
							tempPccs.setCourseCurrentPrice(pccs.getCourseCurrentPrice().subtract(
									pccs.getCourseCurrentPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(times - 1))
											.setScale(0, BigDecimal.ROUND_DOWN)));
							tempPccs.setCourseOriginalPrice(pccs.getCourseOriginalPrice().subtract(
									pccs.getCourseOriginalPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN).multiply(new BigDecimal(times - 1))
											.setScale(0, BigDecimal.ROUND_DOWN)));
							tempPccs.setCourseTotalMinuties(pccs.getCourseTotalMinuties() - pccs.getCourseTotalMinuties() / times * (times - 1));
						}
					} else {
						if (!pccs.getIsStudentOptional()) {
							tempPccs.setCourseCurrentPrice(pccs.getCourseCurrentPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN));
							tempPccs.setCourseOriginalPrice(pccs.getCourseOriginalPrice().divide(new BigDecimal(times), 0, BigDecimal.ROUND_DOWN));
							tempPccs.setCourseTotalMinuties(pccs.getCourseTotalMinuties() / times);
						}
					}
					totalPaymentAmount = totalPaymentAmount.add(tempPccs.getCourseCurrentPrice());
					newCSList.add(tempPccs);
				}
				musicGroupPaymentCalender.setMusicGroupPaymentCalenderCourseSettingsList(newCSList);
			}

			musicGroupPaymentCalender.setPaymentAmount(totalPaymentAmount);
			musicGroupPaymentCalender.setPaymentPattern(musicGroupPaymentCalenderDto.getPaymentPattern());
			musicGroupPaymentCalender.setPaymentValidEndDate(musicGroupPaymentDateRange.getPaymentValidEndDate());
			musicGroupPaymentCalender.setPaymentValidStartDate(musicGroupPaymentDateRange.getPaymentValidStartDate());
			
			if (paymentType == PaymentType.MUSIC_APPLY) {
				if (i == 0) {
					musicGroupPaymentCalender.setPaymentType(paymentType);
				} else {
					musicGroupPaymentCalender.setPaymentType(PaymentType.MUSIC_RENEW);
				}
			} else {
				musicGroupPaymentCalender.setPaymentType(paymentType);
			}
			musicGroupPaymentCalender.setPayUserType(musicGroupPaymentCalenderDto.getPayUserType());
			musicGroupPaymentCalender.setStartPaymentDate(musicGroupPaymentDateRange.getStartPaymentDate());
			if(StringUtils.isNotEmpty(musicGroupPaymentCalenderDto.getStudentIds())){
				musicGroupPaymentCalender.setStudentIds(musicGroupPaymentCalenderDto.getStudentIds());
			}
			musicGroupPaymentCalender.setOperator(sysUser.getId());
			musicGroupPaymentCalender.setCreateTime(date);
			musicGroupPaymentCalender.setUpdateTime(date);
			musicGroupPaymentCalender.setStatus(status);

			if (musicGroupPaymentCalender.getDeadlinePaymentDate() == null && payUserType == PayUserType.STUDENT) {
				musicGroupPaymentCalender.setDeadlinePaymentDate(DateUtil.addDays(musicGroupPaymentCalender.getStartPaymentDate(), 3));
			}
			if (paymentType != ADD_STUDENT && paymentType != ADD_COURSE && paymentType != SPAN_GROUP_CLASS_ADJUST) {
				if(musicGroupPaymentCalender.getPaymentValidStartDate() != null){
					int count = musicGroupPaymentCalenderDao.queryIntersectionByValidDate(musicGroupId, musicGroupPaymentCalender.getPayUserType(),
							musicGroupPaymentCalender.getPaymentValidStartDate(), musicGroupPaymentCalender.getPaymentValidEndDate(), null);
					if (count > 0) {
						throw new BizException("缴费有效期存在冲突，请修改缴费有效期");
					}
				}
			}

			if (paymentType == PaymentType.ADD_STUDENT) {
				musicGroupPaymentCalender.setPayUserType(STUDENT);
				musicGroupPaymentCalender.setIsGiveMusicNetwork(false);
				if (musicGroupPaymentCalender.getStatus() != AUDITING) {
					musicGroupPaymentCalender.setExpectNum(1);
				}
			}
			if (musicGroupPaymentCalender.getStatus() != AUDITING) {
				if (date.after(musicGroupPaymentCalender.getStartPaymentDate())) {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OPEN);
				} else if (date.after(musicGroupPaymentCalender.getDeadlinePaymentDate())) {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OVER);
				} else {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.NO);
				}
			}

			// 设置批次号
			musicGroupPaymentCalender.setBatchNo(batchNo);
			//设置会员缴费金额、级别以及有效期
			musicGroupPaymentCalender.setMemberPaymentAmount(musicGroupPaymentCalenderDto.getMemberPaymentAmount());
			musicGroupPaymentCalender.setMemberRankSettingId(musicGroupPaymentCalenderDto.getMemberRankSettingId());
			musicGroupPaymentCalender.setMemberValidDate(musicGroupPaymentCalenderDto.getMemberValidDate());
			musicGroupPaymentCalenderDto.setBatchNo(batchNo);
			musicGroupPaymentCalenderDao.insert(musicGroupPaymentCalender);
			List<MusicGroupPaymentCalenderCourseSettings> currentMusicGroupPaymentCalenderCourseSettings = musicGroupPaymentCalender
					.getMusicGroupPaymentCalenderCourseSettingsList();

			if (musicGroupPaymentCalender.getStatus() != AUDITING) {
				// 如果是进行中加学生
				if (musicGroupPaymentCalender.getPaymentType() == PaymentType.ADD_STUDENT) {
					addStudent(musicGroupPaymentCalender, currentMusicGroupPaymentCalenderCourseSettings,musicGroup);
				}else if(musicGroupPaymentCalender.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
					//如果是跨团班级合并,添加学员
					musicGroupPaymentCalenderDetailService.batchAdd(musicGroupPaymentCalender,musicGroupPaymentCalenderDto.getMusicGroupPaymentCalenderStudentDetails());
				}
				//将0元未缴费学员缴费状态更新为已缴费
				int j = musicGroupPaymentCalenderDetailDao.updateNoPaymentAndZeroPaymentStatus(musicGroupPaymentCalender.getId());
				musicGroupPaymentCalender.setActualNum((musicGroupPaymentCalender.getActualNum()==null?0:musicGroupPaymentCalender.getActualNum()) + j);
				musicGroupPaymentCalenderDao.update(musicGroupPaymentCalender);
			}

			if (currentMusicGroupPaymentCalenderCourseSettings.size() > 0) {
				MusicGroupOrganizationCourseSettings courseSettings = musicGroupOrganizationCourseSettingsDao.get(musicGroupPaymentCalender
						.getMusicGroupOrganizationCourseSettingId());
				for (MusicGroupPaymentCalenderCourseSettings musicGroupPaymentCalenderCourseSettings : currentMusicGroupPaymentCalenderCourseSettings) {
					musicGroupPaymentCalenderCourseSettings.setMusicGroupPaymentCalenderId(musicGroupPaymentCalender.getId());
					if (courseSettings != null) {
						musicGroupPaymentCalenderCourseSettings.setName(courseSettings.getName());
					}
				}
				musicGroupPaymentCalenderCourseSettingsDao.batchInsert(currentMusicGroupPaymentCalenderCourseSettings);
			}
		}

		// 如果是报名，需要修改乐团状态
		if (paymentType == MUSIC_APPLY) {
			if (status != AUDITING) {
				musicGroup.setStatus(MusicGroupStatusEnum.APPLY);
				// 记录操作日志
				musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroupId, "报名缴费项目创建成功（创建缴费 -> 报名中）", sysUser.getId(), ""));
			} else {
				musicGroup.setStatus(MusicGroupStatusEnum.FEE_AUDIT);
				// 记录操作日志
				musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroupId, "报名缴费项目创建成功（创建缴费 -> 费用审核中）", sysUser.getId(), ""));
			}
			musicGroup.setUpdateTime(date);
			musicGroupDao.update(musicGroup);
		} else if (paymentType == ADD_STUDENT) {
			if (status != AUDITING) {
				// 学生加到班级
				String classGroupIdStr = musicGroupPaymentCalenderDto.getAttribute1();
				if (StringUtils.isNotBlank(classGroupIdStr)) {
					classGroupService.addStudentToClassGroupAndCourseArranging(Integer.parseInt(musicGroupPaymentCalenderDto.getStudentIds()), classGroupIdStr,
							batchNo, musicGroupPaymentCalenderCourseSettingsList,musicGroup);
				}
			}
		}

		if (status == AUDITING) {
			// 如果是审核中
			Set<Integer> roleIds = new HashSet<>(1);
			roleIds.add(SysUserRole.ADMINISTRATOR);
			Organization organization = organizationDao.get(musicGroup.getOrganId());
			sysMessageService.batchSeoMessage(musicGroupDao.queryUserIdByRoleId(roleIds, musicGroup.getOrganId()),
					MessageTypeEnum.BACKSTAGE_PAYMENT_CALENDER_AUDIT, "", organization.getName(), musicGroup.getName());
		}else {
			if(paymentType == ADD_STUDENT){
				MusicGroupStudentClassAdjust musicGroupStudentClassAdjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
				if(musicGroupStudentClassAdjust != null){
					//如果是合班缴费
					autoClassGroupAdjust(musicGroupStudentClassAdjust);
				}else {
					imUserFriendService.refreshGroupImUserFriend(musicGroupId,GroupType.MUSIC);
				}
			}else if(paymentType == SPAN_GROUP_CLASS_ADJUST){
				//删除和解冻班级，课程信息
				MusicGroupStudentClassAdjust adjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
				List<Map> classGroupStudents = JSON.parseArray(adjust.getClassGroupStudents(),Map.class);
				List<Integer> studentIds = JSON.parseArray(adjust.getStudentIds(), Integer.class);
				List<Long> courseIds = JSON.parseArray(adjust.getSubLockCourseIds(), Long.class);
				List<Long> allLockCourseIds = JSON.parseArray(adjust.getAllLockCourseIds(), Long.class);
				classGroupService.spanGroupClassAdjustPass(adjust.getMasterClassGroupId()
						,studentIds,courseIds,classGroupStudents,allLockCourseIds,batchNo,adjust.getMasterTotalPrice());
			}
		}

		//统计变更学员数
		groupEventSource.musicGroupStudentChangeEvent(musicGroupId, StudentMusicGroupStatusEnum.NORMAL, null);

		return batchNo;
	}

	@Override
	public boolean makesureSchoolePaid(Long id, String memo) {
		MusicGroupPaymentCalender musicGroupPaymentCalender = musicGroupPaymentCalenderDao.get(id);
		if(musicGroupPaymentCalender == null){
			throw new BizException("查询缴费信息失败");
		}
		
		if(musicGroupPaymentCalender.getPayUserType() != PayUserType.SCHOOL){
			throw new BizException("只有学校缴费才能确认缴费状态");
		}
		
		musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.PAID);
		musicGroupPaymentCalender.setMemo(memo);
		musicGroupPaymentCalender.setUpdateTime(new Date());
		musicGroupPaymentCalenderDao.update(musicGroupPaymentCalender);
		
		return true;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void addStudent(MusicGroupPaymentCalender musicGroupPaymentCalender,
						   List<MusicGroupPaymentCalenderCourseSettings> musicGroupPaymentCalenderCourseSettingsList,
						   MusicGroup musicGroup){
		//如果是进行中加学生
		Date date = new Date();
		String studentIdStr = musicGroupPaymentCalender.getStudentIds();
		if (StringUtils.isNoneBlank(studentIdStr)) {
			//计算总费用
			BigDecimal expectAmount = BigDecimal.ZERO;
			BigDecimal expectMemberAmount = musicGroupPaymentCalender.getMemberPaymentAmount();
			if(musicGroupPaymentCalenderCourseSettingsList != null && musicGroupPaymentCalenderCourseSettingsList.size() > 0){
				for(MusicGroupPaymentCalenderCourseSettings musicGroupPaymentCalenderCourseSettings : musicGroupPaymentCalenderCourseSettingsList){
					expectAmount = expectAmount.add(musicGroupPaymentCalenderCourseSettings.getCourseCurrentPrice());
				}
			}
			//当前乐团是否有预排课计划
			MusicGroupSchoolTermCourseDetail termCourseDetail = musicGroupSchoolTermCourseDetailDao.findByCourseDateAndMusicGroupId(musicGroup.getId(), null, 0);

			List<MusicGroupPaymentStudentCourseDetail> musicGroupPaymentStudentCourseDetailList = new ArrayList<MusicGroupPaymentStudentCourseDetail>();
			MusicGroupPaymentStudentCourseDetail musicGroupPaymentStudentCourseDetail = null;

			for (String studentId : studentIdStr.split(",")) {
				if(termCourseDetail != null){
					//是否已经预排部分课程
					List<CourseSchedule> courseSchedules = courseScheduleDao.queryPreCourseListByMusicGroupId(musicGroup.getId());
					if(courseSchedules.size() > 0){
						throw new BizException("操作失败：该乐团存在预排课课程，请优先完成预排课");
					}else {
						MusicGroupSchoolTermStudentCourseDetail studentCourseDetail = new MusicGroupSchoolTermStudentCourseDetail();
						studentCourseDetail.setMusicGroupSchoolTermCourseDetailId(termCourseDetail.getId());
						studentCourseDetail.setUserId(Integer.parseInt(studentId));
						studentCourseDetail.setMemberCourseMinutes(termCourseDetail.getTotalCourseTime());
						musicGroupSchoolTermStudentCourseDetailDao.insert(studentCourseDetail);
					}
				}
				// 学生加到calenderDetail
				MusicGroupPaymentCalenderDetail musicGroupPaymentCalenderDetail = new MusicGroupPaymentCalenderDetail();
				musicGroupPaymentCalenderDetail.setMusicGroupPaymentCalenderId(musicGroupPaymentCalender.getId());
				musicGroupPaymentCalenderDetail.setCreateTime(date);
				musicGroupPaymentCalenderDetail.setExpectAmount(expectAmount);
				musicGroupPaymentCalenderDetail.setExpectMemberAmount(expectMemberAmount);
				musicGroupPaymentCalenderDetail.setPaymentStatus(PaymentStatus.NON_PAYMENT);
				musicGroupPaymentCalenderDetail.setUpdateTime(date);
				musicGroupPaymentCalenderDetail.setUserId(Integer.parseInt(studentId));
				musicGroupPaymentCalenderDetail.setStartPaymentDate(musicGroupPaymentCalender.getStartPaymentDate());
				musicGroupPaymentCalenderDetail.setDeadlinePaymentDate(musicGroupPaymentCalender.getDeadlinePaymentDate());

				musicGroupPaymentCalenderDetailDao.insert(musicGroupPaymentCalenderDetail);
				//如果是0元那么更新fee表和studentRegistration表
				if(expectAmount.doubleValue() == 0d && expectMemberAmount.doubleValue() == 0d){
					// 添加会员有效时长
					if(musicGroupPaymentCalender.getMemberRankSettingId() != null){
						CloudTeacherOrder cloudTeacherOrder = new CloudTeacherOrder();
						cloudTeacherOrder.setLevel(musicGroupPaymentCalender.getMemberRankSettingId());
						cloudTeacherOrder.setOrganId(musicGroup.getOrganId());
						cloudTeacherOrder.setType(2);
						cloudTeacherOrder.setStudentId(Integer.parseInt(studentId));
						cloudTeacherOrder.setOrderId(musicGroup.getOrganId().longValue());
						cloudTeacherOrder.setTime(musicGroupPaymentCalender.getMemberValidDate());
						cloudTeacherOrder.setAmount(expectMemberAmount);
						cloudTeacherOrder.setStatus(2);
						cloudTeacherOrder.setStartTime(date);
						cloudTeacherOrder.setEndTime(DateUtil.addMonths(date,musicGroupPaymentCalender.getMemberValidDate()));
						cloudTeacherOrder.setRemark("进行中乐团0元加学生");
						cloudTeacherOrderDao.insert(cloudTeacherOrder);
						studentService.updateMemberRank(Integer.parseInt(studentId),musicGroupPaymentCalender.getMemberRankSettingId(),PeriodEnum.MONTH,musicGroupPaymentCalender.getMemberValidDate());
					}
					MusicGroupStudentFee musicGroupStudentFee = musicGroupStudentFeeDao.findByUser(Integer.parseInt(studentId), musicGroupPaymentCalender.getMusicGroupId());
					if (musicGroupStudentFee != null) {
						musicGroupStudentFee.setUpdateTime(date);
						musicGroupStudentFee.setLatestPaidTime(date);
						musicGroupStudentFee.setPaymentStatus(PaymentStatus.PAID_COMPLETED);
						musicGroupStudentFee.setTemporaryCourseFee(new BigDecimal(0));
						musicGroupStudentFeeDao.update(musicGroupStudentFee);
					}
					//如果是进行中加学员
					StudentRegistration studentRegistration = studentRegistrationDao.queryByUserIdAndMusicGroupId(Integer.parseInt(studentId), musicGroupPaymentCalender.getMusicGroupId());
					studentRegistration.setPaymentStatus(YES);
					studentRegistration.setMusicGroupStatus(StudentMusicGroupStatusEnum.NORMAL);
					studentRegistrationDao.update(studentRegistration);

					//统计变更学员数
					groupEventSource.musicGroupStudentChangeEvent(musicGroupPaymentCalender.getMusicGroupId(), StudentMusicGroupStatusEnum.NORMAL, new ArrayList<>(Arrays.asList(Integer.parseInt(studentId))));
				}
				if(musicGroupPaymentCalenderCourseSettingsList != null && musicGroupPaymentCalenderCourseSettingsList.size() > 0){
					// 学生加课程明细
					for(MusicGroupPaymentCalenderCourseSettings courseSettings : musicGroupPaymentCalenderCourseSettingsList){
						if (courseSettings.getIsStudentOptional() == true) {
							continue;
						}
						musicGroupPaymentStudentCourseDetail = new MusicGroupPaymentStudentCourseDetail();
						musicGroupPaymentStudentCourseDetail.setCourseType(courseSettings.getCourseType());
						musicGroupPaymentStudentCourseDetail.setCreateTime(date);
						musicGroupPaymentStudentCourseDetail.setMusicGroupPaymentCalenderId(musicGroupPaymentCalender.getId());
						musicGroupPaymentStudentCourseDetail.setMusicGroupPaymentCalenderDetailId(musicGroupPaymentCalenderDetail.getId());
						musicGroupPaymentStudentCourseDetail.setTotalCourseMinutes(courseSettings.getCourseTotalMinuties());
						musicGroupPaymentStudentCourseDetail.setCourseOriginalPrice(courseSettings.getCourseOriginalPrice());
						musicGroupPaymentStudentCourseDetail.setCourseCurrentPrice(courseSettings.getCourseCurrentPrice());
						musicGroupPaymentStudentCourseDetail.setUpdateTime(date);
						musicGroupPaymentStudentCourseDetail.setUsedCourseMinutes(courseSettings.getCourseTotalMinuties());
						musicGroupPaymentStudentCourseDetail.setUserId(Integer.parseInt(studentId));
						musicGroupPaymentStudentCourseDetailList.add(musicGroupPaymentStudentCourseDetail);
					}

				}
				if(musicGroupPaymentStudentCourseDetailList.size() > 0){
					musicGroupPaymentStudentCourseDetailDao.batchInsert(musicGroupPaymentStudentCourseDetailList);
				}
			}
		}
	}

	@Override
	public void autoUpdateMusicGroupStudentFeeStatus() {
		Date date = new Date();
		String format = DateUtil.format(date, DateUtil.DEFAULT_PATTERN);
		// 获取距离缴费开启指定天数后的缴费明细
		String configValue = sysConfigDao.findConfigValue("push_payment_detail");
		Map<Long, Long> maps = MapUtil.convertIntegerMap(musicGroupPaymentCalenderDao.queryCalenderByDay(configValue, format));
		if (maps != null && maps.size() > 0) {
			Set<Long> calenderId = maps.keySet();
			List<CalenderPushDto> calenderPushDtos = musicGroupPaymentCalenderDao.getCalenderPushDto(calenderId);
			Map<Long, List<CalenderPushDto>> collect = calenderPushDtos.stream().collect(Collectors.groupingBy(CalenderPushDto::getCalenderId));
			calenderId.forEach(e -> {
				CalenderPushDto calenderPushDto = collect.get(e).get(0);
				Map<Integer, String> receivers = new HashMap<>(1);
				receivers.put(calenderPushDto.getTeacherId(), calenderPushDto.getTeacherPhone());
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, MessageTypeEnum.SYSTEM_SMS_PUSH_PAYMENT_DETAIL, receivers, null, 0,
						null, null, calenderPushDto.getMusicGroupName(), calenderPushDto.getPaymentValidStartDate(), calenderPushDto.getPaymentValidEndDate(),
						maps.get(e));
				Map<Integer, String> receivers1 = new HashMap<>(1);
				receivers1.put(calenderPushDto.getTeacherId(), calenderPushDto.getTeacherId().toString());
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.SYSTEM_SMS_PUSH_PAYMENT_DETAIL, receivers1, null, 0,
						null, "SYSTEM", calenderPushDto.getMusicGroupName(), calenderPushDto.getPaymentValidStartDate(), calenderPushDto.getPaymentValidEndDate(),
						maps.get(e));
			});
		}
		// 获取离截止还有指定时间的缴费项，并发送推送消息给指定老师
		String configValue1 = sysConfigDao.findConfigValue("push_create_payment_calender");
		List<Long> calenderIds = musicGroupPaymentCalenderDao.queryEndIds(configValue1, format,null);
		if (calenderIds != null && calenderIds.size() > 0) {
			List<CalenderPushDto> calenderPushDtos = musicGroupPaymentCalenderDao.getCalenderPushDto(calenderIds);
			Map<Long, List<CalenderPushDto>> collect = calenderPushDtos.stream().collect(Collectors.groupingBy(CalenderPushDto::getCalenderId));
			calenderIds.forEach(e -> {
				CalenderPushDto calenderPushDto = collect.get(e).get(0);
				Map<Integer, String> receivers = new HashMap<>(1);
				receivers.put(calenderPushDto.getTeacherId(), calenderPushDto.getTeacherPhone());
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, MessageTypeEnum.SYSTEM_SMS_PUSH_PAYMENT_CREATE, receivers, null, 0,
						null, null, calenderPushDto.getMusicGroupName(), calenderPushDto.getPaymentValidStartDate(), calenderPushDto.getPaymentValidEndDate());
				Map<Integer, String> receivers1 = new HashMap<>(1);
				receivers1.put(calenderPushDto.getTeacherId(), calenderPushDto.getTeacherId().toString());
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.SYSTEM_SMS_PUSH_PAYMENT_CREATE, receivers1, null, 0,
						null, "SYSTEM", calenderPushDto.getMusicGroupName(), calenderPushDto.getPaymentValidStartDate(), calenderPushDto.getPaymentValidEndDate());
			});
		}

		// 乐团报名到期，提醒教务老师创建缴费
		List<CalenderPushDto> calenderPushDtos = musicGroupPaymentCalenderDao.getMusicCalenderPushDto(configValue1, format);
		if (calenderPushDtos != null && calenderPushDtos.size() > 0) {
			calenderPushDtos.forEach(e -> {
				Map<Integer, String> receivers = new HashMap<>(1);
				receivers.put(e.getTeacherId(), e.getTeacherPhone());
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, MessageTypeEnum.SYSTEM_SMS_PUSH_PAYMENT_CREATE, receivers, null, 0,
						null, null, e.getMusicGroupName(), e.getPaymentValidStartDate(), e.getPaymentValidEndDate());
				Map<Integer, String> receivers1 = new HashMap<>(1);
				receivers1.put(e.getTeacherId(), e.getTeacherId().toString());
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.SYSTEM_SMS_PUSH_PAYMENT_CREATE, receivers1, null, 0,
						null, "SYSTEM", e.getMusicGroupName(), e.getPaymentValidStartDate(), e.getPaymentValidEndDate());
			});
		}
	}

	@Override
	public PageInfo<MusicGroupPaymentCalenderAuditDto> auditList(MusicGroupPaymentCalenderQueryInfo queryInfo) {
		PageInfo<MusicGroupPaymentCalenderAuditDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<>();
		MapUtil.populateMap(params, queryInfo);

		List<MusicGroupPaymentCalenderAuditDto> dataList = null;
		int count = musicGroupPaymentCalenderDao.countAuditList(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = musicGroupPaymentCalenderDao.queryAuditList(params);
			List<Integer> collect = dataList.stream().map(e -> e.getOrganId()).collect(Collectors.toList());
			collect.removeAll(Collections.singleton(null));
			Map<Integer, String> organNameMap = getMap("organization", "id_", "name_", collect, Integer.class, String.class);
			List<Integer> operatorIds = dataList.stream().map(e -> e.getOperator()).collect(Collectors.toList());
			operatorIds.removeAll(Collections.singleton(null));
			Map<Integer, String> realNameMap = getMap("sys_user", "id_", "real_name_", operatorIds, Integer.class, String.class);
			dataList.forEach(e->{
				e.setOrganName(organNameMap.get(e.getOrganId()));
				e.setOperatorName(realNameMap.get(e.getOperator()));
			});
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public MusicGroupPaymentCalenderAuditDetailDto auditListDetail(String batchNo) {
		MusicGroupPaymentCalenderAuditDetailDto calenderAuditDetailDto = new MusicGroupPaymentCalenderAuditDetailDto();
		MusicGroupPaymentCalenderAuditDto auditDto = musicGroupPaymentCalenderDao.getAuditDetail(batchNo);
		calenderAuditDetailDto.setAuditDto(auditDto);
		//获取缴费周期
		List<MusicGroupPaymentCalender> groupPaymentCalenders = musicGroupPaymentCalenderDao.findByBatchNo(batchNo);
		calenderAuditDetailDto.setMusicGroupPaymentCalenders(groupPaymentCalenders);
		MusicGroupPaymentCalender paymentCalender = groupPaymentCalenders.get(0);
		if(paymentCalender.getMemberRankSettingId() != null){
			MemberRankSetting memberRankSetting = memberRankSettingDao.get(paymentCalender.getMemberRankSettingId());
			if(memberRankSetting != null){
				paymentCalender.setMemberRankSettingName(memberRankSetting.getName());
			}
		}
		if(paymentCalender.getPaymentType() == ADD_STUDENT){
			try {
				String studentIds = groupPaymentCalenders.get(0).getStudentIds();
				if(StringUtils.isNotEmpty(studentIds)){
					SysUser sysUser = sysUserFeignService.queryUserById(Integer.parseInt(studentIds));
					calenderAuditDetailDto.setPhone(sysUser.getPhone());
					calenderAuditDetailDto.setUsername(sysUser.getUsername());
				}
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		//获取收费标准
		calenderAuditDetailDto.setMusicGroupPaymentCalenderCourseSettings(musicGroupPaymentCalenderCourseSettingsDao.queryCalenderCourseSettingsByBatchNo(batchNo));
		//获取跨团合班学员缴费详情
		calenderAuditDetailDto.setMusicGroupPaymentCalenderStudentDetails(musicGroupPaymentCalenderStudentDetailDao.findByBatchNo(batchNo));
		return calenderAuditDetailDto;
	}

	//审核通过后自动排课
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void autoClassGroupAdjust(MusicGroupStudentClassAdjust musicGroupStudentClassAdjust){
		List<MusicGroupPaymentCalender> musicGroupPaymentCalenders = musicGroupPaymentCalenderDao.findByBatchNo(musicGroupStudentClassAdjust.getBatchNo());
		for (MusicGroupPaymentCalender musicGroupPaymentCalender : musicGroupPaymentCalenders) {
			if(musicGroupPaymentCalender.getStatus() == AUDITING || musicGroupPaymentCalender.getStatus() == REJECT){
				return;
			}
		}
		List<ClassGroup4MixDto> classGroup4MixDtos = JSON.parseArray(musicGroupStudentClassAdjust.getNewClassGroupJson(), ClassGroup4MixDto.class);
		String musicGroupId = musicGroupPaymentCalenders.get(0).getMusicGroupId();
		MusicGroup musicGroup = musicGroupDao.get(musicGroupId);
		for (MusicGroupPaymentCalender calender : musicGroupPaymentCalenders) {
			//缴费项目添加学员
			int[] ints = Arrays.stream(calender.getStudentIds().split(",")).mapToInt(Integer::parseInt).toArray();
			Set<Integer> list2 = Arrays.stream(ints).boxed().collect(Collectors.toSet());
			musicGroupPaymentCalenderDetailService.batchAdd(calender.getId(),list2,musicGroup);
		}
		//每种课程类型单独排课
		ClassGroup classGroup = null;
		for (ClassGroup4MixDto classGroup4MixDto : classGroup4MixDtos) {
			classGroup4MixDto.setClassGroup(classGroup);
			classGroup4MixDto.setMusicGroupStudentClassAdjustId(musicGroupStudentClassAdjust.getId());
			classGroup4MixDto.setClassCourseMinute(musicGroupStudentClassAdjust.getClassCourseMinute());
			classGroup = classGroupService.classGroupAdjust2(classGroup4MixDto);
		}
		//排课完成后删除所选课程
		List<Long> courseIds = JSON.parseArray(musicGroupStudentClassAdjust.getSubLockCourseIds(), Long.class);
		List<Integer> studentIds = JSON.parseArray(musicGroupStudentClassAdjust.getStudentIds(), Integer.class);
		courseScheduleDao.deleteMusicGroupCourseSchedulesWithStudents(courseIds,studentIds);
		//解冻课程
		List<Long> allCourseIds = JSON.parseArray(musicGroupStudentClassAdjust.getAllLockCourseIds(), Long.class);
		courseScheduleDao.batchUpdateLockByCourseIds(allCourseIds,0,null);
		//解冻班级
		List<Integer> classGroupIds = JSON.parseArray(musicGroupStudentClassAdjust.getClassGroupIds(), Integer.class);
		classGroupDao.batchUpdateLockByClassGroupIds(classGroupIds,0);
		//删除班级学员
		musicGroupStudentClassAdjustDao.update(musicGroupStudentClassAdjust);
		String classGroupStudents = musicGroupStudentClassAdjust.getClassGroupStudents();
		if(StringUtils.isNotEmpty(classGroupStudents)){
			List<Map> maps = JSON.parseArray(classGroupStudents, Map.class);
			for (Map<Integer, String> classGroupStudent : maps) {
				Set<Integer> integers = classGroupStudent.keySet();
				for (Integer integer : integers) {
					List<Integer> collect = Arrays.asList(classGroupStudent.get(integer).split(",")).stream().map(e -> Integer.parseInt(e)).collect(Collectors.toList());
					//学员退出群聊
					imGroupMemberService.quit(integer.longValue(),collect);
					classGroupStudentMapperDao.deleteByClassGroupIdAndStudents(integer,classGroupStudent.get(integer));
				}
			}
		}
		//获取没有学员的课程列表
		List<Long> delCourseIds = courseScheduleDao.findNoStudentCourseIds(courseIds);
		if(delCourseIds != null && delCourseIds.size() > 0){
			//删除课程
			courseScheduleDao.deleteCourseSchedulesByClassGroupIds(delCourseIds);
			//删除教师考勤
			teacherAttendanceDao.batchDeleteByCourseSchedules(delCourseIds);
			//删除教师课酬
			courseScheduleTeacherSalaryDao.batchDeleteByCourseScheduleIds(delCourseIds);
		}
		classGroupService.batchUpdateTeacherSalary(classGroupIds);
		imUserFriendService.refreshGroupImUserFriend(musicGroupId,GroupType.MUSIC);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void auditPass(String batchNs,String auditMemo) {
		String[] split = batchNs.split(",");
		Date date = new Date();
		for (String batchNo : split) {
			List<MusicGroupPaymentCalender> musicGroupPaymentCalenders = musicGroupPaymentCalenderDao.findByBatchNo(batchNo);
			if(musicGroupPaymentCalenders == null || musicGroupPaymentCalenders.size() == 0){
				throw new BizException("缴费项目不存在");
			}
			MusicGroupPaymentCalender calender = musicGroupPaymentCalenders.get(0);
			MusicGroup musicGroup = musicGroupDao.get(calender.getMusicGroupId());
			for (MusicGroupPaymentCalender musicGroupPaymentCalender : musicGroupPaymentCalenders) {
				//如果是报名项目，将乐团改为审核中，缴费项目修改状态，审核状态改为审核通过
				if(musicGroupPaymentCalender.getPayUserType() == SCHOOL){
					if(musicGroupPaymentCalender.getPaymentAmount().doubleValue() > 0){
						musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OPEN);
					}else{
						musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.PAID);
					}
				}else if (date.after(musicGroupPaymentCalender.getStartPaymentDate())) {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OPEN);
				}else if (date.after(musicGroupPaymentCalender.getDeadlinePaymentDate())) {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.OVER);
				}else {
					musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.NO);
				}
				musicGroupPaymentCalender.setAuditMemo(auditMemo);
				musicGroupPaymentCalender.setUpdateTime(date);

				if (musicGroupPaymentCalender.getPaymentType() == PaymentType.ADD_STUDENT) {
					musicGroupPaymentCalender.setExpectNum(1);
					List<MusicGroupPaymentCalenderCourseSettings> musicGroupPaymentCalenderCourseSettings = musicGroupPaymentCalenderCourseSettingsDao.queryCalenderCourseSettings(musicGroupPaymentCalender.getId());
					addStudent(musicGroupPaymentCalender,musicGroupPaymentCalenderCourseSettings,musicGroup);
				}
			}
			if(calender.getPaymentType() == ADD_STUDENT || calender.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
				// 所有缴费项目已完成排课才能创建下一个缴费项目
				List<String> batchNoList = new ArrayList<>();
				batchNoList.add(batchNo);
				String orignBatchNo = musicGroupPaymentStudentCourseDetailDao.getUnUseBatchNoWithStudentAndCourseTypeAndCourseMinutes(calender.getMusicGroupId(), null, null,batchNoList);
				if (StringUtils.isNoneBlank(orignBatchNo)) {
					throw new BizException("{} 存在未排课的缴费项目，请先完成排课再操作",musicGroup.getName());
				}
				List<Integer> userIds = Arrays.stream(calender.getStudentIds().split(",")).mapToInt(Integer::valueOf).boxed().collect(Collectors.toList());
				//获取欠费学员列表
				List<Integer> noPaymentUserIds = musicGroupPaymentCalenderDetailDao.queryNoPaymentUserIds(calender.getMusicGroupId(),userIds);
				if(noPaymentUserIds.size() > 0){
					throw new BizException("{} 有欠费的学员不允许创建缴费",musicGroup.getName());
				}
				//获取缴费状态在审核中或者已拒绝的缴费项目的学员
				String studentIds = musicGroupPaymentCalenderDao.queryCalenderStudentIds(calender.getMusicGroupId(),batchNo);
				if(StringUtils.isNotEmpty(studentIds)){
					for (Integer integer : userIds) {
						if(studentIds.contains(integer.toString())){
							throw new BizException("{} 的 {} 缴费项目，所选学员有待审核或已拒绝的缴费项目",musicGroup.getName(),calender.getPaymentType().getCode());
						}
					}
				}
			}
			//如果是报名，并且所有的报名都审核通过，需要修改乐团状态
			if (calender.getPaymentType() == MUSIC_APPLY) {
				//统计乐团还在审核中或者审核被拒的缴费
				int count = musicGroupPaymentCalenderDao.countAuditReject(calender.getMusicGroupId(),calender.getId());
				if(count == 0){
					musicGroup.setStatus(MusicGroupStatusEnum.APPLY);
					musicGroup.setUpdateTime(date);
					musicGroupDao.update(musicGroup);
					SysUser sysUser = sysUserFeignService.queryUserInfo();
					if(sysUser == null || sysUser.getId() == null){
						throw new BizException("请重新登录");
					}
					//记录操作日志
					musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroup.getId(), "报名缴费项目通过（费用审核中 -> 报名中）", sysUser.getId(), ""));
				}
			} else if (calender.getPaymentType() == ADD_STUDENT) {
				// 学生加到班级
				String classGroupIdStr = calender.getAttribute1();
				if (StringUtils.isNotBlank(classGroupIdStr)) {
					classGroupService.addStudentToClassGroupAndCourseArranging(Integer.parseInt(calender.getStudentIds()), classGroupIdStr, batchNo,
							musicGroupPaymentCalenderCourseSettingsDao.queryCalenderCourseSettingsByBatchNo(batchNo),musicGroup);
				}
				MusicGroupStudentClassAdjust musicGroupStudentClassAdjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
				if(musicGroupStudentClassAdjust != null){
					//如果是合班缴费
					autoClassGroupAdjust(musicGroupStudentClassAdjust);
				}
			}else if(calender.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
				//删除和解冻班级，课程信息
				MusicGroupStudentClassAdjust adjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
				List<Map> classGroupStudents = JSON.parseArray(adjust.getClassGroupStudents(),Map.class);
				List<Integer> studentIds = JSON.parseArray(adjust.getStudentIds(), Integer.class);
				List<Long> courseIds = JSON.parseArray(adjust.getSubLockCourseIds(), Long.class);
				List<Long> allLockCourseIds = JSON.parseArray(adjust.getAllLockCourseIds(), Long.class);
				//如果是跨团班级合并,添加学员
				musicGroupPaymentCalenderDetailService.batchAdd(calender,musicGroupPaymentCalenderStudentDetailDao.findByBatchNo(batchNo));
				classGroupService.spanGroupClassAdjustPass(adjust.getMasterClassGroupId()
						,studentIds,courseIds,classGroupStudents,allLockCourseIds,batchNo,adjust.getMasterTotalPrice());
			}
			for (MusicGroupPaymentCalender musicGroupPaymentCalender : musicGroupPaymentCalenders) {
				//将0元未缴费学员缴费状态更新为已缴费
				int j = musicGroupPaymentCalenderDetailDao.updateNoPaymentAndZeroPaymentStatus(musicGroupPaymentCalender.getId());
				musicGroupPaymentCalender.setActualNum((musicGroupPaymentCalender.getActualNum()==null?0:musicGroupPaymentCalender.getActualNum()) + j);
			}
			musicGroupPaymentCalenderDao.batchUpdate(musicGroupPaymentCalenders);
			if (calender.getPaymentType() == ADD_STUDENT){
				imUserFriendService.refreshGroupImUserFriend(calender.getMusicGroupId(),GroupType.MUSIC);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void auditRefuse(String batchNs,String auditMemo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser == null || sysUser.getId() == null){
			throw new BizException("请重新登录");
		}
		String[] split = batchNs.split(",");
		Date date = new Date();
		for (String batchNo : split) {
			List<MusicGroupPaymentCalender> musicGroupPaymentCalenders = musicGroupPaymentCalenderDao.findByBatchNo(batchNo);
			if(musicGroupPaymentCalenders == null || musicGroupPaymentCalenders.size() == 0){
				throw new BizException("缴费项目不存在");
			}
			for (MusicGroupPaymentCalender musicGroupPaymentCalender : musicGroupPaymentCalenders) {
				musicGroupPaymentCalender.setStatus(PaymentCalenderStatusEnum.REJECT);
				musicGroupPaymentCalender.setUpdateTime(date);
				musicGroupPaymentCalender.setAuditMemo(auditMemo);
			}
			MusicGroupPaymentCalender calender = musicGroupPaymentCalenders.get(0);
			//如果是报名，并且所有的报名都审核通过，需要修改乐团状态
			if (calender.getPaymentType() == MUSIC_APPLY) {
				MusicGroup musicGroup = musicGroupDao.get(calender.getMusicGroupId());
				musicGroup.setStatus(MusicGroupStatusEnum.PRE_BUILD_FEE);
				musicGroup.setUpdateTime(date);
				musicGroupDao.update(musicGroup);
				//记录操作日志
				musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroup.getId(), "报名缴费项目失败（费用审核中 -> 审核拒绝）", sysUser.getId(), ""));
			}
			musicGroupPaymentCalenderDao.batchUpdate(musicGroupPaymentCalenders);
		}
	}

	//推送待续费通知
	public void pushWaitRenewMessage(Long calenderId,MusicGroup musicGroup,Set<Integer> studentIds){
		if(studentIds == null){
			studentIds = musicGroupPaymentCalenderDetailDao.queryNoPaymentStudentIds(calenderId);
		}
		if (studentIds.size() > 0) {
			String baseUrl = sysConfigDao.findConfigValue(SysConfigService.BASE_API_URL);
			String memo = "4?" + baseUrl + "/#/musicGroupRenew?calenderId="+calenderId+"&id=" + musicGroup.getId();
			Map<Integer, String> push = new HashMap<>();
			for (Integer userId : studentIds) {
				push.put(userId, userId + "");
			}
			// 发送续费通知
			sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.STUDENT_PUSH_WAIT_RENEW_MESSAGE, push,
					null, 0, memo, "STUDENT", musicGroup.getName());

			//获取未缴费且不是0元的学员列表
			Map<Integer,String> studentMaps = MapUtil.convertMybatisMap(musicGroupPaymentCalenderDetailDao.queryNoPaymentAndNotZeroStudent(calenderId,studentIds));
			if(studentMaps.size() > 0){
				sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, MessageTypeEnum.STUDENT_SMS_WAIT_RENEW_MESSAGE, studentMaps,
						null, 0, memo, null, musicGroup.getName());
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
	public void revoke(String batchNo) {
		List<MusicGroupPaymentCalender> calenders = musicGroupPaymentCalenderDao.findByBatchNo(batchNo);
		if(calenders.size() == 0){
			throw new BizException("缴费批次不存在");
		}
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		Date date = new Date();
		calenders.forEach(e->{
			if(e.getPaymentType() == ADD_STUDENT || e.getPaymentType() == SPAN_GROUP_CLASS_ADJUST){
				throw new BizException("{} 的缴费项目不允许撤回",e.getStatus().getDesc());
			}
			if(e.getStatus() != AUDITING){
				throw new BizException("{} 的缴费状态不允许撤回",e.getStatus().getDesc());
			}
			if(e.getPaymentType() == MUSIC_APPLY){
				//如果是报名，并且所有的报名都审核通过，需要修改乐团状态
				MusicGroup musicGroup = musicGroupDao.get(e.getMusicGroupId());
				musicGroup.setStatus(MusicGroupStatusEnum.PRE_BUILD_FEE);
				musicGroup.setUpdateTime(date);
				musicGroupDao.update(musicGroup);
				//记录操作日志
				musicGroupBuildLogDao.insert(new MusicGroupBuildLog(musicGroup.getId(), "报名缴费项目撤回（费用审核中 -> 撤回）", sysUser.getId(), ""));
			}
			e.setStatus(DRAFT);
		});
		musicGroupPaymentCalenderDao.batchUpdate(calenders);
	}

	@Override
	@Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
	public boolean autoUpdateMusicGroupPaymentCalenderStatus() {
		Date date = new Date();
		List<PaymentCalenderStatusEnum> statusList = new ArrayList<>();
		statusList.add(PaymentCalenderStatusEnum.NO);
		statusList.add(PaymentCalenderStatusEnum.OPEN);
		List<MusicGroupPaymentCalender> musicGroupPaymentCalenderList = musicGroupPaymentCalenderDao.queryByPaymentStatus(statusList,PayUserType.STUDENT.getCode());

		List<MusicGroupPaymentCalender> updateMusicGroupPaymentCalenderList = new ArrayList<MusicGroupPaymentCalender>();
		for (MusicGroupPaymentCalender mgpc : musicGroupPaymentCalenderList) {
			if (mgpc.getDeadlinePaymentDate() != null && date.after(mgpc.getDeadlinePaymentDate()) && mgpc.getStatus() == PaymentCalenderStatusEnum.OPEN) {
				// “进行中”更新至“已结束”
				mgpc.setUpdateTime(date);
				mgpc.setStatus(PaymentCalenderStatusEnum.OVER);
				//将0元未缴费学员缴费状态更新为已缴费
				int i = musicGroupPaymentCalenderDetailDao.updateNoPaymentAndZeroPaymentStatus(mgpc.getId());
				mgpc.setActualNum((mgpc.getActualNum()==null?0:mgpc.getActualNum()) + i);
				updateMusicGroupPaymentCalenderList.add(mgpc);
				//如果是进行中加学员，将报名表状态改为NORMAL
				if(mgpc.getPaymentType() == MusicGroupPaymentCalender.PaymentType.ADD_STUDENT){
					studentRegistrationDao.updatePaymentStatusByClaenderId(mgpc.getId());
				}
			} else if (mgpc.getStartPaymentDate() != null && date.after(mgpc.getStartPaymentDate()) && mgpc.getStatus() == PaymentCalenderStatusEnum.NO) {
				MusicGroup musicGroup = musicGroupDao.get(mgpc.getMusicGroupId());
				if(musicGroup == null){
					continue;
				}
				//如果乐团还在审核中
				if(musicGroup.getStatus() == MusicGroupStatusEnum.DRAFT || musicGroup.getStatus() == MusicGroupStatusEnum.AUDIT|| musicGroup.getStatus() == MusicGroupStatusEnum.AUDIT_FAILED || musicGroup.getStatus() == MusicGroupStatusEnum.CLOSE){
					continue;
				}
				// “未开始”更新至“进行中”
				mgpc.setUpdateTime(date);
				mgpc.setStatus(PaymentCalenderStatusEnum.OPEN);
				//将0元未缴费学员缴费状态更新为已缴费
				int i = musicGroupPaymentCalenderDetailDao.updateNoPaymentAndZeroPaymentStatus(mgpc.getId());
				mgpc.setActualNum((mgpc.getActualNum()==null?0:mgpc.getActualNum()) + i);
				updateMusicGroupPaymentCalenderList.add(mgpc);
				if(mgpc.getPayUserType() == STUDENT){
					//推送待续费通知
					pushWaitRenewMessage(mgpc.getId(),musicGroup,null);
				}
			}
		}
		if (updateMusicGroupPaymentCalenderList.size() > 0) {
			musicGroupPaymentCalenderDao.batchUpdate(updateMusicGroupPaymentCalenderList);
		}
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void batchInsert(List<MusicGroupPaymentCalender> musicGroupPaymentCalenders) {
		if (musicGroupPaymentCalenders != null && musicGroupPaymentCalenders.size() > 0) {
			musicGroupPaymentCalenderDao.delByGroupId(musicGroupPaymentCalenders.get(0).getMusicGroupId());
			musicGroupPaymentCalenders.forEach(e -> {
				musicGroupPaymentCalenderDao.insert(e);
			});
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void del(Long id) {
		if (id == null) {
			throw new BizException("参数校验失败");
		}
		MusicGroupPaymentCalender calender = musicGroupPaymentCalenderDao.getForLock(id);
		if (calender == null) {
			throw new BizException("缴费信息不存在");
		}
		
		//缴费项目中没有学生即可删除缴费项目
		List<MusicGroupPaymentCalenderDetail> musicGroupPaymentCalenderDetailList = musicGroupPaymentCalenderDetailDao.queryByCalenderId(id, null);
		if (musicGroupPaymentCalenderDetailList != null && musicGroupPaymentCalenderDetailList.size() > 0) {
			throw new BizException("缴费项目中已存在学员，不能删除");
		}
		if (calender.getStatus() != PaymentCalenderStatusEnum.AUDITING && calender.getStatus() != PaymentCalenderStatusEnum.NO
				&& calender.getStatus() != PaymentCalenderStatusEnum.REJECT && calender.getStatus() != DRAFT) {
			throw new BizException("删除失败,{} 的缴费项目不允许删除");
		}
		MusicGroupStudentClassAdjust byBatchNo = musicGroupStudentClassAdjustDao.findByBatchNo(calender.getBatchNo());
		if(byBatchNo != null){
			throw new BizException("删除失败,班级调整的缴费项目不允许删除");
		}

		if (calender.getPaymentType() == PaymentType.MUSIC_APPLY) {
			// 是否存在缴费中的
			List<StudentPaymentOrder> studentPaymentOrderList = studentPaymentOrderDao.queryByDealStatus(calender.getMusicGroupId(), OrderTypeEnum.APPLY,
					DealStatusEnum.ING);
			if (studentPaymentOrderList != null && studentPaymentOrderList.size() > 0) {
				throw new BizException("存在缴费中的学生，不能删除");
			}
		}
		
		musicGroupPaymentCalenderDao.delete(id);
		musicGroupPaymentCalenderDetailDao.deleteByCalenderId(id);
		musicGroupPaymentStudentCourseDetailDao.deleteByMusicGroupPaymentCalenderId(id);
		
		List<Long> calenderIds = new ArrayList<Long>();
		calenderIds.add(id);
		musicGroupPaymentCalenderCourseSettingsDao.deleteByMusicGroupPaymentCalenderId(calenderIds);
		
		//如果乐团是“费用审核”，则需要修改状态
		MusicGroup musicGroup = musicGroupDao.get(calender.getMusicGroupId());
		if(musicGroup != null){
			if(musicGroup.getStatus() == MusicGroupStatusEnum.FEE_AUDIT){
				musicGroup.setStatus(MusicGroupStatusEnum.PRE_BUILD_FEE);
				musicGroup.setUpdateTime(new Date());
				musicGroupDao.update(musicGroup);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByBatchNo(String batchNo) {
		if(StringUtils.isBlank(batchNo)){
			throw new BizException("参数错误");
		}
		List<MusicGroupPaymentCalender> musicGroupPaymentCalenderList = musicGroupPaymentCalenderDao.findByBatchNo(batchNo);
		for(MusicGroupPaymentCalender musicGroupPaymentCalender : musicGroupPaymentCalenderList){
			del(musicGroupPaymentCalender.getId());
		}
		return true;
	}

	@Override
	public MusicGroupPaymentCalender findByMusicGroupRegCalender(String musicGroupId) {
		return musicGroupPaymentCalenderDao.findByMusicGroupRegCalender(musicGroupId);
	}

	@Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
	public MusicGroupStudentFee updateCalender(Long calenderDetailId,Integer userId){
		Date date = new Date();
		MusicGroupPaymentCalenderDetail calenderDetail = musicGroupPaymentCalenderDetailDao.get(calenderDetailId);
		MusicGroupPaymentCalender calender = this.get(calenderDetail.getMusicGroupPaymentCalenderId());
		MusicGroupStudentFee musicGroupStudentFee = musicGroupStudentFeeDao.findByUser(userId, calender.getMusicGroupId());
		if(calender.getIsGiveMusicNetwork() && musicGroupStudentFee != null){
			//赠送网管课
			musicGroupStudentFee.setRemainNetworkClassTimes(musicGroupStudentFee.getRemainNetworkClassTimes() + 1);
		}
		//更新学生的缴费记录状态
		calenderDetail.setPaymentStatus(MusicGroupStudentFee.PaymentStatus.PAID_COMPLETED);
		calenderDetail.setActualAmount(calenderDetail.getExpectAmount().add(calenderDetail.getExpectMemberAmount()));
		calenderDetail.setPayTime(date);
		calenderDetail.setUpdateTime(date);
		musicGroupPaymentCalenderDetailDao.update(calenderDetail);

		//更新实际缴费人数
		Integer actualNum = calender.getActualNum() == null ? 0 : calender.getActualNum();
		calender.setActualNum(actualNum + 1);
		calender.setUpdateTime(date);
		musicGroupPaymentCalenderDao.update(calender);
		return musicGroupStudentFee;
	}

}
