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 mecCourseGroupInfo;
        Date date = new Date();
        for (Course course : courses) {
			CourseGroupInfo courseGroupInfo = courseGroupInfoDao.get(course.getCourseId());
			if (course != null && courseGroupInfo == null) {
                mecCourseGroupInfo = new CourseGroupInfo();
                mecCourseGroupInfo.setCreateTime(date);
                mecCourseGroupInfo.setUpdateTime(date);
                mecCourseGroupInfo.setFeeAmount(course.getPrice());
                mecCourseGroupInfo.setFeeType(course.getChargeType());
                mecCourseGroupInfo.setName(course.getCourseName());
                mecCourseGroupInfo.setPlanNum(course.getEnrollCount());
                mecCourseGroupInfo.setId(course.getCourseId());
                mecCourseGroupInfo.setRegNum(course.getStuCount());
                mecCourseGroupInfo.setSubId(course.getSubId());
                mecCourseGroupInfo.setSubName(course.getSubNames());
                mecCourseGroupInfo.setClassId(course.getClassId());
                courseGroupInfoDao.insert(mecCourseGroupInfo);
            }
			boolean flag = false;
            if (course != null && courseGroupInfo != null && !courseGroupInfo.getPlanNum().equals(course.getEnrollCount())) {
				courseGroupInfo.setPlanNum(course.getEnrollCount());
				flag = true;
            }

            if (course != null && courseGroupInfo != null && !courseGroupInfo.getFeeType().equals(course.getChargeType())) {
                courseGroupInfo.setFeeType(course.getChargeType());
                flag = true;
            }

            if (course != null && courseGroupInfo != null && !courseGroupInfo.getFeeAmount().equals(course.getPrice())) {
                courseGroupInfo.setFeeAmount(course.getPrice());
                flag = true;
            }

            if(flag){
                courseGroupInfoDao.upByIdAndVersion(courseGroupInfo);
            }
        }
    }

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

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

}