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


import com.ssj.bean.kmt.kmt.domain.KmtExampaperDetail;
import com.ssj.bean.kmt.kmtsys.model.TreeDataModel;
import com.ssj.dao.kmt.kmt.dao.KmtExampaperDetailDao;
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.service.KmtExampaperDetailService;
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;


@Transactional
@Service
public class KmtExampaperDetailServiceImpl extends BaseServiceImpl<KmtExampaperDetail, String>
		implements KmtExampaperDetailService {

	@Autowired
	private KmtExampaperDetailDao dao;
	
	@Override
	public PagingAndSortingRepository<KmtExampaperDetail, String> getDao() {
		return dao;
	}

	@Override
	public List<KmtExampaperDetail> findKmtExampaperDetails(String areaId) {
		return dao.findKmtExampaperDetails(areaId);
	}

	@Override
	public List<KmtExampaperDetail> getPidAndCodes(String pid,String areaId) {
		return dao.getPidAndCodes(pid,areaId);
	}

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

	@Override
	public Integer updateSortNo(String id, int sortNo) {
		return dao.updateSortNo(id, sortNo);
	}

	@Override
	public void changeSubCount(String id, boolean increase) {
		KmtExampaperDetail org = this.getById(id);
		if(org!=null){
			 int subCount = org.getSubCount();
			 if(increase){
				 subCount ++;
			 }else{
				 subCount --;
			 }
			 org.setSubCount(subCount);
			 this.save(org);
		}
		
	}

	@Override
	public List<KmtExampaperDetail> findKmtExampaperDetailsByQuId(String areaId, String quId) {
		return dao.findKmtExampaperDetailsByQuId(areaId,quId);
	}

	@Override
	public List<TreeDataModel> findTreeDataModels(String areaId) {
		List<TreeDataModel> treeDataModels=new ArrayList<TreeDataModel>();
		TreeDataModel treeDataModel=null;
		List<KmtExampaperDetail> kmtExampaperDetails= dao.findKmtExampaperDetails(areaId);
		if(kmtExampaperDetails!=null){
			for (KmtExampaperDetail kmtExampaperDetail : kmtExampaperDetails) {
				if(StringUtils.isEmpty(kmtExampaperDetail.getpId()) 
						|| "-1".equals(kmtExampaperDetail.getpId())){//组装出第一级
					treeDataModel=new TreeDataModel();
					treeDataModel.setAreaId(kmtExampaperDetail.getAreaId());
					treeDataModel.setId(kmtExampaperDetail.getId());
					treeDataModel.setName(kmtExampaperDetail.getName());
					treeDataModel.setCode(kmtExampaperDetail.getCode());
					treeDataModel.setOpen(true);
					treeDataModel.setQuestionId(kmtExampaperDetail.getQuestionId());
					treeDataModel.setKmtQuestion(kmtExampaperDetail.getKmtQuestion());
					treeDataModel.setChildren(new ArrayList<TreeDataModel>());
					treeDataModels.add(treeDataModel);
				}
			}
			for (KmtExampaperDetail kmtExampaperDetail : kmtExampaperDetails) {
				if(StringUtils.isNotEmpty(kmtExampaperDetail.getpId())){
					runTree(treeDataModels,kmtExampaperDetail);
				}
			}
		}
		return treeDataModels;
	}

	
	private void  runTree(List<TreeDataModel> treeDataModels,KmtExampaperDetail kmtExampaperDetail){
		TreeDataModel model=null;
		for (TreeDataModel treeDataModel : treeDataModels) {
			if(treeDataModel.getId().equals(kmtExampaperDetail.getpId())){
				model=new TreeDataModel();
				model.setAreaId(kmtExampaperDetail.getAreaId());
				model.setId(kmtExampaperDetail.getId());
				model.setName(kmtExampaperDetail.getName());
				model.setCode(kmtExampaperDetail.getCode());
				model.setOpen(true);
				model.setQuestionId(kmtExampaperDetail.getQuestionId());
				model.setKmtQuestion(kmtExampaperDetail.getKmtQuestion());
				model.setChildren(new ArrayList<TreeDataModel>());
				treeDataModel.getChildren().add(model);
			}else{
				runTree(treeDataModel.getChildren(),kmtExampaperDetail);
			}
		}
	}
	
	
	
	
	
}
