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

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.domain.KmtQuestionKnow;
import com.ssj.dao.kmt.kmt.dao.KmtQueryDao;
import com.ssj.service.kmt.service.KmtQuestionKnowService;
import com.ssj.service.kmt.service.SysQueryService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Transactional
@Service(version ="1.0")
public class SysQueryServiceImpl  implements SysQueryService {

	private   Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private KmtQueryDao queryDao;

	@Autowired
	private KmtQuestionKnowService kmtQuestionKnowService;

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Map<String, Object>> findUserByNameList(String name) {
		return queryDao.findUserByNameList(name);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<Map<String, Object>> findParentQuestionByHowId(
			String howDetailsId, String howDetailsKdId) {
		return queryDao.findParentQuestionByHowId(howDetailsId, howDetailsKdId);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public Integer countQuestionByParentId(String parentId) {
		return queryDao.countQuestionByParentId(parentId);
	}

	
	@Override
	public Integer addRelation(String oldHowId, String oldHowDetailsId,String oldHowDetailsKdId,
			String newHowId, String newHowDetailsId,String newHowDetailsKdId) {
		Integer num=0;
		try {
			Date createTime=new Date();
			List<KmtQuestionKnow> kmtQuestionKnows=kmtQuestionKnowService.findHowDetailsKdId(oldHowDetailsKdId);
			List<KmtQuestionKnow> newKmtQuestionKnows=new ArrayList<KmtQuestionKnow>();
			for (KmtQuestionKnow kmtQuestionKnow : kmtQuestionKnows) {
				num=num+1;
				KmtQuestionKnow newKmtQuestionKnow=new KmtQuestionKnow();
				newKmtQuestionKnow.setCourseName(kmtQuestionKnow.getCourseName());
				newKmtQuestionKnow.setHowId(newHowId);
				newKmtQuestionKnow.setHowDetailsId(newHowDetailsId);
				newKmtQuestionKnow.setHowDetailsKdId(newHowDetailsKdId);
				newKmtQuestionKnow.setIsLoad(kmtQuestionKnow.getIsLoad());
				newKmtQuestionKnow.setNum(kmtQuestionKnow.getNum());
				newKmtQuestionKnow.setQuestionId(kmtQuestionKnow.getQuestionId());
				newKmtQuestionKnow.setTags(kmtQuestionKnow.getTags());
				newKmtQuestionKnow.setCreateTime(createTime);
				newKmtQuestionKnow.setRemarks("来自复制考点ID:"+oldHowDetailsKdId);
				newKmtQuestionKnows.add(newKmtQuestionKnow);
			}
		kmtQuestionKnowService.save(newKmtQuestionKnows);
		} catch (Exception e) {
			//num=0;
			e.printStackTrace();
			throw new RuntimeException();
		}
		return num;
	}

	@Override
	public Integer deleteRelation(String howDetailsKdId, String questionId) {
		Integer num=0;
		try {
			List<KmtQuestionKnow> kmtQuestionKnows=kmtQuestionKnowService.findHowDetailsKdIdAndQuestionId(howDetailsKdId, questionId);
			for (KmtQuestionKnow kmtQuestionKnow : kmtQuestionKnows) {
				kmtQuestionKnowService.delete(kmtQuestionKnow);
				num=num+1;
			}
		} catch (Exception e) {
//			num=0;
			e.printStackTrace();
			throw new RuntimeException();
		}
		return num;
	}

	
}
