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


import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dao.TeacherCourseStatisticsDao;
import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.entity.TeacherCourseStatistics;
import com.ym.mec.biz.dal.page.TeacherCourseStatisticsQueryInfo;
import com.ym.mec.biz.service.TeacherCourseStatisticsService;
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 com.ym.mec.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class TeacherCourseStatisticsServiceImpl extends BaseServiceImpl<Long, TeacherCourseStatistics> implements TeacherCourseStatisticsService {

    @Autowired
    private TeacherCourseStatisticsDao teacherCourseStatisticsDao;
    @Autowired
    private SysConfigDao sysConfigDao;

    @Override
    public BaseDAO<Long, TeacherCourseStatistics> getDAO() {
        return teacherCourseStatisticsDao;
    }

    @Override
    public PageInfo<TeacherCourseStatistics> queryPageDetail(TeacherCourseStatisticsQueryInfo queryInfo) {
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);

        PageInfo<TeacherCourseStatistics> pageInfo = queryPage(queryInfo);
        List<TeacherCourseStatistics> rows = pageInfo.getRows();
        if (rows != null && rows.size() > 0) {
            for (TeacherCourseStatistics row : rows) {
                if (row.getSubjectList() != null && row.getSubjectList().size() > 0) {
                    String subjectList = row.getSubjectList().stream().map(Subject::getName).collect(Collectors.joining(","));
                    row.setSubjectListStr(subjectList);
                }
            }
        }
        return pageInfo;
    }

    @Override
    public void teacherCourseStatistic() {
        Date nextMonthDate = DateUtil.addMonths(new Date(), 1);
        Date startDate = DateUtil.getFirstDayOfMonth(nextMonthDate);
        Date endDate = DateUtil.getLastDayOfMonth(nextMonthDate);
        String salaryConfig = sysConfigDao.findConfigValue("warning_min_course_salary");
        BigDecimal salary = new BigDecimal(salaryConfig);
        List<TeacherCourseStatistics> teacherCourseStatisticsList = teacherCourseStatisticsDao.findUserByWarringSalary(startDate, endDate, salary);
        if (teacherCourseStatisticsList.size() == 0) {
            return;
        }
        List<Integer> userIds = teacherCourseStatisticsList.stream().map(TeacherCourseStatistics::getUserId).collect(Collectors.toList());
        List<TeacherCourseStatistics> teCourseSalaryStatisAndTypes = teacherCourseStatisticsDao.findUserCourseSalaryByUserIds(userIds, startDate, endDate);

        for (TeacherCourseStatistics teacherCourseStatistics : teacherCourseStatisticsList) {
            int vipCourseNum = 0;
            int musicCourseNum = 0;
            BigDecimal expectVipCourseSalary = BigDecimal.ZERO;
            BigDecimal expectMusicCourseSalary = BigDecimal.ZERO;

            for (TeacherCourseStatistics teCourseSalaryStatisAndType : teCourseSalaryStatisAndTypes) {
                if (!teacherCourseStatistics.getUserId().equals(teCourseSalaryStatisAndType.getUserId())) continue;

                if (teCourseSalaryStatisAndType.getGroupType().equals("VIP")) {
                    expectVipCourseSalary = teCourseSalaryStatisAndType.getExpectTotalSalary();
                    vipCourseNum = teCourseSalaryStatisAndType.getMusicCourseNum();
                } else if (teCourseSalaryStatisAndType.getGroupType().equals("MUSIC")) {
                    expectMusicCourseSalary = teCourseSalaryStatisAndType.getExpectTotalSalary();
                    musicCourseNum = teCourseSalaryStatisAndType.getMusicCourseNum();
                }
            }
            teacherCourseStatistics.setExpectVipCourseSalary(expectVipCourseSalary);
            teacherCourseStatistics.setExpectMusicCourseSalary(expectMusicCourseSalary);
            teacherCourseStatistics.setVipCourseNum(vipCourseNum);
            teacherCourseStatistics.setMusicCourseNum(musicCourseNum);

            if (teacherCourseStatistics.getSubjectIdList() == null) {
                teacherCourseStatistics.setSubjectIdList("");
            }
            teacherCourseStatistics.setMonth(startDate);
        }

        teacherCourseStatisticsDao.batchAdd(teacherCourseStatisticsList);
    }
}
