package cn.pluss.platform.controller;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.dto.*;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantIncome;
import cn.pluss.platform.entity.MerchantOrder;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantIncome.MerchantIncomeService;
import cn.pluss.platform.merchantOrder.MerchantOrderService;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.AliPayParam;
import cn.pluss.platform.util.StringUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.request.AlipayOpenAgentConfirmRequest;
import com.alipay.api.request.AlipayOpenAgentCreateRequest;
import com.alipay.api.request.AlipayOpenAgentFacetofaceSignRequest;
import com.alipay.api.request.AlipayOpenAgentOrderQueryRequest;
import com.alipay.api.response.AlipayOpenAgentConfirmResponse;
import com.alipay.api.response.AlipayOpenAgentCreateResponse;
import com.alipay.api.response.AlipayOpenAgentFacetofaceSignResponse;
import com.alipay.api.response.AlipayOpenAgentOrderQueryResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import order.OrderQueryVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yuchen
 * 
 * @Description 
 */
@Deprecated
@RestController
@RequestMapping("/alipay")
@Transactional
@Api(value = "支付宝控制类",tags={"支付宝接口"})
public class AlipayController {
	
	private final static String APP_ID="2019092467786259";
	
	private final static String PRI="MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCN1NubOHroItIz7eLWWBDprPzToDr1cggn/V90ghPaoBWgmnZhD/CejX+KxHmQVQLEBVN6ehQgNSVGBj0hSHxkj1OCCU7Eu9qAIqRzkWY1RkZKoKOIcJbU+yL20XTs3kATgNm5jx8ejVQDsNqekS1bBrRX6xlU/mjMVFtKoZC/Vpv8yAmP6R9onkum4O0OgCWARKC40NuYVp9hPUgRpWJ/NIPSlO4j8zIPYFkjfsh84c8moVLZdhYDDbbEY3vnHutDa0A3d5f4MI4qXBZ6tW5wO5WG93U5E5jat73RUzBOc0zN+3U/Rsk+gq722ChLj4/ifp0NLqgjbKecs4wlM8A3AgMBAAECggEAQ1QjuAjU17QIA1zPTR9bUAKttqyesHukgY9XLBC/6g4KlkYdIjCV8j6LqE1iw/CHMpSwrziaqztvaVF23YDyhC5B3z4Z5Wyj6iYH2VDRq0KkBbNCTnfcMljRnUeLcRKEan3FXcAibrd6hZIEBjg2xrplDENvsBhhmXYGLGSMOTfoRFlc1DhvPdr3qxR9mljD2YcvIrxDVDPZ3P4wTcsIM6xZmXGrsFbI0sGxgm4a4fzMLUYxSvuMtc+auwasQ/393Le/kjdVBWr/hBJZbGIU5/NQN0EesOJD6dIFU07wDDuy4LYLVVjSN3EiynWfMJIrvLbnR1sMgKJ775wY61hIaQKBgQC/mFKwZdZvzsqnEMQmUfkUT5+v8BJvS4JApPpt+O2GMUAKoynk220U0rJW/QyEkTz6A/lR8ZStT6TyM14ulpOYDMJquZqSCoIHMrIdjhZRMqAVONUJHsYVvpLQLbJaDDEaCksnWE+77ULBstZaevkBgLmNsy6V4CxwLJKj0xMDEwKBgQC9giRaS4t2J5ZcNycuDEBhwPteSoFun/6bX4yPsZYdeB88NGs7j1oOk9MXrvQq69X0uXE8zv2DHrDvuoGI920m56bU/4wCuKoqpnLi0xFM1al0na12He6UmFKWa+kGBkH5p5BggPyaCFMwJI/qL3gZft56aWjliviwWSmI8pvOzQKBgHZw8d0+d1vTGJBC2x9dWX9m4a7f2GWY5kKUOoQ1eJCWbaKlz0Y9J6fRwJHqCQ0CxBFC16QClgi1zaA0hSqDx2YdrSpQ5u8VM+DDRDzlE4LKqw+zfG8Q6R+UGio7tZ/ZHZmdd2wxOclRGQ2pQr3Ye4qkSxEADfJ1DMOjqltAJualAoGALafl9C8RGVUuP2c2NDzVYfepV60hg7JeF7asWY4SOWVQOSPz1bSSoMJyb9lBj/mjYUMwCdNgdi91tzu6q3HYxS+4HMa4R4uPy5iwQv9Qwq8TFTpLqRJLAVe1RfIXgNEPIgOBoA5TTRTGXenhFi17hMDw/pLYp7yUb3/7pre0W90CgYAWb86ocBIpS8cKhZY2fcrHALzKh4cuxl68sNQcOxevt3tIak0KG+o/5/5l0uBW2qYSOJOl2u831VWuOWEJBk+h5brYCtTNIsa6lKdknhRP/NfAfWUiWEesPV5nfYKL85s8bf2x/gFv9Ah0S5K4cI+SCMVIzaQzQ2dN+cmlvM8rfQ==";
	private final static String PUB="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjdTbmzh66CLSM+3i1lgQ6az806A69XIIJ/1fdIIT2qAVoJp2YQ/wno1/isR5kFUCxAVTenoUIDUlRgY9IUh8ZI9TgglOxLvagCKkc5FmNUZGSqCjiHCW1Psi9tF07N5AE4DZuY8fHo1UA7DanpEtWwa0V+sZVP5ozFRbSqGQv1ab/MgJj+kfaJ5LpuDtDoAlgESguNDbmFafYT1IEaVifzSD0pTuI/MyD2BZI37IfOHPJqFS2XYWAw22xGN75x7rQ2tAN3eX+DCOKlwWerVucDuVhvd1OROY2re90VMwTnNMzft1P0bJPoKu9tgoS4+P4n6dDS6oI2ynnLOMJTPANwIDAQAB";
	
	@Resource
	private UserAppService userAppService;
	
	@Resource
	private MerchantBaseInfoService merchantBaseInfoService;
	
	@Resource
	private MerchantOrderService merchantOrderService;
	
	@Resource
	private NoticeService noticeService;
	
	@Resource
	private MerchantIncomeService merchantIncomeService;
	
	
	
	@PostMapping
	@ApiOperation(value = "开启代商户签约、创建应用事务",notes = "返回BatchNo",httpMethod = "POST")
	public Result<?> get(@RequestBody AccountInfoDTO dto) throws AlipayApiException{
		AlipayClient alipayClient = getAlipayClient();
		AlipayOpenAgentCreateRequest request = new AlipayOpenAgentCreateRequest();
		if(StringUtil.isEmpty(dto.getAccount())) {
			return ResultGenerator.genFailResult("请输入支付宝账号");
		}
		if(StringUtil.isEmpty(dto.getContactName())) {
			return ResultGenerator.genFailResult("请输入姓名");
		}
		
		request.setBizContent("{" +
		"\"account\":\""+dto.getAccount()+"\"," +
		"\"contact_info\":{" +
		"\"contact_name\":\""+dto.getContactName()+"\"," +
		"\"contact_mobile\":\""+dto.getContactMobile()+"\"," +
		"\"contact_email\":\""+dto.getContactEmail()+"\"" +
		"    }," +"}");
		AlipayOpenAgentCreateResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			if("10000".equals(response.getCode())) {
				return ResultGenerator.genSuccessResult(response.getBatchNo());
			}else {
				return ResultGenerator.genFailResult("调用失败");
			}
		} else {
			return ResultGenerator.genFailResult("调用失败");
		}
	}
	
	
	@PostMapping("/facetofaceSign")
	@ApiOperation(value = "代签约当面付产品",httpMethod = "POST")
	public Result<String> facetofaceSign(@RequestBody BatchInfoDTO dto) throws AlipayApiException{
		AlipayClient alipayClient = getAlipayClient();
		FileItem BusinessLicensePic = new FileItem(dto.getBusinessLicensePic());
		FileItem ShopSignBoardPic = new FileItem(dto.getShopSignBoardPic());
		AlipayOpenAgentFacetofaceSignRequest request = new AlipayOpenAgentFacetofaceSignRequest();
		request.setBatchNo(dto.getBatchNo());
		request.setMccCode(dto.getMccCode());
		request.setBusinessLicenseNo(dto.getBusinessLicenseNo());
		request.setBusinessLicensePic(BusinessLicensePic);
		request.setShopSignBoardPic(ShopSignBoardPic);
		AlipayOpenAgentFacetofaceSignResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			if("10000".equals(response.getCode())) {
				return ResultGenerator.genSuccessResult();
			}else {
				return ResultGenerator.genFailResult("调用失败");
			}
		} else {
			return ResultGenerator.genFailResult("调用失败");
		}
	}
	
	@GetMapping("/agentConfirm")
	@ApiOperation(value = "提交代商户签约、创建应用事务",httpMethod = "GET")
   @ApiImplicitParam(name = "batchNo",value = "ISV代商户操作事务编号", paramType = "batchNo",dataType = "String",required=true)
	public Result<String> agentConfirm(String batchNo) throws AlipayApiException{
		AlipayClient alipayClient = getAlipayClient();
		AlipayOpenAgentConfirmRequest request = new AlipayOpenAgentConfirmRequest();
		request.setBizContent("{" +
		"\"batch_no\":\""+batchNo+"\"" +
		"  }");
		AlipayOpenAgentConfirmResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			if("10000".equals(response.getCode())) {
				return ResultGenerator.genSuccessResult();
			}else {
				return ResultGenerator.genFailResult("调用失败");
			}
		} else {
			return ResultGenerator.genFailResult("调用失败");
		}
	}
	
	@GetMapping("/orderQuery")
	@ApiOperation(value = "查询申请单状态",httpMethod = "GET")
	public  Result<OrderQueryVO> orderQuery(String batchNo) throws AlipayApiException{
		AlipayClient alipayClient = getAlipayClient();
		AlipayOpenAgentOrderQueryRequest request = new AlipayOpenAgentOrderQueryRequest();
		request.setBizContent("{" +
		"\"batch_no\":\""+batchNo+"\"" +
		"  }");
		AlipayOpenAgentOrderQueryResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			if("10000".equals(response.getCode())) {
				OrderQueryVO orderQueryVO=new OrderQueryVO();
				orderQueryVO.setAgentAppId(response.getAgentAppId());
				orderQueryVO.setConfirmUrl(response.getConfirmUrl());
				orderQueryVO.setMerchantPid(response.getMerchantPid());
				if("MERCHANT_INFO_HOLD".equals(response.getOrderStatus())) {
					orderQueryVO.setOrderStatus("暂存");
				}else if("MERCHANT_AUDITING".equals(response.getOrderStatus())) {
					orderQueryVO.setOrderStatus("审核中");
				}else if("MERCHANT_CONFIRM".equals(response.getOrderStatus())) {
					orderQueryVO.setOrderStatus("待商户确认");
				}else if("MERCHANT_CONFIRM_SUCCESS".equals(response.getOrderStatus())) {
					orderQueryVO.setOrderStatus("商户确认成功");
				}else if("MERCHANT_CONFIRM_TIME_OUT".equals(response.getOrderStatus())) {
					orderQueryVO.setOrderStatus("商户超时未确认");
				}else if("MERCHANT_APPLY_ORDER_CANCELED".equals(response.getOrderStatus())) {
					orderQueryVO.setOrderStatus("审核失败或商户拒绝");
				}
				orderQueryVO.setRejectReason(response.getRejectReason());
				return ResultGenerator.genSuccessResult(orderQueryVO);
			}else {
				return ResultGenerator.genFailResult("调用失败");
			}
		} else {
			return ResultGenerator.genFailResult("调用失败");
		}
	}

	@Deprecated
	@PostMapping("/tradePay")
	@ApiOperation(value = "统一收单交易支付接口",httpMethod = "POST")
	public Result<String> tradePay(@RequestBody TradePayDTO dto) throws AlipayApiException{
		throw new MsgException("接口暂不支持");
//		if(StringUtil.isEmpty(dto.getUserId())) {
//			return ResultGenerator.genFailResult("用户id不能为空");
//		}
//		UserApp userApp = getUserApp(dto.getUserId());
//		if(StringUtil.isEmpty(userApp.getAppAuthToken())) {
//			return ResultGenerator.genFailResult(ParametersUtil.domain+"/wap/aliPay/index?userId"+dto.getUserId());//跳转到商家授权H5页面（暂时缺失）
//		}
//		if(StringUtil.isEmpty(dto.getOutTradeNo())) {
//			return ResultGenerator.genFailResult("商户订单号不能为空");
//		}
//		if(StringUtil.isEmpty(dto.getAuthCode())) {
//			return ResultGenerator.genFailResult("支付授权码不能为空");
//		}
//		if(StringUtil.isEmpty(dto.getTotalAmount())) {
//			return ResultGenerator.genFailResult("订单总额不能为空");
//		}
//		AlipayClient alipayClient = getAlipayClient();
//		AlipayTradePayRequest request = new AlipayTradePayRequest();
//		request.putOtherTextParam("app_auth_token", userApp.getAppAuthToken());
//		request.setBizContent("{"+
//		"\"out_trade_no\":\""+dto.getOutTradeNo()+"\"," +
//		"\"scene\":\"bar_code\"," +
//		"\"auth_code\":\""+dto.getAuthCode()+"\"," +
//		"\"subject\":\""+dto.getSubject()+"\"," +
//		"\"total_amount\":\""+dto.getTotalAmount()+"\"" +
//		"  }");
//		AlipayTradePayResponse response = alipayClient.execute(request);
//		if(response.isSuccess()){
//			if("10000".equals(response.getCode())) {
//				return ResultGenerator.genSuccessResult(response.getBody());
//			}else {
//				return ResultGenerator.genFailResult("支付失败");
//			}
//		} else {
//			return ResultGenerator.genFailResult("支付失败");
//		}
	}
	
	@PostMapping("/tradeRefund")
	@ApiOperation(value = "统一收单交易退款接口",httpMethod = "POST")
	public Result<String> tradeRefund(@RequestBody TradeRefundDTO dto) throws AlipayApiException{
		throw new MsgException("暂不支持该接口");
//		if(StringUtil.isEmpty(dto.getUserId())) {
//			return ResultGenerator.genFailResult("用户id不能为空");
//		}
//		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
//		if(StringUtil.isEmpty(dto.getOutTradeNo())) {
//			if(StringUtil.isEmpty(dto.getTradeNo())) {
//				return ResultGenerator.genFailResult("支付宝交易号和商户订单号必须填一个");
//			}else {
//				request.setBizContent("{" +
//						"\"trade_no\":\""+dto.getTradeNo()+"\"," +
//						"\"refund_amount\":"+dto.getRefundAmount()+"" +
//						"  }");
//			}
//		}else {
//			if(StringUtil.isEmpty(dto.getTradeNo())) {
//				request.setBizContent("{" +
//						"\"out_trade_no\":\""+dto.getOutTradeNo()+"\"," +
//						"\"refund_amount\":"+dto.getRefundAmount()+"" +
//						"  }");
//			}else {
//				request.setBizContent("{" +
//						"\"out_trade_no\":\""+dto.getOutTradeNo()+"\"," +
//						"\"trade_no\":\""+dto.getTradeNo()+"\"," +
//						"\"refund_amount\":"+dto.getRefundAmount()+"" +
//						"  }");
//			}
//		}
//		UserApp userApp = getUserApp(dto.getUserId());
//		if(StringUtil.isEmpty(userApp.getAppAuthToken())) {
//			 //跳转到商家授权H5页面（暂时缺失）
//			return ResultGenerator.genFailResult(ParametersUtil.domain+"/wap/aliPay/index?userId="+dto.getUserId());
//			}else {
//				request.putOtherTextParam("app_auth_token", userApp.getAppAuthToken());
//			}
//		AlipayClient alipayClient = getAlipayClient();
//		AlipayTradeRefundResponse response = alipayClient.execute(request);
//		if(response.isSuccess()){
//		return ResultGenerator.genSuccessResult("退款成功");
//		} else {
//			return ResultGenerator.genFailResult("退款失败");
//		}
	}
	
	@PostMapping("/tradeQuery")
	@ApiOperation(value = "统一收单线下交易查询接口",httpMethod = "POST")
	public Result<String>  tradeQuery(@RequestBody QueryOrderDTO queryOrderDTO) throws AlipayApiException{
		throw new MsgException("暂不支持该接口");
//		if(StringUtil.isEmpty(queryOrderDTO.getUserId())) {
//			return ResultGenerator.genFailResult("用户userId不能为空");
//		}
//		if(StringUtil.isEmpty(queryOrderDTO.getOrderNumber())) {
//			return ResultGenerator.genFailResult("商家订单号不能为空");
//		}
//		UserApp userApp = getUserApp(queryOrderDTO.getUserId());
//		if(StringUtil.isEmpty(userApp.getAppAuthToken())) {
//			return ResultGenerator.genFailResult(ParametersUtil.domain+"/wap/aliPay/index?userId="+queryOrderDTO.getUserId()); //跳转到商家授权H5页面（暂时缺失）
//		}
//		MerchantBaseInfo merchantBaseInfo=new MerchantBaseInfo();
//		merchantBaseInfo.setMerchantCode(queryOrderDTO.getMerchantCode());
//		List<MerchantBaseInfo> queryMerchantBaseInfoList = merchantBaseInfoService.queryMerchantBaseInfoList(merchantBaseInfo);
//		if(queryMerchantBaseInfoList==null||queryMerchantBaseInfoList.size()==0) {
//			return ResultGenerator.genFailResult("该商户不存在");
//		}
//		merchantBaseInfo=queryMerchantBaseInfoList.get(0);
//		AlipayClient alipayClient = getAlipayClient();
//		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//		request.putOtherTextParam("app_auth_token", userApp.getAppAuthToken());
//		request.setBizContent("{" +
//		"\"out_trade_no\":\""+queryOrderDTO.getOrderNumber()+"\"" +
//	"  }");
//		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="交易支付成功";
//				Date sendPayDate = response.getSendPayDate();
//				String tradeNo = response.getTradeNo();
//				MerchantOrder order = queryOrderDTO.convertMerchantOrder();
//				order.setTransDt(sendPayDate);
//				merchantIncome(order, "1");
//				saveMerchantOrderStatus(tradeNo,order, merchantBaseInfo);
//				return ResultGenerator.genSuccessResult(tradeStatus);
//			}else if("TRADE_FINISHED".equals(tradeStatus)) {
//				tradeStatus="交易结束，不可退款";
//				return ResultGenerator.genFailResult(tradeStatus);
//			}
//		} else {
//			return ResultGenerator.genFailResult("查询失败");
//		}
//		return ResultGenerator.genFailResult("查询失败");
	}
	
	private MerchantOrder saveMerchantOrderStatus(String outTradeNo,MerchantOrder merchantOrder, MerchantBaseInfo merchantBaseInfo) {
		return null;
//		String payTypeCode = merchantOrder.getPayTypeCode();//当前订单支付方式
//		if("wechatPay".equalsIgnoreCase(payTypeCode)) {
//			merchantOrder.setWeChatTransNo(outTradeNo);
//		}else if("aliPay".equalsIgnoreCase(payTypeCode)) {
//			merchantOrder.setAliPayTransNo(outTradeNo);
//		}else if("leshuaPay0".equalsIgnoreCase(payTypeCode)||"leshuaPay1".equals(payTypeCode)) {
//			merchantOrder.setLeshuaTransNo(outTradeNo);
//		}
//		merchantOrder.setStatus("1");
//		merchantOrderService.updateById(merchantOrder);
//		if (!StringUtil.isEmpty(merchantBaseInfo.getUserId())) {//更新上级推广员的交易金额
//			UserApp userApp = new UserApp();
//			userApp.setUserId(Long.parseLong(merchantBaseInfo.getUserId()));
//			List<UserApp> queryUserAppList = userAppService.queryUserAppList(userApp);//查询当前商户的用户信息
//			if (queryUserAppList != null && queryUserAppList.size()>0) {
//				userApp = queryUserAppList.get(0);//当前商户的用户信息
//				Long parentId = userApp.getParentId();//推荐人的userId
//				UserApp userApp2 = new UserApp();
//				userApp2.setUserId(parentId);
//				if(StringUtil.isNotEmpty(parentId)) {
//					userApp2=userAppService.queryUserApp(userApp2);
//					Double tradeMoney = userApp2.getTradeMoney()==null?0d:userApp2.getTradeMoney();//当前推广员的交易额
//					Double consumeFee = merchantOrder.getConsumeFee()==null?0d:merchantOrder.getConsumeFee();//这一笔订单的消费金额
//					userApp2.setTradeMoney(StringUtil.bigDecimal(tradeMoney+consumeFee));
//					double extendProfit = userApp2.getExtendProfit()==null?0d:userApp2.getExtendProfit();//当前推广员的推广分润
//					double shareMoney = StringUtil.bigDecimal(Double.parseDouble(merchantOrder.getShareMoney()));
//					extendProfit=extendProfit+shareMoney;
//					userApp2.setExtendProfit(extendProfit);
//					userAppService.updateUserApp(userApp2);
//					if (shareMoney > 0.01) {
//						// 生成分润通知
//						Notice notice = new Notice(0, 6, userApp);
//						notice.setConrtent("您有一笔" + merchantOrder.getShareMoney() + "元的商户分润");
//						noticeService.saveNotice(notice);
//						List<String> alias = new ArrayList<String>();
//						alias.add(userApp.getUserId().toString());
//						GeneralPushUtil.sendAllPlatByAlias(alias, "商户分润通知", "您有一笔" + merchantOrder.getShareMoney() + "元的商户分润", "0");
////						JpushClientUtil.sendToRegistrationId2(alias, "商户分润通知", "商户分润通知","您有一笔" + merchantOrder.getShareMoney() + "元的商户分润",
////								"0", DateUtils.currentYourDate("yyyy-MM-dd HH:mm:ss"));
//					}
//				}
//			}
//		}
//		return merchantOrder;
	}
	
	
	
	/**
	 * 
	 * merchantIncome:(添加收益记录). <br/> 
	 * 
	 * @author Administrator  
	 * @since JDK 1.8
	 */
	private void merchantIncome(MerchantOrder merchantOrder,String payTypeCode){
		MerchantIncome merchantIncome=new MerchantIncome();
		if("1".equals(payTypeCode)) {
			merchantIncome.setMerchantCode(merchantOrder.getMerchantCode());
			merchantIncome.setMerchantName(merchantOrder.getMerchantName());
			merchantIncome.setStoreId(merchantOrder.getStoreId());
			merchantIncome.setStoreName(merchantOrder.getStoreName());
			merchantIncome.setDealFee(merchantOrder.getConsumeFee());
			merchantIncome.setIncomeFee(merchantOrder.getEnterFee());
			merchantIncome.setPayTypeCode(payTypeCode);
			merchantIncome.setPayTypeName("微信");
			merchantIncome.setCreateTime(merchantOrder.getTransDt());
			merchantIncomeService.saveMerchantIncome(merchantIncome);
		}else if("0".equals(payTypeCode)) {
			merchantIncome.setMerchantCode(merchantOrder.getMerchantCode());
			merchantIncome.setMerchantName(merchantOrder.getMerchantName());
			merchantIncome.setStoreId(merchantOrder.getStoreId());
			merchantIncome.setStoreName(merchantOrder.getStoreName());
			merchantIncome.setDealFee(merchantOrder.getConsumeFee());
			merchantIncome.setIncomeFee(merchantOrder.getEnterFee());
			merchantIncome.setPayTypeCode(payTypeCode);
			merchantIncome.setPayTypeName("支付宝");
			merchantIncome.setCreateTime(merchantOrder.getTransDt());
			merchantIncomeService.saveMerchantIncome(merchantIncome);
		}
		
	}
	
	public AlipayClient getAlipayClient() {
		return new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AliPayParam.getAppId(),AliPayParam.getPri(),"json","UTF-8",AliPayParam.getPub(),"RSA2");
	}
	
	/**
	 * 
	 * getUserApp:(获取userApp 并得到对应的token). <br/> 
	 * 
	 * @author Administrator 
	 * @param userId
	 * @return 
	 * @since JDK 1.8
	 */
	public UserApp getUserApp(String userId) {
		UserApp userApp=new UserApp();
		userApp.setUserId(Long.valueOf(userId));
		List<UserApp> queryUserAppList = userAppService.queryUserAppList(userApp);
		if(queryUserAppList!=null&&queryUserAppList.size()>0) {
			userApp = queryUserAppList.get(0);
		}
		return userApp;
	}

}

