package cn.hmsoft.frame.service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hmsoft.application.AppHelper;
import cn.hmsoft.frame.constants.FrameValueType;
import cn.hmsoft.frame.data.dao.FrameParamDao;
import cn.hmsoft.frame.data.model.FrameParam;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.BeanHelper;
import cn.hmsoft.helper.LocalDateHelper;
import cn.hmsoft.helper.StringHelper;

/*****************************
 * 系统参数相关事务
 * 
 * @author zxq
 * @create 2018-04-09 21:04:27
 * @version 5.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
@Service
public class FrameParamService extends FrameService {

	@Autowired
	private FrameParamDao daoParam;

	/**********************
	 * 加载系统参数
	 */
	public Map<String, String> loadParam(Class<?> clazz) {
		Map<String, String> paramMap = new HashMap<String, String>();
		for (FrameParam t : this.daoParam.listParam(AppHelper.AppName)) {
			if (StringHelper.isNotEmpty(t.getParam_value()))
				paramMap.put(t.getParam_name().trim(), t.getParam_value().trim());
		}
		if (clazz != null)
			BeanHelper.setStaticFields(clazz, paramMap);
		return paramMap;
	}

	/**************************
	 * 加载系统参数
	 */
	public Map<String, String> loadParam(String className) {
		Class<?> clazz = null;
		if (StringHelper.isNotEmpty(className)) {
			try {
				clazz = Class.forName(className);
			} catch (Exception e) {

			}
		}
		return loadParam(clazz);
	}

	
	public void editParam(FrameParam param) {
		// FrameAssertUtil.adminOptr(optr);
		FrameParam tp = this.daoParam.find( param.getParam_name());
		if (tp == null) {
			return;
		}
		// if (tp.getProtect_status() != null &&
		// tp.getProtect_status().equals(FrameStatus.Active.toString()))
		// throw new BusinessException("这是系统后台关键参数，不允许进行前台修改");
		validParamValue(param);
		this.daoParam.update(param);
	}
	
	public void deleteParam(FrameParam param) {
		this.daoParam.delete("param_name",param.getParam_name());
	}

	private void validParamValue(FrameParam param) {
		if (StringHelper.isNotEmpty(param.getParam_value_type())) {
			if (param.getParam_value_type().equals(FrameValueType.LocalDateTime.toString())) {
				LocalDateTime d = LocalDateHelper.parseDateTime(param.getParam_value());
				FrameAssertUtil.isNotNull(d, "参数值要求日期格式为 yyyy-MM-dd HH:mm:ss");
			} else if (param.getParam_value_type().equals(FrameValueType.LocalDate.toString())) {
				LocalDate d = LocalDateHelper.parseDate(param.getParam_value());
				FrameAssertUtil.isNotNull(d, "参数值要求日期格式为 yyyy-MM-dd");
			} else if (param.getParam_value_type().equals(FrameValueType.LocalTime.toString())) {
				LocalTime d = LocalDateHelper.parseTime(param.getParam_value());
				FrameAssertUtil.isNotNull(d, "参数值要求日期格式为  HH:mm:ss");
			}  else  if (param.getParam_value_type().equals(FrameValueType.Integer.toString())) {
				Integer value = null;
				try {
					value = Integer.parseInt(param.getParam_value());
				} catch (Exception e) {

				}
				FrameAssertUtil.isNotNull(value, "参数值要求数值，请重新输入");
			} else if (param.getParam_value_type().equals(FrameValueType.Boolean.toString())) {
				Boolean value = null;
				try {
					value = Boolean.parseBoolean(param.getParam_value());
				} catch (Exception e) {

				}
				FrameAssertUtil.isNotNull(value, "参数值要求布尔值，请重新输入");
			}
		}
	}

	public void addParam(FrameParam param) {
		// FrameAssertUtil.adminOptr(optr);
		FrameParam tp = this.daoParam.find(param.getParam_name());
		FrameAssertUtil.isNull(tp, "参数【" + param.getParam_name() + "】已经存在，请检查");
		validParamValue(param);
		this.daoParam.insert(param);
	}


}
