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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ym.mec.biz.dal.dao.TeacherCourseStatisticsDao;
import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.dal.entity.TeacherCourseStatistics;
import com.ym.mec.biz.dal.page.TeacherCourseStatisticsQueryInfo;
import com.ym.mec.biz.service.SysConfigService;
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 SysConfigService sysConfigService;

    @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 = new Date();
        Date startDate = DateUtil.getFirstDayOfMonth(nextMonthDate);
        Date endDate = DateUtil.getLastDayOfMonth(nextMonthDate);
        List<TeacherCourseStatistics> teacherCourseStatisticsList = teacherCourseStatisticsDao.findUserByWarringSalary(startDate, endDate);
        if (teacherCourseStatisticsList.size() == 0) {
            return;
        }
        SysConfig salaryConfig = sysConfigService.findByParamName(SysConfigService.WARNING_MIN_COURSE_SALARY);
        JSONObject salaryConfigObject = JSON.parseObject(salaryConfig.getParanValue());

        List<Integer> userIds = teacherCourseStatisticsList.stream().map(TeacherCourseStatistics::getUserId).collect(Collectors.toList());
        List<TeacherCourseStatistics> teCourseSalaryStaticsAndTypes = teacherCourseStatisticsDao.findUserCourseSalaryByUserIds(userIds, startDate, endDate);

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

            for (TeacherCourseStatistics teCourseSalaryStaticsAndType : teCourseSalaryStaticsAndTypes) {
                if (!teacherCourseStatistics.getUserId().equals(teCourseSalaryStaticsAndType.getUserId())) continue;

                if (teCourseSalaryStaticsAndType.getGroupType().equals("VIP")) {
                    expectVipCourseSalary = teCourseSalaryStaticsAndType.getExpectTotalSalary();
                    vipCourseNum = teCourseSalaryStaticsAndType.getMusicCourseNum();
                } else if (teCourseSalaryStaticsAndType.getGroupType().equals("MUSIC")) {
                    expectMusicCourseSalary = teCourseSalaryStaticsAndType.getExpectTotalSalary();
                    musicCourseNum = teCourseSalaryStaticsAndType.getMusicCourseNum();
                } else if (teCourseSalaryStaticsAndType.getGroupType().equals("PRACTICE")) {
                    expectPracticeCourseSalary = teCourseSalaryStaticsAndType.getExpectTotalSalary();
                    practiceCourseNum = teCourseSalaryStaticsAndType.getMusicCourseNum();
                }
            }
            teacherCourseStatistics.setExpectVipCourseSalary(expectVipCourseSalary);
            teacherCourseStatistics.setExpectMusicCourseSalary(expectMusicCourseSalary);
            teacherCourseStatistics.setExpectPracticeCourseSalary(expectPracticeCourseSalary);
            teacherCourseStatistics.setVipCourseNum(vipCourseNum);
            teacherCourseStatistics.setMusicCourseNum(musicCourseNum);
            teacherCourseStatistics.setPracticeCourseNum(practiceCourseNum);

            BigDecimal organWarningSalary = salaryConfigObject.getBigDecimal(teacherCourseStatistics.getOrganId().toString());
            if(Objects.isNull(organWarningSalary)||Objects.isNull(teacherCourseStatistics.getExpectTotalSalary())||teacherCourseStatistics.getExpectTotalSalary().compareTo(organWarningSalary)<0){
                teacherCourseStatistics.setLowSalary(true);
            }else{
                teacherCourseStatistics.setLowSalary(false);
            }

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

        teacherCourseStatisticsDao.deleteByMonth(startDate);
        teacherCourseStatisticsDao.batchAdd(teacherCourseStatisticsList);
    }
}
