package com.ym.mec.biz.event.listener;

import com.ym.mec.biz.dal.dao.IndexBaseMonthDataDao;
import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.entity.IndexBaseMonthData;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.IndexDataType;
import com.ym.mec.biz.dal.enums.StudentMusicGroupStatusEnum;
import com.ym.mec.biz.event.MusicGroupStudentChangeEvent;
import com.ym.mec.util.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

/**
 * @Author Joburgess
 * @Date 2021/2/24 0024
 */
@Component
public class GroupEventListener {

    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private IndexBaseMonthDataDao indexBaseMonthDataDao;

    @Async
    @EventListener
    public void musicGroupStudentChangeMonitor(MusicGroupStudentChangeEvent musicGroupStatusChangeEvent){
        if(Objects.isNull(musicGroupStatusChangeEvent.getMusicGroupId())||Objects.isNull(musicGroupStatusChangeEvent.getStudentMusicGroupStatus())){
            return;
        }
        MusicGroup musicGroup = musicGroupDao.get(musicGroupStatusChangeEvent.getMusicGroupId());
        if(Objects.isNull(musicGroup)){
            return;
        }
        List<Integer> studentIds = musicGroupStatusChangeEvent.getStudentIds();
        if(CollectionUtils.isEmpty(studentIds)) {
            List<StudentRegistration> studentRegistrations = studentRegistrationDao.queryStudentByMusicGroupId(musicGroupStatusChangeEvent.getMusicGroupId());
            studentIds = studentRegistrations.stream().map(StudentRegistration::getUserId).collect(Collectors.toList());
        }
        if(CollectionUtils.isEmpty(studentIds)){
            return;
        }
        List<StudentRegistration> studentNormalRegistration = studentRegistrationDao.getStudentNormalRegistration(new HashSet<>(studentIds));
        Map<Integer, Set<String>> userGroupIdMap = studentNormalRegistration.stream().collect(Collectors.groupingBy(StudentRegistration::getUserId, Collectors.mapping(StudentRegistration::getMusicGroupId, Collectors.toSet())));
        int updateNum = 0;
        for (Integer studentId : studentIds) {
            if(userGroupIdMap.containsKey(studentId)&&userGroupIdMap.get(studentId).size()>1){
                continue;
            }
            if(userGroupIdMap.containsKey(studentId)&&!userGroupIdMap.get(studentId).contains(musicGroupStatusChangeEvent.getMusicGroupId())){
                continue;
            }
            updateNum+=1;
        }
        LocalDate nowDate = LocalDate.now();
        String dayStr = nowDate.toString();
        IndexBaseMonthData indexBaseMonthData = null;
        switch (musicGroupStatusChangeEvent.getStudentMusicGroupStatus()){
            case NORMAL:
                indexBaseMonthData = indexBaseMonthDataDao.getOrganDataWithDayAndDataType(musicGroup.getOrganId(), dayStr, IndexDataType.NEWLY_STUDENT_NUM);
                break;
            case QUIT:
                indexBaseMonthData = indexBaseMonthDataDao.getOrganDataWithDayAndDataType(musicGroup.getOrganId(), dayStr, IndexDataType.QUIT_MUSIC_GROUP_STUDENT_NUM);
                break;
            default:
                return;
        }
        if(Objects.isNull(indexBaseMonthData)){
            indexBaseMonthData = new IndexBaseMonthData();
            Date date = DateUtil.stringToDate(dayStr, "yyyy-MM-dd");
            BigDecimal zero = new BigDecimal(0);
            indexBaseMonthData.setMonth(date);
            indexBaseMonthData.setOrganId(musicGroup.getOrganId());
            indexBaseMonthData.setDataType(StudentMusicGroupStatusEnum.NORMAL.equals(musicGroupStatusChangeEvent.getStudentMusicGroupStatus())?IndexDataType.NEWLY_STUDENT_NUM:IndexDataType.QUIT_MUSIC_GROUP_STUDENT_NUM);
            indexBaseMonthData.setTotalNum(zero);
            indexBaseMonthData.setActivateNum(zero);
            indexBaseMonthData.setPercent(zero);
        }
        indexBaseMonthData.setTotalNum(indexBaseMonthData.getTotalNum().add(new BigDecimal(updateNum)));
        indexBaseMonthData.setActivateNum(indexBaseMonthData.getActivateNum().add(new BigDecimal(updateNum)));
        indexBaseMonthData.setPercent(indexBaseMonthData.getPercent().add(new BigDecimal(updateNum)));
        if(Objects.isNull(indexBaseMonthData.getId())){
            indexBaseMonthDataDao.insert(indexBaseMonthData);
        }else{
            indexBaseMonthDataDao.update(indexBaseMonthData);
        }
    }

}
