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

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.BasicUserDto;
import com.ym.mec.biz.dal.dto.MusicCardDto;
import com.ym.mec.biz.dal.dto.SubFeeSettingDto;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.ClassGroupTypeEnum;
import com.ym.mec.biz.service.MusicGroupService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class MusicGroupServiceImpl extends BaseServiceImpl<String, MusicGroup>  implements MusicGroupService {
	
	@Autowired
	private MusicGroupDao musicGroupDao;
	@Autowired
	private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;
	@Autowired
	private MusicGroupPaymentEntitiesDao musicGroupPaymentEntitiesDao;
	@Autowired
	private MusicGroupSubjectGoodsGroupDao musicGroupSubjectGoodsGroupDao;
	@Autowired
	private MusicGroupSubjectPlanDao musicGroupSubjectPlanDao;

	@Override
	public BaseDAO<String, MusicGroup> getDAO() {
		return musicGroupDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(MusicGroup musicGroup) {
		musicGroupDao.insert(musicGroup);
		String[] split = musicGroup.getPaymentMonths().split(",");
		MusicGroupPaymentCalender mg;
		for (String month:split) {
			mg = new MusicGroupPaymentCalender();
			mg.setMusicGroupId(musicGroup.getId());
			mg.setPaymentMonth(Integer.parseInt(month));
			musicGroupPaymentCalenderDao.insert(mg);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void createGroup(SubFeeSettingDto subFeeSettingDto) {
		MusicGroup musicGroup = subFeeSettingDto.getMusicGroup();
		List<MusicGroupPaymentEntities> musicGroupPaymentEntities = subFeeSettingDto.getMusicGroupPaymentEntities();
		List<MusicGroupSubjectGoodsGroup> musicGroupSubjectGoodsGroups = subFeeSettingDto.getMusicGroupSubjectGoodsGroups();
		List<MusicGroupSubjectPlan> musicGroupSubjectPlans = subFeeSettingDto.getMusicGroupSubjectPlans();
		if(musicGroup == null || musicGroupPaymentEntities == null || musicGroupSubjectGoodsGroups == null || musicGroupSubjectPlans == null){
			String musicGroupId = UUID.randomUUID().toString();
			musicGroup.setId(musicGroupId);
			//保存乐团基本信息
			musicGroupDao.insert(musicGroup);
			//保存乐团付费主体列表
			musicGroupPaymentEntities.forEach(e ->{
				e.setMusicGroupId(musicGroupId);
				musicGroupPaymentEntitiesDao.insert(e);
			});
			//保存学员付费周期
			List<Integer> months = subFeeSettingDto.getMonths();
			if(months != null && months.size() > 0){
				MusicGroupPaymentCalender mg;
				for (Integer month:months) {
					mg = new MusicGroupPaymentCalender();
					mg.setMusicGroupId(musicGroupId);
					mg.setPaymentMonth(month);
					musicGroupPaymentCalenderDao.insert(mg);
				}
			}
			//保存乐团声部规划
			musicGroupSubjectPlans.forEach(e ->{
				e.setMusicGroupId(musicGroupId);
				musicGroupSubjectPlanDao.insert(e);
			});
			//保存乐团声部商品规划
			musicGroupSubjectGoodsGroups.forEach(e->{
				e.setMusicGroupId(musicGroupId);
				musicGroupSubjectGoodsGroupDao.insert(e);
			});
		}
	}

	@Override
	public List<MusicCardDto> queryUserMusicGroups(Integer userId) {
		return musicGroupDao.queryUserMusicGroups(userId);
	}

	@Override
	public MusicCardDto queryPersonalMusicGroups(Integer id) {
		MusicCardDto musicCardDto = musicGroupDao.queryPersonalMusicGroups(id);
		musicCardDto.setMusicGroupNum(musicGroupDao.countUserMusicGroups(id));
		musicCardDto.setVipClassNum(musicGroupDao.countCourseByType(id,ClassGroupTypeEnum.VIP));
		return musicGroupDao.queryPersonalMusicGroups(id);
	}

	@Override
	public List<BasicUserDto> findTeachersByStuId(Integer studentId) {
		return musicGroupDao.findTeachersByStuId(studentId);
	}
}