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

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.entity.SysUserCashAccount;
import com.ym.mec.biz.dal.page.*;
import com.ym.mec.biz.service.StudentManageService;
import com.ym.mec.common.entity.ImResult;
import com.ym.mec.common.entity.ImUserModel;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.im.ImFeignService;
import com.ym.mec.util.collection.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author Joburgess
 * @Date 2019/9/19
 */
@Service
public class StudentManageServiceImpl implements StudentManageService {

    @Autowired
    private StudentManageDao studentManageDao;
    @Autowired
    private MusicGroupStudentFeeDao musicGroupStudentFeeDao;
    @Autowired
    private CourseScheduleStudentPaymentDao scheduleStudentPaymentDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private ImFeignService imFeignService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ClassGroupDao classGroupDao;
    @Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;
    @Autowired
    private VipGroupDao vipGroupDao;
    @Autowired
    private SysUserCashAccountDao sysUserCashAccountDao;

    @Override
    public PageInfo findStudentsByOrganId(StudentManageQueryInfo queryInfo) {
        PageInfo<StudentManageListDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Boolean hasCourse = queryInfo.getHasCourse();
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);
        params.put("offset", pageInfo.getOffset());
        int count = 0;
        if(hasCourse != null){
            List<Integer> userIds;
            if(hasCourse){
                userIds = studentManageDao.queryHasCourse(params);
                count = studentManageDao.countHasCourse(params);
            }else {
                userIds = studentManageDao.queryNotCourse(params);
                count = studentManageDao.countNotCourse(params);
            }
            params.remove("offset");
            params.put("userIds",userIds);
        }else {
            count = studentManageDao.countStudentByOrganId(params);
        }
        List<StudentManageListDto> dataList = null;
        if (count > 0) {
            pageInfo.setTotal(count);
            dataList = studentManageDao.findStudentsByOrganId(params);
            List<Integer> userIds = dataList.stream()
                    .map(StudentManageListDto::getUserId).collect(Collectors.toList());
            List<SysUserCashAccount> byUserIds = sysUserCashAccountDao.findByUserIds(userIds);
//            Map<Integer,Long> hasCourseMap = MapUtil.convertIntegerMap(studentManageDao.queryStudentHasCourse(userIds));
            Map<Integer, SysUserCashAccount> collect = byUserIds.stream()
                    .collect(Collectors.toMap(SysUserCashAccount::getUserId, sysUserCashAccount -> sysUserCashAccount));
            dataList.forEach(e -> {
                if(hasCourse == null){
                    e.setHasCourse(studentManageDao.getHasCourse(e.getUserId()));
                }else {
                    if(hasCourse){
                        e.setHasCourse(1L);
                    }else {
                        e.setHasCourse(0L);
                    }
                }
                if(collect.get(e.getUserId()) != null){
                    e.setCourseBalance(collect.get(e.getUserId()).getCourseBalance());
                }
            });
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public StudentManageListDto findStudentManageBaseInfo(Integer userId) {
        return studentManageDao.findStudentBaseInfoByUserID(userId);
    }

    @Override
    public List<StudentManageBaseInfoOfMusicGroupDto> findStudentMusicGroupsByUserId(Integer userId) {
        List<StudentManageBaseInfoOfMusicGroupDto> musicGroups = studentManageDao.findStudentMusicGroupsByUserId(userId);
        //获取学员报名时所选乐器
        if(musicGroups != null && musicGroups.size() > 0){
            for (StudentManageBaseInfoOfMusicGroupDto e:musicGroups) {
                e.setUserGoodsDtos(studentPaymentOrderDao.findGoodsIds(e.getMusicGroupId(),e.getUserId()));
            }
        }
        return musicGroups;
    }

    @Override
    public PageInfo findStudentCourseList(StudentManageCourseQueryInfo queryInfo) {
        PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List dataList = null;
        int count = studentManageDao.countStudentCourses(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.findStudentCourseList(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public PageInfo findStudentAttendances(StudentManageAttendanceQueryInfo queryInfo) {
        PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List<StudentManageCourseListDto> dataList = null;
        int count = studentManageDao.countStudentAttendances(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.findStudentAttendances(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public PageInfo<StudentManageVipGroupDto> findStudentVipGroups(StudentManageCourseQueryInfo queryInfo) {
        PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List dataList = null;
        int count = vipGroupDao.countStudentVipGroups(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = vipGroupDao.findStudentVipGroups(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public StudentManageAccountBaseInfoDto getStudentAccountBaseInfo(Integer userId) {
        return studentManageDao.getStudentAccountBaseInfo(userId);
    }

    @Override
    public PageInfo queryStudentPayment(TeacherPaymentRecordInfo queryInfo) {
        PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List dataList = null;
        int count = studentManageDao.countStudentPayment(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.queryStudentPayment(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public PageInfo<MusicGroupStudentsDto> queryMusicGroupStudent(MusicGroupStudentQueryInfo queryInfo) {
        PageInfo<MusicGroupStudentsDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);

        List<MusicGroupStudentsDto> dataList = null;
        int count = studentManageDao.countMusicGroupStudent(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.queryMusicGroupStudent(params);
            Map<Integer,String> maps = MapUtil.convertIntegerMap(musicGroupStudentFeeDao.findPaymentStatusMap(queryInfo.getMusicGroupId()));
            dataList.forEach(e->{
                e.setPaymentStatus(maps.get(e.getUserId()));
            });
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public Object musicGroupStudentsSum(String musicGroupId) {
        Map<String,Integer> resultMap = new HashMap<>(3);
        //乐团在读人数
        resultMap.put("studying",studentManageDao.countStudyNum(musicGroupId));
        //退团人数
        resultMap.put("quit",studentManageDao.countQuitNum(musicGroupId));
        //新增人数
        resultMap.put("add",studentManageDao.countAddNum(musicGroupId));
        return resultMap;
    }

    @Override
    public PageInfo<MusicGroupStudentSignDto> queryMusicGroupStudentsSign(StudentSignQueryInfo queryInfo) {
        PageInfo<MusicGroupStudentSignDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);

        List<MusicGroupStudentSignDto> dataList = null;
        int count = studentManageDao.countMusicGroupStudentsSign(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.queryMusicGroupStudentsSign(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public List<ConditionDto> queryStudentClassGroup(String musicGroupId, Integer teacherId) {
        return classGroupDao.queryStudentClassGroup(musicGroupId,teacherId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(SysUser sysUser) {
        studentManageDao.updateUser(sysUser);
        StudentRegistration studentRegistration = new StudentRegistration();
        studentRegistration.setUserId(sysUser.getId());
        studentRegistration.setCurrentClass(sysUser.getCurrentClass());
        studentRegistration.setCurrentGrade(sysUser.getCurrentGrade());
        studentRegistrationDao.updateCurrentClass(studentRegistration);
    }

    @Override
    public PageInfo<StudentManageVipGroupClassDto> findStudentVipGroupClass(StudentManageVipClassQueryInfo queryInfo) {
        PageInfo<StudentManageVipGroupClassDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);

        List<StudentManageVipGroupClassDto> dataList = null;
        int count = studentManageDao.countStudentVipGroupClass(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.findStudentVipGroupClass(params);
            Set<Integer> teacherIds = dataList.stream().map(e -> e.getTeacherId()).collect(Collectors.toSet());
            List<Map<Integer, String>> maps = teacherDao.queryNameByIds(StringUtils.join(teacherIds, ","));
            Map<Integer, String> teacherNameMap = MapUtil.convertMybatisMap(maps);
            dataList.forEach(e->{
                e.setTeacherName(teacherNameMap.get(e.getTeacherId()));
            });
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int studentRegister(SysUser sysUser) {
        if(sysUser == null || StringUtils.isEmpty(sysUser.getPhone())){
            throw new BizException("参数校验失败");
        }
        SysUser sysUser1 = sysUserFeignService.queryUserByMobile(sysUser.getPhone());
        if(sysUser1 != null){
            if(sysUser1.getUserType().contains("STUDENT")){
                throw new BizException("手机号已被占用");
            }else {
                sysUser.setId(sysUser1.getId());
                sysUser.setUserType(sysUser1.getUserType() + ",STUDENT");
                teacherDao.updateUser(sysUser);
                return sysUser1.getId();
            }
        }
        sysUser.setUserType("STUDENT");
        teacherDao.addSysUser(sysUser);
        Integer userId = sysUser.getId();
        ImResult register = imFeignService.register(new ImUserModel(userId.toString(), sysUser.getPhone(), null));
        sysUser.setImToken(register.getToken());
        teacherDao.updateUser(sysUser);
        sysUserCashAccountDao.insert(new SysUserCashAccount(userId, "CNY"));
        return userId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int studentUpdate(SysUser sysUser) {
        Integer userId = sysUser.getId();
        if(userId == null || StringUtils.isEmpty(sysUser.getPhone())){
            throw new BizException("参数校验失败");
        }
        SysUser sysUser1 = sysUserFeignService.queryUserByMobile(sysUser.getPhone());
        if(sysUser1 != null && !userId.equals(sysUser1.getId())){
            throw new BizException("手机号已被占用");
        }
        teacherDao.updateUser(sysUser);
        return userId;
    }

    @Override
    public List<StudentRegisterPerDto> queryStudentPer() {
        Set<Integer> musicUserIds = scheduleStudentPaymentDao.queryMusicStudentPer();
        Set<Integer> vipUserIds = scheduleStudentPaymentDao.queryVipStudentPer();
        musicUserIds.addAll(vipUserIds);
        return studentRegistrationDao.queryStudentPer(musicUserIds);
    }

    @Override
    public List<StudentHasCourseDto> queryHasCourseStudent(String organId) {
//        Set<Integer> musicUserIds = scheduleStudentPaymentDao.queryMusicStudentPer();
//        Set<Integer> vipUserIds = scheduleStudentPaymentDao.queryVipStudentPer();
//        musicUserIds.addAll(vipUserIds);
        //学员编号", "姓名", "分部", "所在乐团", "乐团所属声部", "所在vip课","是否激活","是否预约
//        获取编号，姓名，分部,"是否激活","是否预约
//        List<StudentHasCourseDto> studentHasCourseDtos = studentManageDao.queryCourseStudent(vipUserIds);
        return studentManageDao.queryHasCourseStudent(organId);
    }

    @Override
    public Map<String, Integer> sumStudentAttendance(Integer courseScheduleId) {
        Map<String, Integer> sum = new HashMap<>(5);
        Integer studentNum = scheduleStudentPaymentDao.countStudentNum(courseScheduleId);
        sum.put("studentNum",studentNum);
        Integer signInNum = studentManageDao.countStudentSignInNum(courseScheduleId, "NORMAL");
        sum.put("signInNum",signInNum);
        Integer leaveNum = studentManageDao.countStudentSignInNum(courseScheduleId, "LEAVE");
        sum.put("leaveNum",leaveNum);
        studentNum = studentNum==null?0:studentNum;
        signInNum = signInNum==null?0:signInNum;
        leaveNum = leaveNum==null?0:leaveNum;
        sum.put("truantNum",studentNum-signInNum-leaveNum);
        sum.put("homeworkNum",studentManageDao.countHomeworkNum(courseScheduleId));
        sum.put("repliedNum",studentManageDao.countRepliedNum(courseScheduleId));
        return sum;
    }
}
