package com.gxwebsoft.shop.task;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.google.gson.Gson;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.shop.config.ExpressConfig;
import com.gxwebsoft.shop.entity.Cart;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.param.CartParam;
import com.gxwebsoft.shop.param.OrderParam;
import com.gxwebsoft.shop.service.OrderService;
import lombok.val;
import lombok.var;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Component
public class WxPayTask {

  @Resource
  private OrderService orderService;
  @Resource
  private ExpressConfig expressConfig;

  /**
   * 秒 分 时 日 月 周
   * *：没秒都执行
   * 1-3: 从第一秒开始，到第三秒执行
   * 0/3: 从第0秒开始，每隔3秒执行一次
   * 1,2,3: 在指定的第几秒执行
   * ?: 不指定
   * 日和周不能同时指定，指定其中之一，则另一个设置为?
   */
//  @Scheduled(cron = "0/5 * * * * ?")
//  public void task1(){
//    System.out.println("taso1 被执行");
//    JSONObject json = new JSONObject();
//    json.put("dev_id",expressConfig.getDevId());
//    json.put("shop_id",expressConfig.getShopId());
//    json.put("shop_order_id","ABCD1234567890");
//
//    String sign = getSign(json.toString(),expressConfig.getDevId(),expressConfig.getDevKey());
//    String serverHost = expressConfig.getServerHost().concat("/open/api/external/createorder?sign=").concat(sign);
//    System.out.println(sign);
//    System.out.println(serverHost);
//    HttpPost httpPost = new HttpPost(serverHost);
//    httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
//  }

  private String httpPost(String url,List list) {
    // 创建HttpClient
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    CloseableHttpClient httpClient = httpClientBuilder.build();
    // 创建Post请求
    HttpPost post = new HttpPost(url);
    //发送post请求  //发票信息采集
    CloseableHttpResponse response = null;
    String content = "";
    try {
      post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
      response = httpClient.execute(post);
      //获取请求模型返回的实体
      HttpEntity responseEntity = response.getEntity();
      content = EntityUtils.toString(responseEntity,"UTF-8");
    } catch (Exception e) {
      e.printStackTrace();
    }

    return content;
  }


  public String getSign(String postData, Long appId, String appKey) {
    StringBuilder sb =  new StringBuilder();
    sb.append(postData);
    sb.append("&" + appId + "&" + appKey);
    System.out.println("sb:===");
    System.out.println(sb);
    MessageDigest md = null;
    try {
      md = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
    byte[] md5 = new byte[0];
    try {
      md5 = md.digest(sb.toString().getBytes("utf-8"));
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    int i;
    StringBuffer buf = new StringBuffer("");
    for (int offset = 0; offset < md5.length; offset++) {
      i = md5[offset];
      if (i < 0) {
        i += 256;
      }
      if (i < 16) {
        buf.append("0");
      }
      buf.append(Integer.toHexString(i));
    }

    String ret = null;
    try {
      ret = Base64.encodeBase64String(buf.toString().getBytes("utf-8"));
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return  ret;
  }
}
