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

import com.ym.mec.collectfee.common.dao.BaseDAO;
import com.ym.mec.collectfee.common.service.impl.BaseServiceImpl;
import com.ym.mec.collectfee.dao.CourseGroupInfoDao;
import com.ym.mec.collectfee.entity.Course;
import com.ym.mec.collectfee.entity.CourseGroupInfo;
import com.ym.mec.collectfee.entity.ResponseCourseEntity;
import com.ym.mec.collectfee.service.CourseGroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseGroupInfoServiceImpl extends BaseServiceImpl<Integer, CourseGroupInfo> implements CourseGroupInfoService {
	
	@Autowired
	private CourseGroupInfoDao courseGroupInfoDao;

	@Override
	public BaseDAO<Integer, CourseGroupInfo> getDAO() {
		return courseGroupInfoDao;
	}


	@Override
	public void upSetCourseGroup(ResponseCourseEntity xmlToObject){
		List<Course> courses = xmlToObject.getCourses();
		CourseGroupInfo courseGroupInfo;
		Date date = new Date();
		for (Course course:courses) {
			if(course != null && courseGroupInfoDao.get(course.getCourseId()) == null){
				courseGroupInfo = new CourseGroupInfo();
				courseGroupInfo.setCreateTime(date);
				courseGroupInfo.setUpdateTime(date);
				courseGroupInfo.setFeeAmount(course.getPrice());
				courseGroupInfo.setFeeType(course.getChargeType());
				courseGroupInfo.setName(course.getCourseName());
				courseGroupInfo.setPlanNum(course.getEnrollCount());
				courseGroupInfo.setId(course.getCourseId());
				courseGroupInfo.setRegNum(course.getStuCount());
				courseGroupInfo.setSubId(course.getSubId());
				courseGroupInfo.setSubName(course.getSubNames());
				courseGroupInfo.setClassId(course.getClassId());
				courseGroupInfoDao.insert(courseGroupInfo);
			}
		}
	}

	@Override
	public List<CourseGroupInfo> getCourses(Integer classId) {
		return courseGroupInfoDao.getCourses(classId);
	}

	@Override
	public int upByIdAndVersion(CourseGroupInfo courseGroupInfo) {
		return courseGroupInfoDao.upByIdAndVersion(courseGroupInfo);
	}

}