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.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 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(Long.valueOf(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 StudentManageBaseDto findStudentManageBaseInfo(Long userID) {
        StudentManageBaseDto studentManageBaseDto = studentManageDao.findStudentBaseInfoByUserID(userID);
        for(int i=0;i<studentManageBaseDto.getMusicGroups().size();i++){
            String[] goodsIdList=studentManageBaseDto.getMusicGroups().get(i).getGoodsIdList().split("\\|");
            studentManageBaseDto.getMusicGroups().get(i).setGoodsNameList(studentManageDao.findGoodsNamesByIds(goodsIdList));
        }
        return studentManageBaseDto;
    }

    @Override
    public List<StudentManageBaseInfoOfMusicGroupDto> findStudentMusicGroupsByUserId(Long userID) {
        return studentManageDao.findStudentMusicGroupsByUserId(userID);
    }

    @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(Long userID) {
        return vipGroupDao.findStudentVipGroups(userID);
    }

    @Override
    public StudentManageAccountBaseInfoDto getStudentAccountBaseInfo(Long userID) {
        return studentManageDao.getStudentAccountBaseInfo(userID);
    }
}
