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

import com.ym.mec.biz.dal.dao.CourseScheduleTeacherSalaryDao;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.SalarySettlementTypeEnum;
import com.ym.mec.biz.dal.enums.TeachTypeEnum;
import com.ym.mec.biz.service.CourseScheduleService;
import com.ym.mec.biz.service.CourseScheduleTeacherSalaryService;
import com.ym.mec.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.ClassGroupTeacherMapperDao;
import com.ym.mec.biz.service.ClassGroupTeacherMapperService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class ClassGroupTeacherMapperServiceImpl extends BaseServiceImpl<Long, ClassGroupTeacherMapper> implements ClassGroupTeacherMapperService {

    @Autowired
    private ClassGroupTeacherMapperDao classGroupTeacherMapperDao;
    @Autowired
    private CourseScheduleService courseScheduleService;
    @Autowired
    private CourseScheduleTeacherSalaryDao courseScheduleTeacherSalaryDao;

    @Override
    public BaseDAO<Long, ClassGroupTeacherMapper> getDAO() {
        return classGroupTeacherMapperDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int classGroupTeachersInsert(List<ClassGroupTeacherMapper> classGroupTeacherMapperList) {
        //删除班级已经设置的老师
        classGroupTeacherMapperDao.delMusicGroupTeacherMapper(classGroupTeacherMapperList);
        return classGroupTeacherMapperDao.classGroupTeachersInsert(classGroupTeacherMapperList);
    }


    @Override
    public List<ClassGroupTeacherMapper> findClassGroupTeachers(List ClassGroupList) {
        return classGroupTeacherMapperDao.findClassGroupTeachers(ClassGroupList);
    }

    public boolean classGroupTeacherMapperUpdate(List<ClassGroupTeacherMapper> classGroupTeacherMapperList) throws Exception {
        Date date = new Date();

        List<ClassGroupTeacherSalary> classGroupTeacherSalaryList = new ArrayList<>();
        List<CourseScheduleTeacherSalary> courseScheduleTeacherSalaryList = new ArrayList<>();

        List<CourseSchedule> classGroupNoStartCourseSchedules = courseScheduleService.findClassGroupNoStartCourseSchedules(classGroupTeacherMapperList);
        for (CourseSchedule classGroupNoStartCourseSchedule : classGroupNoStartCourseSchedules) {

            if (!classGroupNoStartCourseSchedule.getType().equals(CourseSchedule.CourseScheduleType.SINGLE) &&
                    !classGroupNoStartCourseSchedule.getType().equals(CourseSchedule.CourseScheduleType.MIX) &&
                    !classGroupNoStartCourseSchedule.getType().equals(CourseSchedule.CourseScheduleType.HIGH)
            ) {
                continue;
            }
            //课时长度
            int duration = DateUtil.minutesBetween(classGroupNoStartCourseSchedule.getEndClassTime(), classGroupNoStartCourseSchedule.getStartClassTime());
            for (ClassGroupTeacherMapper classGroupTeacherMapper : classGroupTeacherMapperList) {
                if (!classGroupTeacherMapper.getClassGroupId().equals(classGroupNoStartCourseSchedule.getClassGroupId())) {
                    continue;
                }
                BigDecimal salary = new BigDecimal("0");
                List<TeacherDefaultMusicGroupSalary> teacherDefaultMusicGroupSalaryList = classGroupTeacherMapper.getTeacherDefaultMusicGroupSalaryList();
                for (TeacherDefaultMusicGroupSalary teacherDefaultMusicGroupSalary : teacherDefaultMusicGroupSalaryList) {
                    //对应基准课酬
                    BigDecimal baseSalary = classGroupTeacherMapper.getTeacherRole().equals(TeachTypeEnum.BISHOP) ? teacherDefaultMusicGroupSalary.getMainTeacherSalary() : teacherDefaultMusicGroupSalary.getAssistantTeacherSalary();
                    //基准课酬
                    if (teacherDefaultMusicGroupSalary.getSettlementType().equals(SalarySettlementTypeEnum.TEACHER_DEFAULT)) {
                        salary = new BigDecimal(duration).divide(new BigDecimal(30)).multiply(baseSalary).setScale(2, BigDecimal.ROUND_HALF_UP);
                        break;
                    }
                    //阶梯课酬
                    if (duration >= teacherDefaultMusicGroupSalary.getDurationMin() && duration <= teacherDefaultMusicGroupSalary.getDurationMin()) {
                        salary = baseSalary;
                        break;
                    }
                }

                //班级与老师课酬表
//                ClassGroupTeacherSalary classGroupTeacherSalary = new ClassGroupTeacherSalary();
//                classGroupTeacherSalary.setMusicGroupId(classGroupTeacherMapper.getMusicGroupId());
//                classGroupTeacherSalary.setClassGroupId(classGroupTeacherMapper.getClassGroupId());
//                classGroupTeacherSalary.setTeacherRole(classGroupTeacherMapper.getTeacherRole());
//                classGroupTeacherSalary.setUserId(classGroupTeacherMapper.getUserId());
//                classGroupTeacherSalary.setDuration(duration);
//                classGroupTeacherSalary.setSalary(salary);
//                classGroupTeacherSalary.setCreateTime(date);
//                classGroupTeacherSalary.setUpdateTime(date);
//                classGroupTeacherSalaryList.add(classGroupTeacherSalary);

                //课程与老师薪水表
                CourseScheduleTeacherSalary courseScheduleTeacherSalary = new CourseScheduleTeacherSalary();
                courseScheduleTeacherSalary.setCourseScheduleId(classGroupNoStartCourseSchedule.getId());
                courseScheduleTeacherSalary.setTeacherRole(classGroupTeacherMapper.getTeacherRole());
                courseScheduleTeacherSalary.setUserId(classGroupTeacherMapper.getUserId());
                courseScheduleTeacherSalary.setExpectSalary(salary);
                courseScheduleTeacherSalary.setCreateTime(date);
                courseScheduleTeacherSalary.setCreateUpdate(date);
                courseScheduleTeacherSalaryList.add(courseScheduleTeacherSalary);
            }
        }
        courseScheduleTeacherSalaryDao.batchInsert(courseScheduleTeacherSalaryList);
        return true;
    }

}