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

import com.ym.mec.biz.dal.dao.CourseScheduleDao;
import com.ym.mec.biz.dal.dao.CourseScheduleStudentPaymentDao;
import com.ym.mec.biz.dal.dao.VipGroupActivityDao;
import com.ym.mec.biz.dal.dao.VipGroupDao;
import com.ym.mec.biz.dal.entity.CourseSchedule;
import com.ym.mec.biz.dal.entity.CourseScheduleStudentPayment;
import com.ym.mec.biz.dal.entity.VipGroup;
import com.ym.mec.biz.dal.entity.VipGroupActivity;
import com.ym.mec.biz.dal.enums.TeachModeEnum;
import com.ym.mec.biz.dal.enums.VipGroupActivityTypeEnum;
import com.ym.mec.biz.service.CourseScheduleStudentPaymentService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
public class CourseScheduleStudentPaymentServiceImpl extends BaseServiceImpl<Long, CourseScheduleStudentPayment>  implements CourseScheduleStudentPaymentService {
	
	@Autowired
	private CourseScheduleStudentPaymentDao courseScheduleStudentPaymentDao;
	@Autowired
	private VipGroupDao vipGroupDao;
	@Autowired
	private VipGroupActivityDao vipGroupActivityDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;

	@Override
	public BaseDAO<Long, CourseScheduleStudentPayment> getDAO() {
		return courseScheduleStudentPaymentDao;
	}

	@Override
	public int createCourseScheduleStudentPaymentForVipGroup(Long vipGroupId, Long userId) {
		VipGroup vipGroupApplyBaseInfoDto=vipGroupDao.get(vipGroupId);

		if(Objects.isNull(vipGroupApplyBaseInfoDto)){
			throw new BizException("未找到相关vip课程");
		}

		//获取活动信息
		VipGroupActivity vipGroupActivity = vipGroupActivityDao.get(vipGroupId.intValue());

		List<CourseSchedule> vipGroupCourseSchedules=courseScheduleDao.findVipGroupCourseSchedules(vipGroupId);
		if(CollectionUtils.isEmpty(vipGroupCourseSchedules)){
			throw new BizException("为获取到排课信息");
		}
		List<CourseScheduleStudentPayment> courseScheduleStudentPayments=new ArrayList<>();
		Map<TeachModeEnum, List<CourseSchedule>> courseScheduleGroupByTeachMode = vipGroupCourseSchedules.stream().collect(Collectors.groupingBy(CourseSchedule::getTeachMode));
		for(TeachModeEnum teachModeEnum:courseScheduleGroupByTeachMode.keySet()){
			List<CourseSchedule> courseSchedules = courseScheduleGroupByTeachMode.get(teachModeEnum)
					.stream()
					.sorted(Comparator.comparing(CourseSchedule::getStartClassTime)).collect(Collectors.toList());
			for(int i=0;i<courseSchedules.size();i++) {
				//创建学生课程应缴费记录
				CourseScheduleStudentPayment courseScheduleStudentPayment = new CourseScheduleStudentPayment();
				courseScheduleStudentPayment.setCourseScheduleId(courseSchedules.get(i).getId());

				if (teachModeEnum == TeachModeEnum.ONLINE) {
					//学生线上单节课应缴费计算
					switch (vipGroupActivity.getType()) {
						case BASE_ACTIVITY:
							courseScheduleStudentPayment.setExpectPrice(vipGroupApplyBaseInfoDto.getOnlineClassesUnitPrice());
							break;
						case DISCOUNT:
							courseScheduleStudentPayment.setExpectPrice(
									vipGroupApplyBaseInfoDto.getOnlineClassesUnitPrice().multiply(
											new BigDecimal(vipGroupActivity.getAttribute1())
									)
							);
							break;
						case GIVE_CLASS:
							if (vipGroupActivity.getType() == VipGroupActivityTypeEnum.GIVE_CLASS
									&& vipGroupApplyBaseInfoDto.getGiveTeachMode() == teachModeEnum
									&& i >= Integer.parseInt(vipGroupActivity.getAttribute1())) {
								courseScheduleStudentPayment.setExpectPrice(new BigDecimal(0));
							} else {
								courseScheduleStudentPayment.setExpectPrice(vipGroupApplyBaseInfoDto.getOnlineClassesUnitPrice());
							}
							break;
						default:
							throw new BizException("活动类型错误!");
					}
				}else{
					//学生线下单节课应缴费计算
					switch (vipGroupActivity.getType()) {
						case BASE_ACTIVITY:
							courseScheduleStudentPayment.setExpectPrice(vipGroupApplyBaseInfoDto.getOfflineClassesUnitPrice());
							break;
						case DISCOUNT:
							courseScheduleStudentPayment.setExpectPrice(
									vipGroupApplyBaseInfoDto.getOfflineClassesUnitPrice().multiply(
											new BigDecimal(vipGroupActivity.getAttribute1())
									)
							);
							break;
						case GIVE_CLASS:
							if (vipGroupActivity.getType() == VipGroupActivityTypeEnum.GIVE_CLASS
									&& vipGroupApplyBaseInfoDto.getGiveTeachMode() == teachModeEnum
									&& i >= Integer.parseInt(vipGroupActivity.getAttribute1())) {
								courseScheduleStudentPayment.setExpectPrice(new BigDecimal(0));
							} else {
								courseScheduleStudentPayment.setExpectPrice(vipGroupApplyBaseInfoDto.getOfflineClassesUnitPrice());
							}
							break;
						default:
							throw new BizException("活动类型错误!");
					}
					courseScheduleStudentPayments.add(courseScheduleStudentPayment);
				}
			}
		}
		return courseScheduleStudentPaymentDao.batchInsert(courseScheduleStudentPayments);
	}
}