package abacus.springboot.example.controller;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.abacus.xpos.foundation.pubs.UserContext;

import abacus.commons.exception.BusinessException;
import abacus.config.ext.AbacusExtConfig;

/**
 * <p>Title:BaseServlet</p>
   <p>Description: 基础控制器</p>
   @author chuanZeng 
   @date 2026年8月18日
 */
public class BaseServlet{
	
	@Autowired
	private AbacusExtConfig abacusExtConfig;
	
	/**
	 * value-获取配置参数
	 * @param configKey
	 * @return
	 * @throws WebserviceException
	 */
	protected String getConfigValue(String configKey) throws BusinessException {
		String configValueStr = abacusExtConfig.getAbacusConfigValue(configKey);
		if (StringUtils.isBlank(configValueStr)) {
			throw new BusinessException("配置参数[" + configKey + "]值不存在！");
		}
		return configValueStr;
	}
	
	/**
	 * set-获取配置参数
	 * @param configKey
	 * @return
	 * @throws WebserviceException
	 */
	protected Map<String, String> getConfigSet(String keyGroup, String configKey) throws BusinessException {
		Map<String, String> configValueStr = abacusExtConfig.getAbacusConfigSetByKeyGroupByKey(keyGroup, configKey);
		if (configValueStr.isEmpty()) {
			throw new BusinessException("系统[" + keyGroup + "]配置参数[" + configKey + "]值不存在！");
		}
		return configValueStr;
	}
	
	/**
	 * 获取登陆用户id
	 * @return
	 * @throws Exception 
	 */
	protected String getUserId() throws BusinessException {
		String operatorId = UserContext.getUserId();
		if (StringUtils.isBlank(operatorId)){
			throw new BusinessException("操作员不存在，请重新登录！");
		}
		
		return operatorId;
	}
	
	/**
	 * 获取登陆用户姓名
	 * @return
	 */
	protected String getUserName() throws BusinessException{
		String operatorName = UserContext.getUserName();
		if (StringUtils.isBlank(operatorName)){
			throw new BusinessException("操作员不存在，请重新登录！");
		}
		
		return operatorName;
	}
	
	/**
	 * 获取登陆用户所属部门id
	 * @return
	 */
	protected String getDeptId() throws BusinessException{
		String deptId = UserContext.getDeptId();
		if (StringUtils.isBlank(deptId)){
			throw new BusinessException("操作员所属部门不存在，请重新登录！");
		}
		
		return deptId;
	}
	
	/**
	 * 获取登陆用户所属部门名称
	 * @return
	 */
	protected String getDeptName() throws BusinessException{
		String deptName = UserContext.getDeptName();
		if (StringUtils.isBlank(deptName)){
			throw new BusinessException("操作员所属部门不存在，请重新登录！");
		}
		
		return deptName;
	}
	
	/**
	 * 
	 * 获取double型的参数
	 * 
	 * @param request
	 * @param paramName
	 * @return
	 */
	protected Double getDoubleParameter(HttpServletRequest request, String paramName) {
		// 
		String paramValue = request.getParameter(paramName);
		if (StringUtils.isBlank(paramValue)) {
			return null;
		}
		
		return Double.valueOf(paramValue);
	}
	
	/**
	 * 
	 * 获取long型的参数
	 * 
	 * @param request
	 * @param paramName
	 * @return
	 */
	protected Long getLongParameter(HttpServletRequest request, String paramName) {
		// 
		String paramValue = request.getParameter(paramName);
		if (StringUtils.isBlank(paramValue)) {
			return null;
		}
		
		return Long.valueOf(paramValue);
	}
	
	/**
	 * 
	 * 获取int型的参数
	 * 
	 * @param request
	 * @param paramName
	 * @return
	 */
	protected Integer getIntegerParameter(HttpServletRequest request, String paramName) {
		// 
		String paramValue = request.getParameter(paramName);
		if (StringUtils.isBlank(paramValue)) {
			return null;
		}
		
		return Integer.valueOf(paramValue);
	}
	
	/**
	 * 
	 * 获取Boolean型的参数
	 * 
	 * @param request
	 * @param paramName
	 * @return
	 */
	protected Boolean getBooleanParameter(HttpServletRequest request, String paramName) {
		// 
		String paramValue = request.getParameter(paramName);
		
		if ("true".equalsIgnoreCase(paramValue)) {
			return true;
		}else if ("false".equalsIgnoreCase(paramValue)) {
			return false;
		}else {
			return null;
		}
	}
	
	/**
	 * 正则表达式校验手机号码
	 * 
	 * @return false 则手机号码不合法，true 则手机号码校验通过
	 */
	protected Boolean validatePhoneNumber(String phoneNumber) {
		if (phoneNumber.length() != 11) {
			System.out.print("手机号应为11位数 ");
			return false;
		} else {
			String regPattern = "^(1)\\d{10}$";
			Pattern pattern = Pattern.compile(regPattern);
			Matcher matcher = pattern.matcher(phoneNumber);
			boolean isMatch = matcher.matches();
			if (!isMatch) {
				System.out.print("请填入正确的手机号 ");
			}
			return isMatch;
		}
	}
	
	/**
	 * 良友-crm回调函数
	 */
	protected Map<String, Object> crmReturn(int code, String message, String content) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		
		map.put("code", code);
		map.put("message", message);
		map.put("content", content);
		
		return map;
	}
	
	
	
	
}
