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.SchoolDao;
import com.ym.mec.collectfee.entity.Course;
import com.ym.mec.collectfee.entity.ResponseCourseEntity;
import com.ym.mec.collectfee.entity.School;
import com.ym.mec.collectfee.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class SchoolServiceImpl extends BaseServiceImpl<Integer, School> implements SchoolService {
	
	@Autowired
	private SchoolDao schoolDao;

	@Override
	public BaseDAO<Integer, School> getDAO() {
		return schoolDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public School upsetSchool(ResponseCourseEntity courseEntity) {
		List<Course> courses = courseEntity.getCourses();
		if(courses != null && courses.size() > 0){
			Course course = courses.get(0);
			School school = schoolDao.get(course.getClassId());
			Date date = new Date();
			if(school == null){
				school = new School();
				school.setBranchId(course.getBranchId());
				school.setName(course.getClassName());
				school.setSchoolId(course.getClassId());
				school.setStatus(course.getStatus());
				school.setCreateTime(date);
				school.setUpdateTime(date);
				schoolDao.insert(school);
			}else {
				school.setStatus(course.getStatus());
				school.setUpdateTime(date);
				schoolDao.update(school);
			}
			return school;
		}
		return null;
	}
}