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

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.BasicUserDto;
import com.ym.mec.biz.dal.dto.PageInfoDegree;
import com.ym.mec.biz.dal.dto.StudentVisitDto;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.entity.StudentVisit;
import com.ym.mec.biz.dal.page.StudentVisitQueryInfo;
import com.ym.mec.biz.service.StudentVisitService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
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.util.*;
import java.util.stream.Collectors;

@Service
public class StudentVisitServiceImpl extends BaseServiceImpl<Integer, StudentVisit> implements StudentVisitService {

    @Autowired
    private StudentVisitDao studentVisitDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private StudentRepairDao studentRepairDao;
    @Autowired
    private TeacherDao teacherDao;


    @Override
    public BaseDAO<Integer, StudentVisit> getDAO() {
        return studentVisitDao;
    }

    @Override
    public StudentVisit addVisit(StudentVisit studentVisit) {
        SysUser user = teacherDao.getUser(studentVisit.getStudentId());
        studentVisit.setOrganId(user.getOrganId());
        studentVisit.setCreateTime(new Date());
        studentVisitDao.insert(studentVisit);
        return studentVisit;
    }

    @Override
    public StudentVisitDto getInfo(Integer id) {
        return studentVisitDao.getInfo(id);
    }

    @Override
    public PageInfo<StudentVisitDto> getPageList(StudentVisitQueryInfo queryInfo) {
        if (queryInfo.getEndTime() != null) {
            queryInfo.setEndTime(DateUtil.getLastTimeWithDay(queryInfo.getEndTime()));
        }
        PageInfo<StudentVisitDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List<StudentVisitDto> dataList = new ArrayList<>();
        int count = this.findCount(params);

        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentVisitDao.getPageList(params);
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public List<StudentRegistration> getMusicGroupStu(String musicGroupId, Integer teacherId) {
        return studentRegistrationDao.getMusicGroupStu4Teacher(musicGroupId, teacherId);
    }

    @Override
    public List<MusicGroup> getMusicGroup(Integer teacherId) {
        return musicGroupDao.getTeacherMusicGroup(teacherId);
    }

    @Override
    public PageInfo<BasicUserDto> getStudents(StudentVisitQueryInfo queryInfo) {
        PageInfo<BasicUserDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);
        List<BasicUserDto> dataList = new ArrayList<>();
        int count = studentVisitDao.getStudentsCount(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentVisitDao.getStudents(params);
            Set<Integer> userIds = dataList.stream().map(BasicUserDto::getUserId).collect(Collectors.toSet());
            List<MusicGroup> musicGroups = studentRepairDao.getUserMusicGroup(userIds);
            Map<Integer, List<MusicGroup>> userMusicGroup = musicGroups.stream().collect(Collectors.groupingBy(MusicGroup::getRepairUserId));
            for (BasicUserDto basicUserDto : dataList) {
                if (!userMusicGroup.containsKey(basicUserDto.getUserId())) {
                    continue;
                }
                MusicGroup musicGroup = userMusicGroup.get(basicUserDto.getUserId()).get(0);
                basicUserDto.setMusicGroupId(musicGroup.getId());
                basicUserDto.setMusicGroupName(musicGroup.getName());
            }
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }
}
