package com.ssj.service.sys.imlibuser.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.imlibuser.TbClassStudent;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibJoin;
import com.ssj.dao.sys.imlibuser.dao.TbClassQueryDao;
import com.ssj.dao.sys.imlibuser.dao.TbClassStudentDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.conch.conch.service.ConchDerverService;
import com.ssj.service.sys.imlibuser.service.TbClassStudentService;
import com.ssj.service.weixin.library.service.ILibVipService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(version = "1.0")
@Transactional
public class TbClassStudentServiceImpl extends BaseServiceImpl<TbClassStudent, String> implements TbClassStudentService {

	@Autowired
	private TbClassStudentDao dao;
	
	@Autowired
	private TbClassQueryDao queryDao;
	
	@Autowired
	private ConchDerverService conchDerverService;
	
	@Autowired
	private ILibVipService vipService;
	
	
	
	@Override
	public PagingAndSortingRepository<TbClassStudent, String> getDao() {
		return dao;
	}

	
	@Override
	public Page<Map<String, Object>> findLibImByPage(
			Map<String, Object> searchParam, Pageable pageable) {
		return queryDao.findLibImByPage(searchParam, pageable);
	}

	@Override
	public Page<Map<String, Object>> findStudentByPage(
			Map<String, Object> searchParam, Pageable pageable) {
		return queryDao.findStudentByPage(searchParam, pageable);
	}


	@Override
	public TbClassStudent getTbClassTeacherByVipId(String classId, String vipId) {
		return dao.getTbClassTeacherByVipId(classId, vipId);
	}


	@Override
	public void untyingTbClassTeacherByVipId(String vipId) {
		LibVip libVip=vipService.getById(vipId);
		if(libVip!=null && StringUtils.isNotEmpty(libVip.getDerver())){
			conchDerverService.untyingParents(libVip.getDerver());
		}
	}


	@Override
	public TbLibJoin getTbLibJoinByLeagueName(String leagueName) {
		return queryDao.getTbLibJoinByLeagueName(leagueName);
	}

	@Override
	public List<TbClassStudent> findByVipId(String vipId) {
		return dao.findByVipId(vipId);
	}

	@Override
	public int getTbClassStudentByClassId(String calssId,String libId) {
		return dao.getTbClassStudentByClassId(calssId, libId);
	}

	@Override
	public void updateTbClassTeacherByClassIdAndVipIdsAndLibId(String classId,List<String>vipIds,String libId){
		dao.updateTbClassStudentByVipIdsAndClassIdAndlibId(classId,vipIds,libId);
	}

	@Override
	public TbClassStudent findByLibIdAndVipId(String libId, String vipId) {
		return dao.findTopByLibIdAndVipIdAndStatus(libId, vipId, 1);
	}

	@Override
	public List<TbClassStudent> findByVipIdAndStatus(String vipId, int status) {
		return dao.findByVipIdAndStatus(vipId, status);
	}

	@Override
	public void deleteByLibIdAndClassId(String libId, String classId) {
		 dao.deleteByLibIdAndClassId(libId, classId);
	}


	@Override
	public void updateByVipIdAndLibId(String vipId, String libId) {
		dao.updateByVipIdAndLibId(vipId, libId);
	}

    @Override
    public void updateTbClassStudentByClassIdAndVipIdAndLibId(String classId, String vipId, String libId) {
        dao.updateTbClassStudentByVipIdAndClassIdAndLibId(classId, vipId, libId);
    }

    @Override
    public TbClassStudent findByVipIdAndLibId(String vipId, String libId) {
        return dao.findByVipIdAndLibId(vipId, libId);
    }

    @Override
    public void rebackStudentToClass(String vipId, String libId) {
	    //最近所在的班级
        String classId = dao.findRecentOwnedClassId(vipId, libId);
        if (StringUtil.isNotBlank(classId)) {
            //更新为有效
            dao.updateStatusByVipIdAndLibId(vipId, libId, classId);
        }
    }


}
