admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/util/taobao/TaoBaoCouponUtil.java
@@ -1,189 +1,189 @@
package com.yeshi.fanli.util.taobao;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import com.yeshi.fanli.entity.taobao.TaoBaoCoupon;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.util.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
public class TaoBaoCouponUtil {
   private final static String CAT = "天猫";
   private final static String TAOBAO = "淘宝";
   public static Set<TaoBaoCoupon> getTaoBaoCouponList(TaoBaoUnionConfig config) {
      String cookie = TaoBaoOrderUtil.getTaoBaoCookie(config.getAccount());
      if (com.yeshi.fanli.util.StringUtil.isNullOrEmpty(cookie)) {
         return null;
      }
      HttpClient client = new HttpClient();
      String siteId = config.getDefaultPid().split("_")[2];
      String adzoneId = config.getDefaultPid().split("_")[3];
      String url = "http://pub.alimama.com/coupon/qq/export.json?adzoneId=" + adzoneId + "&siteId=" + siteId;
      GetMethod method = new GetMethod(url);
      method.setRequestHeader("Cookie", cookie);
      method.setRequestHeader("User-Agent",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
      method.setRequestHeader("Referer", "http://pub.alimama.com/myunion.htm");
      client.setParams(new HttpClientParams());
      InputStream inputStream = null;
      try {
         client.executeMethod(method);
         if (method.getStatusCode() == 200) {
            System.out.println(method.getResponseHeader("Content-Type").getValue());
            if (!method.getResponseHeader("Content-Type").getValue().contains("excel")) {
               TaoBaoOrderUtil.reportCookieInvalid(config.getAccount());
               return null;
            }
            inputStream = method.getResponseBodyAsStream();
            return parseTaoBaoCoupon(inputStream);
         }
      } catch (HttpException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      return null;
   }
   private static Set<TaoBaoCoupon> parseTaoBaoCoupon(InputStream inputStream) {
      jxl.Workbook rwb = null;
      try {
         rwb = Workbook.getWorkbook(inputStream);
      } catch (BiffException e) {
         return null;
      } catch (IOException e) {
         return null;
      }
      Sheet sheet = rwb.getSheet(0);
      Set<TaoBaoCoupon> set = new HashSet<TaoBaoCoupon>();
      TaoBaoCoupon taoBaoCoupon = null;
      int rows = sheet.getRows();
      for (int r = 1; r < rows; r++) {
         taoBaoCoupon = new TaoBaoCoupon();
         taoBaoCoupon.setAuctionId(sheet.getCell(0, r).getContents().trim());
         taoBaoCoupon.setTitle(sheet.getCell(1, r).getContents().trim());
         taoBaoCoupon.setPictUrl(sheet.getCell(2, r).getContents().trim());
         taoBaoCoupon.setAuctionUrl(sheet.getCell(3, r).getContents().trim());
         taoBaoCoupon.setClassNames(sheet.getCell(4, r).getContents().trim());
         // taoBaoCoupon.setTaobaokeUrl(sheet.getCell(5,
         // r).getContents().trim());
         String zkStr = sheet.getCell(6, r).getContents().trim();
         String bizStr = sheet.getCell(7, r).getContents().trim();
         taoBaoCoupon.setShopWangWang(sheet.getCell(10, r).getContents().trim());
         taoBaoCoupon.setShopTitle(sheet.getCell(12, r).getContents().trim());
         String typeStr = sheet.getCell(13, r).getContents().trim();
         int type = 0;
         if (TAOBAO.equals(typeStr)) {
            type = 1;
         } else if (CAT.equals(typeStr)) {
            type = 2;
         }
         taoBaoCoupon.setShopType(type);
         taoBaoCoupon.setCouponId(sheet.getCell(14, r).getContents().trim());
         String info = sheet.getCell(17, r).getContents().trim();
         List<BigDecimal> arr = getCouponInfo(info);
         taoBaoCoupon.setCouponinfo(info);
         taoBaoCoupon.setCouponStartFee(arr.get(0));
         taoBaoCoupon.setCouponAmount(arr.get(1));
         taoBaoCoupon.setCouponBegin(sheet.getCell(18, r).getContents().trim());
         taoBaoCoupon.setCouponEnd(sheet.getCell(19, r).getContents().trim());
         taoBaoCoupon.setCouponLink(sheet.getCell(20, r).getContents().trim());
         // taoBaoCoupon.setGeneralizeUrl(sheet.getCell(21,
         // r).getContents().trim());
         taoBaoCoupon.setCreatetime(System.currentTimeMillis());
         try {
            taoBaoCoupon.setZkPrice(new BigDecimal(zkStr));
            taoBaoCoupon.setBiz30day(Integer.parseInt(bizStr));
            taoBaoCoupon.setTkRate(Double.parseDouble(sheet.getCell(8, r).getContents().trim()));
            taoBaoCoupon.setBrokerage(new BigDecimal(sheet.getCell(9, r).getContents().trim()));
            taoBaoCoupon.setSellerId(Long.parseLong(sheet.getCell(11, r).getContents().trim()));
            taoBaoCoupon.setCouponSum(Integer.parseInt(sheet.getCell(15, r).getContents().trim()));
            taoBaoCoupon.setCouponCount(Integer.parseInt(sheet.getCell(16, r).getContents().trim()));
         } catch (NumberFormatException e) {
            e.printStackTrace();
            continue;
         }
         if (MoneyBigDecimalUtil.sub(taoBaoCoupon.getZkPrice(), taoBaoCoupon.getCouponStartFee())
               .doubleValue() >= 0) {
            taoBaoCoupon.setQuanPrice(
                  MoneyBigDecimalUtil.sub(taoBaoCoupon.getZkPrice(), taoBaoCoupon.getCouponAmount()));
            taoBaoCoupon.setShowType(3);
         } else {
            taoBaoCoupon.setQuanPrice(taoBaoCoupon.getZkPrice());
            taoBaoCoupon.setShowType(2);
         }
         taoBaoCoupon.setOrderby(10000);
         set.add(taoBaoCoupon);
      }
      return set;
   }
   public static List<BigDecimal> getCouponInfo(String info) {
      Pattern p = Pattern.compile("满\\d+(\\.\\d+)?元减\\d+(\\.\\d+)?元");
      Matcher matcher = p.matcher(info);
      Pattern p2 = Pattern.compile("\\d+元无条件券");
      Matcher matcher2 = p2.matcher(info);
      List<BigDecimal> list = new ArrayList<BigDecimal>();
      if (matcher.matches()) {
         String[] sts = info.split("元减");
         list.add(new BigDecimal(sts[0].replace("满", "").trim()));
         list.add(new BigDecimal(sts[1].replace("元", "").trim()));
      } else if (matcher2.matches()) {
         String[] split = info.split("元无条件券");
         list.add(new BigDecimal(0));
         list.add(new BigDecimal(split[0]));
      } else {
         list.add(new BigDecimal(0));
         list.add(new BigDecimal(0));
      }
      return list;
   }
   /**
    * 获取领券链接
    *
    * @return
    */
   public static String getCoupleUrl(String activityId, String pid, String auctionId) {
      if (!StringUtil.isNullOrEmpty(pid))
         return String.format("https://uland.taobao.com/coupon/edetail?activityId=%s&pid=%s&itemId=%s", activityId,
               pid, auctionId);
      else
         return String.format("https://uland.taobao.com/coupon/edetail?activityId=%s&itemId=%s", activityId,
               auctionId);
   }
}
package com.yeshi.fanli.util.taobao;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import com.yeshi.fanli.entity.taobao.TaoBaoCoupon;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import org.yeshi.utils.MoneyBigDecimalUtil;
import com.yeshi.fanli.util.StringUtil;
public class TaoBaoCouponUtil {
   private final static String CAT = "天猫";
   private final static String TAOBAO = "淘宝";
   public static Set<TaoBaoCoupon> getTaoBaoCouponList(TaoBaoUnionConfig config) {
      String cookie = TaoBaoOrderUtil.getTaoBaoCookie(config.getAccount());
      if (com.yeshi.fanli.util.StringUtil.isNullOrEmpty(cookie)) {
         return null;
      }
      HttpClient client = new HttpClient();
      String siteId = config.getDefaultPid().split("_")[2];
      String adzoneId = config.getDefaultPid().split("_")[3];
      String url = "http://pub.alimama.com/coupon/qq/export.json?adzoneId=" + adzoneId + "&siteId=" + siteId;
      GetMethod method = new GetMethod(url);
      method.setRequestHeader("Cookie", cookie);
      method.setRequestHeader("User-Agent",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
      method.setRequestHeader("Referer", "http://pub.alimama.com/myunion.htm");
      client.setParams(new HttpClientParams());
      InputStream inputStream = null;
      try {
         client.executeMethod(method);
         if (method.getStatusCode() == 200) {
            System.out.println(method.getResponseHeader("Content-Type").getValue());
            if (!method.getResponseHeader("Content-Type").getValue().contains("excel")) {
               TaoBaoOrderUtil.reportCookieInvalid(config.getAccount());
               return null;
            }
            inputStream = method.getResponseBodyAsStream();
            return parseTaoBaoCoupon(inputStream);
         }
      } catch (HttpException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
      return null;
   }
   private static Set<TaoBaoCoupon> parseTaoBaoCoupon(InputStream inputStream) {
      jxl.Workbook rwb = null;
      try {
         rwb = Workbook.getWorkbook(inputStream);
      } catch (BiffException e) {
         return null;
      } catch (IOException e) {
         return null;
      }
      Sheet sheet = rwb.getSheet(0);
      Set<TaoBaoCoupon> set = new HashSet<TaoBaoCoupon>();
      TaoBaoCoupon taoBaoCoupon = null;
      int rows = sheet.getRows();
      for (int r = 1; r < rows; r++) {
         taoBaoCoupon = new TaoBaoCoupon();
         taoBaoCoupon.setAuctionId(sheet.getCell(0, r).getContents().trim());
         taoBaoCoupon.setTitle(sheet.getCell(1, r).getContents().trim());
         taoBaoCoupon.setPictUrl(sheet.getCell(2, r).getContents().trim());
         taoBaoCoupon.setAuctionUrl(sheet.getCell(3, r).getContents().trim());
         taoBaoCoupon.setClassNames(sheet.getCell(4, r).getContents().trim());
         // taoBaoCoupon.setTaobaokeUrl(sheet.getCell(5,
         // r).getContents().trim());
         String zkStr = sheet.getCell(6, r).getContents().trim();
         String bizStr = sheet.getCell(7, r).getContents().trim();
         taoBaoCoupon.setShopWangWang(sheet.getCell(10, r).getContents().trim());
         taoBaoCoupon.setShopTitle(sheet.getCell(12, r).getContents().trim());
         String typeStr = sheet.getCell(13, r).getContents().trim();
         int type = 0;
         if (TAOBAO.equals(typeStr)) {
            type = 1;
         } else if (CAT.equals(typeStr)) {
            type = 2;
         }
         taoBaoCoupon.setShopType(type);
         taoBaoCoupon.setCouponId(sheet.getCell(14, r).getContents().trim());
         String info = sheet.getCell(17, r).getContents().trim();
         List<BigDecimal> arr = getCouponInfo(info);
         taoBaoCoupon.setCouponinfo(info);
         taoBaoCoupon.setCouponStartFee(arr.get(0));
         taoBaoCoupon.setCouponAmount(arr.get(1));
         taoBaoCoupon.setCouponBegin(sheet.getCell(18, r).getContents().trim());
         taoBaoCoupon.setCouponEnd(sheet.getCell(19, r).getContents().trim());
         taoBaoCoupon.setCouponLink(sheet.getCell(20, r).getContents().trim());
         // taoBaoCoupon.setGeneralizeUrl(sheet.getCell(21,
         // r).getContents().trim());
         taoBaoCoupon.setCreatetime(System.currentTimeMillis());
         try {
            taoBaoCoupon.setZkPrice(new BigDecimal(zkStr));
            taoBaoCoupon.setBiz30day(Integer.parseInt(bizStr));
            taoBaoCoupon.setTkRate(Double.parseDouble(sheet.getCell(8, r).getContents().trim()));
            taoBaoCoupon.setBrokerage(new BigDecimal(sheet.getCell(9, r).getContents().trim()));
            taoBaoCoupon.setSellerId(Long.parseLong(sheet.getCell(11, r).getContents().trim()));
            taoBaoCoupon.setCouponSum(Integer.parseInt(sheet.getCell(15, r).getContents().trim()));
            taoBaoCoupon.setCouponCount(Integer.parseInt(sheet.getCell(16, r).getContents().trim()));
         } catch (NumberFormatException e) {
            e.printStackTrace();
            continue;
         }
         if (MoneyBigDecimalUtil.sub(taoBaoCoupon.getZkPrice(), taoBaoCoupon.getCouponStartFee())
               .doubleValue() >= 0) {
            taoBaoCoupon.setQuanPrice(
                  MoneyBigDecimalUtil.sub(taoBaoCoupon.getZkPrice(), taoBaoCoupon.getCouponAmount()));
            taoBaoCoupon.setShowType(3);
         } else {
            taoBaoCoupon.setQuanPrice(taoBaoCoupon.getZkPrice());
            taoBaoCoupon.setShowType(2);
         }
         taoBaoCoupon.setOrderby(10000);
         set.add(taoBaoCoupon);
      }
      return set;
   }
   public static List<BigDecimal> getCouponInfo(String info) {
      Pattern p = Pattern.compile("满\\d+(\\.\\d+)?元减\\d+(\\.\\d+)?元");
      Matcher matcher = p.matcher(info);
      Pattern p2 = Pattern.compile("\\d+元无条件券");
      Matcher matcher2 = p2.matcher(info);
      List<BigDecimal> list = new ArrayList<BigDecimal>();
      if (matcher.matches()) {
         String[] sts = info.split("元减");
         list.add(new BigDecimal(sts[0].replace("满", "").trim()));
         list.add(new BigDecimal(sts[1].replace("元", "").trim()));
      } else if (matcher2.matches()) {
         String[] split = info.split("元无条件券");
         list.add(new BigDecimal(0));
         list.add(new BigDecimal(split[0]));
      } else {
         list.add(new BigDecimal(0));
         list.add(new BigDecimal(0));
      }
      return list;
   }
   /**
    * 获取领券链接
    *
    * @return
    */
   public static String getCoupleUrl(String activityId, String pid, String auctionId) {
      if (!StringUtil.isNullOrEmpty(pid))
         return String.format("https://uland.taobao.com/coupon/edetail?activityId=%s&pid=%s&itemId=%s", activityId,
               pid, auctionId);
      else
         return String.format("https://uland.taobao.com/coupon/edetail?activityId=%s&itemId=%s", activityId,
               auctionId);
   }
}