admin
2024-01-23 81da61b828e29b7745e1382dfbbaeb685dc083ef
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
package com.yeshi.fanli.controller.client.v1;
 
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
import com.yeshi.fanli.entity.SystemEnum;
import com.yeshi.fanli.util.SystemInfoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.exception.WXOrderException;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.entity.common.Config;
import com.yeshi.fanli.entity.shop.BanLiShopGoods;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSetsPay;
import com.yeshi.fanli.entity.shop.BanLiShopOrder;
import com.yeshi.fanli.entity.system.ConfigKeyEnum;
import com.yeshi.fanli.exception.redpack.RedPackBalanceException;
import com.yeshi.fanli.exception.shop.BanLiShopOrderException;
import com.yeshi.fanli.service.AdminUserService;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService;
import com.yeshi.fanli.service.inter.redpack.RedPackBalanceService;
import com.yeshi.fanli.service.inter.redpack.RedPackForbidService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopOrderService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreadUtil;
import com.yeshi.fanli.util.email.MailSenderUtil;
import com.yeshi.fanli.util.factory.shop.BanLiShopOrderGoodsVOFactory;
import com.yeshi.fanli.util.wx.BanLiShopWXPayUtil;
import com.yeshi.fanli.vo.homemodule.BannerVO;
import com.yeshi.fanli.vo.shop.BanLiShopOrderVO;
 
import net.sf.json.JSONObject;
 
/**
 * 板栗商城
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("api/v1/banlishop")
public class BanLiShopController {
 
    @Resource
    private BanLiShopGoodsService banLiShopGoodsService;
 
    @Resource
    private BanLiShopOrderService banLiShopOrderService;
 
    @Resource
    private BanLiShopOrderPayService banLiShopOrderPayService;
 
    @Resource
    private SwiperPictureService swiperPictureService;
 
    @Resource
    private RedPackBalanceService redPackBalanceService;
 
    @Resource
    private BanLiShopGoodsSetPayService banLiShopGoodsSetPayService;
 
    @Resource
    private BanLiShopGoodsSetService banLiShopGoodsSetService;
 
    @Resource
    private RedPackForbidService redPackForbidService;
    
    @Resource
    private ConfigService configService;
    
    
    @Resource
    private AdminUserService adminUserService;
 
    /**
     * 商品列表
     * 
     * @param acceptData
     * @param page
     * @param callback
     * @param out
     */
    @RequestMapping(value = "goodsList")
    public void goodsList(AcceptData acceptData, int page, Long uid, String callback, PrintWriter out) {
        if (page <= 0)
            page = 1;
        List<BanLiShopGoods> goodsList = banLiShopGoodsService.listGoods(null, BanLiShopGoods.STATE_ONLINE, page,
                Constant.PAGE_SIZE);
        long count = banLiShopGoodsService.countGoods(null, BanLiShopGoods.STATE_ONLINE);
        // 获取banner
        List<BannerVO> pictureList = swiperPictureService.getByBannerCardAndVersion("hongbao_exchange_goods_list",
                acceptData.getPlatform(), Integer.parseInt(acceptData.getVersion()),acceptData.getSystem());
 
        JSONObject data = new JSONObject();
        if (pictureList != null && pictureList.size() > 0) {
            data.put("picture", pictureList.get(0).getSrc());
        }
        data.put("data", goodsList);
        data.put("count", count);
        data.put("redPackLock", redPackForbidService.verifyForbid(uid));
 
        if (!StringUtil.isNullOrEmpty(callback))
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
        else
            out.print(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "orderList")
    public void orderList(AcceptData acceptData, Long uid, int page, String callback, String from, PrintWriter out) {
        if (page <= 0)
            page = 1;
        List<BanLiShopOrder> orderList = banLiShopOrderService.listByUid(uid, page, Constant.PAGE_SIZE);
        if (!StringUtil.isNullOrEmpty("from") && from.equalsIgnoreCase("pay")) {// 从支付界面过来
            // 判断第一个订单是否支付成功
            if (orderList.size() > 0) {
                BanLiShopOrder latestOrder = orderList.get(0);
                if (latestOrder.getMoneyPayment() != null
                        && latestOrder.getMoneyPayment().compareTo(new BigDecimal(0)) > 0) {
                    // 判断微信支付是否成功
                    try {
                        boolean isPaySuccess = BanLiShopWXPayUtil.isPaySuccess(latestOrder.getOrderNo());
                        if (isPaySuccess) {
                            try {
                                banLiShopOrderPayService.payOrderByMoney(latestOrder.getId(),
                                        latestOrder.getMoneyPayment());
                                orderList = banLiShopOrderService.listByUid(uid, page, Constant.PAGE_SIZE);
                            } catch (BanLiShopOrderException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (WXOrderException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
 
        List<BanLiShopOrderVO> voList = new ArrayList<>();
        for (BanLiShopOrder order : orderList) {
            voList.add(BanLiShopOrderGoodsVOFactory.create(order));
        }
        long count = banLiShopOrderService.countByUid(uid);
        JSONObject data = new JSONObject();
        data.put("data", voList);
        data.put("count", count);
        if (!StringUtil.isNullOrEmpty(callback))
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
        else
            out.print(JsonUtil.loadTrueResult(data));
    }
 
    private String getPayName(int payType) {
        switch (payType) {
        case BanLiShopGoodsSetsPay.PAY_TYPE_BALANCE:
            return "仅账户余额";
        case BanLiShopGoodsSetsPay.PAY_TYPE_HONGBAO:
            return "仅用红包";
 
        case BanLiShopGoodsSetsPay.PAY_TYPE_HONGBAO_MONEY:
            return "现金和红包";
        case BanLiShopGoodsSetsPay.PAY_TYPE_MONEY:
            return "仅用现金";
        }
        return "";
    }
 
    /**
     * 获取商品详情
     * 
     * @param acceptData
     * @param uid
     * @param id
     * @param callback
     * @param out
     */
    @RequestMapping(value = "getGoodsDetail")
    public void getGoodsDetail(AcceptData acceptData, Long uid, Long id, String callback, PrintWriter out) {
        BanLiShopGoods goods = banLiShopGoodsService.getGoodsDetail(id);
        if (goods != null)
            for (BanLiShopGoodsSets set : goods.getSetsList()) {
                for (BanLiShopGoodsSetsPay pay : set.getPayList()) {
                    pay.setName(getPayName(pay.getPayType()));
                }
            }
        BigDecimal money = redPackBalanceService.getBalance(uid);
 
 
        JSONObject data = new JSONObject();
        data.put("goods", goods);
        data.put("hongBaoBalance", money);
        if (!StringUtil.isNullOrEmpty(callback))
            out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
        else
            out.print(JsonUtil.loadTrueResult(data));
    }
 
    @RequestMapping(value = "buy")
    public void buy(AcceptData acceptData, Long uid, Long payId, String account, String callback,
            HttpServletRequest request, PrintWriter out) {
        if (payId == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("请选择支付方式"));
            return;
        }
 
        if (uid == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult("用户未登录"));
            return;
        }
 
        SystemEnum system= SystemInfoUtil.getSystem(acceptData);
 
        try {
            BanLiShopOrder order = banLiShopOrderService.createOrder(payId, account, uid);
            banLiShopOrderService.addOrder(order);
            // 红包支付
            if (order.getHongBaoPayment() != null) {
                try {
                    banLiShopOrderPayService.payOrderByHongBao(order.getId());
                    // 红包支付成功
 
                } catch (RedPackBalanceException e) {
                    if (!StringUtil.isNullOrEmpty(callback))
                        out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(100, "红包支付失败")));
                    else
                        out.print(JsonUtil.loadFalseResult(100, "红包支付失败"));
                    return;
                }
            }
            // 看是否需要微信支付
            if (order.getMoneyPayment() != null && order.getMoneyPayment().compareTo(new BigDecimal(0)) > 0) {
                // 走微信支付
                String payUrl = BanLiShopWXPayUtil.getWXH5PayUrl(order.getOrderNo(), request.getRemoteAddr(),
                        order.getOrderGoods().getGoodsName(), order.getMoneyPayment(), acceptData.getPlatform());
                if (StringUtil.isNullOrEmpty(payUrl)) {
                    if (!StringUtil.isNullOrEmpty(callback))
                        out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(102, "微信支付创建失败")));
                    else
                        out.print(JsonUtil.loadFalseResult(102, "微信支付创建失败"));
                    return;
                }
                JSONObject data = new JSONObject();
                data.put("wxPayUrl", payUrl);
                if (!StringUtil.isNullOrEmpty(callback))
                    out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
                else
                    out.print(JsonUtil.loadTrueResult(data));
            } else {// 不需要微信支付
                JSONObject data = new JSONObject();
                if (!StringUtil.isNullOrEmpty(callback))
                    out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadTrueResult(data)));
                else
                    out.print(JsonUtil.loadTrueResult(data));
                
                ThreadUtil.run(new Runnable() {
                    @Override
                    public void run() {
                        long[] targetUids = new long[] { 3L, 4L };
                        Config config = configService.getConfig(ConfigKeyEnum.extractCodeEmailFrom.getKey(),system);
                        String[] sts = config.getValue().split(",");
                        String account = sts[0];
                        String pwd = sts[1];
                        for (long adminId : targetUids) {
                            AdminUser adminUser = adminUserService.selectByPrimaryKey(adminId);
                            String msg = "红包商城有新的订单,请前往审核";
                            boolean isS = MailSenderUtil.sendEmail(adminUser.getEmail(), account, pwd, "红包商城新订单提醒", msg);
                        }
                    }
                });
                
            }
 
        } catch (BanLiShopOrderException e) {
            if (!StringUtil.isNullOrEmpty(callback))
                out.print(JsonUtil.loadJSONP(callback, JsonUtil.loadFalseResult(e.getCode(), e.getMsg())));
            else
                out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
        }
 
    }
 
}