package com.ssj.service.kmt.kmt.service.impl;

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

import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.domain.KmtQuestionKnow;
import com.ssj.bean.kmt.kmt.model.NumModel;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.dao.kmt.kmt.dao.KmtQueryDao;
import com.ssj.dao.kmt.kmt.dao.KmtQuestionKnowDao;
import com.ssj.service.kmt.service.KmtQuestionKnowService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;


@Transactional
@Service(version ="1.0")
public class KmtQuestionKnowServiceImpl extends BaseServiceImpl<KmtQuestionKnow, String> implements KmtQuestionKnowService {

	private   Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private KmtQuestionKnowDao dao;
	
	@Autowired
	private KmtQueryDao queryDao;
	
	
	@Override
	public PagingAndSortingRepository<KmtQuestionKnow, String> getDao() {
		return dao;
	}


	@Override
	public List<KmtQuestionKnow> findKBByList(String questionId,
			String howDetailsId) {
		return dao.findQuestionByTitle(questionId, howDetailsId);
	}


	@Override
	public List<KmtQuestionKnow> findQuByList(String questionId) {
		return dao.findQuByList(questionId);
	}


	@Override
	public KmtQuestionKnow findQuByHowKdList(String howDetailsId,
			String questionId, String howDetailsKdId) {
		KmtQuestionKnow kmtQuestionKnow=null;
		if(StringUtils.isNotEmpty(howDetailsKdId)){
			kmtQuestionKnow=dao.findQuByHowKdList(howDetailsId, questionId,howDetailsKdId);
		}else{
			kmtQuestionKnow=dao.findQuByHowKdList(howDetailsId, questionId);
		}
		return kmtQuestionKnow;
	}

	
	@Override
	@Transactional
	public void updateFULLNotIsLoadByIds() {
		List<String> ids=new ArrayList<String>();
		
		//1:全量更新数学的是否加载到sorl中字段
		dao.updateFULLIsLoad();	
		
		//2:算出要更新的id
		List<Map<String,Object>> mathNumMaps=queryDao.findMathByNum();
		
		for (Map<String, Object> map : mathNumMaps) {//处理一条数据
			String tid="";//ids
			try {
				tid=map.get("tid").toString();//ids
				String hnum=map.get("hnum").toString();//单元的num			
				String dnum=map.get("dnum").toString();//知识的num			
				String kdnum=map.get("kdnum").toString();//考点的num
				
				String[] tids=tid.split(",");
				String[] hnums=hnum.split(",");
				String[] dnums=dnum.split(",");
				String[] kdnums=kdnum.split(",");
				List<NumModel> numModels=new ArrayList<NumModel>();
				for (int i = 0; i < tids.length; i++) {
					NumModel numModel=new NumModel();
					numModel.setId(tids[i]);
					numModel.setNum(hnums[i]);
					numModel.concat(dnums, i);
					numModel.concat(kdnums, i);
					numModels.add(numModel);
				}
				Collections.sort(numModels);
				for (int i = 0; i < numModels.size()-1; i++) {
					ids.add(numModels.get(i).getId());
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.info("算出要更新的id异常..."+tid);
			}
		}
		dao.updateFULLNotIsLoadByIds(0, ids);
	}


	@Override
	@Transactional
	public void loadFULLTOSolr() {
		
		updateFULLNotIsLoadByIds();//先更新数学的超纲数据
		
		//再调接口更新整个solr数据
		
		

	}


	@Override
	public List<KmtQuestionKnow> findHowDetailsKdId(String howDetailsKdId) {
		return dao.findHowDetailsKdId(howDetailsKdId);
	}


	@Override
	public List<KmtQuestionKnow> findHowDetailsKdIdAndQuestionId(String howDetailsKdId, String questionId) {
		return dao.findHowDetailsKdIdAndQuestionId(howDetailsKdId, questionId);
	}
	

}
