package com.ssj.framework.basic.config.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
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 com.ssj.framework.basic.config.bean.Config;
import com.ssj.framework.basic.config.dao.ConfigDao;
import com.ssj.framework.basic.config.service.ConfigService;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;


/**
 * 
 * 类名称：ConfigServiceImpl<br>
 * 类描述：系统参数服务接口实现<br>
 * @version v1.0
 *
 */
@Service("configService")
public class ConfigServiceImpl extends BaseServiceImpl<Config, String> implements ConfigService {

    @Autowired
    public ConfigDao configDao;

    @Override
    public PagingAndSortingRepository<Config, String> getDao() {
        return configDao;
    }

    private static Map<String, Object> configMap = null;

    /**
     * 初始化系统参数数据
     */
    private void initConfigMap() {
        configMap = new HashMap<String, Object>();
        List<Config> list = configDao.findAll();
        if (list != null && list.size() > 0) {
            Config config = null;
            for (int i = 0; i < list.size(); i++) {
                config = list.get(i);
                configMap.put(config.getParamCode(), config.getParamVal());
            }
        }
    }

    /*
     * (non-Javadoc)
     */
    public void clearConfigCache()  {
        if (configMap != null && !configMap.isEmpty()) {
            configMap.clear();
            configMap = null;
        }
        initConfigMap();
    }

    /*
     * (non-Javadoc)
     */
    public String getParamValByCode(String paramCode)  {
        return getParamValByCode(paramCode, null);
    }

    /*
     * (non-Javadoc)
     */
    public String getParamValByCode(String paramCode, String defVal) {
        if (configMap == null) {
            initConfigMap();
        }
        String paramVal = (String) configMap.get(paramCode);
        if (paramVal == null) {
            return defVal;
        } else {
            return paramVal;
        }
    }

	@Override
	@Cacheable(value = "key", keyGenerator = "keyGenerator_keyCode_ConfigSemester")
	public String findByParamCode(String paramCode) {
		Config config=configDao.findByParamCode(paramCode);
		return config.getParamVal();
	}

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

	@Override
	public Config findConfigByParamCode(String paramCode) {
		return configDao.findByParamCode(paramCode);
	}
}
