mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package com.sinata.rest.modular.mall.controller;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sinata.common.enums.EnumCityRole;
import com.sinata.common.enums.EnumIsDelete;
import com.sinata.common.enums.mall.EnumMallOrderState;
import com.sinata.common.util.QRCodeUtils;
import com.sinata.rest.common.ApiUtils;
import com.sinata.rest.modular.auth.util.ThreadPoolUtil;
import com.sinata.rest.modular.mall.controller.body.BodyMallOrder;
import com.sinata.rest.modular.mall.controller.body.BodyMallOrderDetail;
import com.sinata.rest.modular.mall.controller.body.BodyOrderNo;
import com.sinata.rest.modular.mall.controller.vo.VoGoodsSku;
import com.sinata.rest.modular.mall.controller.vo.VoMallOrder;
import com.sinata.rest.modular.mall.controller.vo.VoMallOrderDetail;
import com.sinata.rest.modular.mall.controller.vo.VoMallOrderDetailGroupSpecGoods;
import com.sinata.rest.modular.mall.model.MallOrder;
import com.sinata.rest.modular.mall.model.MallOrderDetail;
import com.sinata.rest.modular.mall.model.MallOrderMain;
import com.sinata.rest.modular.mall.service.IMallGoodsService;
import com.sinata.rest.modular.mall.service.IMallGoodsSkuService;
import com.sinata.rest.modular.mall.service.IMallGroupSpecService;
import com.sinata.rest.modular.mall.service.IMallOrderDetailGroupSpecService;
import com.sinata.rest.modular.mall.service.IMallOrderDetailService;
import com.sinata.rest.modular.mall.service.IMallOrderMainService;
import com.sinata.rest.modular.mall.service.IMallOrderService;
import com.sinata.rest.modular.member.model.MemUser;
import com.sinata.rest.modular.member.model.MyCoupon;
import com.sinata.rest.modular.member.service.IMyCouponService;
import com.sinata.rest.modular.system.service.ISystemSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
 
/**
 * 订单接口
 * @author goku
 * @date 2023/3/11
 */
@Slf4j
@RestController
@RequestMapping("/mall/order")
@Api(tags = "商城-订单接口", description = "订单接口")
public class MallOrderController {
 
    @Resource
    RestTemplate restTemplate;
 
    @Autowired
    ISystemSetService systemSetService;
 
    @Autowired
    IMallOrderService orderService;
 
    @Autowired
    IMallOrderMainService orderMainService;
 
    @Autowired
    IMallOrderDetailService orderDetailService;
 
    @Autowired
    IMyCouponService myCouponService;
 
    @Autowired
    private IMallGoodsService mallGoodsService;
 
    @Autowired
    private IMallGoodsSkuService mallGoodsSkuService;
 
    @Autowired
    private IMallGroupSpecService mallGroupSpecService;
 
    @Autowired
    private IMallOrderDetailGroupSpecService mallOrderDetailGroupSpecService;
 
    @GetMapping(value = "/list")
    @ApiOperation(value = "获取订单列表", notes = "获取订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", defaultValue = "10", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "0 待支付 1 待使用 11 待退款 ", defaultValue = "", dataType = "Integer", paramType = "query"),
    })
    public List<VoMallOrder> list(Integer current, Integer size, Integer state) {
        // 获取用户ID
        Integer userId = ThreadPoolUtil.getUserId();
        // 分页
        Page page = new Page<MemUser>(current == null ? 1 : current, size == null ? 10 : size);
        // 获取订单信息
        List<VoMallOrder> list = orderService.getOrderByUserIdList(userId, null, state, page);
        return list;
    }
 
    @GetMapping(value = "/shareOrderList")
    @ApiOperation(value = "推广订单列表", notes = "获取订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页条数", defaultValue = "10", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "推广用户ID", defaultValue = "", dataType = "Integer", paramType = "query"),
    })
    public List<VoMallOrder> shareOrderList(Integer current, Integer size, Integer userId) {
        // 分页
        Page page = new Page<MemUser>(current == null ? 1 : current, size == null ? 10 : size);
        // 获取订单信息
        List<VoMallOrder> list = orderService.getOrderByUserIdList(null, userId, EnumMallOrderState.SUCCESS.index, page);
        return list;
    }
 
    @GetMapping(value = "/detail")
    @ApiOperation(value = "获取订单详细", notes = "获取订单详细", response = VoMallOrder.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单编号", defaultValue = "1", dataType = "String", paramType = "query", required = true),
    })
    public ApiUtils<VoMallOrder> detail(String orderNo) {
        VoMallOrder o = orderService.getOrderByOrderNo(orderNo);
        if (o == null) {
            return ApiUtils.returnNG(null, "订单信息不存在");
        }
        wrapperVoMallOrder(orderNo, o);
        return ApiUtils.returnOK(o);
    }
 
    public VoMallOrder wrapperVoMallOrder(String orderNo, VoMallOrder voMallOrder) {
        try {
            voMallOrder.setOrderCode(QRCodeUtils.crateQRCode(orderNo, 200, 200));
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<VoMallOrderDetail> detailList = orderService.getOrderDetailByOrderNoList(orderNo);
        for (VoMallOrderDetail od : detailList) {
            List<VoMallOrderDetailGroupSpecGoods> voMallOrderDetailGroupSpecGoodsList = mallOrderDetailGroupSpecService.getListByOrderNo(null, od.getOrderDetailNo());
            od.setGroupSpecGoodsList(voMallOrderDetailGroupSpecGoodsList);
//            List<Integer> groupSpecGoodsIdList = new ArrayList<>();
//            MallGoodsSku goodsSku = mallGoodsSkuService.getById(od.getSkuId());
//            if (goodsSku != null && StrUtil.isNotBlank(goodsSku.getSpecIds())) {
//                String[] specIdArray = goodsSku.getSpecIds().split(",");
//                List<MallGroupSpec> groupSpecList = mallGroupSpecService.list(
//                        Wrappers.<MallGroupSpec>query().lambda()
//                                .in(MallGroupSpec::getId, specIdArray)
//                );
//                for (MallGroupSpec mallGroupSpec : groupSpecList) {
//                    groupSpecGoodsIdList.addAll(
//                            Arrays.stream(mallGroupSpec.getGoodsIds().split(",")).map(Integer::parseInt).collect(Collectors.toList())
//                    );
//                }
//            }
//            if (groupSpecGoodsIdList != null && groupSpecGoodsIdList.size() > 0) {
//                List<MallGoods> groupSpecGoodsList = mallGoodsService.list(
//                        Wrappers.<MallGoods>query().lambda()
//                                .in(MallGoods::getId, groupSpecGoodsIdList)
//                );
//                od.setGroupSpecGoodsList(groupSpecGoodsList);
//            }
        }
        voMallOrder.setDetailList(detailList);
        return voMallOrder;
    }
 
    @GetMapping(value = "/getOrderDetailGroupSpecGoods")
    @ApiOperation(value = "获取订单套餐规格组商品列表", notes = "获取订单套餐规格组商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "String", paramType = "query", required = true),
    })
    public ApiUtils<List<VoMallOrderDetailGroupSpecGoods>> getOrderDetailGroupSpecGoods(String orderNo) {
        return ApiUtils.returnOK(mallOrderDetailGroupSpecService.getListByOrderNo(orderNo, null));
    }
 
    @GetMapping(value = "/getSkuByMerchant")
    @ApiOperation(value = "获取门店sku信息", notes = "获取门店sku信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuIds", value = "商品Sku串(逗号分隔)", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "merchantId", value = "门店ID", defaultValue = "1", dataType = "String", paramType = "query", required = true),
    })
    public List<VoGoodsSku> getSkuByMerchant(String skuIds, Integer merchantId) {
        // 查询商品SKU
        List<VoGoodsSku> skuVoList = mallGoodsService.getGoodsBySkuIdMerchant(skuIds.split(","), merchantId);
        return skuVoList;
    }
 
    @PostMapping(value = "/createOrder")
    @ApiOperation(value = "下单", notes = "下单", response = MallOrderMain.class)
    public Object createOrder(@RequestBody BodyMallOrder body) {
        return orderService.createOrder(Arrays.asList(body));
    }
 
    @PostMapping(value = "/createOrderBatch")
    @ApiOperation(value = "批量下单", notes = "批量下单", response = MallOrderMain.class)
    public Object createOrderBatch(@RequestBody List<BodyMallOrder> list) {
        return orderService.createOrder(list);
    }
 
    @PutMapping(value = "/cancel")
    @ApiOperation(value = "取消订单", notes = "取消订单 返回True 或 FALSE", response = ApiUtils.class)
    public Object cancel(@RequestBody BodyOrderNo body) {
        return orderService.cancel(body.getOrderNo());
    }
 
 
    @PutMapping(value = "/refund")
    @ApiOperation(value = "申请退款", notes = "申请退款 返回True 或 FALSE", response = ApiUtils.class)
    public Object refund(@RequestBody BodyOrderNo body) {
        LambdaQueryWrapper<MallOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallOrder::getOrderNo,body.getOrderNo());
        MallOrder order = orderService.getOne(queryWrapper);
        if(Objects.isNull(order)){
            throw  new  IllegalArgumentException("错误的订单信息");
        }
        if(!order.getState().equals(EnumMallOrderState.WAIT_CHECK.index)){
            throw  new  IllegalArgumentException("订单状态无法退款");
        }
        if(!order.getUseUserId().equals(0)){
            throw  new  IllegalArgumentException("订单材料信息已经被转让");
        }
        LambdaQueryWrapper<MallOrderDetail> detailWrapper = new LambdaQueryWrapper<>();
        detailWrapper.eq(MallOrderDetail::getOrderNo,body.getOrderNo());
        detailWrapper.isNotNull(MallOrderDetail::getUseTime);
        List<MallOrderDetail> detailList = orderDetailService.list(detailWrapper);
        if(Objects.nonNull(detailList) && detailList.size() > 0){
            throw  new  IllegalArgumentException("订单已经被核销");
        }
        MallOrder updateOrder = new MallOrder();
        updateOrder.setState(EnumMallOrderState.WAIT_REFUND.index);
        updateOrder.setApplyTime(new Date());
        UpdateWrapper<MallOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no",body.getOrderNo());
        updateWrapper.eq("state",EnumMallOrderState.WAIT_CHECK.index);
        return orderService.update(updateOrder,updateWrapper);
    }
 
    @PutMapping(value = "/cancelRefund")
    @ApiOperation(value = "取消退款", notes = "取消退款 返回True 或 FALSE", response = ApiUtils.class)
    public Object cancelRefund(@RequestBody BodyOrderNo body) {
        LambdaQueryWrapper<MallOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MallOrder::getOrderNo,body.getOrderNo());
        MallOrder order = orderService.getOne(queryWrapper);
        if(Objects.isNull(order)){
            throw  new  IllegalArgumentException("错误的订单信息");
        }
        if(!order.getState().equals(EnumMallOrderState.WAIT_REFUND.index)){
            throw  new  IllegalArgumentException("订单状态无法取消退款");
        }
        MallOrder updateOrder = new MallOrder();
        updateOrder.setState(EnumMallOrderState.WAIT_CHECK.index);
        UpdateWrapper<MallOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no",body.getOrderNo());
        updateWrapper.eq("state",EnumMallOrderState.WAIT_REFUND.index);
        return orderService.update(updateOrder,updateWrapper);
    }
 
    @GetMapping(value = "/orderCoupon")
    @ApiOperation(value = "下单可用优惠券", notes = "下单可用优惠券", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
            @ApiImplicitParam(name = "goodsId", value = "商品id", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "订单价格", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "goodsType", value = "商品类型,1普通,2黄金,3钻石", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
    })
    public ApiUtils<List<MyCoupon>> orderCoupon(Integer userId, Integer goodsId,BigDecimal price ,Integer goodsType) {
        //Integer userId = ThreadPoolUtil.getUserId();
        return ApiUtils.returnOK(myCouponService.orderCoupon(userId, goodsId,goodsType, price,DateUtil.date().toString()));
    }
 
 
    @GetMapping(value = "/orderCouponCount")
    @ApiOperation(value = "下单可用优惠券统计", notes = "下单可用优惠券", response = Boolean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
            @ApiImplicitParam(name = "goodsId", value = "商品id", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "price", value = "订单价格", defaultValue = "1", dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "goodsType", value = "商品类型,1普通,2黄金,3钻石", defaultValue = "1", dataType = "Int", paramType = "query", required = true),
    })
    public ApiUtils<HashMap> orderCouponCount(Integer userId, Integer goodsId,BigDecimal price ,Integer goodsType) {
        HashMap<String,Integer> data = new HashMap<>();
        data.put("count",myCouponService.orderCoupon(userId, null,null,null,DateUtil.date().toString()).size());
        data.put("canUse",myCouponService.orderCoupon(userId, goodsId,goodsType, price,DateUtil.date().toString()).size());
        return ApiUtils.returnOK(data);
    }
 
 
    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除订单", notes = "删除订单 返回True 或 FALSE", response = ApiUtils.class)
    public Object delete(@RequestBody BodyOrderNo body) {
        MallOrder order = orderService.getById(body.getOrderNo());
        if(order != null && order.getState() == EnumMallOrderState.CANCEL.index) {
            // 删除订单
            order.setIsDelete(EnumIsDelete.DELETED.index);
            orderService.updateById(order);
            return ApiUtils.returnOK();
        }
        return ApiUtils.returnNG();
    }
 
 
//    @PostMapping(value = "/test_createOrder")
//    @ApiOperation(value = "测试-下单", notes = "下单", response = ApiUtils.class)
    public Object test_createOrder() {
        List<BodyMallOrder> list = new ArrayList<>();
        BodyMallOrder o = new BodyMallOrder();
        o.setMerchantId(1);
        o.setTakeName("悟空");
        o.setPhone("1980000000");
        o.setIdCard("111111111111111111111111");
 
        List<BodyMallOrderDetail> skuList = new ArrayList<>();
        BodyMallOrderDetail od = new BodyMallOrderDetail();
        od.setSkuId(428);
        od.setGoodsNum(10);
        skuList.add(od);
 
        od = new BodyMallOrderDetail();
        od.setSkuId(437);
        od.setGoodsNum(1);
        skuList.add(od);
 
        o.setSkuList(skuList);
        list.add(o);
 
 
        o = new BodyMallOrder();
        o.setMerchantId(2);
        o.setGoodsMoney(BigDecimal.TEN);
        o.setNumber(4);
        o.setTakeName("悟空");
        o.setPhone("1980000000");
        o.setIdCard("111111111111111111111111");
 
        skuList = new ArrayList<>();
        od = new BodyMallOrderDetail();
        od.setSkuId(1);
        od.setGoodsNum(1);
        skuList.add(od);
 
        od = new BodyMallOrderDetail();
        od.setSkuId(2);
        od.setGoodsNum(1);
        skuList.add(od);
 
        od = new BodyMallOrderDetail();
        od.setSkuId(3);
        od.setGoodsNum(1);
        skuList.add(od);
 
        od = new BodyMallOrderDetail();
        od.setSkuId(4);
        od.setGoodsNum(1);
        skuList.add(od);
 
        o.setSkuList(skuList);
        list.add(o);
 
        long s = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("http://localhost:8321/rest/mall/order/createOrder", list, String.class);
        }
        long e = System.currentTimeMillis();
 
        log.error("----" + (e - s));
        return null;
    }
 
}