admin
2019-11-16 a6f34b99ee20e528b766c8ed5a61bd9c5aa0e7ae
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.yeshi.fanli.util;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.dto.HongBao;
import com.yeshi.fanli.dto.order.OrderItem;
import com.yeshi.fanli.entity.bus.user.HongBaoExtra;
import com.yeshi.fanli.entity.taobao.OrderItemHongBao;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaobaoOrderHongBao;
import com.yeshi.fanli.log.LogHelper;
 
public class HongBaoUtil {
 
    private static final Map<Integer, String> titleMap = new HashMap<Integer, String>();
    private static final Map<Integer, String> infoMap = new HashMap<Integer, String>();
 
    static {
        titleMap.put(1, "hongbao_type_1_title");
        titleMap.put(2, "hongbao_type_2_title");
        titleMap.put(3, "hongbao_type_3_title");
        titleMap.put(4, "hongbao_type_4_title");
        titleMap.put(5, "hongbao_type_5_title");
        titleMap.put(6, "hongbao_type_6_title");
        titleMap.put(7, "hongbao_type_7_title");
        titleMap.put(20, "hongbao_type_20_title");
        titleMap.put(21, "hongbao_type_21_title");
        titleMap.put(22, "hongbao_type_22_title");
 
        infoMap.put(1, "hongbao_type_1_info");
        infoMap.put(2, "hongbao_type_2_info");
        infoMap.put(3, "hongbao_type_3_info");
        infoMap.put(4, "hongbao_type_4_info");
        infoMap.put(5, "hongbao_type_5_info");
        infoMap.put(6, "hongbao_type_6_info");
        infoMap.put(7, "hongbao_type_7_info");
        infoMap.put(20, "hongbao_type_20_info");
        infoMap.put(21, "hongbao_type_21_info");
        infoMap.put(22, "hongbao_type_22_info");
    }
 
    public static String getHongBaoTitle(int type) {
        Map<String, String> map = PropertiesUtil.getMap();
        String title = map.get(titleMap.get(type));
        if (title == null) {
            title = map.get(titleMap.get(Constant.HB_HUODONG));
        }
        return title;
    }
 
    public static String getHongBaoInfo(int type) {
        Map<String, String> map = PropertiesUtil.getMap();
        String info = map.get(infoMap.get(type));
        if (info == null) {
            info = map.get(infoMap.get(Constant.HB_HUODONG));
        }
        return info;
    }
 
    /**
     * 转换成包装类
     * 
     * @param hongBao
     * @return
     */
    public static HongBaoExtra convert(HongBao hongBao) {
        HongBaoExtra hbx = new HongBaoExtra(hongBao);
        String param = hongBao.getParam();
        if (param != null) {
            Map<String, String> map = JsonUtil.parseData(param);
            if (map != null) {
                String picture = map.get("picture");
                if (picture != null) {
                    hbx.setPicture(picture);
                } else {
                    hbx.setPicture("");
                }
            } else {
                hbx.setPicture("");
            }
        }
        hbx.setTitle(getHongBaoTitle(hongBao.getType()));
        hbx.setInfo(getHongBaoInfo(hongBao.getType()));
        return hbx;
    }
 
    public static String convertState(int state) {
        if (state == 1 || state == 2) {
            return "未到账";
        } else if (state == 3) {
            return "已到账";
        } else if (state == 4) {
            return "已失效";
        } else {
            return "状态不知";
        }
    }
 
    public static List<OrderItemHongBao> sortHongBaoAndOrderItem(List<OrderItem> orderItemList,
            List<HongBao> hongBaoList) {
        if (orderItemList.size() != hongBaoList.size())
            return null;
 
        Comparator<OrderItem> orderCM = new Comparator<OrderItem>() {
 
            @Override
            public int compare(OrderItem o1, OrderItem o2) {
                return o1.getAuctionId().longValue() == o2.getAuctionId() ? o1.getPayMoney().compareTo(o2.getPayMoney())
                        : (int) ((o1.getAuctionId() - o2.getAuctionId())
                                / Math.abs(o1.getAuctionId() - o2.getAuctionId()));
            }
        };
 
        Comparator<HongBao> hongBaoCM = new Comparator<HongBao>() {
 
            @Override
            public int compare(HongBao o1, HongBao o2) {
                return o1.getAuctionId().longValue() == o2.getAuctionId() ? o1.getPayMoney().compareTo(o2.getPayMoney())
                        : (int) ((o1.getAuctionId() - o2.getAuctionId())
                                / Math.abs(o1.getAuctionId() - o2.getAuctionId()));
            }
        };
 
        Collections.sort(orderItemList, orderCM);
        Collections.sort(hongBaoList, hongBaoCM);
 
        List<OrderItemHongBao> list = new ArrayList<>();
        for (int i = 0; i < orderItemList.size(); i++) {
            list.add(new OrderItemHongBao(hongBaoList.get(i), orderItemList.get(i)));
        }
        return list;
    }
 
    public static List<TaobaoOrderHongBao> sortHongBaoAndTaoBaoOrder(List<TaoBaoOrder> orderList,
            List<HongBao> hongBaoList) {
        if (orderList.size() != hongBaoList.size())
            return null;
        List<TaoBaoOrder> validTaoBaoOrderList = new ArrayList<>();
        List<HongBao> validHongBaoList = new ArrayList<>();
        for (int i = 0; i < orderList.size(); i++) {
            TaoBaoOrder taoBaoOrder = orderList.get(i);
            if (!taoBaoOrder.getOrderState().equalsIgnoreCase("订单失效")) {
                validTaoBaoOrderList.add(taoBaoOrder);
                orderList.remove(i);
                i--;
            }
        }
 
        for (int i = 0; i < validTaoBaoOrderList.size(); i++) {
            TaoBaoOrder taoBaoOrder = validTaoBaoOrderList.get(i);
            for (int n = 0; n < hongBaoList.size(); n++) {
                HongBao hongBao = hongBaoList.get(n);
 
                if (taoBaoOrder.getOrderState().equalsIgnoreCase("订单结算")) {
                    if (taoBaoOrder.getAuctionId().longValue() == hongBao.getAuctionId()
                            &&( taoBaoOrder.getSettlement().compareTo(hongBao.getPayMoney()) == 0||taoBaoOrder.getPayment().compareTo(hongBao.getPayMoney()) == 0)) {
                        validHongBaoList.add(hongBao);
                        hongBaoList.remove(n);
                        break;
                    }
                }
 
                else {
 
                    if (taoBaoOrder.getAuctionId().longValue() == hongBao.getAuctionId()
                            && taoBaoOrder.getPayment().compareTo(hongBao.getPayMoney()) == 0) {
                        validHongBaoList.add(hongBao);
                        hongBaoList.remove(n);
                        break;
                    }
                }
            }
        }
 
        if (validTaoBaoOrderList.size() != validHongBaoList.size()) {
            LogHelper.test("淘宝订单与红包数量不对应");
            return null;
        }
 
        // 先排序尚未失效的商品
 
        Comparator<TaoBaoOrder> orderCM = new Comparator<TaoBaoOrder>() {
 
            @Override
            public int compare(TaoBaoOrder o1, TaoBaoOrder o2) {
                return o1.getAuctionId().longValue() == o2.getAuctionId() ? o1.getPayment().compareTo(o2.getPayment())
                        : (int) ((o1.getAuctionId() - o2.getAuctionId())
                                / Math.abs(o1.getAuctionId() - o2.getAuctionId()));
            }
        };
 
        Comparator<HongBao> hongBaoCM = new Comparator<HongBao>() {
 
            @Override
            public int compare(HongBao o1, HongBao o2) {
                return o1.getAuctionId().longValue() == o2.getAuctionId() ? o1.getPayMoney().compareTo(o2.getPayMoney())
                        : (int) ((o1.getAuctionId() - o2.getAuctionId())
                                / Math.abs(o1.getAuctionId() - o2.getAuctionId()));
            }
        };
 
        Collections.sort(validTaoBaoOrderList, orderCM);
        Collections.sort(validHongBaoList, hongBaoCM);
 
        // 加入剩下的
 
        // orderCM = new Comparator<TaoBaoOrder>() {
        //
        // @Override
        // public int compare(TaoBaoOrder o1, TaoBaoOrder o2) {
        //
        // if (o1.getAuctionId() - o2.getAuctionId() != 0)
        // return (int) ((o1.getAuctionId() - o2.getAuctionId())
        // / Math.abs(o1.getAuctionId() - o2.getAuctionId()));
        // else
        // o1.getAuctionId()
        // }
        // };
        //
        // hongBaoCM = new Comparator<HongBao>() {
        // @Override
        // public int compare(HongBao o1, HongBao o2) {
        // return (int) ((o1.getAuctionId() - o2.getAuctionId())
        // / Math.abs(o1.getAuctionId() - o2.getAuctionId()));
        // }
        // };
 
        if (orderList.size() > 0) {
            Collections.sort(orderList, orderCM);
            validTaoBaoOrderList.addAll(orderList);
        }
 
        if (hongBaoList.size() > 0) {
            Collections.sort(hongBaoList, hongBaoCM);
            validHongBaoList.addAll(hongBaoList);
        }
        List<TaobaoOrderHongBao> list = new ArrayList<>();
        for (int i = 0; i < validHongBaoList.size(); i++) {
            list.add(new TaobaoOrderHongBao(validHongBaoList.get(i), validTaoBaoOrderList.get(i)));
        }
        return list;
    }
 
    public static List<OrderItem> convertToTiChengOrder(List<HongBao> hongBaoList) {
        List<OrderItem> orderItemList = new ArrayList<>();
        if (hongBaoList != null)
            for (HongBao hb : hongBaoList) {
                OrderItem orderItem = new OrderItem();
                orderItem.setTitle("奖金订单商品");
                if (hb.getParent() != null) {
                    orderItem.setPayMoney(hb.getParent().getPayMoney());
                    orderItem.setOrderId(hb.getParent().getOrderId());
                } else {
                    orderItem.setPayMoney(hb.getPayMoney());
                    orderItem.setOrderId(hb.getOrderId());
                }
 
                if (!StringUtil.isNullOrEmpty(orderItem.getOrderId())) {
                    orderItem.setOrderId(
                            orderItem.getOrderId().substring(0, orderItem.getOrderId().length() - 6) + "******");
                }
 
                orderItem.setFanMoney(hb.getMoney());
 
                if (hb.getState() == HongBao.STATE_BUKELINGQU || hb.getState() == HongBao.STATE_KELINGQU) {
                    orderItem.setDesc("订单确认收货的次月25日后到账");
                    orderItem.setStateDesc("未到账");
 
                } else if (hb.getState() == HongBao.STATE_YILINGQU) {
 
                    orderItem.setDesc("已自动转入你的余额");
                    orderItem.setStateDesc("已到账");
 
                } else if (hb.getState() == HongBao.STATE_SHIXIAO) {
                    if (hb.getParent() != null && hb.getParent().getBalanceTime() != null) {
                        orderItem.setDesc("售后订单无奖金");
                        orderItem.setStateDesc("已售后");
                    } else {
                        orderItem.setDesc("退款订单无奖金");
                        orderItem.setStateDesc("已退款");
                    }
                }
 
                orderItem.setCreateTime(hb.getCreatetime());
                orderItem.setState(hb.getState());
                orderItem.setPicture(Constant.systemCommonConfig.getDefaultOrderPicture());
 
                orderItemList.add(orderItem);
            }
        return orderItemList;
    }
 
}