package com.ssj.service.weixin.zuoyb.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.weixin.zyb.domain.ZuoybStudentWorkDetails;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybStudentWorkDetailsDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.weixin.zuoyb.service.ZuoybStudentWorkDetailsService;


/** 
 * @author  ZhangCaibao 
 * @version 2017年7月19日 下午4:39:22 
 * 类说明 
 */
@Service(version="1.0")
@Transactional
public class ZuoybStudentWorkDetailsServiceImpl extends BaseServiceImpl<ZuoybStudentWorkDetails, String> implements ZuoybStudentWorkDetailsService{

	@Autowired
	private  ZuoybStudentWorkDetailsDao  zuoybStudentWorkDetailsDao;
	
	@Override
	public PagingAndSortingRepository<ZuoybStudentWorkDetails, String> getDao() {
		return zuoybStudentWorkDetailsDao;
	}

	@Override
	public List<ZuoybStudentWorkDetails> getRecentByLibIdAndVipId(String libId,
			String vipId) {
		return zuoybStudentWorkDetailsDao.getRecentByLibIdAndVipId(libId, vipId);
	}

	@Override
	public void saveWorkDetails(List<ZuoybStudentWorkDetails> workDetailsList,String workId,String libId) {
		 for (ZuoybStudentWorkDetails zuoybStudentWorkDetails : workDetailsList) {
			  if(zuoybStudentWorkDetails.getParentName()==null || zuoybStudentWorkDetails.getChildName() ==null){
				  continue;
			  }
			  zuoybStudentWorkDetails.setStudentWorkId(workId);
			  zuoybStudentWorkDetails.setScoringTime(new Date());
			  zuoybStudentWorkDetails.setLibId(libId);
			  this.save(zuoybStudentWorkDetails);
		 }
		
	}

	@Override
	public void deleteWorkDetalByStudentWorkId(String studentWorkId) {
		zuoybStudentWorkDetailsDao.deleteWorkDetalByStudentWorkId(studentWorkId);
	}

	@Override
	public List<String> getUnfinishList(String workId) {
		ZuoybStudentWorkDetails details = zuoybStudentWorkDetailsDao.findByStudentWorkIdAndParentName(workId, "其中未完成");
		List<String> result = new ArrayList<String>();
		if (details != null && StringUtil.isNotEmpty(details.getChildName())){
			String[] items = details.getChildName().split(",");
			if (items != null && items.length > 0){
				for (String item : items){
					result.add(item);
				}
			}
		}
		return result;
	}

	@Override
	public ZuoybStudentWorkDetails getFinishSituation(String workId) {
		return this.getDetailByNum(workId, 1);//作业完成情况
	}

	@Override
	public ZuoybStudentWorkDetails getDetailByNum(String workId, int num) {
		return zuoybStudentWorkDetailsDao.findByStudentWorkIdAndNum(workId, num);
	}

	@Override
	public List<ZuoybStudentWorkDetails> getConcernPoint(String workId) {
		return zuoybStudentWorkDetailsDao.findByStudentWorkIdAndNumBetweenAndChildStat(workId, 101, 104, "3");
	}

	@Override
	public List<ZuoybStudentWorkDetails> getLearningStatus(String workId) {
		return zuoybStudentWorkDetailsDao.findByStudentWorkIdAndNumBetween(workId, 101, 104);
	}
}
