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

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.CourseScheduleStudentPaymentDao;
import com.ym.mec.biz.dal.dto.PracticeSumDto;
import com.ym.mec.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.PracticeLessonApplyDao;
import com.ym.mec.biz.dal.entity.PracticeLessonApply;
import com.ym.mec.biz.service.PracticeLessonApplyService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class PracticeLessonApplyServiceImpl extends BaseServiceImpl<Integer, PracticeLessonApply>  implements PracticeLessonApplyService {
	
	@Autowired
	private PracticeLessonApplyDao practiceLessonApplyDao;
	@Autowired
	private CourseScheduleStudentPaymentDao scheduleStudentPaymentDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Integer, PracticeLessonApply> getDAO() {
		return practiceLessonApplyDao;
	}

	@Override
	public PracticeLessonApply findByUserId(Integer userId) {
		return practiceLessonApplyDao.findByUserId(userId);
	}

	@Override
	public Map<String, Object> practiceSum() {
		Set<Integer> musicUserIds = scheduleStudentPaymentDao.queryMusicStudentPer();
		Set<Integer> vipUserIds = scheduleStudentPaymentDao.queryVipStudentPer();
		musicUserIds.addAll(vipUserIds);
		List<PracticeSumDto> practiceSumDtos = practiceLessonApplyDao.practiceSum(musicUserIds);
		Integer totalNum = practiceLessonApplyDao.getTotalNum(musicUserIds);
		Integer activeNum = practiceLessonApplyDao.getActiveNum(musicUserIds);
		Integer makeNum = practiceLessonApplyDao.getMakeNum(musicUserIds);
		Map<String,Object> map = new HashMap<>(4);
		map.put("totalNum",totalNum);
		map.put("activeNum",activeNum);
		map.put("makeNum",makeNum);
		map.put("practiceSumDtos",practiceSumDtos);
		return map;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(String memo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			throw new BizException("获取用户信息失败");
		}
		PracticeLessonApply lessonApply = practiceLessonApplyDao.findByUserId(sysUser.getId());
		if(lessonApply != null){
			throw new BizException("您已申请过网管课!");
		}
		PracticeLessonApply practiceLessonApply = new PracticeLessonApply();
		practiceLessonApply.setCreateTime(new Date());
		practiceLessonApply.setMemo(memo);
		practiceLessonApply.setUserId(sysUser.getId());
		practiceLessonApplyDao.insert(practiceLessonApply);
	}
}