luodangjia
2024-09-27 1044c9c052907047990625674c4b9702bea2b44f
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
package com.ruoyi.order.controller;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ruoyi.account.api.feignClient.AppCouponClient;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.TAppUserAddress;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.dto.ExchangeDto;
import com.ruoyi.common.core.utils.OrderCodeUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.model.TExchangeOrder;
import com.ruoyi.order.api.model.TShoppingOrder;
import com.ruoyi.order.api.query.ShoppingOrderQuery;
import com.ruoyi.order.dto.*;
import com.ruoyi.order.service.TShoppingOrderService;
import com.ruoyi.other.api.domain.TCoupon;
import com.ruoyi.other.api.domain.TGoods;
import com.ruoyi.other.api.feignClient.CouponClient;
import com.ruoyi.other.api.feignClient.GoodsClient;
import com.ruoyi.payment.api.feignClient.AliPaymentClient;
import com.ruoyi.payment.api.feignClient.WxPaymentClient;
import com.ruoyi.payment.api.model.RefundReq;
import com.ruoyi.payment.api.model.RefundResp;
import com.ruoyi.payment.api.model.WxPaymentRefundModel;
import com.ruoyi.payment.api.vo.WxRefundNotifyResp;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.feignClient.SysUserClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2024-08-07
 */
@Api(tags = "购物订单")
@RestController
@RequestMapping("/t-shopping-order")
public class TShoppingOrderController {
    
    @Resource
    private TShoppingOrderService shoppingOrderService;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private GoodsClient goodsClient;
    @Resource
    private CouponClient couponClient;
    @Resource
    private TokenService tokenService;
    @Resource
    private AliPaymentClient aliPaymentClient;
    @Resource
    private WxPaymentClient wxPaymentClient;
    @Resource
    private AppCouponClient appCouponClient;
    @Resource
    private SysUserClient sysUserClient;
    
    
    @PostMapping("/getShoppingOrderList")
    @ApiOperation(value = "列表查询", tags = {"管理后台-购物订单"})
    public AjaxResult<PageInfo<TShoppingOrder>> getShoppingOrderList(@RequestBody ShoppingOrderQuery query) {
        if (StringUtils.hasLength(query.getPhone())) {
            List<Long> data = appUserClient.getUserIdsByPhone(query.getPhone()).getData();
            if (data.isEmpty()) {
                return AjaxResult.success(new PageInfo<TShoppingOrder>());
            }
            query.setUserIds(data);
        }
        if (StringUtils.hasLength(query.getName())) {
            List<Integer> data = goodsClient.getGoodsIdsByName(query.getName()).getData();
            query.setGoodsIds(data);
            List<Integer> data1 = couponClient.getCouponIdsByName(query.getName()).getData();
            query.setCouponIds(data1);
            if (data.isEmpty() && data1.isEmpty()) {
                return AjaxResult.success(new PageInfo<TShoppingOrder>());
            }
        }
        PageInfo<TShoppingOrder> res = shoppingOrderService.pageList(query);
        return AjaxResult.success(res);
    }
    
    @GetMapping("/getShoppingOrderInfoById")
    @ApiOperation(value = "根据订单id查看订单详情", tags = {"管理后台-购物订单"})
    public AjaxResult<TShoppingOrder> getShoppingOrderList(String id) {
        TShoppingOrder byId = shoppingOrderService.getById(id);
        if (byId.getConsignerId() != null) {
            SysUser data = sysUserClient.getSysUser(byId.getConsignerId()).getData();
            if (data != null) {
                byId.setConsignerName(data.getUserName());
            }
        }
        if (byId.getCancellationId() != null) {
            SysUser data = sysUserClient.getSysUser(byId.getCancellationId()).getData();
            if (data != null) {
                byId.setCancellationName(data.getUserName());
            }
        }
        if (byId.getAppUserAddressId() != null) {
            TAppUserAddress data = appUserClient.getAddressById(byId.getAppUserAddressId()).getData();
            if (data != null) {
                byId.setReceivingName(data.getName() + "-" + data.getPhone());
                byId.setReceivingAddress(data.getAddress());
            }
        }
        if (byId.getOrderType()==1){
            TGoods data = goodsClient.getGoodsById(byId.getGoodsId()).getData();
            if (data!=null){
                byId.setName(data.getName());
            }
        }else {
         TCoupon data = couponClient.getCouponById1(byId.getCouponId()).getData();
         if (data!=null){
             byId.setName(data.getName());
         }
        }
        return AjaxResult.success(byId);
    }
    
    @GetMapping("/deleteShoppingOrder")
    @ApiOperation(value = "批量删除订单", tags = {"管理后台-购物订单"})
    public AjaxResult<TShoppingOrder> deleteShoppingOrder(String ids) {
        shoppingOrderService.removeBatchByIds(Arrays.asList(ids.split(",")));
        return AjaxResult.success();
    }
    
    @GetMapping("/cancelShoppingOrder")
    @ApiOperation(value = "取消订单", tags = {"管理后台-购物订单"})
    public AjaxResult cancelShoppingOrder(String id) {
        TShoppingOrder byId = shoppingOrderService.getById(id);
        Long userid = tokenService.getLoginUser().getUserid();
        // todo 根据支付方式 取消订单
        byId.setCancellationId(userid);
        byId.setCancellationTime(LocalDateTime.now());
        byId.setStatus(4);
        switch (byId.getPaymentType()) {
            case 1:
                // 微信
                WxPaymentRefundModel wxPaymentRefundModel = new WxPaymentRefundModel();
                wxPaymentRefundModel.setTransaction_id(byId.getSerialNumber());
                wxPaymentRefundModel.setOut_trade_no(byId.getCode());
                wxPaymentRefundModel.setOut_refund_no(OrderCodeUtil.getOrderCode("GW"));
                // todo 部署到线上之后写回调地址
//                wxPaymentRefundModel.setNotify_url("");
                String string = byId.getPaymentAmount().multiply(new BigDecimal("100")).toString();
                if (string.contains(".")) {
                    string = string.substring(0, string.indexOf("."));
                }
                int i = Integer.parseInt(string);
                WxPaymentRefundModel.RefundAmount refundAmount = new WxPaymentRefundModel.RefundAmount();
                refundAmount.setTotal(i);
                refundAmount.setRefund(i);
                wxPaymentRefundModel.setAmount(refundAmount);
                wxPaymentClient.refundOrderR(wxPaymentRefundModel);
                break;
            case 2:
                // 支付宝
                RefundReq refundReq = new RefundReq();
                refundReq.setOutTradeNo(byId.getSerialNumber());
                String gw = OrderCodeUtil.getOrderCode("GW");
                refundReq.setOutRequestNo(gw);
                refundReq.setRefundAmount(byId.getPaymentAmount().toString());
                refundReq.setRefundReason("后台退款");
                RefundResp data = aliPaymentClient.refund(refundReq).getData();
                if (data != null) {
                    // 退款成功 回退优惠券
//                    byId.setStatus(4);
                    if (byId.getAppCouponId() != null) {
                        appCouponClient.refund(byId.getAppCouponId().toString());
                        byId.setRefundCode(gw);
                        byId.setRefundAmount(byId.getPaymentAmount());
                        byId.setRefundStatus(2);
                        byId.setRefundSerialNumber(data.getTradeNo());
                    }
                }
                break;
        }
        shoppingOrderService.updateById(byId);
        return AjaxResult.success();
    }
    
    @GetMapping("/consignerShoppingOrder")
    @ApiOperation(value = "发货", tags = {"管理后台-购物订单"})
    public AjaxResult consignerShoppingOrder(String id, String companyName, String code) {
        TShoppingOrder byId = shoppingOrderService.getById(id);
        byId.setExpressCompany(companyName);
        byId.setExpressNumber(code);
        Long userid = tokenService.getLoginUser().getUserid();
        byId.setConsignerId(userid);
        byId.setConsignerTime(LocalDateTime.now());
        byId.setStatus(2);
        shoppingOrderService.updateById(byId);
        return AjaxResult.success();
    }
    
    @GetMapping("/getMyShoppingOrderList")
    @ApiOperation(value = "获取购买订单列表", tags = {"小程序-商城购买订单"})
    public AjaxResult<Map<String, Object>> getMyShoppingOrderList(GetMyShoppingOrderList query) {
        Map<String, Object> list = shoppingOrderService.getMyShoppingOrderList(query);
        return AjaxResult.success(list);
    }
    
    @GetMapping("/getMyShoppingOrderListNum")
    @ApiOperation(value = "获取购买订单列表数量", tags = {"小程序-商城购买订单"})
    public AjaxResult<Map<String, Object>> getMyShoppingOrderListNum() {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        long dfh = shoppingOrderService.count(new LambdaQueryWrapper<TShoppingOrder>().eq(TShoppingOrder::getDelFlag, 0)
                .eq(TShoppingOrder::getStatus, 1).eq(TShoppingOrder::getAppUserId, userId));
        long dsh = shoppingOrderService.count(new LambdaQueryWrapper<TShoppingOrder>().eq(TShoppingOrder::getDelFlag, 0)
                .eq(TShoppingOrder::getStatus, 2).eq(TShoppingOrder::getAppUserId, userId));
        long ywc = shoppingOrderService.count(new LambdaQueryWrapper<TShoppingOrder>().eq(TShoppingOrder::getDelFlag, 0)
                .eq(TShoppingOrder::getStatus, 3).eq(TShoppingOrder::getAppUserId, userId));
        Map<String, Object> map = new HashMap<>();
        map.put("dfh", dfh);
        map.put("dsh", dsh);
        map.put("ywc", ywc);
        return AjaxResult.success(map);
    }
    
    
    @GetMapping("/getMyShoppingOrderInfo/{id}")
    @ApiOperation(value = "获取购买订单详情", tags = {"小程序-商城购买订单", "管理后台-支付订单-订单信息"})
    public AjaxResult<MyShoppingOrderInfo> getMyShoppingOrderInfo(@PathVariable String id) {
        MyShoppingOrderInfo info = shoppingOrderService.getMyShoppingOrderInfo(id);
        return AjaxResult.success(info);
    }
    
    
    @PutMapping("/confirmReceipt/{id}")
    @ApiOperation(value = "确认收货操作", tags = {"小程序-商城购买订单"})
    public AjaxResult confirmReceipt(@PathVariable String id) {
        TShoppingOrder shoppingOrder = shoppingOrderService.getById(id);
        if (shoppingOrder.getStatus() == 3) {
            return AjaxResult.error("不能重复确认收货");
        }
        if (shoppingOrder.getStatus() == 1) {
            return AjaxResult.error("订单还未发货呢");
        }
        if (shoppingOrder.getStatus() == 4) {
            return AjaxResult.error("订单已取消,不允许操作。");
        }
        shoppingOrder.setStatus(3);
        shoppingOrderService.updateById(shoppingOrder);
        return AjaxResult.success();
    }
    
    
    @PutMapping("/cancelOrder/{id}")
    @ApiOperation(value = "取消订单操作", tags = {"小程序-商城购买订单"})
    public AjaxResult cancelOrder(@PathVariable String id) {
        return shoppingOrderService.cancelOrder(id);
    }
    
    /**
     * 商城订单取消微信退款回调
     */
    @PostMapping("/cancelShoppingOrderWxRefund")
    public void cancelShoppingOrderWxRefund(HttpServletRequest request) {
        WxRefundNotifyResp data = wxPaymentClient.refundNotify(request).getData();
        if (null != data) {
            String out_refund_no = data.getOut_refund_no();
            String refund_id = data.getRefund_id();
            String tradeState = data.getTradeState();
            String success_time = data.getSuccess_time();
            shoppingOrderService.cancelShoppingOrderWxRefund(out_refund_no, refund_id, tradeState, success_time);
        }
    }
    
    
    @ResponseBody
    @GetMapping(value = "/getNoInvoicedOrder")
    @ApiOperation(value = "获取未开票的订单数据", tags = {"小程序-充电发票"})
    public AjaxResult<List<MyShoppingOrderList>> getNoInvoicedOrder(GetNoInvoicedOrder query) {
        List<MyShoppingOrderList> list = shoppingOrderService.getNoInvoicedOrder(query);
        return AjaxResult.success(list);
    }
    
    @PostMapping("/create")
    public R<TShoppingOrder> shopCreate(@RequestBody ExchangeDto exchangeDto) {
        TShoppingOrder shoppingOrder = new TShoppingOrder();
        shoppingOrder.setTitle(exchangeDto.getTitle());
        shoppingOrder.setCode(OrderCodeUtil.getOrderCode("GW"));
        shoppingOrder.setAppUserId(exchangeDto.getUserId());
        shoppingOrder.setOrderType(exchangeDto.getGoodType());
        if (exchangeDto.getGoodType() == 1) {
            shoppingOrder.setGoodsId(exchangeDto.getGoodId());
        } else {
            shoppingOrder.setCouponId(exchangeDto.getGoodId());
        }
        shoppingOrder.setPurchaseQuantity(exchangeDto.getNum());
        shoppingOrder.setAppUserAddressId(exchangeDto.getAddressId());
        shoppingOrder.setOrderAmount(exchangeDto.getOrderPrice());
        if (exchangeDto.getCouponId() != null) {
            shoppingOrder.setAppCouponId(exchangeDto.getCouponId());
        }
        shoppingOrder.setCouponDiscountAmount(exchangeDto.getDiscountPrice());
        shoppingOrder.setVipDiscount(new BigDecimal("0"));
        shoppingOrder.setVipDiscountAmount(exchangeDto.getVipDiscount());
        shoppingOrder.setPaymentAmount(exchangeDto.getPayPrice());
        shoppingOrder.setPaymentStatus(1);
        shoppingOrder.setPaymentType(exchangeDto.getPayMethod());
        shoppingOrder.setRemark(exchangeDto.getRemark());
        shoppingOrder.setStatus(1);
        shoppingOrder.setPhone(exchangeDto.getPhone());
        shoppingOrder.setCreateTime(LocalDateTime.now());
        shoppingOrder.setDelFlag(false);
        shoppingOrderService.save(shoppingOrder);
        
        return R.ok(shoppingOrder);
        
    }
    
    @PostMapping("/callBack")
    public R callBack(@RequestParam("code") String code, @RequestParam("outTradeNo") String outTradeNo) {
        shoppingOrderService.callBack(code, outTradeNo);
        return R.ok();
        
    }
}