package com.community.huiju.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.community.commom.constant.Constant;
import com.community.commom.session.UserElement;
import com.community.huiju.common.perproties.PayNotifyPerproties;
import com.community.huiju.common.wxpay.HfWxConfig;
import com.community.huiju.common.wxpay.WXPay;
import com.community.huiju.common.wxpay.WXPayConstants;
import com.community.huiju.common.wxpay.WXPayUtil;
import com.community.huiju.dao.*;
import com.community.huiju.model.TaSysRole;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.TpBillInvoice;
import com.community.huiju.model.TpBillOrder;
import com.community.huiju.service.AliPayServiceI;
import com.community.huiju.service.WxPayServiceI;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static com.alipay.api.AlipayConstants.CHARSET_UTF8;

/**
 * @author FXF
 * @date 2018-10-25
 */
@Service("aliPayService")
@Transactional(rollbackFor = Exception.class)
public class AliPayServiceImpl implements AliPayServiceI {
	private Logger log = LoggerFactory.getLogger(AliPayServiceImpl.class);
	
	@Autowired
	private TpBillInvoiceMapper tpBillInvoiceMapper;
	
	@Autowired
	private TpBillStatementMapper tpBillStatementMapper;

	@Autowired
	private TpBillMapper tpBillMapper;

	@Autowired
	private TaUserMapper taUserMapper;

	@Autowired
	private TaSysRoleMapper taSysRoleMapper;

	@Autowired
	private TpBillOrderMapper tpBillOrderMapper;
	
	@Value("${alipay.APP_PRIVATE_KEY}")
	private String APP_PRIVATE_KEY;
	
	@Value("${alipay.APP_ID}")
	private String ALI_APP_ID;
	
	@Value("${alipay.ALIPAY_PUBLIC_KEY}")
	private String ALIPAY_PUBLIC_KEY;

	@Autowired
	private WxPayServiceI wxPayServiceI;


	/**
	 * 支付宝统一下单
	 *
	 * @return
	 * @param outTradeNo
	 * @param userElement
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String AliPayOrder(String outTradeNo, String type, UserElement userElement) throws Exception {

		String typeName = Constant.PAY_TYPE_BILL.equals(type) ? "物业缴费" : Constant.PAY_TYPE_CAR.equals(type) ? "车缴费" : "缴费";
		Map<String, Object> orderPriceAndNotifyUrl = wxPayServiceI.getOrderPriceAndNotifyUrl(type, outTradeNo, "ali");
		
		//下单
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", ALI_APP_ID, APP_PRIVATE_KEY, "json", CHARSET_UTF8, ALIPAY_PUBLIC_KEY, "RSA2");
		//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
		AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
		//SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
		AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
		model.setBody(typeName);
		model.setSubject(typeName);
		model.setOutTradeNo(outTradeNo);
		model.setTimeoutExpress("30m");
		model.setTotalAmount(BigDecimal.valueOf(Long.valueOf(String.valueOf(orderPriceAndNotifyUrl.get("payPrice")))).divide(new BigDecimal(100)).toString());
		model.setProductCode("QUICK_MSECURITY_PAY");
		request.setBizModel(model);
		request.setNotifyUrl(String.valueOf(orderPriceAndNotifyUrl.get("notifyUrl")));
		try {
			//这里和普通的接口调用不同，使用的是sdkExecute
			AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
			//就是orderString 可以直接给客户端请求，无需再做处理。
			System.out.println(response.getBody());
			return response.getBody();
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return null;
	}
}
