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.domain.GrantPayOrder;
import com.neusoft.smart.pos.payment.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payment.persistent.domain.PayRefundOrder;
import com.neusoft.smart.pos.payment.persistent.ext.domain.BizGrantPayNotify;
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 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 GrantPayNotifyRepository {

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

	/**
	 * 向第三方业务系统发送通知使用方法
	 * @param grantPayOrder
	 * @return
	 */
	public Boolean sendPayNotify(GrantPayOrder grantPayOrder, String key){
		//1.整理发送数据对象
		val bizPayNotify = new BizGrantPayNotify();
		bizPayNotify.setMcode(grantPayOrder.getMcode());
		bizPayNotify.setOrderNumber(grantPayOrder.getmOrderNumber());
		bizPayNotify.setTradeId(grantPayOrder.getTradeId());
		bizPayNotify.setPayType(grantPayOrder.getPayType());
		String payTime = LocalDateTimeUtils.convertLocalDateTimeToString(LocalDateTimeUtils.convertDateToLocalDateTime(grantPayOrder.getGrantTime()),LocalDateTimeUtils.SIMPLE_DATETIME_FORMAT);
		bizPayNotify.setGrantTime(payTime);
		bizPayNotify.setGrantAmount(grantPayOrder.getGrantAmount().toString());
		bizPayNotify.setAuthNo(grantPayOrder.getAuthNo());
		bizPayNotify.setOuterOrderNumber(grantPayOrder.getOuterOrderNumber());
		//生成签名信息
		String sign = SignUtils.generateSign(MapBeanUtils.beanToMap(bizPayNotify),key);
		bizPayNotify.setSign(sign);

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

		//3.记录日志信息
		LogUtils.saveMchPaymentTradeLog(grantPayOrder.getMcode(),grantPayOrder.getmOrderNumber(),grantPayOrder.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;
	}

}
