package com.xt.hb.gis.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xt.hb.gis.common.GlobalData;
import com.xt.hb.gis.service.SystemService;
import com.xt.hb.gis.system.mappers.SysParamMapper;
import com.xt.hb.gis.system.model.SysParam;
import com.yuanxd.tools.utils.Reflections;
import com.yuanxd.tools.utils.string.StringUtils;

@Service
public class SystemServiceImpl implements SystemService {
	private final Logger log = LoggerFactory.getLogger(SystemService.class);
	@Autowired
	private SysParamMapper paramMapper;

	@Override
	public String reloadParam() {
		List<SysParam> params = paramMapper.selectBySystemOrNull(GlobalData.CONST_SYSTEM_NAME);
		if (null == params || params.size() == 0) {
			return "没有获取到属性";
		}
		for (SysParam sp : params) {
			Class<?> clazz;
			try {
				if (StringUtils.hasText(sp.getClazz())) {
					clazz = Class.forName(sp.getClazz());
				} else {
					clazz = GlobalData.class;
				}
				Reflections.setFieldValueSilence(clazz, sp.getKey(), sp.getValue());
			} catch (ClassNotFoundException e) {
				log.error("系统参数加载失败（ID：{},key:{}）", sp.getId(), sp.getKey());
			}
		}
		return "系统参数加载成功";
	}

}
