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


import com.ssj.bean.weixin.zyb.domain.ZuoybKnowHowDetails;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybKnowHowDetailsDao;
import com.ssj.dao.weixin.zuoyb.dao.ZuoybQueryDao;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.service.kmt.dto.KnowHowDetailsDTO;
import com.ssj.service.kmt.dto.TestPointDTO;
import com.ssj.service.weixin.zuoyb.service.ZuoybKnowHowDetailsService;
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.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/** 
 * @author  ZhangCaibao 
 * @version 2017年9月26日 下午2:02:57 
 * 类说明 
 */
@Service
@Transactional
public class ZuoybKnowHowDetailsServiceImpl extends BaseServiceImpl<ZuoybKnowHowDetails, String> implements ZuoybKnowHowDetailsService{

	@Autowired
	private  ZuoybKnowHowDetailsDao  zuoybKnowHowDetailsDao;
	@Autowired
	private ZuoybQueryDao queryDao;
	
	@Override
	public PagingAndSortingRepository<ZuoybKnowHowDetails, String> getDao() {
		return zuoybKnowHowDetailsDao;
	}

	@Override
	public void deleteByHowId(String howId) {
		zuoybKnowHowDetailsDao.deleteByHowId(howId);
		zuoybKnowHowDetailsDao.deleteSortByHowId(howId);
		zuoybKnowHowDetailsDao.deleteKdByHowId(howId);
		
	}
	@Override
	public void deleteById(String id) {
		zuoybKnowHowDetailsDao.delete(id);
		zuoybKnowHowDetailsDao.deleteSortByHowDetailsId(id);
		zuoybKnowHowDetailsDao.deleteKdByHowDetailsId(id);
		
	}

	@Override
	public Page<ZuoybKnowHowDetails> findByPage(Map<String, Object> params,
			Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<ZuoybKnowHowDetails> specification = DynamicSpecifications.bySearchFilter(filters.values(), ZuoybKnowHowDetails.class);    
		return  zuoybKnowHowDetailsDao.findAll(specification, sp);
	}

	@Override
	public void updateKnowHowDetailNum(String id, Integer num) {
			zuoybKnowHowDetailsDao.updateKnowHowDetailNum(id, num);		
	}

	@Override
	public List<ZuoybKnowHowDetails> getNameByHowId(String howDetialsId) {
		return zuoybKnowHowDetailsDao.getNameByHowId(howDetialsId);
	}

	@Override
	public List<Map<String, Object>> getKonwDetail(Map<String, Object> params) {
		
		return queryDao.getKonwDetail(params);
	}

	@Override
	public List<ZuoybKnowHowDetails> findByHowId(String howId) {
		return zuoybKnowHowDetailsDao.findByHowId(howId);
	}

	@Override
	public List<ZuoybKnowHowDetails> findByHowIdNotNull(String howId,
			String libId,String courseName,String grade,String semester ,String type) {
		return zuoybKnowHowDetailsDao.findByHowIdNotNull(libId, courseName, grade, semester, type, howId);
	}
	@Override
	public List<ZuoybKnowHowDetails> findByHowIdNotNull(String howId,
			String libId,String courseName,String grade,String semester ,String type,int weekNum) {
		return zuoybKnowHowDetailsDao.findByHowIdNotNull(libId, courseName, grade, semester, type, howId,weekNum);
	}

	@Override
	public ZuoybKnowHowDetails findByHowIdAndName(String howId, String name) {
		return zuoybKnowHowDetailsDao.findByHowIdAndName(howId, name);
	}

	@Override
	public List<ZuoybKnowHowDetails> nextMonthHowDtails(String libId,
			String courseName, String grade, String semester, String type,
			int weekNum) {
		
		weekNum = weekNum <=0?1:weekNum;
		//一个月就是两个单元
		int start =weekNum;
		int end =weekNum +2;
		
		List<ZuoybKnowHowDetails> list =new ArrayList<ZuoybKnowHowDetails>();
		List<Object[]> objlist =zuoybKnowHowDetailsDao.nextMonthHowDtails(libId, courseName, grade, semester, type, start,end);
		for (Object[] objects : objlist) {
			ZuoybKnowHowDetails  details =new  ZuoybKnowHowDetails();
			details.setId(objects[0].toString());
			details.setHowId(objects[1]!=null?objects[1].toString():null);
			details.setCreateTime(objects[2]!=null?DateHelper.parseDate(objects[2].toString(), "yyyy-MM-dd HH:mm:ss.SSS"):null);
			details.setLibId(objects[3]!=null?objects[3].toString():null);
			details.setName(objects[4]!=null?objects[4].toString():null);
			details.setBeforeName(objects[5]!=null?objects[5].toString():null);
			details.setAfterName(objects[6]!=null?objects[6].toString():null);
			details.setType(objects[7]!=null?objects[7].toString():null);
			details.setIsHot(objects[8]!=null?Integer.parseInt(objects[8].toString()):null);
			details.setMemorize(objects[9]!=null?Integer.parseInt(objects[9].toString()):null);
			details.setUnderstanding(objects[10]!=null?Integer.parseInt(objects[10].toString()):null);
			details.setApply(objects[11]!=null?Integer.parseInt(objects[11].toString()):null);
			details.setDuration(objects[12]!=null?objects[12].toString():null);
			details.setTwoModule(objects[13]!=null?objects[13].toString():null);
			details.setNum(objects[14]!=null?Integer.parseInt(objects[14].toString()):null);
			details.setModule(objects[15]!=null?objects[15].toString():null);
			list.add(details);
		}
		return list;
		
	}
	@Override
	public List<String> nextMonthZuoybHowDtails(String libId,
			String courseName, String grade, String semester, String type,
			int weekNum) {
		//两周一单元
				weekNum = weekNum==0?1:weekNum;
				weekNum = weekNum%2==0?weekNum/2:weekNum/2+1;
						//一个月就是两个单元
						int start =weekNum;
						int end =weekNum +2;
				
		
		return zuoybKnowHowDetailsDao.nextMonthZuoybHowDtails(libId, courseName, grade, semester, type, start,end);
		
	}


	@Override
	public List<ZuoybKnowHowDetails> findByIds(List<String> ids) {
		if (ids != null && ids.size() > 0){
			return zuoybKnowHowDetailsDao.findByIds(ids);
		} else {
			return null;
		}
	}
	@Override
	public List<String> findKnowHowDetailsByIds(List<String> ids) {
		return zuoybKnowHowDetailsDao.findKnowHowDetailsByIds(ids);
	}

	@Override
	public List<ZuoybKnowHowDetails> findApplyKnowHowDetail(List<String> ids, List<String> names) {
		return zuoybKnowHowDetailsDao.findApplyKnowHowDetails(ids, names);
	}

	@Override
	public List<ZuoybKnowHowDetails> getZybKnowHowById(List<String> ids) {
		return zuoybKnowHowDetailsDao.getZybKnowHowById(ids);
	}

	@Override
	public List<Map<String, Object>> getKonwKdDetail(Map<String, Object> params) {
		return queryDao.getKonwKdDetail(params);
	}
	@Override
	public Page<Map<String, Object>> getZuoybKnowHowDetails(
			Map<String, Object> searchParams, Pageable pageable) {
		return queryDao.getZuoybKnowHowDetails(searchParams, pageable);
	}

	@Override
	public List<ZuoybKnowHowDetails> findCourseKnowDetailList(String courseKnowId) {
		return zuoybKnowHowDetailsDao.findCourseKnowDetailList(courseKnowId);
	}

	@Override
	 public Page<Map<String, Object>> getKonwTBDetail(Map<String, Object> params, Pageable sp){
		return queryDao.getKonwTBDetail(params,sp);
	}

	@Override
	public List<Object[]> findKnowAndYdslByIds(List<String> ids) {
		
		return zuoybKnowHowDetailsDao.findKnowAndYdslByIds(ids);
	}

	@Override
	public List<ZuoybKnowHowDetails> findByNum(int num){
		return zuoybKnowHowDetailsDao.findByNum(num);
	}

	@Override
	public List<ZuoybKnowHowDetails> findlatestHowDetails(String libId,String howId,
			String courseName, String grade, Integer banben, Integer semester) {
		return queryDao.findlatestHowDetails(libId, howId, courseName, grade, banben, semester, null);
	}

	@Override
	public List<ZuoybKnowHowDetails> findlatestHowDetails(String libId,
			String howId, String courseName, String grade, Integer banben,
			Integer semester, String howDetailsName) {
		return queryDao.findlatestHowDetails(libId, howId, courseName, grade, banben, semester, howDetailsName);
	}

	@Override
	public List<String> getHowDetailsIds(String libId, String courseName,
			String grade, String semester, String type, int start, int end) {
		return zuoybKnowHowDetailsDao.getHowDetailsIds(libId, courseName, grade, semester, type, start, end);
	}

	@Override
	public List<Object[]> getHowDetailsIds(String libId, String courseName,
			String grade, String semester, String type, int start, int end,
			Integer pageSize, String module) {
		List<Object[]> list=zuoybKnowHowDetailsDao.getHowDetailsIds(libId, courseName, grade, semester, type, start, end,module);
		int listcount =list.size();
		 if(listcount==0){
			 return null;
		 }
		 if(list.size()<=pageSize){
			 return list;
		 }
		List<Object[]> newlist =new ArrayList<Object[]>();
		
		List<Integer> intlist =new ArrayList<Integer>();
		 int i=1;
		  while( i <= pageSize ) {
//			  int count = SolrQueryUtils.getRandom(listcount);
//				if(!intlist.contains(count)){
//					intlist.add(count);
//					i++;
//					newlist.add(list.get(count));
//				}
	      }
		return newlist;
	}

	@Override
	public List<Object[]> getMathHowDetailsIds(String libId, String courseName,
			String grade, String semester, String type, String howName) {
		return zuoybKnowHowDetailsDao.getMathHowDetailsIds(libId, courseName, grade, semester, type, howName);
	}

	@Override
    public List<KnowHowDetailsDTO> findTestPoint(List<String> detailIdList, int versionType, String grade, String subject, int semester) {
		if (StringUtils.isNotBlank(grade)){
			return queryDao.findDetailsListWithKd(detailIdList, versionType, grade, subject, semester);
		} else {
			return queryDao.findDetailsListWithKd(detailIdList, versionType);
		}
    }

	@Override
	public List<TestPointDTO> findTestPoint(String detailId, Integer versionType, String grade, int semester) {
		return queryDao.findPointsByDetailId(detailId, versionType, grade, semester);
	}
}
