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
package com.yeshi.fanli.controller.admin.shop;
 
import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
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.bus.user.UserInfo;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.exception.shop.BanLiShopOrderException;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
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.StringUtil;
import com.yeshi.fanli.util.TimeUtil;
 
import net.sf.json.JSONObject;
 
@Controller
@RequestMapping("admin/new/api/v1/blOrder")
public class BanLiShopOrderAdminController {
 
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
 
    @Resource
    private BanLiShopOrderPayService banLiShopOrderPayService;
 
    @Resource
    private UserInfoService userInfoService;
 
    /**
     * 获取订单列表
     * 
     * @param callback
     * @param orderId
     *            -订单号 可为空
     * @param page
     *            -页码
     * @param state-状态数组
     *            [1,2]
     * @param uid
     *            用户ID
     * @param out
     */
    @RequestMapping(value = "listOrder")
    public void listOrder(String callback, String orderId, int page, String state, Long uid, PrintWriter out) {
        long count = 0;
        List<BanLiShopOrder> orderList = new ArrayList<>();
        if (!StringUtil.isNullOrEmpty(orderId)) {
            BanLiShopOrder order = banLiShopOrderService.selectByOrderNo(orderId);
            if (order != null) {
                orderList.add(order);
                count = 1;
            }
        } else {
            List<Integer> stateList = null;
            if (!StringUtil.isNullOrEmpty(state)) {
                net.sf.json.JSONArray array = net.sf.json.JSONArray.fromObject(state);
 
                for (int i = 0; i < array.size(); i++) {
                    if (stateList == null)
                        stateList = new ArrayList<>();
 
                    stateList.add(array.optInt(i));
                }
 
            }
            orderList = banLiShopOrderService.listByUidAndState(uid, stateList, page, Constant.PAGE_SIZE);
            count = banLiShopOrderService.countByUidAndState(uid, stateList);
        }
 
        GsonBuilder gb = new GsonBuilder();
        gb.registerTypeAdapter(java.util.Date.class, new JsonSerializer<Date>() {
 
            public JsonElement serialize(Date arg0, Type arg1, JsonSerializationContext arg2) {
                return new JsonPrimitive(TimeUtil.getGernalTime(arg0.getTime(), "yyyy-MM-dd HH:mm:ss"));
            }
        });
        PageEntity pe = new PageEntity(page, Constant.PAGE_SIZE, count,
                (int) (count % Constant.PAGE_SIZE == 0 ? count / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1));
 
        Gson gson = gb.create();
        JSONObject data = new JSONObject();
        data.put("pe", pe);
        data.put("list", gson.toJson(orderList));
        out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
    }
 
    // 订单充值
    @RequestMapping(value = "charge")
    public void charge(String callback, Long id, String code, HttpServletRequest request, PrintWriter out) {
 
        /* 检验是否登陆 */
        AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
        if (admin == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("当前账户失效,请重新登陆。"));
            return;
        }
 
        /* 检验是否通过验证 */
        String codeType = (String) request.getSession().getAttribute(Constant.SESSION_EXTRACT_VERIFY_RESULT);
        if (!"1".equals(codeType)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(9999, "邮箱验证未通过"));
            return;
        }
 
        try {
            banLiShopOrderPayService.charge(id);
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("")));
        } catch (BanLiShopOrderException e) {
            e.printStackTrace();
            BanLiShopOrder order = new BanLiShopOrder(id);
            String stateDesc = String.format("错误码:%s 错误原因:%s", e.getCode() + "", e.getMsg());
            order.setStateDesc(stateDesc);
            order.setUpdateTime(new Date());
            banLiShopOrderService.udpateSelectiveByPrimaryKey(order);
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(e.getCode(), e.getMsg())));
        }
    }
 
    @RequestMapping(value = "reject")
    public void reject(String callback, Long id, String desc,HttpServletRequest request, PrintWriter out) {
        /* 检验是否登陆 */
        AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
        if (admin == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("当前账户失效,请重新登陆。"));
            return;
        }
 
        /* 检验是否通过验证 */
        String codeType = (String) request.getSession().getAttribute(Constant.SESSION_EXTRACT_VERIFY_RESULT);
        if (!"1".equals(codeType)) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(9999, "邮箱验证未通过"));
            return;
        }
        try {
            banLiShopOrderService.rejectOrder(id, desc);
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult("")));
        } catch (BanLiShopOrderException e) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(e.getCode(), e.getMsg())));
        }
    }
 
    @RequestMapping(value = "getOrderDetail")
    public void getOrderDetail(String callback, Long id, PrintWriter out) {
        BanLiShopOrder order = banLiShopOrderService.selectByPrimaryKey(id);
        if (order == null) {
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(1, "订单不存在")));
            return;
        }
        // 查询用户信息
        UserInfo user = userInfoService.selectByPKey(order.getUid());
        JSONObject data = new JSONObject();
        data.put("order", order);
        data.put("user", user);
        out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
    }
 
}