admin
2019-07-30 573c491b4a1ba60e12a5678a01c1546c0077c1ee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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);
    }
 
}