package cn.hmsoft.pay.impl;

import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.helper.BeanHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.log.LogHelper;
import cn.hmsoft.pay.data.dao.PayBillDao;
import cn.hmsoft.pay.data.model.PayBill;
import cn.hmsoft.pay.data.model.PayChannelDefine;
import cn.hmsoft.pay.data.model.PayMerchantChannel;
import cn.hmsoft.pay.data.model.PayMerchantDefine;
import cn.hmsoft.pay.data.model.PayReplyLog;
import cn.hmsoft.pay.util.PayParamUtil;

/********************************
 * 
 * @note
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017年8月21日 下午7:25:31
 * @version 4.0.0
 */
public abstract class PayChannelAbstract {

	protected PayChannelDefine channel;
	protected PayMerchantChannel payMerchantChannel;
	protected PayMerchantDefine merchant;
	protected Map<String, String> channelParamMap;
	protected Map<String, String> merchantParamMap;
	
	

	public Map<String, String> getChannelParamMap() {
		return channelParamMap;
	}

	public void setChannelParamMap(Map<String, String> channelParamMap) {
		this.channelParamMap = channelParamMap;
	}

	public Map<String, String> getMerchantParamMap() {
		return merchantParamMap;
	}

	public void setMerchantParamMap(Map<String, String> merchantParamMap) {
		this.merchantParamMap = merchantParamMap;
	}

	public PayChannelDefine getChannel() {
		return channel;
	}

	public PayMerchantChannel getPayMerchantChannel() {
		return payMerchantChannel;
	}

	public PayMerchantDefine getMerchant() {
		return merchant;
	}

	/***********************
	 * 初始化
	 * 
	 * @param channel 渠道
	 * @param payMerchantChannel 商户开通的渠道
	 * @param merchant 商户
	 * @return
	 */
	public boolean init(PayChannelDefine channel,
			PayMerchantChannel payMerchantChannel, PayMerchantDefine merchant) {
		this.channel = channel;
		this.payMerchantChannel = payMerchantChannel;
		this.merchant = merchant;
		this.channelParamMap = StringHelper.toMap(channel.getChannel_param());
		this.merchantParamMap = StringHelper.toMap(payMerchantChannel
				.getMc_params());

		try {
			return this.init();
		} catch (Exception e) {
			return false;
		}

	}

	/********************
	 * 初始化渠道
	 */
	protected abstract boolean init() throws Exception;

	/**************************
	 * 生成订单号
	 */
	public abstract String makeBillNo(PayBill bill);

	/**************************
	 * 生成订单请求
	 */
	public  Map<String, String> makeRequestAttr(PayBill bill){
		return null;
	}
	/**************************
	 * 生成订单请求
	 */
	public String makeRequestString(PayBill bill){
		return null;
	}

	/**************************
	 * 处理银行消息回传
	 */
	public abstract PayBill processReply(PayBillDao daoBill,PayBill bill,
			HttpServletRequest request,Map<String, Object> paramMap);

	public PayBill queryBill(PayBillDao daoBill, PayBill bill) {
		return bill;
	}

	/***************************
	 * 输出错误信息
	 */
	protected boolean error(String msg) {
		LogHelper.error(PayParamUtil.ModuleAlias + ":商户["
				+ merchant.getMerchant_name() + "],渠道["
				+ channel.getChannel_name() + "]无法使用,具体原因为:" + msg);
		return false;
	}

	/**************************************
	 * 记录银行回传信息
	 * 
	 * @param process_result 处理结果
	 * @param log_value 记录值
	 */
	protected void logReply(String process_result, String log_value,
			HttpServletRequest request) {
		logReply(null, process_result, log_value, request);
	}

	/**************************************
	 * 记录银行回传信息
	 * 
	 * @param process_result 处理结果
	 * @param log_value 记录值
	 */
	protected void logReply(PayBill bill, String process_result,
			String log_value, HttpServletRequest request) {
		PayReplyLog log = new PayReplyLog();

		if (bill != null) {
			log.setBill_code(bill.getBill_no());
			log.setBill_id(bill.getBill_id());
		}
		log.setReply_msg(log_value);
		log.setReply_time(LocalDateTime.now());
		log.setProce_result(process_result);
		log.setReplay_addr(request.getRemoteAddr());
		try {
			SpringHelper.AppGobalDao.insert(log);
		} catch (Exception e) {

		}
	}

	/****************************
	 * 获取请求的字符串并转换为对象
	 */
	@SuppressWarnings("deprecation")
	public static <T> T getRequestEntity(HttpServletRequest request,
			Class<T> clazz) {
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> array = request.getParameterNames();
		while (array.hasMoreElements()) {
			String v = array.nextElement();
			String value = URLDecoder.decode(request.getParameter(v));
			map.put(v, value);
		}
		return BeanHelper.fromMap(map, clazz);
	}

	/****************************
	 * 获取请求的字符串并转换为Map
	 */
	public static Map<String, String> getRequestMap(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> array = request.getParameterNames();
		//String vs = "";
		while (array.hasMoreElements()) {
			String v = array.nextElement();
			@SuppressWarnings("deprecation")
			String value = URLDecoder.decode(request.getParameter(v));
			map.put(v, value);
			//vs = vs + "&" + v + "=" + value;
		}
		//LogHelper.info(vs);
		return map;
	}
	
	/****************************
	 * 获取请求的字符串并转换为Map
	 */
	public static Map<String, Object> getRequestMapWithObject(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> array = request.getParameterNames();
		//String vs = "";
		while (array.hasMoreElements()) {
			String v = array.nextElement();
			@SuppressWarnings("deprecation")
			String value = URLDecoder.decode(request.getParameter(v));
			map.put(v, value);
			//vs = vs + "&" + v + "=" + value;
		}
		//LogHelper.info(vs);
		return map;
	}

}
