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

import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender;
import com.ym.mec.biz.dal.entity.OperatingReportCloud;
import com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail;
import com.ym.mec.biz.dal.enums.OrderDetailTypeEnum;
import com.ym.mec.biz.service.CloudTeacherFreeCourseService;
import com.ym.mec.biz.service.OperatingReportCloudService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class OperatingReportCloudServiceImpl extends BaseServiceImpl<Integer, OperatingReportCloud>  implements OperatingReportCloudService {
	
	@Autowired
	private OperatingReportCloudDao operatingReportCloudDao;
	@Autowired
	private OrganCourseTypeOriginalCostDao organCourseTypeOriginalCostDao;
	@Autowired
	private CloudTeacherFreeCourseService cloudTeacherFreeCourseService;
	@Autowired
	private StudentPaymentOrderDetailDao studentPaymentOrderDetailDao;
	@Autowired
	private StudentPaymentOrderDao studentPaymentOrderDao;
	@Autowired
	private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;

	@Override
	public BaseDAO<Integer, OperatingReportCloud> getDAO() {
		return operatingReportCloudDao;
	}

	@Override
	public OperatingReportCloudDao getDao() {
		return operatingReportCloudDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateSet(MusicGroupPaymentCalender calender, Long orderId) {
		if(!calender.getCloudTeacherPaymentFlag()){
			return;
		}
		BigDecimal amount = BigDecimal.ZERO;
		//查询云教练价格
		List<StudentPaymentOrderDetail> orderDetail = studentPaymentOrderDetailDao.findApplyOrderGoods(orderId);
		if(CollectionUtils.isNotEmpty(orderDetail)){
			amount = orderDetail.stream().filter(e->e.getType() == OrderDetailTypeEnum.CLOUD_TEACHER || e.getType() == OrderDetailTypeEnum.CLOUD_TEACHER_PLUS)
					.map(e->e.getPrice()).reduce(BigDecimal.ZERO,BigDecimal::add);
		}else {
			amount = studentPaymentOrderDao.get(orderId).getActualAmount();
		}
		if(calender.getPaymentType() == MusicGroupPaymentCalender.PaymentType.ADD_STUDENT){
			this.saveMonthOperating(calender,amount);
			return;
		}
		//找出非当月结算的缴费项目预算，如果为空则创建。如果已结算则创建当月结转的云教练预收、否则修改云教练预算
		//获取云教练信息
		OperatingReportCloud reportCloud = operatingReportCloudDao.findByCalenderId(calender.getId());
		if(Objects.isNull(reportCloud)){
			reportCloud = new OperatingReportCloud();
			reportCloud.setCalenderId(calender.getId());
			reportCloud.setCloudPrice(amount);
			reportCloud.setOrganId(calender.getOrganId());
			operatingReportCloudDao.insert(reportCloud);
		}else {
			if(reportCloud.getSettlementFlag()){
				this.saveMonthOperating(calender,amount);
			}else {
				reportCloud.setCloudPrice(reportCloud.getCloudPrice().add(amount));
				reportCloud.setUpdateTime(new Date());
				operatingReportCloudDao.update(reportCloud);
			}
		}
	}

	private void saveMonthOperating(MusicGroupPaymentCalender calender,BigDecimal amount){
		OperatingReportCloud currentReportCloud = operatingReportCloudDao.findByCurrentCalenderId(calender.getId());
		if(Objects.isNull(currentReportCloud)){
			currentReportCloud = new OperatingReportCloud();
			currentReportCloud.setCalenderId(calender.getId());
			currentReportCloud.setCloudPrice(amount);
			currentReportCloud.setOrganId(calender.getOrganId());
			currentReportCloud.setMonthFlag(true);
			operatingReportCloudDao.insert(currentReportCloud);
		}else {
			currentReportCloud.setCloudPrice(currentReportCloud.getCloudPrice().add(amount));
			operatingReportCloudDao.update(currentReportCloud);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateSet(Long calenderId,List<Long> scheduleIdList) {
		//如果是进行中加学员，全部算在当月云教练结转。不用摊到课程
		MusicGroupPaymentCalender calender = musicGroupPaymentCalenderDao.get(calenderId);
		if(calender.getPaymentType() == MusicGroupPaymentCalender.PaymentType.ADD_STUDENT){
			return;
		}
		OperatingReportCloud reportCloud = operatingReportCloudDao.findByCalenderId(calenderId);
		if(Objects.isNull(reportCloud)){
			throw new BizException("操作失败，请联系管理员");
		}
		if(reportCloud.getSettlementFlag()){
			return;
		}
		cloudTeacherFreeCourseService.batchInsert(scheduleIdList,calenderId);
		//计算课程预收
		BigDecimal costPrice = organCourseTypeOriginalCostDao.getCourseCostPrice(StringUtils.join(scheduleIdList,","));
		if(Objects.isNull(costPrice)){
			throw new BizException("操作失败，当前课程类型不支持云教练赠送");
		}
		reportCloud.setCloudPrice(reportCloud.getCloudPrice().subtract(costPrice));
		reportCloud.setCoursePrice(reportCloud.getCoursePrice().add(costPrice));
		reportCloud.setUpdateTime(new Date());
		operatingReportCloudDao.update(reportCloud);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(MusicGroupPaymentCalender musicGroupPaymentCalender) {
		if(musicGroupPaymentCalender.getCloudTeacherPaymentFlag()){
			OperatingReportCloud reportCloud = new OperatingReportCloud();
			reportCloud.setCalenderId(musicGroupPaymentCalender.getId());
			reportCloud.setCloudPrice(BigDecimal.ZERO);
			reportCloud.setOrganId(musicGroupPaymentCalender.getOrganId());
			if(musicGroupPaymentCalender.getPaymentType() == MusicGroupPaymentCalender.PaymentType.ADD_STUDENT){
				reportCloud.setMonthFlag(true);
			}
			operatingReportCloudDao.insert(reportCloud);
		}
	}
}