zhangxu
2022-12-06 46bedbc61c28f7c8c9a0e8740c87c78f0bf5a51d
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
package com.hx.phip.service.order.impl;
 
import com.alibaba.fastjson.JSON;
import com.hx.common.service.CommonService;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.common.ConsumePayConstants;
import com.hx.phiappt.common.OrderLogConstants;
import com.hx.phiappt.common.OrderTotalConstants;
import com.hx.phiappt.common.RefundSoruceConstants;
import com.hx.phiappt.model.BaseEntity;
import com.hx.phiappt.model.PaymentMethod;
import com.hx.phiappt.model.coupon.CouponNumber;
import com.hx.phiappt.model.coupon.CouponOrderDiscountLog;
import com.hx.phiappt.model.order.*;
import com.hx.phiappt.model.refund.*;
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.service.PaymentMethodService;
import com.hx.phip.service.order.OrderRefundService;
import com.hx.phip.service.order.OrderService;
import com.hx.phip.service.refund.RefundRecordItemService;
import com.hx.phip.service.refund.RefundRecordMethodService;
import com.hx.phip.service.refund.RefundRecordService;
import com.hx.phip.tool.CreateNo;
import com.hx.phip.util.api.ApiOrderUtil;
import com.hx.phip.util.api.PartialRefundUtil;
import com.hx.util.StringUtils;
import com.hz.crm.feign.FOderService;
import com.hz.his.dto.order.*;
import com.hz.his.feign.service.marketing.MOrderService;
import com.hz.his.feign.service.sync.SyncExecutionRecordService;
import com.hz.his.feign.service.sync.SyncOrderService;
import com.hz.his.feign.service.sync.SyncUserCardService;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @Author
 */
@Transactional
@Service
public class OrderRefundServiceImpl implements OrderRefundService {
 
    //log4j日志
    private static Logger logger = LoggerFactory.getLogger(OrderRefundServiceImpl.class.getName());
    @Resource
    private OrdersTotalMapper ordersTotalMapper;
    @Resource
    private OrderItemMapper orderItemMapper;
    @Resource
    private ConsumePayMapper consumePayMapper;
    @Resource
    private OrderService orderService;
    @Resource
    private UserProjectItemMapper userProjectItemMapper;
    @Resource
    private UserCardMapper userCardMapper;
    @Resource
    private UserCardUsedMapper userCardUsedMapper;
    @Resource
    private OrderItemSonMapper orderItemSonMapper;
    @Resource
    private RefundNoteMapper refundNoteMapper;
    @Resource
    private RefundNoteItemsMapper refundNoteItemsMapper;
 
    @Resource
    private CommonService commonService;
 
    @Resource
    private FOderService fOderService;
 
    @Resource
    private SyncOrderService syncOrderService;
    @Resource
    private SyncExecutionRecordService syncExecutionRecordService;
    @Resource
    private SyncUserCardService syncUserCardService;
 
    @Resource
    private SystemParameterMapper systemParameterMapper;
    @Resource
    private RefundRecordService refundRecordService;
    @Resource
    private PaymentMethodService paymentMethodService;
    @Resource
    private RefundRecordMethodService refundRecordMethodService;
    @Resource
    private RefundRecordItemService refundRecordItemService;
    @Resource
    private RefundRecordItemSourceMapper refundRecordItemSourceMapper;
    @Resource
    private CustomParameter customParameter;
    @Resource
    private MOrderService mOrderService;
 
    @Resource
    private CouponNumberMapper couponNumberMapper;
    @Resource
    private CouponOrderDiscountLogMapper couponOrderDiscountLogMapper;
    @Resource
    private RefundRecordCouponMapper refundRecordCouponMapper;
    @Override
    public PlatformResult partRefund(OrderRefundDto orderRefundDto) {
        logger.info("部分退款打印参数:{}", JSON.toJSONString(orderRefundDto));
        //先判断项目的状态是否存在
        OrdersTotal ordersTotal=null;
        try {
            ordersTotal=ordersTotalMapper.selectOneByKey(orderRefundDto.getOrderId());
            if(ordersTotal==null){
                logger.info("取消订单流程:未找到总订单信息!订单id:{}",orderRefundDto.getOrderId());
                return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:未找到总订单信息!");
            }
        }catch (Exception e){
            logger.info("取消订单流程:根据订单id查询多条总订单信息!订单id:{}",orderRefundDto.getOrderId());
            return PlatformResult.failure(PlatformCode.ERROR_TIPS,"取消订单流程:根据订单id查询多条总订单信息!");
        }
 
        Map<String, Object>refund =handlePartRefund(ordersTotal,orderRefundDto);
 
 
        return PlatformResult.success(refund);
    }
 
    public Map<String,Object>  handlePartRefund(OrdersTotal ordersTotal,OrderRefundDto orderRefundDto){
 
        //订单节点日志
        OrdersNodeLog ordersNodeLog = new OrdersNodeLog();
        StringBuilder orderNodeBuilder = new StringBuilder();
 
        orderNodeBuilder.append("开始部分退款");
 
        List<OrderPayMethodDto> refundPayMethod=orderRefundDto.getRefundPayMethod();
        List<OrderItemRefundDto> refundList=orderRefundDto.getRefundList();
        List<OrderCouponRefunDto> couponList=orderRefundDto.getCouponList();
 
        BigDecimal sumMoney=refundPayMethod.stream().map(OrderPayMethodDto::getMoney).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal refundSumMoney=refundList.stream().map(OrderItemRefundDto::getRefundMoney).reduce(BigDecimal.ZERO,BigDecimal::add);
        BigDecimal percentage=sumMoney.divide(refundSumMoney,2,BigDecimal.ROUND_HALF_UP);
 
        Map<String, String> operator = ApiOrderUtil.getOperator(commonService, orderRefundDto.getOperatorId());//获取操作人信息
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
 
 
        //退款总记录
        orderNodeBuilder.append("-处理退款总记录");
        String totalCode = CreateNo.createTimeNo(systemParameterMapper, "R", 16, "yyyyMMddHHmmss");//总订单编号
        RefundRecord refundRecord=new RefundRecord(totalCode,ordersTotal.getShopId(),ordersTotal.getShopName(),sumMoney, OrderTotalConstants.STATUS_REFUND_APPLY,0, RefundSoruceConstants.TYPE_SOURCE_ORDER,"phis部分退款",ordersTotal.getId(),ordersTotal.getUserId());
        refundRecordService.insert(refundRecord);
 
 
        //记录退回客户的支付方式
 
        RefundRecordItem refundRecordItem=null;
        RefundRecordMethod refundRecordMethod=null;
        RefundRecordItemSource refundRecordItemSource=null;
        orderNodeBuilder.append("-处理退款方式");
        for (OrderPayMethodDto orderPayMethodDto : refundPayMethod) {
            String payMethodNo = orderPayMethodDto.getPayMethodNo();//支付方式编码
            BigDecimal money = orderPayMethodDto.getMoney();//实退金额
            PaymentMethod paymentMethod = paymentMethodService.selectNumberNo(payMethodNo);
            if(paymentMethod==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该支付方式编码"+payMethodNo);
            }
            if(money ==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"实退金额为空");
            }
            refundRecordMethod=new RefundRecordMethod(payMethodNo,paymentMethod.getName(),money, ConsumePayConstants.STATUS_ORDER,ordersTotal.getId(),refundRecord.getId(),paymentMethod.getId());
            refundRecordMethod.setActualTotal(money);
            refundRecordMethod.setRealRefundTotal(money);//实退款总金额 后期需要修改
            refundRecordMethod.setRemarks(orderPayMethodDto.getRemarks());
            refundRecordMethodService.insert(refundRecordMethod);
        }
 
        List<OrderItemSourceRefundDto> orderItemSourceRefundDtos=null;
        String orderItemId =null;
        Integer refundNum =null;
        BigDecimal refundMoney =null;
        String refundReason =null;
        String type =null;
        OrderItem orderItem =null;
        OrderItemSon orderItemSon =null;
        orderNodeBuilder.append("-处理退款详情");
        for (OrderItemRefundDto orderItemRefundDto : refundList) {
            orderItemId = orderItemRefundDto.getOrderItemId();//orderItemId
            refundNum = orderItemRefundDto.getRefundNum();//实退数
            refundMoney = orderItemRefundDto.getRefundMoney().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);//实退金额
            refundReason = orderItemRefundDto.getRefundReason();//实退原因
            type =orderItemRefundDto.getType();
            orderItemSourceRefundDtos = orderItemRefundDto.getOrderItemSourceRefundDtos();
            orderItem = orderItemMapper.selectOneByKey(orderItemId);
            if(StringUtils.isEmpty(orderItemId)){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单的子订单id为空");
            }
            if(refundNum==null ){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退数");
            }
            if(refundMoney==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退金额");
            }
            if(orderItem==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该子项订单信息");
            }else{
                //保存退款详情订单
                refundRecordItem=new RefundRecordItem(orderItem.getGoodsNo(),orderItem.getGoodsName(),orderItem.getCommonId(),refundNum,refundMoney,refundReason,refundRecord.getId(),orderItem.getSpecs(),orderItem.getId());
                refundRecordItem.setType(type);
                refundRecordItem.setRealRefundNum(refundNum);//实退次数 后期需要修改
                refundRecordItem.setRealRefundTotal(refundMoney);//实退款总金额 后期需要修改
                refundRecordItemService.insert(refundRecordItem);
            }
            if(orderItemSourceRefundDtos == null || orderItemSourceRefundDtos.size()<1){
                continue;
            }
            for (OrderItemSourceRefundDto orderItemSourceRefundDto : orderItemSourceRefundDtos) {
                orderItemId = orderItemSourceRefundDto.getOrderItemSonId();//orderItemId
                refundNum = orderItemSourceRefundDto.getRefundNum();//实退数
                refundMoney = orderItemSourceRefundDto.getRefundMoney().multiply(percentage).setScale(2,BigDecimal.ROUND_HALF_UP);//实退金额
                refundReason = orderItemSourceRefundDto.getRefundReason();//实退原因
                type =orderItemSourceRefundDto.getType();
 
                orderItemSon = orderItemSonMapper.selectOneByKey(orderItemId);
                if(StringUtils.isEmpty(orderItemId)){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单的子订单id为空");
                }
                if(refundNum==null ){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退数");
                }
                if(refundMoney==null){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请正确填写退款实退金额");
                }
                if(orderItem==null){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"未找到该子项订单信息");
                }else{
                    //保存退款详情订单
                    refundRecordItemSource=new RefundRecordItemSource(type,orderItemSon.getGoodsNo(),orderItemSon.getGoodsName(),orderItemSon.getSpecs(),orderItemSon.getGoodsId(),refundNum,refundMoney,refundReason,refundRecord.getId(),refundRecordItem.getId(),orderItemSon.getId());
                    refundRecordItemSource.setRealRefundNum(refundNum);//实退次数 后期需要修改
                    refundRecordItemSource.setRealRefundTotal(refundMoney);//实退款总金额 后期需要修改
                    refundRecordItemSourceMapper.insert(refundRecordItemSource);
                }
 
            }
 
        }
        //处理优惠卷
        orderNodeBuilder.append("-处理退款优惠卷");
        CouponOrderDiscountLog couponOrderDiscountLog=null;
        RefundRecordCoupon refundRecordCoupon=null;
        for (OrderCouponRefunDto orderCouponRefunDto : couponList) {
            couponOrderDiscountLog=couponOrderDiscountLogMapper.selectOneByKey(orderCouponRefunDto.getOrderCouponId());
            if(couponOrderDiscountLog==null){
                throw new PlatTipsException(PlatformCode.ERROR_TIPS,"订单优惠卷id不正确");
            }
            refundRecordCoupon=new  RefundRecordCoupon(ordersTotal.getId(),refundRecordItem.getId(),ordersTotal.getUserId(),1,1,couponOrderDiscountLog.getId(),
                    couponOrderDiscountLog.getCouponId(),couponOrderDiscountLog.getTitle(),couponOrderDiscountLog.getCouponNumberId());
            refundRecordCouponMapper.insert(refundRecordCoupon);
        }
 
 
        String operationId=operator.get("operatorId");
        String operatorName=operator.get("operatorName");
 
        orderNodeBuilder.append("-处理退款支付方式,用户待执行项目");
        PartialRefundUtil.refundProcess(commonService, operationId, operatorName, refundRecord.getId(), customParameter,ordersTotal.getAppIdCode());
        //返回数据
        Map<String,Object> data=new HashMap<>();
        data.put("refundRecordId",refundRecord.getId());
        data.put("refundRecordCode",refundRecord.getCode());
 
 
        //记录操作日志
        orderNodeBuilder.append("-记录操作日志");
        StringBuffer stringBuffer=new StringBuffer("操作人:"+operator.get("operatorName")+",进行部分退款");
        OrderLog orderLog=new OrderLog(ordersTotal.getId(), OrderLogConstants.LOG_TYPE_REFUND,stringBuffer.toString()
                ,operator.get("operatorName"),operator.get("operatorId"),OrderTotalConstants.STATUS_CANCEL);
        commonService.insert(OrderLogMapper.class,orderLog);
 
        orderNodeBuilder.append("-结束处理退款");
        ordersNodeLog.setCommonType(OrdersNodeLog.TYPE_COMMON_ORDER_REFUND);
        ordersNodeLog.setContent(orderNodeBuilder.toString());
        ordersNodeLog.setOrderId(ordersTotal.getId());
        commonService.insert(OrdersNodeLogMapper.class,ordersNodeLog);
 
        return data;
    }
}