陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.web.customer;
 
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.base.BaseController;
import com.mzl.flower.base.R;
import com.mzl.flower.base.ReturnDataDTO;
import com.mzl.flower.dto.PriceDTO;
import com.mzl.flower.dto.request.flower.*;
import com.mzl.flower.dto.request.payment.CartCommitDTO;
import com.mzl.flower.dto.request.payment.CartDeleteDTO;
import com.mzl.flower.dto.request.payment.CartSaveDTO;
import com.mzl.flower.dto.request.payment.OrderCommitDTO;
import com.mzl.flower.dto.request.transport.TransportGetDTO;
import com.mzl.flower.dto.response.flower.*;
import com.mzl.flower.dto.response.payment.PreOrderDTO;
import com.mzl.flower.dto.response.transport.TransportOrderDTO;
import com.mzl.flower.service.flower.FlowerCategoryService;
import com.mzl.flower.service.flower.FlowerParamService;
import com.mzl.flower.service.flower.FlowerService;
import com.mzl.flower.service.payment.OrderService;
import com.mzl.flower.service.transport.TransportService;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.ValidationException;
import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RestController
@RequestMapping("/api/customer/flower")
@Api(value = "交易大厅-花店", tags = "交易大厅-花店")
@Validated
@Slf4j
public class FlowerCustomerController extends BaseController {
 
    @Autowired
    private FlowerService flowerService;
 
    @Autowired
    private FlowerParamService paramService;
 
    @Autowired
    private FlowerCategoryService categoryService;
 
    @Autowired
    private OrderService orderService;
 
    @Autowired
    private TransportService transportService;
 
    @GetMapping("/category/tree")
    @ApiOperation(value = "获取商品分类树")
    public ResponseEntity<ReturnDataDTO<List<FlowerCategoryTreeDTO>>> selectCategoryTree(FlowerCategoryQueryDTO dto){
        return returnData(R.SUCCESS.getCode(), categoryService.selectCustomerCategoryTree(dto));
    }
 
    @GetMapping("/category/tree/view")
    @ApiOperation(value = "查询商品分类详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品分类id", required = true, dataType = "String", paramType = "query")
    })
    public ResponseEntity<ReturnDataDTO<FlowerCategoryDTO>> getCategoryShow(@NotBlank(message = "id不能为空") String id){
        return returnData(R.SUCCESS.getCode(), categoryService.getCategoryShow(id));
    }
 
    @GetMapping("/params")
    @ApiOperation(value = "获取分类参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "商品分类id", required = true, dataType = "Long", paramType = "query")
    })
    public ResponseEntity<ReturnDataDTO<List<ParamItemDTO>>> getParamItemListByCategoryId(Long categoryId){
        return returnData(R.SUCCESS.getCode(), paramService.getParamItemListByCategoryId(categoryId));
    }
 
    @PostMapping("/list")
    @ApiOperation(value = "商品列表")
    public ResponseEntity<ReturnDataDTO<Page<FlowerShowListDTO>>> selectShowFlowerList(@RequestBody FlowerShowQueryDTO dto){
        Page page = new Page(dto.getCurrent(), dto.getSize());
        return returnData(R.SUCCESS.getCode(), flowerService.selectShowFlowerList(page, dto));
    }
 
    @GetMapping("/list/view")
    @ApiOperation(value = "商品详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "partnerId", value = "合伙人id", required = true, dataType = "Long", paramType = "query"),
    })
    public ResponseEntity<ReturnDataDTO<FlowerShowDTO>> getShowFlowerDetail(Long id, Long partnerId) {
        return returnData(R.SUCCESS.getCode(), flowerService.getShowFlowerDetail(id, partnerId));
    }
 
    @GetMapping("/up/stock")
    @ApiOperation(value = "在售商品数量")
    public ResponseEntity<ReturnDataDTO<Integer>> getUpFlowerStock() {
        return returnData(R.SUCCESS.getCode(), flowerService.getUpFlowerStock());
    }
 
    @PostMapping("/cart/change-num")
    @ApiOperation(value = "添加/编辑购物车商品(如果已存在,进行增量的更新)")
    public ResponseEntity<ReturnDataDTO<?>> changeFlower2Cart(@RequestBody CartSaveDTO dto){
        orderService.changeFlower2Cart(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @PostMapping("/cart/save")
    @ApiOperation(value = "添加/编辑购物车商品")
    public ResponseEntity<ReturnDataDTO<?>> saveFlower2Cart(@RequestBody CartSaveDTO dto){
        orderService.saveFlower2Cart(dto);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @GetMapping("/cart/delete")
    @ApiOperation(value = "删除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "Long", paramType = "query")
    })
    public ResponseEntity<ReturnDataDTO<?>> deleteFlower4Cart(Long id){
        orderService.deleteFlower4Cart(id);
        return returnData(R.SUCCESS.getCode(), null);
    }
 
    @PostMapping("/cart/delete/batch")
    @ApiOperation(value = "批量删除购物车商品")
    public ResponseEntity<ReturnDataDTO<?>> deleteBatchFlower4Cart(CartDeleteDTO dto){
        if(dto == null || CollectionUtils.isEmpty(dto.getIds())){
            throw new ValidationException("ids不能为空");
        }
        orderService.deleteBatchFlower4Cart(dto.getIds());
        return returnData(R.SUCCESS.getCode(), null);
    }
 
 
 
    @GetMapping("/cart/list")
    @ApiOperation(value = "查询购物车商品")
    public ResponseEntity<ReturnDataDTO<List<FlowerCartListWrapDTO>>> getFlowerCartList(){
        return returnData(R.SUCCESS.getCode(), orderService.getFlowerCartList());
    }
 
    @GetMapping("/cart/flower/count")
    @ApiOperation(value = "获取购物车商品数量")
    public ResponseEntity<ReturnDataDTO<Integer>> getCartFlowerCount(String flowerId){
        return returnData(R.SUCCESS.getCode(), orderService.getCartFlowerCount(flowerId));
    }
 
    @PostMapping("/order/confirm/info")
    @ApiOperation(value = "订单确认信息(购物车选择商品和数量后提交调用)")
    public ResponseEntity<ReturnDataDTO<PreOrderDTO>> getPreOrderInfo(@RequestBody CartCommitDTO dto){
        return returnData(R.SUCCESS.getCode(), orderService.getPreOrderInfo(dto.getFlowers()));
    }
 
    @PostMapping("/order/confirm/transports")
    @ApiOperation(value = "订单确认信息页面-获取物流")
    public ResponseEntity<ReturnDataDTO<List<TransportOrderDTO>>> getPreOrderTransportList(@RequestBody TransportGetDTO dto){
        return returnData(R.SUCCESS.getCode(), transportService.getPreOrderTransportList(dto.getAddressId(), dto.getWeight()));
    }
 
    @PostMapping("/order/commit")
    @ApiOperation(value = "提交订单")
    public ResponseEntity<ReturnDataDTO<?>> commitOrder(@RequestBody OrderCommitDTO dto){
        Map<Long, PriceDTO > priceMap = new HashMap<>();
        PreOrderDTO p = orderService.processPreOrderInfo(dto.getFlowers(), priceMap);
        Map map;
        try {
            map = orderService.commitOrder(dto, p, priceMap);
        } catch (ValidationException e) {
            orderService.revertFlowerStock(dto.getFlowers());
            throw e;
        } catch (Exception e) {
            orderService.revertFlowerStock(dto.getFlowers());
            throw e;
        }
 
        return returnData(R.SUCCESS.getCode(), map);
    }
 
}