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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ym.mec.biz.dal.dao.CourseScheduleTeacherSalaryDao;
import com.ym.mec.biz.dal.dao.TeacherDefaultPracticeGroupSalaryDao;
import com.ym.mec.biz.dal.entity.CourseSchedule.CourseScheduleType;
import com.ym.mec.biz.dal.entity.CourseScheduleTeacherSalary;
import com.ym.mec.biz.dal.entity.TeacherDefaultPracticeGroupSalary;
import com.ym.mec.biz.service.TeacherDefaultPracticeGroupSalaryService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;

@Service
public class TeacherDefaultPracticeGroupSalaryServiceImpl extends BaseServiceImpl<Long, TeacherDefaultPracticeGroupSalary>  implements TeacherDefaultPracticeGroupSalaryService {
	
	@Autowired
	private TeacherDefaultPracticeGroupSalaryDao teacherDefaultPracticeGroupSalaryDao;

	@Autowired
	private CourseScheduleTeacherSalaryDao courseScheduleTeacherSalaryDao;

	@Override
	public BaseDAO<Long, TeacherDefaultPracticeGroupSalary> getDAO() {
		return teacherDefaultPracticeGroupSalaryDao;
	}

	@Override
	public List<TeacherDefaultPracticeGroupSalary> queryByUserId(Integer userId) {
		return teacherDefaultPracticeGroupSalaryDao.queryByUserId(userId);
	}

	@Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean update(List<TeacherDefaultPracticeGroupSalary> teacherDefaultPracticeGroupSalaryList, Date startDate) {
		
		if(teacherDefaultPracticeGroupSalaryList != null && teacherDefaultPracticeGroupSalaryList.size() > 0){
			Integer userId = teacherDefaultPracticeGroupSalaryList.get(0).getUserId();
			BigDecimal salary = teacherDefaultPracticeGroupSalaryList.get(0).getMainTeacherSalary();
			
			//查询原课酬
			List<TeacherDefaultPracticeGroupSalary> origSalaryList = teacherDefaultPracticeGroupSalaryDao.queryByUserId(userId);
			TeacherDefaultPracticeGroupSalary origSalary = null;
			if(origSalaryList.size() > 0){
				origSalary = origSalaryList.get(0);
			}
			
			//先清空
			teacherDefaultPracticeGroupSalaryDao.deleteByUserId(userId);
			//再批量新增
			teacherDefaultPracticeGroupSalaryDao.batchInsert(teacherDefaultPracticeGroupSalaryList);
			
			if(startDate != null){
				//更新课酬

				String typeList = CourseScheduleType.PRACTICE.getCode();
				// 查询所有需要修改的课酬
				List<CourseScheduleTeacherSalary> updateTeacherSalaryList = courseScheduleTeacherSalaryDao.queryUserNoSettlementListByTypeAndStartClassDate(
						userId, typeList, startDate);
				
				if (updateTeacherSalaryList != null && updateTeacherSalaryList.size() > 0) {
					
					Date date = new Date();

					List<CourseScheduleTeacherSalary> list = new ArrayList<CourseScheduleTeacherSalary>();

					for (CourseScheduleTeacherSalary ts : updateTeacherSalaryList) {
						if (origSalary != null && origSalary.getMainTeacherSalary().compareTo(ts.getExpectSalary()) == 0
								&& origSalary.getMainTeacherSalary().compareTo(salary) != 0) {
							ts.setExpectSalary(salary);
							ts.setUpdateTime(date);
							list.add(ts);
						}
					}
					if (list.size() > 0) {
						courseScheduleTeacherSalaryDao.batchUpdateTeacherExpectSalarys(list);
					}
				}
			}
			
			return true;
		}
		
		return false;
	}
	
}