admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package com.yeshi.fanli.controller.admin.order;
 
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.yeshi.fanli.entity.taobao.TaoBaoOrder;
import com.yeshi.fanli.entity.taobao.TaoBaoWeiQuanOrder;
import com.yeshi.fanli.job.order.taobao.UpdateOrderJob;
import com.yeshi.fanli.job.order.taobao.UpdateTBRelationAndSpecialOrderJob;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoOrderService;
import com.yeshi.fanli.service.inter.order.tb.TaoBaoWeiQuanOrderService;
import com.yeshi.fanli.tag.PageEntity;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/taobaoOrder")
public class TaoBaoOrderController {
 
    @Resource
    private TaoBaoOrderService taoBaoOrderService;
 
    @Resource
    private TaoBaoWeiQuanOrderService taoBaoWeiQuanOrderService;
 
    @Resource
    private UpdateTBRelationAndSpecialOrderJob updateRelationAndSpecialOrderJob;
 
    @Resource
    private UpdateOrderJob updateOrderJob;
 
    /**
     * 查询列表 - 新后台
     * 
     * @param callback
     * @param key
     *            查询词 名称
     * @param pageIndex
     * @param out
     */
    @RequestMapping(value = "listTaoBaoOrder")
    public void listTaoBaoOrder(String callback, String key, Integer pageIndex, PrintWriter out) {
 
        try {
 
            if (pageIndex == null || pageIndex < 0) {
                pageIndex = 1;
            }
 
            List<TaoBaoOrder> orderList = null;
            Long count = null;
 
            if (!StringUtil.isNullOrEmpty(key)) {
                orderList = taoBaoOrderService.getTaoBaoOrderByOrderId(key);
                count = (long) orderList.size();
            } else {
                orderList = taoBaoOrderService.listAllOrder(pageIndex, Constant.PAGE_SIZE);
                count = taoBaoOrderService.countAllOrder();
            }
 
            int pageSize = Constant.PAGE_SIZE;
 
            int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
            PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
 
            Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                @Override
                public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
                    if (value == null) {
                        return new JsonPrimitive("");
                    } else {
                        return new JsonPrimitive(TimeUtil.getGernalTime(value.getTime(), "yyyy-MM-dd HH:mm:ss"));
                    }
                }
            }).create();
 
            JSONObject data = new JSONObject();
            data.put("pe", pe);
            data.put("result_list", gson.toJson(orderList));
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("查询失败"));
            e.printStackTrace();
        }
    }
 
    @RequestMapping(value = "updateTaoBaoOrder")
    public void updateTaoBaoOrder(String callback, String ids, PrintWriter out) {
        try {
            if (StringUtil.isNullOrEmpty(ids)) {
                JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请上传订单ID"));
                return;
            }
            net.sf.json.JSONArray idsArray = net.sf.json.JSONArray.fromObject(ids);
 
            Set<String> orderIds = new HashSet<>();
 
            for (int i = 0; i < idsArray.size(); i++) {
                TaoBaoOrder taoBaoOrder = taoBaoOrderService.selectByPrimaryKey(idsArray.optLong(i));
                orderIds.add(taoBaoOrder.getOrderId());
            }
 
            Long[] startTimes = new Long[orderIds.size()];
            Long[] endTimes = new Long[orderIds.size()];
 
            int p = 0;
            for (Iterator<String> its = orderIds.iterator(); its.hasNext();) {
                String orderId = its.next();
                List<TaoBaoOrder> list = taoBaoOrderService.getTaoBaoOrderByOrderId(orderId);
                startTimes[p] = TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss") - 1000L;
                endTimes[p] = TimeUtil.convertToTimeTemp(list.get(0).getCreateTime(), "yyyy-MM-dd HH:mm:ss") + 1000L;
                p++;
            }
 
            for (int i = 0; i < idsArray.size(); i++) {
                updateRelationAndSpecialOrderJob.updateRelationAndSpecialOrder(startTimes[i], endTimes[i]);
                updateOrderJob.updateOrder(startTimes[i], endTimes[i]);
            }
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("更新成功"));
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("更新失败"));
            e.printStackTrace();
        }
    }
 
    @RequestMapping(value = "listTaoBaoWQOrder")
    public void listTaoBaoWeiQuanOrder(String callback, String key, Integer pageIndex, PrintWriter out) {
        try {
            if (pageIndex == null || pageIndex < 0) {
                pageIndex = 1;
            }
            List<TaoBaoWeiQuanOrder> orderList = null;
            Long count = null;
            if (!StringUtil.isNullOrEmpty(key)) {
                orderList = taoBaoWeiQuanOrderService.listByOrderId(key);
                count = (long) orderList.size();
            } else {
                orderList = taoBaoWeiQuanOrderService.listByState(null, pageIndex, Constant.PAGE_SIZE);
                count = taoBaoWeiQuanOrderService.countByState(null);
            }
 
            int pageSize = Constant.PAGE_SIZE;
 
            int totalPage = (int) (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
            PageEntity pe = new PageEntity(pageIndex, pageSize, count, totalPage);
 
            Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                @Override
                public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
                    if (value == null) {
                        return new JsonPrimitive("");
                    } else {
                        return new JsonPrimitive(TimeUtil.getGernalTime(value.getTime(), "yyyy-MM-dd HH:mm:ss"));
                    }
                }
            }).create();
 
            JSONObject data = new JSONObject();
            data.put("pe", pe);
            data.put("result_list", gson.toJson(orderList));
 
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult(data));
 
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("查询失败"));
            e.printStackTrace();
        }
    }
 
}