package com.ssj.service.sys.sort.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.sort.domain.Sort;
import com.ssj.dao.sys.sort.dao.SortDao;
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.framework.core.util.RedisUtil;
import com.ssj.service.sys.sort.service.SortService;
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 java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(version = "1.0")
public class SortServiceImpl extends BaseServiceImpl<Sort, String> implements SortService   {
    private static final int MAX_CALLCOUNT = 5;
	
    @Autowired
    private SortDao sortDao;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public PagingAndSortingRepository<Sort, String> getDao() {
        return sortDao;
    }
	  /**
     * 初始化系统参数数据
     */
	@SuppressWarnings("static-access")
	private void initConfigMap() {
    	List<Sort> list = this.findByPid("0");
    	if(list != null && list.size() > 0){
    		for (Sort sort : list) {
                redisUtil.del(sort.getCode().trim().getBytes());
				redisUtil.set(sort.getCode().trim().getBytes(), redisUtil.serializeList(this.getChildrenAndIshotList(sort.getId())));
			}
    	}
    }
	@Override
	public void clearConfigCache() {
		//redisUtil.flushDB(indexdb);
	    	initConfigMap();
	}

	@Override
	public List<Sort> getParamValByCode(String paramCode) {
		byte[] bytes=redisUtil.get(paramCode.trim().getBytes());
    	if(bytes == null || bytes.length<=0){
    		initConfigMap();
    	}
    	List<Sort> paramVal = (List<Sort>) redisUtil.unserializeList(redisUtil.get(paramCode.trim().getBytes()));
    	if(paramVal == null){
    		return new ArrayList<Sort>();
    	}else{
    		return paramVal;
    	}
    	
    }
	@Override
	public List<String> getParamValByCodeList(String paramCode){
		List<Sort> paramVal =getParamValByCode(paramCode);
		List<String> list =new ArrayList<String>();
		for (Sort sort : paramVal) {
			list.add(sort.getCode());
		}
		return  list;
	}
	
	@Override
	public List<Sort> findByPid(String pid) {
		return sortDao.findByPidOrderBySortAsc(pid);
	}
	@Override
	public List<Sort> findByPidAndIshot(String pid, Integer ishot) {
		return sortDao.findByPidAndIshotOrderBySortAsc(pid, ishot);
	}
	@Override
	public Sort findByCode(String code) {
		return sortDao.findByCodeAndPid(code, "0").get(0);
	}
	@Override
	public Page<Sort> findByPage(Map<String, Object> params, Pageable sp)
			throws Exception {
		 Map<String, SearchFilter> filters = SearchFilter.parse(params);
	     Specification<Sort> specification = DynamicSpecifications.bySearchFilter(filters.values(), Sort.class);
	     return sortDao.findAll(specification, sp);
	}
	@Override
	public Sort getSortChildren(String code) {
	  Sort sort = this.findByCode(code);
	  if(sort!=null){
		 sort.setChildren(this.getParamValByCode(code));
	  }
		return sort;
	}
	private List<Sort> getChildrenAndIshotList(String pid){
		List<Sort> sortList=this.findByPidAndIshot(pid,1);
		  if(sortList!=null&&sortList.size()>0){
			  for (Sort so : sortList) {
				  classList(so, so.getId(),0);
			  }
		  }
		  return sortList;
	}
	 //递归调用
    private void classList(Sort sort, String pid,int callCount){
    	 callCount ++;
    	 //判断递归深度
    	 if(callCount > MAX_CALLCOUNT){
             return;
         }
        List<Sort> typeList=this.findByPidAndIshot(pid,1);
        sort.setChildren(typeList);
        for(Sort cla: typeList){
        	classList(cla, cla.getId(),callCount);
        }
    }

	@Override
	public String getNameByCode(String code, String paramCode) {
		List<Sort> list  =this.getParamValByCode(code);
		String name="";
		if(paramCode !=null ){
			String[] array = paramCode.split(";");
			for(int i=0;i<array.length;i++){
				for (Sort sort : list) {
					if(sort.getCode().equals(array[i])){
						name +=sort.getName()+";";
					}
				}
			}
		}
		if(name !="" ){
			name =name.substring(0,name.lastIndexOf(';'));
		}
		return name;
	}
	@Override
	public String getBeforeNameByCode(String code, String paramCode) {
		List<Sort> list  =this.getParamValByCode(code);
		StringBuffer  name =  new StringBuffer();
		if(paramCode !=null ){
			String[] array = paramCode.split(";");
			for(int i=0;i<array.length;i++){
				for (Sort sort : list) {
					if(sort.getCode().equals(array[i])){
						name.append("<span class=\"tags_b\"> ").append(sort.getName()).append("</span>");
					}
				}
			}
		}
		return name.toString();
	}
	@Override
	public String getSpanNameByCode(String code, String paramCode) {
		List<Sort> list  =this.getParamValByCode(code);
		StringBuffer  name =  new StringBuffer();
		if(paramCode !=null ){
			String[] array = paramCode.split(";");
			for(int i=0;i<array.length;i++){
				for (Sort sort : list) {
					if(sort.getCode().equals(array[i])){
						name.append("<span>").append(sort.getName()).append("</span>");
					}
				}
			}
		}
		return name.toString();
	}

	@Override
	public Sort findByIdAndChildren(String id) {
		Sort sort = this.getById(id);
		if(sort!=null){
			List<Sort> list  =this.getChildrenList(id);
			sort.setChildren(list);
		}
		return sort;
	}

	private List<Sort> getChildrenList(String pid){
		List<Sort> sortList=sortDao.findByPidOrderBySortAsc(pid);
		  if(sortList!=null&&sortList.size()>0){
			  for (Sort so : sortList) {
				  classSortList(so, so.getId(),0);
			  }
		  }
		  return sortList;
	}
	 //递归调用
    private void classSortList(Sort sort, String pid,int callCount){
    	 callCount ++;
    	 //判断递归深度
    	 if(callCount > MAX_CALLCOUNT){
             return;
         }
        List<Sort> typeList=sortDao.findByPidOrderBySortAsc(pid);
        sort.setChildren(typeList);
        for(Sort cla: typeList){
        	classSortList(cla, cla.getId(),callCount);
        }
    }
    
	@Override
	public void updateSequenceSort(String id, int sort) {
		sortDao.updateSequenceSort(id, sort);
	}

	@Override
	public Integer getSortChildrenCount(String pid) {
		return sortDao.getSortChildrenCount(pid);
	}

	@Override
	public Sort getSort(String code) {
		return sortDao.getSort(code);
	}


}
