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

import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dto.StudentApplyDetailDto;
import com.ym.mec.biz.dal.dto.StudentFeeDto;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.StudentRegistrationQueryInfo;
import com.ym.mec.biz.service.StudentRegistrationService;
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.collection.MapUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, StudentRegistration> implements StudentRegistrationService {

    @Autowired
    private StudentRegistrationDao studentRegistrationDao;

    @Override
    public BaseDAO<Long, StudentRegistration> getDAO() {
        return studentRegistrationDao;
    }

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

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

    @Override
    public boolean batchUpdateSubject(String userIds, Integer subId) {
        String[] userId = userIds.split(",");
        StudentRegistration studentRegistration;
        Date date = new Date();
        for (String str : userId) {
            studentRegistration = studentRegistrationDao.get(Long.parseLong(str));
            if (studentRegistration.getPaymentStatus() == YesOrNoEnum.NO) {
                studentRegistration.setUpdateTime(date);
                studentRegistration.setSubjectId(subId);
                studentRegistrationDao.update(studentRegistration);
            }
        }
        return true;
    }

    @Override
    public StudentFeeDto queryFeeDetail(Integer studentId, String musicGroupId) {
//		return studentRegistrationDao.queryFeeDetail(studentId,musicGroupId);
        return null;
    }

    @Override
    public List<StudentRegistration> getNoClassStuBySubjectId(Integer musicGroupId, Integer actualSubjectId) {
        return studentRegistrationDao.getNoClassStuBySubjectId(musicGroupId, actualSubjectId);
    }

    @Override
    public Integer getNoClassStuCountBySubjectId(Integer musicGroupId, Integer actualSubjectId) {
        return studentRegistrationDao.getNoClassStuCountBySubjectId(musicGroupId,actualSubjectId);
    }

    @Override
    public Integer updateByUserIdAndMusicGroupId(StudentRegistration studentRegistration) {
        return studentRegistrationDao.updateByUserIdAndMusicGroupId(studentRegistration);
    }
}