//package cn.pluss.platform.controller.job;
//
//import java.util.Date;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import com.alipay.api.AlipayApiException;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.DefaultAlipayClient;
//import com.alipay.api.request.AlipayTradeQueryRequest;
//import com.alipay.api.response.AlipayTradeQueryResponse;
//
//import cn.pluss.platform.entity.MerchantOrder;
//import cn.pluss.platform.entity.UserApp;
//import cn.pluss.platform.wap.HttpUrlConnection;
//import cn.pluss.platform.wap.WxPaySignatureUtils;
//import cn.pluss.platform.wap.XmlUtil;
//import cn.pluss.platform.merchantOrder.MerchantOrderService;
//import cn.pluss.platform.systemConfig.SystemConfigService;
//import cn.pluss.platform.userApp.UserAppService;
//import cn.pluss.platform.util.DateUtils;
//import cn.pluss.platform.util.StringUtil;
//import net.sf.json.JSONObject;
//
///**
// * @author yuchen
// *
// * @Description 
// * 定时任务查订单状态为等待付款的订单
// */
//@Component
//@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
//public class SaticScheduleTask {
//	
//	@Autowired
//	private MerchantOrderService merchantOrderService;
//	
//	@Autowired
//	private SystemConfigService systemConfigService;
//	
//	@Autowired
//	private UserAppService userAppService;
//	
////    @Scheduled(fixedRate=1000*60*30)
//    private void configureTasks() throws AlipayApiException {
//    	MerchantOrder merchantOrder=new MerchantOrder();
//    	merchantOrder.setStatus("5");
//    	String endTime = DateUtils.formatDateDefault(new Date(),"yyyy-MM-dd 23:59:59");
//    	String startTime = DateUtils.getDay(endTime, (int)0);
//    	merchantOrder.setStartTime(startTime);
//    	merchantOrder.setEndTime(endTime);
//		List<MerchantOrder> queryMerchantOrderList = merchantOrderService.queryMerchantOrderList(merchantOrder);
//		for (MerchantOrder m : queryMerchantOrderList) {
//			merchantOrder.setId(m.getId());
//			if("wechatPay".equalsIgnoreCase(m.getPayTypeCode())) {
//				getwechatOrderQuery(merchantOrder);
//			}else if("aliPay".equalsIgnoreCase(m.getPayTypeCode())) {
//				getaliPayOrderQuery(merchantOrder);
//			}
//		}
//    }
//    
//    /**
//     * getaliPayOrderQuery:(查询支付宝订单 若trade_status=TRADE_SUCCESS 修改订单). <br/> 
//     *
//     * @author Administrator  
//     * @throws AlipayApiException 
//     * @since JDK 1.8
//     */
//	private void getaliPayOrderQuery(MerchantOrder merchantOrder) throws AlipayApiException {
//		UserApp userApp=new UserApp();
//		userApp.setMerchantCode(merchantOrder.getMerchantCode());
//		List<UserApp> queryUserAppList = userAppService.queryUserAppList(userApp);
//		if(queryUserAppList!=null&&queryUserAppList.size()>0) {
//			userApp = queryUserAppList.get(0);
//		}
//		if(userApp.getAppAuthToken()==null) {
//			return;
//		}
//		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",systemConfigService.querySystemConfigValue("ALI_APP_ID"),systemConfigService.querySystemConfigValue("PRI"),"json","GBK",systemConfigService.querySystemConfigValue("PUB"),"RSA2");
//		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
//		request.setBizContent("{" +
//				"\"out_trade_no\":\""+merchantOrder.getOrderNumber()+"\"" +
//			"  }");
//		AlipayTradeQueryResponse response = alipayClient.execute(request);
//		request.putOtherTextParam("app_auth_token", userApp.getAppAuthToken());
//		if(response.isSuccess()){
//			String body = response.getBody();
//			JSONObject object = (JSONObject) JSONObject.fromObject(body).get("alipay_trade_query_response");
//			String tradeStatus = (String) object.get("trade_status");
//			if("TRADE_SUCCESS".equals(tradeStatus)) {
//				merchantOrder.setStatus("0");
//				merchantOrderService.updateMerchantOrder(merchantOrder);
//			}
//		}
//	}
//    
//	/**
//	 * 
//	 * getwechatOrderQuery:(查询微信订单). <br/> 
//	 *
//	 * @author Administrator  
//	 * @since JDK 1.8
//	 */
//	private void getwechatOrderQuery(MerchantOrder merchantOrder) {
//		UserApp userApp=new UserApp();
//		userApp.setMerchantCode(merchantOrder.getMerchantCode());
//		List<UserApp> queryUserAppList = userAppService.queryUserAppList(userApp);
//		if(queryUserAppList!=null&&queryUserAppList.size()>0) {
//			userApp = queryUserAppList.get(0);
//		}
//		if(userApp.getSubMchId()==null) {
//			return;
//		}
//		Map<String, String> map=new HashMap<String, String>(16);
//		map.put("appid", systemConfigService.querySystemConfigValue("APPID"));
//		map.put("mch_id", systemConfigService.querySystemConfigValue("PID"));
//		map.put("sub_mch_id", userApp.getSubMchId());
//		map.put("out_trade_no", merchantOrder.getOrderNumber());
//		map.put("nonce_str", StringUtil.getBillno());
//		String WX_PAY_KEY = systemConfigService.querySystemConfigValue("KEY");
//		String sign = WxPaySignatureUtils.signature(map, WX_PAY_KEY);
//		StringBuffer xml=new StringBuffer();
//		xml.append("<xml>");
//		xml.append("<appid>"+ systemConfigService.querySystemConfigValue("APPID") +"</appid>");
//		xml.append("<mch_id>"+ systemConfigService.querySystemConfigValue("PID") +"</mch_id>");
//		xml.append("<nonce_str>"+ StringUtil.getBillno() +"</nonce_str>");
//		xml.append("<out_trade_no>"+ merchantOrder.getOrderNumber() +"</out_trade_no>");
//		xml.append("<sign>"+ sign +"</sign>");
//		xml.append("</xml>");
//		String httpXML = HttpUrlConnection.httpXML("https://api.mch.weixin.qq.com/pay/orderquery", xml.toString());
//		Map<String, Object> result = XmlUtil.toMap(httpXML);
//		if("SUCCESS".equals(result.get("return_code"))) {
//			String tradeState = (String) result.get("trade_state");
//			if("SUCCESS".equals(tradeState)){
//				merchantOrder.setStatus("0");
//				merchantOrderService.updateMerchantOrder(merchantOrder);
//			}
//		}
//	}
//
//}
//
