package com.neusoft.smart.pos.payment.repository;

import com.alibaba.fastjson.JSON;
import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.framework.constants.ApplicationConstants;
import com.neusoft.smart.pos.framework.utils.DateUtils;
import com.neusoft.smart.pos.framework.utils.LocalDateTimeUtils;
import com.neusoft.smart.pos.framework.utils.MapBeanUtils;
import com.neusoft.smart.pos.framework.utils.SignUtils;
import com.neusoft.smart.pos.payment.persistent.ext.domain.BizPayNotify;
import com.neusoft.smart.pos.payment.persistent.ext.domain.BizPayRefundNotify;
import com.neusoft.smart.pos.payment.utils.HttpClientUtils;
import com.neusoft.smart.pos.payment.utils.LogUtils;
import com.neusoft.smart.pos.payorder.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payorder.persistent.domain.PayRefundOrder;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author zcf
 * @date 2018-01-09 10：43
 * @description 支付管理使用Repository
 */
@Component
public class PayNotifyRepository {

	private final static Logger logger = LoggerFactory.getLogger(PayNotifyRepository.class);

	/**
	 * 向第三方业务系统发送通知使用方法
	 * @param payOrder
	 * @return
	 */
	public Boolean sendPayNotify(PayOrder payOrder, String key){
		//1.整理发送数据对象
		val bizPayNotify = new BizPayNotify();
		bizPayNotify.setMcode(payOrder.getMcode());
		bizPayNotify.setOrderNumber(payOrder.getmOrderNumber());
		bizPayNotify.setTradeId(payOrder.getTradeId());
		bizPayNotify.setPayType(payOrder.getPayType());
		String payTime = LocalDateTimeUtils.convertLocalDateTimeToString(LocalDateTimeUtils.convertDateToLocalDateTime(payOrder.getPayTime()),LocalDateTimeUtils.SIMPLE_DATETIME_FORMAT);
		bizPayNotify.setPayTime(payTime);
		bizPayNotify.setOrderAmount(payOrder.getPayAmount().toString());
		//生成签名信息
		String sign = SignUtils.generateSign(MapBeanUtils.beanToMap(bizPayNotify),key);
		bizPayNotify.setSign(sign);

		//2.发送数据至业务系统
		String resultData = HttpClientUtils.postData(payOrder.getNotifyUrl(), JSON.toJSONString(bizPayNotify));

		//3.记录日志信息
		LogUtils.saveMchPaymentTradeLog(payOrder.getMcode(),payOrder.getmOrderNumber(),payOrder.getPayType(),LogUtils.LOG_DESCRIPTION_ZFCGHD,JSON.toJSONString(bizPayNotify),resultData,DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());

		//4.判断业务系统执行是否成功
		val resultMap = JSON.parseObject(resultData, HashMap.class);
		if(null != resultMap){
			if(null != resultMap.get("returnCode") && ApplicationConstants.SUCCESS.equalsIgnoreCase(String.valueOf(resultMap.get("returnCode")))){
				return true;
			}
		}
		return false;
	}

	/**
	 * 向第三方业务系统发送通知使用方法
	 * @param payRefundOrder
	 * @param key
	 * @return
	 */
	public Boolean sendPayRefundNotify(PayRefundOrder payRefundOrder, String key){
		//1.整理发送数据对象
		val bizPayRefundNotify = new BizPayRefundNotify();
		bizPayRefundNotify.setMcode(payRefundOrder.getMcode());
		bizPayRefundNotify.setOrderNumber(payRefundOrder.getmOrderNumber());
		bizPayRefundNotify.setRefundNumber(payRefundOrder.getRefundNumber());
		bizPayRefundNotify.setRefundAmount(payRefundOrder.getRefundedAmount().toString());
		if(payRefundOrder.getRefundStatus().equals(DictionaryConstant.REFUND_STATUS_TKWC.getCode())){
			bizPayRefundNotify.setRefundStatus("1");	//退款成功
		}else{
			bizPayRefundNotify.setRefundStatus("0");	//退款失败
		}
		bizPayRefundNotify.setThirdRefundNumber(payRefundOrder.getThirdRefundNumber());
		bizPayRefundNotify.setPayType(payRefundOrder.getPayType());
		bizPayRefundNotify.setRefundTime(DateUtils.format(payRefundOrder.getRefundedTime(),DateUtils.FORMAT_LONG));

		//生成签名信息
		String sign = SignUtils.generateSign(MapBeanUtils.beanToMap(bizPayRefundNotify),key);
		bizPayRefundNotify.setSign(sign);

		//2.发送数据至业务系统
		String resultData = HttpClientUtils.postData(payRefundOrder.getRefundNotifyUrl(), JSON.toJSONString(bizPayRefundNotify));

		//3.记录日志信息
		LogUtils.saveMchPaymentTradeLog(payRefundOrder.getMcode(),payRefundOrder.getmOrderNumber(),payRefundOrder.getPayType(),LogUtils.LOG_DESCRIPTION_TKCGHD,JSON.toJSONString(bizPayRefundNotify),resultData,DictionaryConstant.PAYMENT_REQUEST_TYPE_ZD.getCode());

		//4.判断业务系统执行是否成功
		val resultMap = JSON.parseObject(resultData, HashMap.class);
		if(null != resultMap){
			if(null != resultMap.get("returnCode") && ApplicationConstants.SUCCESS.equalsIgnoreCase(String.valueOf(resultMap.get("returnCode")))){
				return true;
			}
		}
		return false;
	}

}
