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

import com.alibaba.fastjson.JSONObject;
import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.payment.persistent.domain.MchPayChannel;
import com.neusoft.smart.pos.payment.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payment.persistent.domain.PayTypeDict;
import com.neusoft.smart.pos.payment.persistent.ext.domain.ThirdPlatformCancelOrder;
import com.neusoft.smart.pos.payment.persistent.ext.domain.ThirdPlatformCreateOrder;
import com.neusoft.smart.pos.payment.persistent.ext.domain.ThirdPlatformQueryOrder;
import com.neusoft.smart.pos.payment.persistent.ext.domain.ThirdPlatformRefundOrder;
import com.neusoft.smart.pos.payment.persistent.ext.domain.ThirdPlatformReverseOrder;
import com.neusoft.smart.pos.payment.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 大连食堂易石消费和退款
 */
@Component
public class YumstonePayRepository implements IThirdPayRepository {
	
	@Value("${yumstone.trade-url}")
	private String tradeUrl;
	
	@Value("${yumstone.refund-url}")
	private String refundUrl;
	
	@Override
	public ThirdPlatformCreateOrder createPayOrderToThirdPlatform(PayOrder payOrder, MchPayChannel mchPayChannel, PayTypeDict payTypeDict) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("employeeCardNo", payOrder.getScanCode());
		jsonObject.put("deposit", payOrder.getOrderAmount());
		String responseStr = HttpClientUtils.postData(tradeUrl, jsonObject.toJSONString());
		JSONObject responseJson = JSONObject.parseObject(responseStr);
		ThirdPlatformCreateOrder thirdPlatformCreateOrder = new ThirdPlatformCreateOrder();
		if (responseJson.containsKey("code") && responseJson.getInteger("code").equals(200)) {
			thirdPlatformCreateOrder.setPayStatus(DictionaryConstant.PAY_STATUS_YFK.getCode());
		} else {
			throw new BusinessCommonException(responseJson.getInteger("code").toString(), responseJson.getString("message"));
		}
		return thirdPlatformCreateOrder;
	}
	
	@Override
	public ThirdPlatformQueryOrder queryPayOrderFromThirdPlatform(PayOrder payOrder, MchPayChannel mchPayChannel) {
		return null;
	}
	
	@Override
	public ThirdPlatformCancelOrder cannelPayOrderFromThirdPlatform(PayOrder payOrder, MchPayChannel mchPayChannel) {
		return null;
	}
	
	@Override
	public ThirdPlatformRefundOrder refundPayOrderFromThirdPlatform(PayOrder payOrder, String refundNumber, Long refundAmount, MchPayChannel mchPayChannel, PayTypeDict payTypeDict) {
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("employeeCardNo", payOrder.getScanCode());
		jsonObject.put("deposit", payOrder.getOrderAmount());
		String responseStr = HttpClientUtils.postData(tradeUrl, jsonObject.toJSONString());
		JSONObject responseJson = JSONObject.parseObject(responseStr);
		ThirdPlatformRefundOrder thirdPlatformRefundOrder = new ThirdPlatformRefundOrder();
		if (responseJson.containsKey("code") && responseJson.getInteger("code").equals(200)) {
			thirdPlatformRefundOrder.setRefundStatus(DictionaryConstant.REFUND_STATUS_TKWC.getCode());
		} else {
			throw new BusinessCommonException(responseJson.getInteger("code").toString(), responseJson.getString("message"));
		}
		return thirdPlatformRefundOrder;
	}
	
	@Override
	public ThirdPlatformReverseOrder reversePayOrderFromThirdPlatform(PayOrder payOrder, String orderNumber, MchPayChannel mchPayChannel) {
		return null;
	}
}
