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.StudentManageDao;
import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
import com.ym.mec.biz.dal.dao.VipGroupDao;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.page.StudentManageAttendanceQueryInfo;
import com.ym.mec.biz.dal.page.StudentManageCourseQueryInfo;
import com.ym.mec.biz.dal.page.StudentManageQueryInfo;
import com.ym.mec.biz.service.StudentManageService;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private StudentManageDao studentManageDao;
    @Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private VipGroupDao vipGroupDao;

    @Override
    public PageInfo findStudentsByOrganId(StudentManageQueryInfo queryInfo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        PageInfo<StudentManageListDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        queryInfo.setOrganId(user.getOrganId());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List<StudentManageListDto> dataList = null;
        int count = studentManageDao.countStudentByOrganId(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = studentManageDao.findStudentsByOrganId(params);
        }
        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 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 List<StudentManageVipGroupDto> findStudentVipGroups(Integer userId) {
        return vipGroupDao.findStudentVipGroups(userId);
    }

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