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

import com.ym.mec.biz.dal.dao.StudentDao;
import com.ym.mec.biz.dal.entity.Student;
import com.ym.mec.biz.dal.page.StudentQueryInfo;
import com.ym.mec.biz.service.StudentService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class StudentServiceImpl extends BaseServiceImpl<Integer, Student> implements StudentService {

	@Autowired
	private StudentDao studentDao;

	@Override
	public BaseDAO<Integer, Student> getDAO() {
		return studentDao;
	}

	@Override
	public PageInfo findStudentVipGroupList(StudentQueryInfo queryInfo) {
		PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List dataList = null;
		int count = studentDao.countStudents(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = studentDao.findStudents(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Long upSet(Student student) {
		if(studentDao.get(student.getUserId()) == null){
			return studentDao.insert(student);
		}else {
			student.setUpdateTime(new Date());
			studentDao.update(student);
			return student.getUserId().longValue();
		}
	}
}
