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

import com.ym.mec.biz.dal.dao.InspectionItemDao;
import com.ym.mec.biz.dal.dao.StudentVisitDao;
import com.ym.mec.biz.dal.dto.TeacherVisitDto;
import com.ym.mec.biz.dal.enums.InspectionItemEnum;
import com.ym.mec.biz.dal.page.InspectionQueryInfo;
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.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ym.mec.biz.dal.entity.InspectionItem;
import com.ym.mec.biz.service.InspectionItemService;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class InspectionItemServiceImpl extends BaseServiceImpl<Long, InspectionItem> implements InspectionItemService {

    @Autowired
    private InspectionItemDao inspectionItemDao;
    @Autowired
    private StudentVisitDao studentVisitDao;

    @Override
    public BaseDAO<Long, InspectionItem> getDAO() {
        return inspectionItemDao;
    }

    @Override
    public PageInfo<InspectionItem> getPageList(InspectionQueryInfo queryInfo) {
        if (queryInfo.getMonth() != null) {
            queryInfo.setStartTime(DateUtil.getFirstDayOfMonth(queryInfo.getMonth()));
            queryInfo.setEndTime(DateUtil.getLastTimeWithDay(DateUtil.getLastDayOfMonth(queryInfo.getMonth())));
        }
        if (queryInfo.getStartTime() != null) {
            queryInfo.setStartTime(DateUtil.getFirstDayOfMonth(queryInfo.getStartTime()));
        }
        if (queryInfo.getEndTime() != null) {
            queryInfo.setEndTime(DateUtil.getLastTimeWithDay(DateUtil.getLastDayOfMonth(queryInfo.getEndTime())));
        }
        PageInfo<InspectionItem> pageInfo = this.queryPage(queryInfo);
        if (pageInfo.getRows().size() <= 0) {
            return pageInfo;
        }
        List<InspectionItem> rows = pageInfo.getRows();
        List<Integer> teacherIds = rows.stream().filter(e -> e.getItem().equals(InspectionItemEnum.VISIT)).map(InspectionItem::getUserId).collect(Collectors.toList());
        if (teacherIds.size() > 0) {
            Date startTime = rows.stream().map(InspectionItem::getMonth).min(Date::compareTo).get();
            Date EndTime = rows.stream().map(InspectionItem::getMonth).max(Date::compareTo).get();
            startTime = DateUtil.getFirstDayOfMonth(startTime);
            EndTime = DateUtil.getLastTimeWithDay(DateUtil.getLastDayOfMonth(EndTime));
            List<TeacherVisitDto> teacherVisitCounts = studentVisitDao.getTeacherVisitCount(teacherIds, startTime, EndTime);
            for (InspectionItem row : rows) {
                if (row.getItem().equals(InspectionItemEnum.INSPECT)) continue;
                for (TeacherVisitDto teacherVisitCount : teacherVisitCounts) {
                    if (teacherVisitCount.getTeacherId().equals(row.getUserId()) &&
                            DateUtil.dateToString(row.getMonth(), "yyyy-MM").equals(teacherVisitCount.getMonth())
                    ) {
                        row.setSubmittedTimes(teacherVisitCount.getNum());
                    }
                }
            }
        }
        return pageInfo;
    }
}


