yujian
2019-12-02 2afe3a586ca9feb26f30e4d5af36941add5eca60
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
package com.yeshi.fanli.controller.client.v2;
 
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.DateUtil;
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.exception.order.CommonOrderException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.count.HongBaoV2CountService;
import com.yeshi.fanli.service.inter.order.CommonOrderService;
import com.yeshi.fanli.service.inter.user.UserCustomSettingsService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.account.UserUtil;
import com.yeshi.fanli.vo.order.CommonOrderVO;
import com.yeshi.fanli.vo.order.OrderCountVO;
import com.yeshi.fanli.vo.user.UserInfoExtraVO;
import com.yeshi.fanli.vo.user.UserSettingsVO;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("api/v2/user/order")
public class UserOrderControllerV2 {
 
    @Resource
    private ConfigService configService;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private UserInfoExtraService userInfoExtraService;
 
    @Resource
    private CommonOrderService commonOrderService;
 
    @Resource
    private HongBaoV2CountService hongBaoV2CountService;
 
    @Resource
    private RedisManager redisManager;
    
    @Resource
    private UserCustomSettingsService userCustomSettingsService;
 
    /**
     * 订单列表
     * 
     * @param acceptData
     * @param page
     * @param uid
     * @param state
     *            状态:0全部 1-未到账 2-已到账 3-已失效 4已收货
     * @param type
     *            类型:1-返利订单 2-分享订单 3-邀请订单
     * @param orderState
     *            1有效订单 2 维权订单 3失效订单
     * @param orderNo
     *            订单号
     * @param startTime
     *            起始时间
     * @param endTime
     *            结束时间
     * @param slotTime
     *            时间段:1-最近三天 2-最近七天 3最近半月 4本月 5近三月 6近半年
     * @param dateType
     *            1-今日 2-昨天 3-本月 4-上个月 |
     * @param needCount
     * @param out
     */
    @RequestMapping(value = "getOrderList", method = RequestMethod.POST)
    public void getOrderList(AcceptData acceptData, Integer page, Long uid, Integer state, String type, 
            Integer orderState, String orderNo,    String startTime, String endTime, Integer slotTime, 
            Integer dateType, Integer goodsType, PrintWriter out) {
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
            return;
        }
        
        // 转换状态
        if (state != null) {
            if (state == 0) {
                state = null; // 所有
            } else if (state == 4) {
                state = null;
                orderState = 4; // 已收货
            } else if (state == 5) {
                state = null;
                orderState = 2; // 已维权
            }
        }
        if (state != null && orderState != null && (orderState == 2 || orderState == 3)) {
            state = null;
        }
 
        // 订单类型
        Integer orderType = null;
        if (StringUtil.isNullOrEmpty(type)) {
            orderType = null;
        } else if (!"0".equalsIgnoreCase(type)){
            orderType = Integer.parseInt(type); 
        } 
 
        // 筛选时间
        if (slotTime != null) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            try {
                endTime = sd.format(new Date());
                startTime = convertDate(slotTime, endTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (endTime != null && endTime.trim().length() > 0) {
            endTime += " 23:59:59";
        }
 
        try {
            // 筛选结果顶部统计
            String validMoney = "0.00";
            String invalidMoney = "0.00";
            if (page == 1) {
                BigDecimal predictMoney = hongBaoV2CountService.getRewardMoneyByToSearch(uid, dateType, orderType, 1,
                        orderNo, goodsType, startTime, endTime);
                if (predictMoney != null) 
                    validMoney = predictMoney.setScale(2, BigDecimal.ROUND_DOWN).toString();
                
                BigDecimal postSaleMoney = hongBaoV2CountService.getRewardMoneyByToSearch(uid, dateType, orderType, 2,
                        orderNo, goodsType, startTime, endTime);
                if (postSaleMoney != null) 
                    invalidMoney = postSaleMoney.setScale(2, BigDecimal.ROUND_DOWN).toString();
                
                if (dateType == 4)
                    dateType = 5; // 新版查询本月到账
            }
                        
            // 查询列表
            List<CommonOrderVO> list = commonOrderService.getOrderByUid(acceptData, page, uid, state, orderType, orderState,
                    orderNo, startTime, endTime, dateType, goodsType);
            // 统计总数
            long count = commonOrderService.countGroupOrderNoByUid(uid, state, orderType, orderState, orderNo, startTime,
                    endTime, dateType, goodsType);
 
            // 判断是否需要显示订单找回提示   8秒内请求3次触发
            String orderFindUrl = null;
            if (state ==null && page == 1) {
                if (redisManager.frequencyLimit("orderlist-" + acceptData.getDevice(), 8, 4)||count==0L) {
                    orderFindUrl = configService.get("order_find_notify_url");
                }
            }
            
            JSONObject data = new JSONObject();
            data.put("count", count);
            data.put("result_list", JsonUtil.getApiCommonGson().toJson(list));
            data.put("helpUrl", configService.get("order_list_help"));
            data.put("validMoney", "¥ " + validMoney);
            data.put("invalidMoney", "¥ " + invalidMoney);
            data.put("findOrderHelpUrl", orderFindUrl);
            out.print(JsonUtil.loadTrueResult(data));
        } catch (CommonOrderException e) {
            out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult(1, "查询失败"));
            LogHelper.errorDetailInfo(e);
        }
    }
 
    
    /**
     * 时间转换
     * 
     * @param slotTime
     * @param startTime
     * @return
     * @throws Exception
     */
    private String convertDate(Integer slotTime, String endTime) throws Exception {
        String startTime = null;
 
        switch (slotTime) {
        case 1: // 最近三天
            startTime = DateUtil.reduceDay(2, endTime);
            break;
        case 2: // 最近七天
            startTime = DateUtil.reduceDay(6, endTime);
            break;
        case 3: // 最近15天 (半月)
            startTime = DateUtil.reduceDay(14, endTime);
            break;
        case 4: // 最近三十天 (本月)
            startTime = DateUtil.reduceDay(29, endTime);
            break;
        case 5: // 最近九十天(近三月)
            startTime = DateUtil.reduceDay(3 * 30 - 1, endTime);
            break;
        case 6: // 最近一百八十天(近半年)
            startTime = DateUtil.reduceDay(6 * 30 - 1, endTime);
            break;
        default:
            break;
        }
        return startTime;
    }
    
    
    /**
     * 用户订单统计
     * 
     * @param acceptData
     * @param uid
     *            用户id
     * @param out
     */
    @RequestMapping(value = "getOrderCount", method = RequestMethod.POST)
    public void getOrderCount(AcceptData acceptData, Long uid, Integer type, PrintWriter out) {
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult(1, "请求参数不正确"));
            return;
        }
        
        try {
            UserInfo user = userInfoService.selectByPKey(uid);
            if (user == null) {
                out.print(JsonUtil.loadFalseResult(1, "用户不存在"));
                return;
            }
 
            UserInfo userInfo = UserUtil.filterForClientUser(user);
            UserInfoExtraVO userInfoExtra = userInfoExtraService.getInfoExtraVOByUid(user.getId());
            if (userInfoExtra != null && userInfoExtra.getUserRank() != null) {
                userInfo.setRankIcon(userInfoExtra.getUserRank().getIcon());
                userInfo.setRankNamePicture(userInfoExtra.getUserRank().getPicture());
            }
            JSONObject data = new JSONObject();
            data.put("userInfo", userInfo);
            
            if (type == null || type == 0) {
                OrderCountVO total = commonOrderService.getOrderCount(uid, null, type);
                data.put("total", total.getSelf() + total.getShared() + total.getInvite());
                data.put("self", total.getSelf());
                data.put("shared", total.getShared());
                data.put("invite", total.getInvite());
            } else {
                data.put("total", 0);
                data.put("self", 0);
                data.put("shared", 0);
                data.put("invite", 0);
            }
                        
            // 今天
            OrderCountVO today = commonOrderService.getOrderCount(uid, 1, type);
            today.setTotal(today.getSelf() + today.getShared() + today.getInvite());
            // 昨天
            OrderCountVO yesterday = commonOrderService.getOrderCount(uid, 2, type);
            yesterday.setTotal(yesterday.getSelf() + yesterday.getShared() + yesterday.getInvite());
            // 本月
            OrderCountVO thisMonth = commonOrderService.getOrderCount(uid, 3, type);
            thisMonth.setTotal(thisMonth.getSelf() + thisMonth.getShared() + thisMonth.getInvite());
            // 上月
            OrderCountVO lastMonth = commonOrderService.getOrderCount(uid, 4, type);
            lastMonth.setTotal(lastMonth.getSelf() + lastMonth.getShared() + lastMonth.getInvite());
 
            data.put("today", today);
            data.put("yesterday", yesterday);
            data.put("thisMonth", thisMonth);
            data.put("lastMonth", lastMonth);
            out.print(JsonUtil.loadTrueResult(data));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult(1, "获取信息失败"));
            e.printStackTrace();
        }
    }
 
 
    /**
     * 统计奖金
     * @param acceptData
     * @param uid
     * @param dateType  1-今日 2-昨天 3-本月 4-上个月
     * @param out
     */
    @RequestMapping(value = "getBonusCount", method = RequestMethod.POST)
    public void getBonusCount(AcceptData acceptData, Long uid, Integer dateType, PrintWriter out) {
        if (uid == null) {
            out.print(JsonUtil.loadFalseResult(1, "用户未登录"));
            return;
        }
        
        boolean show = false;
        UserSettingsVO settings = null;
        try {
            settings = userCustomSettingsService.getMySettings(uid);
        } catch (Exception e) {
        }
        
        // 验证是否显示模块
        if (settings != null && settings.getNoBonusCount()!= null) {
            if(settings.getNoBonusCount() == 1) {
                show = false;
            } else if(settings.getNoBonusCount() == 0) {
                show = true;
            }
        } else {
            show = hongBaoV2CountService.getHongBaoCount(uid, null) > 0;
        }
        
        BigDecimal selfMoney = hongBaoV2CountService.getRewardMoneyToCount(uid, dateType, 1);
        BigDecimal shareMoney = hongBaoV2CountService.getRewardMoneyToCount(uid, dateType, 2);
        BigDecimal inviteMoney = hongBaoV2CountService.getRewardMoneyToCount(uid, dateType, 3);
        
        JSONObject data = new JSONObject();
        data.put("show", show);
        data.put("selfMoney", selfMoney.setScale(2,BigDecimal.ROUND_DOWN).toString());
        data.put("shareMoney", shareMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
        data.put("inviteMoney", inviteMoney.setScale(2, BigDecimal.ROUND_DOWN).toString());
        out.print(JsonUtil.loadTrueResult(data));
    }
 
    
}