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

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.curator.TbLibBindAudit;
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.ILibJoinService;
import com.ssj.service.weixin.library.service.ILibVipService;
import com.ssj.service.weixin.sales.service.LibVipServiceService;
import com.ssj.service.weixin.user.service.UserService;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@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;

	@Autowired
	private UserService userService;

	@Autowired
	private ILibJoinService joinService;

	@Autowired
	private LibVipServiceService vipServiceService;
	
	
	
	@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 Page<Map<String, Object>> findInvalidStudentByPage(Map<String, Object> params, Pageable initPage) {
		return queryDao.findInvalidStudentByPage(params,initPage);
	}

	@Override
	public Response delStudent(String libId, String vipId) {
		//先检查数据是否可以删除
		LibVip vip = vipService.getById(vipId);
		Response response = new Response();
//		check(vip,response);
//		if(!response.getMeta().isSuccess()){
//			return  response;
//		}
		//查询vipId 是否有S003,'C024','C025'外的服务
        int count = dao.findServiceNotAny(vipId,libId);
		if(count==0){
			//删除user表数据
			if(StringUtils.isNotBlank(vip.getChildUserId())){
				userService.delete(vip.getChildUserId());
			}
			//删除vip表数据 ?
			vipService.delete(vipId);
		}
		//删除service表数据S003,'C024','C025'
		dao.delVipServiceData(vipId,libId);
		//删除student表数据
		dao.delStudentData(vipId,libId);
		return response.success();
	}

	@Override
	public Response addChildBack(String libId, String vipId)  throws Exception{
		LibVip libVip = vipService.getById(vipId);
		Response response = new Response();
		if (StringUtil.isBlank(libVip.getUserId())) {
			return response.failure("此学⽣已被删除，执行失败");
		}
		check(libVip,response);
		TbLibJoin libJoin =joinService.getById(libId);
		//已经升级新服务了
		if (libJoin.getSignType() != null && !Objects.equals(libJoin.getSignType(), 0)) {
			int studentNum = vipService.countByLibId(libId);
			if (studentNum > libJoin.getLibService()) {
				return response.failure("校区⼈数达到上限，重启失败");
			}
			//添加校区批改服务
			vipServiceService.insertLibServiceV1(vipId, libId);
		}else {
			//添加校区批改服务
			vipServiceService.insertLibCorrectService(vipId, libId);
		}
		libVip.setLibId(libId);
		vipService.save(libVip);
		//还原原来所在班级
		//最近所在的班级
		String classId = dao.findRecentOwnedClassId(vipId, libId);
		if (StringUtil.isNotBlank(classId)) {
			//更新为有效
			dao.updateStatusByVipIdAndLibId(vipId, libId, classId);
		}
		return response;
	}

	private Response check(LibVip libVip,Response response){
		if (StringUtil.isNotBlank(libVip.getLibId())) {
			return response.failure("此学⽣已绑定其他校区，执行失败");
		}

		int count = dao.findCountByVipIdAndStatus(libVip.getId(), 1);
		if (count >0) {
			return response.failure("此学⽣已在校区绑定审核中，执行失败");
		}
		response.success();
		return response;
	}

}
