xiangpei
2025-05-09 641b4a3b1572f3a75ce0bd7d645e34252237cf9c
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
package cn.lili.modules.order.order.serviceimpl;
 
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.distribution.service.DistributionOrderService;
import cn.lili.modules.order.aftersale.entity.dos.AfterSale;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.order.order.entity.dos.StoreFlow;
import cn.lili.modules.order.order.entity.dto.StoreFlowProfitSharingDTO;
import cn.lili.modules.order.order.entity.dto.StoreFlowQueryDTO;
import cn.lili.modules.order.order.entity.enums.FlowTypeEnum;
import cn.lili.modules.order.order.entity.enums.PayStatusEnum;
import cn.lili.modules.order.order.entity.enums.ProfitSharingStatusEnum;
import cn.lili.modules.order.order.mapper.StoreFlowMapper;
import cn.lili.modules.order.order.service.OrderItemService;
import cn.lili.modules.order.order.service.OrderService;
import cn.lili.modules.order.order.service.StoreFlowService;
import cn.lili.modules.payment.entity.RefundLog;
import cn.lili.modules.payment.service.RefundLogService;
import cn.lili.modules.store.entity.dos.Bill;
import cn.lili.modules.store.entity.dto.BillSearchParams;
import cn.lili.modules.store.entity.vos.StoreFlowPayDownloadVO;
import cn.lili.modules.store.entity.vos.StoreFlowRefundDownloadVO;
import cn.lili.modules.store.service.BillService;
import cn.lili.modules.system.entity.dto.payment.WechatPaymentSetting;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
/**
 * 商家订单流水业务层实现
 *
 * @author Chopper
 * @since 2020/11/17 7:38 下午
 */
@Slf4j
@Service
public class StoreFlowServiceImpl extends ServiceImpl<StoreFlowMapper, StoreFlow> implements StoreFlowService {
 
    /**
     * 订单
     */
    @Autowired
    private OrderService orderService;
    /**
     * 订单货物
     */
    @Autowired
    private OrderItemService orderItemService;
    /**
     * 退款日志
     */
    @Autowired
    private RefundLogService refundLogService;
 
    @Autowired
    private BillService billService;
 
    @Autowired
    private DistributionOrderService distributionOrderService;
 
    /**
     * 店铺订单支付流水
     *
     * @param orderSn 订单编号
     */
    @Override
    public void payOrder(String orderSn) {
        //根据订单编号获取子订单列表
        List<OrderItem> orderItems = orderItemService.getByOrderSn(orderSn);
        //根据订单编号获取订单数据
        Order order = orderService.getBySn(orderSn);
 
        //循环子订单记录流水
        for (OrderItem item : orderItems) {
            StoreFlow storeFlow = new StoreFlow(order, item, FlowTypeEnum.PAY);
            saveProfitSharing(storeFlow);
 
            //添加付款交易流水
            this.save(storeFlow);
        }
    }
 
    @Override
    public void orderCancel(String orderSn) {
        //根据订单编号获取订单数据
        Order order = orderService.getBySn(orderSn);
        // 判断订单是否是付款
        if (!PayStatusEnum.PAID.name()
                .equals((order.getPayStatus()))) {
            return;
        }
        List<OrderItem> items = orderItemService.getByOrderSn(order.getSn());
        List<StoreFlow> storeFlows = new ArrayList<>();
 
        //修改付款记录
        this.update(new LambdaUpdateWrapper<StoreFlow>()
                .eq(StoreFlow::getOrderSn, order.getSn())
                .set(StoreFlow::getBillTime, new Date())
                .set(StoreFlow::getProfitSharingStatus, ProfitSharingStatusEnum.ORDER_CANCEL.name())
                .set(StoreFlow::getFullRefund, true));
 
        //记录退款记录
        for (OrderItem item : items) {
            StoreFlow storeFlow = new StoreFlow(order, item, FlowTypeEnum.REFUND);
            storeFlow.setProfitSharingStatus(ProfitSharingStatusEnum.ORDER_CANCEL.name());
            storeFlow.setBillTime(new Date());
            storeFlows.add(storeFlow);
        }
        this.saveBatch(storeFlows);
    }
 
 
    /**
     * 店铺订单退款流水
     *
     * @param afterSale 售后单
     */
    @Override
    public void refundOrder(AfterSale afterSale) {
        StoreFlow storeFlow = new StoreFlow();
        //退款
        storeFlow.setFlowType(FlowTypeEnum.REFUND.name());
        storeFlow.setSn(SnowFlake.createStr("SF"));
        storeFlow.setRefundSn(afterSale.getSn());
        storeFlow.setOrderSn(afterSale.getOrderSn());
        storeFlow.setOrderItemSn(afterSale.getOrderItemSn());
        storeFlow.setStoreId(afterSale.getStoreId());
        storeFlow.setStoreName(afterSale.getStoreName());
        storeFlow.setMemberId(afterSale.getMemberId());
        storeFlow.setMemberName(afterSale.getMemberName());
        storeFlow.setGoodsId(afterSale.getGoodsId());
        storeFlow.setGoodsName(afterSale.getGoodsName());
        storeFlow.setSkuId(afterSale.getSkuId());
        storeFlow.setImage(afterSale.getGoodsImage());
        storeFlow.setSpecs(afterSale.getSpecs());
 
 
 
        //获取付款信息
        StoreFlow payStoreFlow = this.getOne(new LambdaUpdateWrapper<StoreFlow>().eq(StoreFlow::getOrderItemSn, afterSale.getOrderItemSn())
                .eq(StoreFlow::getFlowType, FlowTypeEnum.PAY));
        //申请商品退款数量
        storeFlow.setNum(afterSale.getNum());
        //分类ID
        storeFlow.setCategoryId(payStoreFlow.getCategoryId());
        //佣金 = (佣金/订单商品数量)* 售后商品数量
        storeFlow.setCommissionPrice(CurrencyUtil.mul(CurrencyUtil.div(payStoreFlow.getCommissionPrice(), payStoreFlow.getNum()), afterSale.getNum()));
        //分销佣金 =(分销佣金/订单商品数量)* 售后商品数量
        storeFlow.setDistributionRebate(CurrencyUtil.mul(CurrencyUtil.div(payStoreFlow.getDistributionRebate(), payStoreFlow.getNum()), afterSale.getNum()));
        //流水金额 = 支付最终结算金额
        storeFlow.setFinalPrice(afterSale.getActualRefundPrice());
        //站点优惠券补贴返还金额=(站点优惠券补贴金额/购买商品数量)*退款商品数量
        storeFlow.setSiteCouponCommission(CurrencyUtil.mul(CurrencyUtil.div(payStoreFlow.getSiteCouponCommission() == null ? 0 : payStoreFlow.getSiteCouponCommission(), payStoreFlow.getNum()), afterSale.getNum()));
        //平台优惠券 使用金额
        storeFlow.setSiteCouponPrice(payStoreFlow.getSiteCouponPrice());
        //站点优惠券佣金比例
        storeFlow.setSiteCouponPoint(payStoreFlow.getSiteCouponPoint());
 
        // 退单结算金额 相当于付款结算金额反计算逻辑,对平台收取的佣金,分销收取的佣金进行返还,对平台优惠券的补贴应该相加
        // 由于退单的结算金额为正数,所以需要将结算金额计算方式,相较于付款结算金额计算方式进行反转
 
        // 退款结算金额 = flowPrice(实际退款金额) + storeCouponCommission(getSiteCouponCommission) - platFormCommission(平台收取交易佣金) - distributionCommission(单品分销返现支出) ")
 
        storeFlow.setBillPrice(
                CurrencyUtil.add(
                        afterSale.getActualRefundPrice(),
                        storeFlow.getSiteCouponCommission(),
                        -storeFlow.getCommissionPrice(),
                        -storeFlow.getDistributionRebate()
                )
        );
        //退款日志
        RefundLog refundLog = refundLogService.queryByAfterSaleSn(afterSale.getSn());
        //第三方流水单号
        storeFlow.setTransactionId(refundLog.getReceivableNo());
        //支付方式
        storeFlow.setPaymentName(refundLog.getPaymentName());
 
 
 
        //修改付款的StoreFlow
        StoreFlowProfitSharingDTO storeFlowProfitSharingDTO = JSONUtil.toBean(payStoreFlow.getProfitSharing(), StoreFlowProfitSharingDTO.class);
        if (storeFlow.getBillPrice()
                .equals(payStoreFlow.getFinalPrice())) {
            payStoreFlow.setFullRefund(true);
            storeFlowProfitSharingDTO.setPrice(0D);
            storeFlowProfitSharingDTO.setStorePrice(0D);
            storeFlowProfitSharingDTO.setPlatformPrice(0D);
            storeFlowProfitSharingDTO.setDistributionPrice(0D);
            storeFlowProfitSharingDTO.setSubsidies(0D);
            payStoreFlow.setBillTime(new Date());
            payStoreFlow.setProfitSharingStatus(ProfitSharingStatusEnum.ORDER_CANCEL.name());
            //设置退款时间
            storeFlow.setBillTime(new Date());
        } else {
            //计算 累计订单退款金额,修改分账信息
            Integer refundNum = this.baseMapper.getRefundNum(payStoreFlow.getOrderItemSn());
            refundNum = refundNum == null ? 0 : refundNum;
            int allNum = storeFlow.getNum() + refundNum;
            Double proportion = CurrencyUtil.div((payStoreFlow.getNum() - allNum), payStoreFlow.getNum());
            if (proportion.equals(0D)) {
                payStoreFlow.setFullRefund(true);
                storeFlowProfitSharingDTO.setPrice(0D);
                storeFlowProfitSharingDTO.setStorePrice(0D);
                storeFlowProfitSharingDTO.setPlatformPrice(0D);
                storeFlowProfitSharingDTO.setDistributionPrice(0D);
                storeFlowProfitSharingDTO.setSubsidies(0D);
                payStoreFlow.setBillTime(new Date());
                payStoreFlow.setProfitSharingStatus(ProfitSharingStatusEnum.ORDER_CANCEL.name());
                //设置退款时间
                storeFlow.setBillTime(new Date());
            } else {
                storeFlowProfitSharingDTO.setPrice(CurrencyUtil.mul(payStoreFlow.getFinalPrice(), proportion));
                storeFlowProfitSharingDTO.setStorePrice(CurrencyUtil.mul(payStoreFlow.getBillPrice(), proportion));
                storeFlowProfitSharingDTO.setPlatformPrice(CurrencyUtil.mul(payStoreFlow.getCommissionPrice(), proportion));
                storeFlowProfitSharingDTO.setSubsidies(CurrencyUtil.mul(payStoreFlow.getSiteCouponCommission(), proportion));
                storeFlowProfitSharingDTO.setDistributionPrice(CurrencyUtil.mul(payStoreFlow.getDistributionRebate(), proportion));
            }
        }
        payStoreFlow.setProfitSharing(JSONUtil.toJsonStr(storeFlowProfitSharingDTO));
        //修改付款流水
        this.updateById(payStoreFlow);
        //保存退款流水·
        this.save(storeFlow);
    }
 
 
    @Override
    public IPage<StoreFlow> getStoreFlow(StoreFlowQueryDTO storeFlowQueryDTO) {
        return this.page(PageUtil.initPage(storeFlowQueryDTO.getPageVO()), generatorQueryWrapper(storeFlowQueryDTO));
    }
 
    @Override
    public StoreFlow queryOne(StoreFlowQueryDTO storeFlowQueryDTO) {
        return this.getOne(generatorQueryWrapper(storeFlowQueryDTO));
    }
 
    @Override
    public List<StoreFlowPayDownloadVO> getStoreFlowPayDownloadVO(StoreFlowQueryDTO storeFlowQueryDTO) {
        return baseMapper.getStoreFlowPayDownloadVO(generatorQueryWrapper(storeFlowQueryDTO));
    }
 
    @Override
    public List<StoreFlowRefundDownloadVO> getStoreFlowRefundDownloadVO(StoreFlowQueryDTO storeFlowQueryDTO) {
        return baseMapper.getStoreFlowRefundDownloadVO(generatorQueryWrapper(storeFlowQueryDTO));
    }
 
 
    @Override
    public IPage<StoreFlow> getStoreFlow(String id, String type, PageVO pageVO) {
        Bill bill = billService.getById(id);
        return this.getStoreFlow(StoreFlowQueryDTO.builder().type(type).pageVO(pageVO).bill(bill).build());
    }
 
    @Override
    public IPage<StoreFlow> getDistributionFlow(String id, PageVO pageVO) {
        Bill bill = billService.getById(id);
        return this.getStoreFlow(StoreFlowQueryDTO.builder().pageVO(pageVO).bill(bill).build());
    }
 
    @Override
    public List<StoreFlow> listStoreFlow(StoreFlowQueryDTO storeFlowQueryDTO) {
        return this.list(generatorQueryWrapper(storeFlowQueryDTO));
    }
 
    @Override
    public void updateProfitSharingStatus() {
        //获取已完成的列表,进行相关的处理
        List<StoreFlow> storeFlowList = this.baseMapper.completeList();
 
        for (StoreFlow storeFlow : storeFlowList) {
            distributionOrderService.completeOrder(storeFlow);
        }
        this.baseMapper.updateProfitSharingStatus();
    }
 
    @Override
    public Bill getRefundBill(BillSearchParams searchParams) {
        return this.baseMapper.getRefundBill(searchParams.queryWrapper());
    }
 
    @Override
    public Bill getOrderBill(BillSearchParams searchParams) {
        return this.baseMapper.getOrderBill(searchParams.queryWrapper());
    }
 
 
    /**
     * 生成查询wrapper
     *
     * @param storeFlowQueryDTO 搜索参数
     * @return 查询wrapper
     */
    private LambdaQueryWrapper generatorQueryWrapper(StoreFlowQueryDTO storeFlowQueryDTO) {
 
 
        LambdaQueryWrapper<StoreFlow> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //分销订单过滤是否判定
        lambdaQueryWrapper.isNotNull(storeFlowQueryDTO.getJustDistribution() != null && storeFlowQueryDTO.getJustDistribution(),
                StoreFlow::getDistributionRebate);
 
        //流水类型判定
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(storeFlowQueryDTO.getType()),
                StoreFlow::getFlowType, storeFlowQueryDTO.getType());
 
        //售后编号判定
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(storeFlowQueryDTO.getRefundSn()),
                StoreFlow::getRefundSn, storeFlowQueryDTO.getRefundSn());
 
        //订单编号判定
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(storeFlowQueryDTO.getOrderSn()),
                StoreFlow::getOrderSn, storeFlowQueryDTO.getOrderSn());
 
        //订单货物编号
        lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(storeFlowQueryDTO.getOrderItemSn()),
                StoreFlow::getOrderItemSn, storeFlowQueryDTO.getOrderItemSn());
        //结算单非空,则校对结算单参数
        if (storeFlowQueryDTO.getBill() != null) {
            Bill bill = storeFlowQueryDTO.getBill();
            lambdaQueryWrapper.eq(CharSequenceUtil.isNotEmpty(bill.getStoreId()), StoreFlow::getStoreId, bill.getStoreId());
            lambdaQueryWrapper.ge(bill.getStartTime() != null && bill.getEndTime() != null,
                    StoreFlow::getBillTime, bill.getStartTime());
            lambdaQueryWrapper.lt(bill.getStartTime() != null && bill.getEndTime() != null,
                    StoreFlow::getBillTime, bill.getEndTime());
        }
        return lambdaQueryWrapper;
    }
 
 
    /**
     * 添加分账内容
     *
     * @param storeFlow 店铺流水
     */
    private void saveProfitSharing(StoreFlow storeFlow) {
 
        StoreFlowProfitSharingDTO storeFlowProfitSharingDTO = new StoreFlowProfitSharingDTO();
        //店铺获取
        storeFlowProfitSharingDTO.setStorePrice(storeFlow.getBillPrice());
        //平台佣金
        storeFlowProfitSharingDTO.setPlatformPrice(storeFlow.getCommissionPrice());
        //分销佣金
        storeFlowProfitSharingDTO.setDistributionPrice(storeFlow.getDistributionRebate());
        //总金额
        storeFlowProfitSharingDTO.setPrice(storeFlow.getFinalPrice());
        //平台补差
        storeFlowProfitSharingDTO.setSubsidies(storeFlow.getSiteCouponCommission());
        //分账详情
        storeFlow.setProfitSharing(JSONUtil.toJsonStr(storeFlowProfitSharingDTO));
        //分账状态
        storeFlow.setProfitSharingStatus(ProfitSharingStatusEnum.WAIT_COMPLETE.name());
 
    }
}