Administrator
2018-10-30 c0c91fbda1ba601c4c8cb6d12fd43dfbe02eea5d
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.yeshi.fanli.controller.admin;
 
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import net.sf.json.JSONObject;
 
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yeshi.fanli.entity.admin.ReslutOrder;
import com.yeshi.fanli.entity.bus.user.HongBao;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.service.inter.count.TaoBaoOrderCountService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
import org.yeshi.utils.JsonUtil;
 
@Controller
@RequestMapping("admin/new/api/v1/taoBaoOrder")
public class TaoBaoOrderAdminController {
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
 
    @Resource
    private TaoBaoOrderCountService taoBaoOrderCountService;
 
    @Resource
    private HongBaoService hongBaoService;
 
    @Resource
    private RedisManager redisManager;
    @Resource
    private UserInfoService userInfoService;
 
    @RequestMapping(value = "queryJoinHongBao")
    public void queryJoinHongBao(String callback, Integer pageIndex, Integer pageSize, String key, String startTime, String endTime, Integer type, Integer goodstype, Integer days, PrintWriter out) {
        try {
 
            if (pageSize == null)
                pageSize = Constant.PAGE_SIZE;
 
            if (pageIndex == null)
                pageIndex = 1;
 
            if (!StringUtil.isNullOrEmpty(endTime)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date enddate = sdf.parse(endTime);
                Calendar c = Calendar.getInstance();
                c.setTime(enddate);
                c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
                endTime = sdf.format(c.getTime());
            }
 
            List<ReslutOrder> reslutOrders = taoBaoOrderService.queryJoinHongBao((pageIndex - 1) * pageSize, pageSize, key, startTime, endTime, type, days);
 
            /* 暂无数据显示 返回消息 */
            if (reslutOrders == null || reslutOrders.size() == 0) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("暂无数据"));
                return;
            }
 
            /* 验证网络: */
            StringBuffer auctionIdBuf = new StringBuffer();
            for (ReslutOrder reslutOrder : reslutOrders) {
                Long auctionId = reslutOrder.getAuctionId();
                if (auctionId != null) {
                    auctionIdBuf.append(auctionId + ",");
                }
            }
 
            /* 验证网络: 获取商品链接 图片链接 */
            List<TaoBaoGoodsBrief> goodsBriefList = null;
            if (auctionIdBuf != null && auctionIdBuf.length() > 0) {
                String auctionIds = auctionIdBuf.toString();
 
                String ids = auctionIds.substring(0, auctionIds.length() - 1);
                goodsBriefList = TaoKeApiUtil.getBatchGoodsInfos(ids);
            }
 
            for (ReslutOrder reslutOrder : reslutOrders) {
 
                String orderId = reslutOrder.getOrderId();
                BigDecimal payment = reslutOrder.getPayment();
 
                String orderState = reslutOrder.getOrderState();
                if ("订单结算".equals(orderState)) {
                    reslutOrder.setOrderStateAdmin("1");
                } else if ("订单付款".equals(orderState)) {
                    reslutOrder.setOrderStateAdmin("2");
                } else if ("订单失效".equals(orderState)) {
                    reslutOrder.setOrderStateAdmin("3");
                }
 
                /* 网上爬取商品 图片、链接 */
                Long auctionId = reslutOrder.getAuctionId();
 
                if (goodsBriefList != null && goodsBriefList.size() > 0) {
                    for (TaoBaoGoodsBrief taoBaoGoodsBrief : goodsBriefList) {
                        Long auctionIdTB = taoBaoGoodsBrief.getAuctionId();
                        if (auctionId.equals(auctionIdTB)) {
                            reslutOrder.setGoodsStae("0");// 在售
                            String auctionUrl = taoBaoGoodsBrief.getAuctionUrl();
                            String pictUrl = taoBaoGoodsBrief.getPictUrl();
                            String shopTitle = taoBaoGoodsBrief.getShopTitle();
                            reslutOrder.setShopTitle(shopTitle);
                            reslutOrder.setAuctionUrl(auctionUrl);
                            reslutOrder.setPictUrl(pictUrl);
                        }
                    }
                } else {
                    reslutOrder.setGoodsStae("1");// 停售
                }
 
                String goodsStae = reslutOrder.getGoodsStae();
                if (StringUtil.isNullOrEmpty(goodsStae)) {
                    reslutOrder.setGoodsStae("1");// 停售
                }
 
                UserInfo userInfo = reslutOrder.getUserInfo();
                if (userInfo == null) {
                    userInfo = new UserInfo();
                    reslutOrder.setUserInfo(userInfo);
                } else {
                    Long uid = userInfo.getId();
                    if (uid != null) {
                        UserInfo currtUser = userInfoService.selectByPKey(uid);
                        if (currtUser != null) {
                            reslutOrder.setUserInfo(currtUser);
                        }
                    }
                }
 
                /* 订单号为空 则不进行查询红包 */
                if (StringUtil.isNullOrEmpty(orderId)) {
                    continue;
                }
 
                /* 根据订单号 + 付款金额 进行匹配红包 */
 
                List<HongBao> listHB = hongBaoService.queryByOrderIDAndPayMoney(Long.parseLong(orderId), payment);
 
                /* 无红包 */
                if (listHB == null || listHB.size() == 0) {
                    Long uid = userInfo.getId();
                    if (uid != null) {
                        listHB = hongBaoService.queryByOrderIDAndUid(Long.parseLong(orderId), uid);
                    }
 
                    if (listHB == null || listHB.size() == 0) {
                        continue;
                    }
                }
 
                HongBao hongBao = listHB.get(0);
 
                reslutOrder.setHongbaoId(hongBao.getId());
                reslutOrder.setGetTime(hongBao.getGetTime());
                reslutOrder.setMoney(hongBao.getMoney());
                reslutOrder.setType(hongBao.getType());
                // reslutOrder.setState(hongBao.getState());
                reslutOrder.setCreatetime(hongBao.getCreatetime());
                reslutOrder.setPreGettime(hongBao.getPreGettime());
 
                /* 查询间接收益 红包id */
                Long pid = reslutOrder.getHongbaoId();
 
                if (pid != null) {
 
                    List<HongBao> childHongBaoList = hongBaoService.findChildHongBaoList(pid);
 
                    if (childHongBaoList != null && childHongBaoList.size() > 0) {
 
                        for (HongBao hb : childHongBaoList) {
 
                            BigDecimal money = hb.getMoney();
                            Integer childType = hb.getType();
                            Long uid = null;
                            String nickName = null;
 
                            UserInfo user = hb.getUserInfo();
                            if (user != null) {
                                uid = user.getId();
                                UserInfo cuent = userInfoService.getUserByIdWithMybatis(uid);
                                if (cuent != null)
                                    nickName = cuent.getNickName();
                            }
 
                            if (childType != null) {
                                if (childType == HongBao.TYPE_YIJI || childType == HongBao.TYPE_SHARE_YIJI) {
                                    // 一级
                                    reslutOrder.setLevelOne(uid);
                                    reslutOrder.setLevelOneMoney(money);
                                    reslutOrder.setLevelOneName(nickName);
                                } else if (childType == HongBao.TYPE_ERJI || childType == HongBao.TYPE_SHARE_ERJI) {
                                    reslutOrder.setLevelTwo(uid);
                                    reslutOrder.setLevelTwoMoney(money);
                                    reslutOrder.setLevelTwoName(nickName);
                                }
                            }
 
                        }
                    }
                }
 
                DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置格式
                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
 
                String createTime = reslutOrder.getCreateTime();
                if (!StringUtil.isNullOrEmpty(createTime)) {
                    Date d = f.parse(createTime);
                    reslutOrder.setCreateTime(format.format(d));
                }
 
                String settlementTime = reslutOrder.getSettlementTime();
                if (!StringUtil.isNullOrEmpty(settlementTime)) {
                    Date d = f.parse(settlementTime);
                    reslutOrder.setSettlementTime(format.format(d));
                }
 
                /* 到账状态 */
                int state = hongBao.getState();
 
                if (HongBao.STATE_YILINGQU == state) {
                    reslutOrder.setMoneyState("3"); // 已到账
                } else if (HongBao.STATE_BUKELINGQU == state || HongBao.STATE_KELINGQU == state) {
                    reslutOrder.setMoneyState("2"); // 未到账
                } else {
                    reslutOrder.setMoneyState("4"); // 已失效
                }
 
                String rebateSource = null;
                Integer typeHongBao = reslutOrder.getType();
                if (typeHongBao != null) {
 
                    if (typeHongBao == HongBao.TYPE_HUODONG || typeHongBao == HongBao.TYPE_XINREN) {
                        /* 3-活动红包 4-新人红包 */
                        rebateSource = "1";
 
                    } else if (typeHongBao == HongBao.TYPE_JINGDONG || typeHongBao == HongBao.TYPE_TAOBAO || typeHongBao == HongBao.TYPE_YAOQING) {
                        /* 自购订单 */
                        rebateSource = "2";
                    } else if (typeHongBao == HongBao.TYPE_YIJI || typeHongBao == HongBao.TYPE_ERJI || typeHongBao == HongBao.TYPE_SHARE_YIJI || typeHongBao == HongBao.TYPE_SHARE_ERJI) {
                        /* 邀请订单 分销红包对应的最上级红包 */
                        rebateSource = "3";
                    } else if (typeHongBao == HongBao.TYPE_SHARE_GOODS) {
                        /* 分享订单 */
                        rebateSource = "4";
                    }
                    reslutOrder.setRebateSource(rebateSource);
                }
 
            }
 
            int count = taoBaoOrderService.countQueryJoinHongBao(key, startTime, endTime, type, days);
            int totalPage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
 
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.serializeNulls(); // 重点
            Gson gson = gsonBuilder.setDateFormat("yyyy/MM/dd HH:mm:ss").create();
 
            JSONObject data = new JSONObject();
            data.put("pe", pe);
            data.put("list", gson.toJson(reslutOrders));
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            e.printStackTrace();
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("操作异常"));
        }
    }
 
    @RequestMapping(value = "countByType")
    public void countByType(String callback, HttpServletRequest request, PrintWriter out) {
 
        try {
            Map<String, Object> countByOdrerType = taoBaoOrderCountService.countByOdrerType();
 
            JSONObject data = new JSONObject();
            data.put("countType", countByOdrerType);
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("统计失败"));
            e.printStackTrace();
        }
    }
 
    @RequestMapping(value = "countByDays")
    public void countByDays(String callback, HttpServletRequest request, PrintWriter out) {
 
        try {
            // 统计今日 未失效订单数量 相同订单号合并为一单
            int countToday = taoBaoOrderCountService.countToday();
            // 统计昨日 未失效订单数量 相同订单号合并为一单
            int countYesterday = taoBaoOrderCountService.countYesterday();
 
            JSONObject data = new JSONObject();
            data.put("countToday", countToday);
            data.put("countYesterday", countYesterday);
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("统计失败"));
            e.printStackTrace();
        }
    }
 
    
}