package cn.pluss.platform.transQuery.impl;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.leshua.LeshuaService;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.transQuery.TransQueryService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.AliPayParam;

/**
 * @author yuchen
 *
 * @Description 
 */
@Transactional
@Service("transQueryService")
public class TransQueryServiceImpl implements TransQueryService {
	
	 private static final Logger logger = LoggerFactory.getLogger(TransQueryServiceImpl.class);
	
	@Resource
	private HttpServletRequest request;
	
	@Resource
	private UserAppService userAppService;
	
	@Resource
	private MerchantBaseInfoService merchantBaseInfoService;
	
	@Resource
	private LeshuaService leshuaService;
	
	private String[] wechatPayNotSureResult = new String[] {"SYSTEMERROR", "USERPAYING", "BANKERROR"};

	@Override
	public Result<Map<String, Object>> transQuery(Map<String, Object> map) throws Exception {
		return null;
//		Map<String, Object> result=new HashMap<String, Object>(16);
//		String aisle = (String) map.get("aisle");
//		String orderNumber = (String) map.get("orderNumber");
//		String authCode = (String) map.get("authCode");
//		String token = request.getHeader("token");
//		UserApp userApp=new UserApp();
//		userApp.setToken(token);
//		userApp=userAppService.queryUserApp(userApp);
//		if("0".equals(aisle)) {//支付宝 微信
//			authCode = authCode.substring(0,1);
//			if("2".equals(authCode)) {
//				String appAuthToken = userApp.getAppAuthToken();//支付宝授权token
//				AlipayClient alipayClient = getAlipayClient();
//				AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//				request.putOtherTextParam("app_auth_token",appAuthToken);
//				request.setBizContent("{" +
//				"\"out_trade_no\":\""+orderNumber+"\"" +
//			"  }");
//				AlipayTradeQueryResponse response = alipayClient.execute(request);
//				if(response.isSuccess()){
//					String tradeStatus = response.getTradeStatus();
//					if("WAIT_BUYER_PAY".equals(tradeStatus)) {
//						tradeStatus="交易创建，等待买家付款";
//					return	ResultGenerator.genTransunKonwResult(tradeStatus);
//					}else if("TRADE_CLOSED".equals(tradeStatus)) {
//						tradeStatus="未付款交易超时关闭，或支付完成后全额退款";
//					return	ResultGenerator.genFailResult(tradeStatus);
//					}else if("TRADE_SUCCESS".equals(tradeStatus)) {
//						tradeStatus="交易支付成功";
//						String tradeNo = response.getTradeNo();
//						Date sendPayDate = response.getSendPayDate();
//						result.put("tradeNo", tradeNo);
//						result.put("payTime", sendPayDate);
//						return ResultGenerator.genSuccessResult(result);
//					}else if("TRADE_FINISHED".equals(tradeStatus)) {
//						tradeStatus="交易结束，不可退款";
//						return ResultGenerator.genFailResult(tradeStatus);
//					}
//				} else {
//					return ResultGenerator.genFailResult("查询失败");
//				}
//				return ResultGenerator.genFailResult("查询失败");
//			}else if("1".equals(authCode)) {
//				String merchantCode = userApp.getMerchantCode();
//				MerchantBaseInfo merchantBaseInfo=new MerchantBaseInfo();
//				merchantBaseInfo.setMerchantCode(merchantCode);
//				merchantBaseInfo=merchantBaseInfoService.queryMerchantBaseInfo(merchantBaseInfo);
//				Map<String, String> requestMap = new HashMap<String, String>(16);
//				requestMap.put("appid", "wxed824282c345b7a0");
//				requestMap.put("mch_id", "1551169371");
//				requestMap.put("sub_mch_id", merchantBaseInfo.getSubMchId());
//				requestMap.put("out_trade_no", orderNumber);
//				requestMap.put("nonce_str", StringUtil.genRandomNum(10));
//				requestMap.put("sign_type", "HMAC-SHA256");
//				requestMap.put("sign", WechatSignUtil.sha256Sign(requestMap, "7F05NZBTJ3XNDK5HLSDC3H25ZXOQKDHT"));
//				String requestResult = WXPayHttpUtil.WXHttpPostXMLWithoutCert("https://api.mch.weixin.qq.com/pay/orderquery",requestMap);
//				Map<String, String> wexiMap = WXPayXmlUtil.parseXMLToMap(requestResult);
//				if ("SUCCESS".equalsIgnoreCase(wexiMap.get("return_code"))) {
//					if ("SUCCESS".equalsIgnoreCase(wexiMap.get("result_code"))&&"SUCCESS".equalsIgnoreCase(wexiMap.get("trade_state"))) {
//						String transaction_id = wexiMap.get("transaction_id");
//						String time_end = (String) map.get("time_end");
//						SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//						result.put("tradeNo", transaction_id);
//						result.put("payTime", sFormat.parse(time_end));
//						return ResultGenerator.genSuccessResult(result);
//					} else {
//						boolean isNeedQuery = Arrays.asList(wechatPayNotSureResult).contains(wexiMap.get("trade_state"));//判断是否需要条用查询接口
//						if (isNeedQuery) {
//							return ResultGenerator.genTransunKonwResult("交易未知");
//						} else {
//							return ResultGenerator.genFailResult("交易失败");
//						}
//					}
//				}
//			}
//
//		}else if("1".equals(aisle)) {//乐刷
//			String merchantCode = userApp.getMerchantCode();
//			MerchantBaseInfo merchantBaseInfo=new MerchantBaseInfo();
//			merchantBaseInfo.setMerchantCode(merchantCode);
//			merchantBaseInfo=merchantBaseInfoService.queryMerchantBaseInfo(merchantBaseInfo);
//			Map<String, String> reqMap = new HashMap<String, String>(16);
//			reqMap.put("service", "query_status");
//			reqMap.put("merchant_id", userApp.getLeshuaMchId());
//			reqMap.put("third_order_id", orderNumber);
//			reqMap.put("nonce_str", StringUtil.genRandomNum(32));
//			String requestPay = leshuaService.requestPay(reqMap, "60E3F31EDEC8061C33721737B36FA86D", true);
//			logger.info("======支付结果====" + requestPay);
//			Map<String, Object> leshuaMap = XmlUtil.toMapLeshua(requestPay);
//			String resp_code = (String) leshuaMap.get("resp_code");
//			if ("0".equals(resp_code)) {//0表示通信成功
//				String result_code = (String) leshuaMap.get("result_code");
//				if ("0".equals(result_code)) {
//					// 0支付中 2支付成功 6订单关闭 8支付失败 10退款中 11退款成功 12退款失败
//					String status = (String) leshuaMap.get("status");
//					if ("2".equals(status)) {
//						String leshua_order_id = (String) leshuaMap.get("leshua_order_id");
//						String  payTime =(String) map.get("pay_time");
//						SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//						result.put("payTime", sFormat.parse(payTime));
//						result.put("tradeNo", leshua_order_id);
//						return ResultGenerator.genSuccessResult(result);
//					} else if ("8".equals(status)) {
//						return ResultGenerator.genFailResult("交易失败");
//					} else if ("0".equals(status)) {
//						return ResultGenerator.genTransunKonwResult("交易中");
//					} else if ("6".equals(status)) {
//						return ResultGenerator.genFailResult("订单关闭");
//					} else if ("10".equals(status)) {
//						return ResultGenerator.genFailResult("退款中");
//					} else if ("11".equals(status)) {
//						return ResultGenerator.genFailResult("退款成功");
//					} else if ("12".equals(status)) {
//						return ResultGenerator.genFailResult("退款失败");
//					}
//				} else {
//					return ResultGenerator.genFailResult("查询失败");
//				}
//			} else {
//				return ResultGenerator.genFailResult("查询失败");
//			}
//		}
//		return ResultGenerator.genFailResult("查询失败");
	}

	public AlipayClient getAlipayClient() {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",AliPayParam.getAppId(),AliPayParam.getPri(),"json","GBK",AliPayParam.getPub(),"RSA2");
		return alipayClient;
	}

}

