fwq
2024-03-04 36304ec32986dc5873992a84391769c0d4be1f3e
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package com.hx.phip.controller.refund;
 
import com.alibaba.fastjson.JSON;
import com.hx.common.service.CommonService;
import com.hx.exception.TipsException;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.common.OrderTotalConstants;
import com.hx.phiappt.dao.mapper.OrdersTotalMapper;
import com.hx.phiappt.model.BaseEntity;
import com.hx.phiappt.model.Employee;
import com.hx.phiappt.model.EmployeeRole;
import com.hx.phiappt.model.consume.ConsumeNotify;
import com.hx.phiappt.model.order.OrdersTotal;
import com.hx.phiappt.model.refund.RefundRecord;
import com.hx.phip.service.EmployeeRoleService;
import com.hx.phip.service.EmployeeService;
import com.hx.phip.service.OrdersTotalService;
import com.hx.phip.service.consume.ConsumeNotifyService;
import com.hx.phip.service.order.OrderRefundService;
import com.hx.phip.service.refund.RefundRecordService;
import com.hx.phip.tool.payment.ConsumeTool;
import com.hx.resultTool.Result;
import com.hx.util.StringUtils;
import com.hz.his.dto.order.OrderRefundDto;
import com.hz.his.vo.order.refund.RefundCancelVo;
import com.hz.his.vo.order.refund.qr.RefundQrVo;
import com.platform.constants.LoginConstant;
import com.platform.entity.ThirtApplication;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import com.platform.resultTool.PlatformResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Author  部分退款工具类
 */
@RestController
@RequestMapping("/order")
public class OrderRefundController {
 
    private static final Logger logger = LoggerFactory.getLogger(OrderRefundController.class);
 
    @Resource
    private OrderRefundService orderRefundService;
    @Resource
    private CommonService commonService;
    @Resource
    private OrdersTotalService ordersTotalService;
    @Resource
    private RefundRecordService refundRecordService;
    @Resource
    private EmployeeService employeeService;
    @Resource
    private EmployeeRoleService employeeRoleService;
    @Resource
    private ConsumeNotifyService consumeNotifyService;
 
 
    /** 部分退款显示数据接口-获取可退款数量*/
    @RequestMapping("/refund/details")
    public Result refundDetails(@RequestBody OrderRefundDto orderRefundDto){
        if (StringUtils.isEmpty(orderRefundDto.getOrderId())) {
            throw new TipsException("订单标识为空!");
        }
        OrdersTotal ordersTotal = ordersTotalService.selectOneByKey(orderRefundDto.getOrderId());
        if (ordersTotal == null) {
            throw new TipsException("订单标识错误!");
        }
        if(ordersTotal.getIsDel().equals(OrdersTotal.YES)){
            throw new TipsException("订单处于删除状态!");
        }
 
        if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
            throw new TipsException("充值订单不支持部分退款!");
        }
 
        if(OrderTotalConstants.PAY_STATUS_SUC != ordersTotal.getPayStatus()){
            throw new TipsException("订单未支付!");
        }
 
        if(OrderTotalConstants.STATUS_PAY != ordersTotal.getStatus()
                && OrderTotalConstants.STATUS_WAIT_RECEIVE != ordersTotal.getStatus()
                && OrderTotalConstants.STATUS_DONE != ordersTotal.getStatus()){
            throw new TipsException("当前订单状态不能退款!");
        }
 
        if(OrderTotalConstants.STATUS_REFUND_NONE != ordersTotal.getRefundStatus()
                && OrderTotalConstants.STATUS_REFUND_PART != ordersTotal.getRefundStatus()){
            throw new TipsException("订单退款状态不正确!");
        }
 
        //返回集合
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("details",ordersTotal);
        List<Map<String, Object>> refundDetails=orderRefundService.refundDetails(ordersTotal);
        returnMap.put("refundDetails",refundDetails);
 
        return Result.success(returnMap);
    }
 
    /** 选完数量点击下一步显示数据接口 */
    @RequestMapping("/refund/nextStep")
    public Result nextStep(@RequestBody OrderRefundDto orderRefundDto){
        if (StringUtils.isEmpty(orderRefundDto.getOrderId())) {
            throw new TipsException("订单标识为空!");
        }
        if (orderRefundDto.getRefundList()==null) {
            throw new TipsException("退款信息集合为空!");
        }
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("orderId", orderRefundDto.getOrderId());
 
        sqlSentence.setSqlSentence("SELECT * FROM  orders_total WHERE id = #{m.orderId} and isDel = #{m.isDel} ");
        OrdersTotal ordersTotal = ordersTotalService.selectOne(sqlSentence);
        if (ordersTotal == null) {
            throw new TipsException("订单标识错误!");
        }
        if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
            throw new TipsException("充值订单不支持部分退款!");
        }
        if(OrderTotalConstants.PAY_STATUS_SUC != ordersTotal.getPayStatus()){
            throw new TipsException("订单未支付!");
        }
 
        if(OrderTotalConstants.STATUS_PAY != ordersTotal.getStatus()
                && OrderTotalConstants.STATUS_WAIT_RECEIVE != ordersTotal.getStatus()
                && OrderTotalConstants.STATUS_DONE != ordersTotal.getStatus()){
            throw new TipsException("当前订单状态不能退款!");
        }
 
        if(OrderTotalConstants.STATUS_REFUND_NONE != ordersTotal.getRefundStatus()
                && OrderTotalConstants.STATUS_REFUND_PART != ordersTotal.getRefundStatus()){
            throw new TipsException("订单退款状态不正确!");
        }
 
        //返回集合
        OrderRefundDto returnMap = orderRefundService.nextStep(ordersTotal,orderRefundDto);
        returnMap.setRefundPayMethod(returnMap.getPayMethodList());
        returnMap.setCouponList(returnMap.getPayCouponList());
        return Result.success(returnMap);
    }
 
    /**
     * 部分退款
     */
    @RequestMapping(value = "/partial/refund",method = RequestMethod.POST)
    public PlatformResult partRefund(HttpServletRequest request, @RequestBody OrderRefundDto orderRefundDto)  {
        //操作平台
        ThirtApplication thirtApplication = (ThirtApplication) request.getSession().getAttribute(LoginConstant.LOGIN_APPLY);
        if(thirtApplication==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"签名失败,请检查签名!");
        }
 
        if(StringUtils.isEmpty(orderRefundDto.getOrderId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"订单标识不能为空");
        }
 
        if(StringUtils.isEmpty(orderRefundDto.getRoleId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作角色标识不能为空");
        }
        if(StringUtils.isEmpty(orderRefundDto.getOperatorId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工标识不能为空");
        }
 
        if(orderRefundDto.getIsApproval()==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"是否需要审批不能为空");
        }
 
        if(orderRefundDto.getIsRefund() == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"是否自动生成退款单不能为空");
        }
        if(orderRefundDto.getRefundStatistics() == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"退款统计类型不能为空!");
        }
 
        OrdersTotal ordersTotal = ordersTotalService.selectOneByKey(orderRefundDto.getOrderId());
        if (ordersTotal == null) {
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单标识错误!");
        }
        if(ordersTotal.getIsDel().equals(OrdersTotal.YES)){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单已删除!");
        }
        if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
            throw new TipsException("充值订单不支持部分退款!");
        }
 
        orderRefundDto.setRefundOperationType(BaseEntity.YES);
        return orderRefundService.partRefund(ordersTotal,orderRefundDto,thirtApplication);
    }
 
    /**
     * 全部退款
     */
    @RequestMapping(value = "/whole/refund",method = RequestMethod.POST)
    public PlatformResult wholeRefund(HttpServletRequest request,@RequestBody OrderRefundDto orderRefundDto)  {
        //操作平台
        ThirtApplication thirtApplication= (ThirtApplication) request.getSession().getAttribute(LoginConstant.LOGIN_APPLY);
        if(thirtApplication==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"签名失败,请检查签名!");
        }
 
        if(StringUtils.isEmpty(orderRefundDto.getOrderId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"订单id不能为空");
        }
        if(StringUtils.isEmpty(orderRefundDto.getOperatorId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作人不能为空");
        }
        if(orderRefundDto.getIsApproval()==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"是否需要审批不能为空");
        }
        if(orderRefundDto.getIsRefund()==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"是否自动生成退款单不能为空");
        }
        if(orderRefundDto.getRefundStatistics() == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"退款统计类型不能为空!");
        }
 
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("id", orderRefundDto.getOrderId());
        sqlSentence.setSqlSentence("SELECT * FROM orders_total  WHERE id = #{m.id}  and  isDel = #{m.isDel} ");
        OrdersTotal ordersTotal = commonService.selectOne(OrdersTotalMapper.class, sqlSentence);
        if (ordersTotal == null) {
            throw new TipsException("订单标识错误!");
        }
        if(!(OrderTotalConstants.PAY_STATUS_SUC==ordersTotal.getPayStatus())){
            throw new TipsException("订单未支付!");
        }
        if(!(OrderTotalConstants.STATUS_PAY==ordersTotal.getStatus())  && !(OrderTotalConstants.STATUS_WAIT_RECEIVE==ordersTotal.getStatus()) && !(OrderTotalConstants.STATUS_DONE==ordersTotal.getStatus())){
            throw new TipsException("订单不是已支付状态!");
        }
 
        if(OrderTotalConstants.STATUS_REFUND_NONE!=ordersTotal.getRefundStatus()){
            throw new TipsException("订单退款状态不正确!");
        }
 
        return orderRefundService.wholeRefund(orderRefundDto,thirtApplication);
    }
 
    /**
     * 伪造部分退款处理退款逻辑
     *
     */
    /*@RequestMapping(value = "/forge/partial/refund",method = RequestMethod.POST)*/
    public PlatformResult forgePartRefund(@RequestBody OrderRefundDto orderRefundDto)  {
        if(StringUtils.isEmpty(orderRefundDto.getOrderId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"订单id不能为空");
        }
        if(StringUtils.isEmpty(orderRefundDto.getOperatorId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作人不能为空");
        }
        if(orderRefundDto.getIsApproval()==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"是否需要审批不能为空");
        }
        if(orderRefundDto.getIsRefund()==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"是否自动生成退款单不能为空");
        }
        SqlSentence sqlSentence = new SqlSentence();
        Map<String, Object> sqlMap = new HashMap<>();
        sqlSentence.setM(sqlMap);
        sqlMap.put("isDel", BaseEntity.NO);
        sqlMap.put("id", orderRefundDto.getOrderId());
        sqlSentence.setSqlSentence("SELECT * FROM orders_total  WHERE id = #{m.id}  and  isDel = #{m.isDel} ");
        OrdersTotal ordersTotal = commonService.selectOne(OrdersTotalMapper.class, sqlSentence);
        if (ordersTotal == null) {
            throw new TipsException("订单标识错误!");
        }
        if(!(OrderTotalConstants.PAY_STATUS_SUC==ordersTotal.getPayStatus())){
            throw new TipsException("订单未支付!");
        }
        if(!(OrderTotalConstants.STATUS_PAY==ordersTotal.getStatus())  && !(OrderTotalConstants.STATUS_WAIT_RECEIVE==ordersTotal.getStatus()) && !(OrderTotalConstants.STATUS_DONE==ordersTotal.getStatus())){
            throw new TipsException("订单不是已支付状态!");
        }
 
        if(!(OrderTotalConstants.STATUS_REFUND_NONE==ordersTotal.getRefundStatus()) &&
                !(OrderTotalConstants.STATUS_REFUND_PART==ordersTotal.getRefundStatus())){
            throw new TipsException("订单退款状态不正确!");
        }
     /*   if (OrderTotalConstants.TYPE_RECHARGE.equals(ordersTotal.getType())) {
            throw new TipsException("充值订单不支持部分退款!");
        }*/
        orderRefundDto.setRefundOperationType(BaseEntity.YES);
        return orderRefundService.forgePartRefund(orderRefundDto);
    }
    /**
     * 根据订单 重新绑定订单退款状态
     *
     */
    @RequestMapping(value = "/verify/order/refundStatus",method = RequestMethod.POST)
    public PlatformResult verifyOrderRefundStatus(@RequestBody OrdersTotal ordersTotalDto)  {
        if(ordersTotalDto==null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"参数不能为空!");
        }
        if(StringUtils.isEmpty(ordersTotalDto.getId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"订单id不能为空");
        }
        orderRefundService.verifyOrderRefundStatus(ordersTotalDto);
        return PlatformResult.success();
    }
 
    /**
     * 作废订单退款单
     *
     */
    @RequestMapping(value = "/refund/cancel",method = RequestMethod.POST)
    public PlatformResult refundCancel(HttpServletRequest request,@RequestBody RefundCancelVo refundCancelVo)  {
        //操作平台
        ThirtApplication thirtApplication= (ThirtApplication) request.getSession().getAttribute(LoginConstant.LOGIN_APPLY);
        if(thirtApplication==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未获取到操作平台!");
        }
 
        if(StringUtils.isEmpty(refundCancelVo.getId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"退款单标识必传!");
        }
        RefundRecord refundRecord = refundRecordService.selectOneByKey(refundCancelVo.getId());
        if(refundRecord == null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"退款单标识错误!");
        }
        if(StringUtils.isEmpty(refundCancelVo.getRemarks())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"作废备注必填!");
        }
        if(StringUtils.isEmpty(refundCancelVo.getStaffId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工必填!");
        }
        Employee employee = employeeService.selectOneByKey(refundCancelVo.getStaffId());
        if(employee == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工标识错误!");
        }
        if(StringUtils.isEmpty(refundCancelVo.getStaffRoleId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工角色必填!");
        }
        EmployeeRole employeeRole = employeeRoleService.getEmployeeRole(refundCancelVo.getStaffRoleId());
        if(employeeRole == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工角色标识错误!");
        }
 
        orderRefundService.refundCancel(refundCancelVo,refundRecord,employee,employeeRole,thirtApplication);
        return PlatformResult.success();
    }
 
    /**
     * 订单二维码调起退款
     * 针对未支付的订单,但是已经使用了订单二维码支付
     * 只能全部退款,不能部分退款,逻辑删除支付回调记录
     */
    @RequestMapping(value = "/qr/refund",method = RequestMethod.POST)
    public PlatformResult orderQrRefund(HttpServletRequest request,@RequestBody RefundQrVo refundQrVo)  {
        logger.info("订单二维码调起退款数据:{}", JSON.toJSONString(refundQrVo));
 
        //操作平台
        ThirtApplication thirtApplication= (ThirtApplication) request.getSession().getAttribute(LoginConstant.LOGIN_APPLY);
        if(thirtApplication==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未获取到操作平台!");
        }
        logger.info("订单二维码调起退款操作平台:{},{},{}", thirtApplication.getAppId(),thirtApplication.getName(),thirtApplication.getAppIdCode());
 
        if(StringUtils.isEmpty(refundQrVo.getOrderId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"订单标识必传!");
        }
        if(refundQrVo.getRefundTotal() == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"退款金額!");
        }
        if(refundQrVo.getRefundTotal().compareTo(BigDecimal.ZERO) < 1){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"退款金額错误!");
        }
        if(StringUtils.isEmpty(refundQrVo.getPayNo())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"支付订单编号必传!");
        }
        if(StringUtils.isEmpty(refundQrVo.getEmployeeId())){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工必传!");
        }
        Employee employee = employeeService.selectOneByKey(refundQrVo.getEmployeeId());
        if(employee == null){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"操作员工标识错误!");
        }
 
        //找到回调信息
        ConsumeNotify consumeNotify = ConsumeTool.selectConsumeNotifyByOrderNo(refundQrVo.getOrderId(),refundQrVo.getPayNo(),refundQrVo.getPayNo(),commonService);
        if(consumeNotify == null){
            throw new TipsException("未找到支付记录!");
        }
 
        return PlatformResult.success( consumeNotifyService.consumeNotifyRefund(consumeNotify,employee,refundQrVo));
    }
 
 
}