Quellcode durchsuchen

精彩生活区-订单分账:向支付机构发起分账功能

family vor 1 Jahr
Ursprung
Commit
1e11bf40ef

+ 305 - 0
app/command/GreatLifeOrderIndependentAccount.php

@@ -0,0 +1,305 @@
1
+<?php
2
+
3
+namespace app\command;
4
+
5
+use app\common\model\store\order\StoreOrder;
6
+use app\common\model\user\UserCertification;
7
+use app\common\repositories\merchant\order\OrderGzcRepository;
8
+use app\common\repositories\merchant\order\OrderMerchantRepository;
9
+use app\common\repositories\store\order\StoreOrderRepository;
10
+use app\common\repositories\store\order\StoreOrderStatusRepository;
11
+use app\common\repositories\user\UserBillRepository;
12
+use app\common\repositories\user\UserRepository;
13
+use Carbon\Carbon;
14
+use think\console\Command;
15
+use think\console\Input;
16
+use think\console\Output;
17
+use think\facade\Db;
18
+use think\facade\Log;
19
+
20
+class GreatLifeOrderIndependentAccount extends Command
21
+{
22
+    protected function configure()
23
+    {
24
+        // 指令配置
25
+        $this->setName('great-life-order-independent-account')
26
+            ->setDescription('精彩生活区订单分账');
27
+    }
28
+
29
+    protected function execute(Input $input, Output $output)
30
+    {
31
+        // @todo 精彩生活区订单分账
32
+        $this->doGreatLifeOrderIndependentAccount();
33
+    }
34
+
35
+
36
+    /**
37
+     * 精彩生活区订单分账
38
+     *
39
+     * @param $timeLimit
40
+     * @param $merId
41
+     * @return void
42
+     * @throws \think\db\exception\DataNotFoundException
43
+     * @throws \think\db\exception\DbException
44
+     * @throws \think\db\exception\ModelNotFoundException
45
+     */
46
+    private function doGreatLifeOrderIndependentAccount($timeLimit = 30, $merId = 439)
47
+    {
48
+        $time = time();
49
+        // 订单状态(0:待发货;1:待收货;2:待评价;3:已完成;4已申请退款;-1:已退款)5未支付 6已取消'
50
+        $status = [2,3];
51
+        // 已支付、未结算
52
+        $where = [
53
+            'paid' => 1, 'gzc' => 0, 'mer_id' => $merId, 'is_del' => 0, 'is_system_del' => 0
54
+        ];
55
+        // 普通订单
56
+        $cursor = Db::table('rrx_store_order')->where($where)->where('pay_type', 'in', '1,2,4')->whereIn('status', $status)->cursor();
57
+
58
+        $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
59
+        $OrderMerchantRepository    = app()->make(OrderMerchantRepository::class);
60
+        $OrderGzcRepository         = app()->make(OrderGzcRepository::class);
61
+        // 商户信息
62
+        $merchant = Db::name('merchant')->where('mer_id', $merId)->find();
63
+
64
+        /** @var StoreOrderRepository $storeOrderRepository */
65
+        $storeOrderRepository = app()->make(StoreOrderRepository::class);
66
+
67
+        foreach ($cursor as $key => $value) {
68
+            $settlement_period_time = strtotime($value['create_time']) + $timeLimit * 24 * 3600;
69
+            // @todo 下单后第{$timeLimit}天后可分账
70
+            if ($settlement_period_time >= $time) {
71
+                // 成本价
72
+                $cost = $value['cost'];
73
+                // 利润金额
74
+                $con_pri = $value['con_pri'];
75
+                $group_order = Db::name('store_group_order')->alias('sgo')->where('sgo.group_order_id', $value['group_order_id'])->find();
76
+
77
+                // @todo 预分账处理
78
+                $isSuccessed = $storeOrderRepository->prepareExecuteIndependentAccount($value, $group_order, $merchant, $value['cost'], $value['con_pri']);
79
+
80
+                Db::name("log")->insert(array('data' => '精彩生活区-订单分账:' . json_encode($value,JSON_FORCE_OBJECT)));//日志记录
81
+
82
+//                Db::transaction(function () use ($certification, $online, $pension, $OrderGzcRepository, $OrderMerchantRepository, $StoreOrderRepository) {
83
+//                    //新增入账明细记录
84
+//                    $orderId   = $OrderMerchantRepository->getNewOrderId('IN10');
85
+//                    $create    = [
86
+//                        'uid'          => $online['uid'],
87
+//                        'platform_no'  => $orderId,
88
+//                        'account_type' => $online['pay_type'] == 1 ? 3 : 2,
89
+//                        'user_name'    => $certification['user_name'],
90
+//                        'mobile'       => $certification['mobile'],
91
+//                        'user_card'    => $certification['user_card'],
92
+//                        'pension'      => $pension,
93
+//                        'order_type'   => 2,
94
+//                        'out_trade_no' => $online['order_sn']
95
+//                    ];
96
+//                    $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
97
+//                    $check     = Db::name("gzc_logs")->where("out_trade_no", $online['order_sn'])->where("order_type", 2)->count();
98
+//                    if ($test_user == 0 && $check <= 0) {
99
+//                        $OrderGzcRepository->create($create);
100
+//                    }
101
+//                    $order                  = StoreOrder::getInstance()->find($online['order_id']);
102
+//                    $order->gzc             = 1;
103
+//                    $order->save();
104
+//                });
105
+            }
106
+        }
107
+
108
+        // 普通订单(红包,积分,京东,佣金,复购金,养老金,贡献值)
109
+//        $cursor = Db::table('rrx_store_order')->where('paid', 1)->where('is_settlement', 0)->where('pay_type', 'in', '0,1,2,4')->cursor();
110
+//        $StoreOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
111
+//        $OrderMerchantRepository    = app()->make(OrderMerchantRepository::class);
112
+//        $OrderGzcRepository         = app()->make(OrderGzcRepository::class);
113
+//        foreach ($cursor as $online) {
114
+//            $status = $StoreOrderStatusRepository->getWhere(['order_id' => $online['order_id'], 'change_type' => 'take']);
115
+//            if ($online['mer_id'] == 496) {
116
+//                if (!in_array($online['status'], [0, 1, 2, 3])) {
117
+//                    continue;
118
+//                }
119
+//                if (time() < strtotime($online['pay_time']) + 3600 * 24) {
120
+//                    Log::info('还没有到收货后' . $timeLimit . '天');
121
+//                    continue;
122
+//                }
123
+//            } else {
124
+//                if (!in_array($online['status'], [2, 3])) {
125
+//                    continue;
126
+//                }
127
+//                if (Carbon::now()->modify("-{$timeLimit} days")->lt($status->change_time ?? $status['change_time'])) {
128
+//                    Log::info('还没有到收货后' . $timeLimit . '天');
129
+//                    continue;
130
+//                }
131
+//            }
132
+//
133
+//            $StoreOrderRepository = app()->make(StoreOrderRepository::class);
134
+//            Db::transaction(function () use ($online, $StoreOrderRepository) {
135
+//                $order = StoreOrder::getInstance()->find($online['order_id']);
136
+//                $order->is_settlement   = 1;
137
+//                $order->settlement_time = date('Y-m-d H:i:s');
138
+//                $order->save();
139
+//
140
+//                // 结算积分
141
+//                $user_sign_score_list = Db::name('user_sign_score')
142
+//                    ->where('status', -1)
143
+//                    ->where('order_id', $online['order_id'])
144
+//                    ->whereIn('type', [1, 2])
145
+//                    ->select()
146
+//                    ->toArray();
147
+//                foreach ($user_sign_score_list as $user_sign_score) {
148
+//                    // 修改积分
149
+//                    Db::name('user')
150
+//                        ->where('uid', $user_sign_score['uid'])
151
+//                        ->inc('score', (float)$user_sign_score['reward_socre'])
152
+//                        ->update();
153
+//                    Db::name('user_sign_score')
154
+//                        ->where('status', -1)
155
+//                        ->where('id', $user_sign_score['id'])
156
+//                        ->update([
157
+//                            'status'          => 1,
158
+//                            'settlement_time' => date('Y-m-d H:i:s')
159
+//                        ]);
160
+//                }
161
+//
162
+//                // 结算红包
163
+//                $user_sign_hongbao_list = Db::name('user_sign_hongbao')
164
+//                    ->where('status', -1)
165
+//                    ->where('order_id', $online['order_id'])
166
+//                    ->whereIn('type', [1, 2])
167
+//                    ->select()
168
+//                    ->toArray();
169
+//                foreach ($user_sign_hongbao_list as $user_sign_hongbao) {
170
+//                    // 修改红包
171
+//                    Db::name('user')
172
+//                        ->where('uid', $user_sign_hongbao['uid'])
173
+//                        ->inc('hongbao', (float)$user_sign_hongbao['number'])
174
+//                        ->update();
175
+//                    Db::name('user_sign_hongbao')
176
+//                        ->where('status', -1)
177
+//                        ->where('id', $user_sign_hongbao['id'])
178
+//                        ->update([
179
+//                            'status'          => 1,
180
+//                            'settlement_time' => date('Y-m-d H:i:s')
181
+//                        ]);
182
+//                }
183
+//
184
+//
185
+//                // 结算京豆
186
+//                $user_sign_jingdou_list = Db::name('user_sign_jingdou')
187
+//                    ->where('status', -1)
188
+//                    ->where('order_id', $online['order_id'])
189
+//                    ->whereIn('type', [1, 2])
190
+//                    ->select()
191
+//                    ->toArray();
192
+//                foreach ($user_sign_jingdou_list as $user_sign_jingdou) {
193
+//                    // 修改京豆
194
+//                    Db::name('user')
195
+//                        ->where('uid', $user_sign_jingdou['uid'])
196
+//                        ->inc('jingdou', (float)$user_sign_jingdou['number'])
197
+//                        ->update();
198
+//                    Db::name('user_sign_jingdou')
199
+//                        ->where('status', -1)
200
+//                        ->where('id', $user_sign_jingdou['id'])
201
+//                        ->update([
202
+//                            'status'          => 1,
203
+//                            'settlement_time' => date('Y-m-d H:i:s')
204
+//                        ]);
205
+//                }
206
+//
207
+//
208
+//
209
+//                // 结算复购金
210
+//                $user_sign_fugou_list = Db::name('user_sign_fugou')
211
+//                    ->where('status', -1)
212
+//                    ->where('order_id', $online['order_id'])
213
+//                    ->whereIn('type', [1, 2])
214
+//                    ->select()
215
+//                    ->toArray();
216
+//                foreach ($user_sign_fugou_list as $user_sign_fugou) {
217
+//                    // 修改复购金
218
+//                    Db::name('user')
219
+//                        ->where('uid', $user_sign_fugou['uid'])
220
+//                        ->inc('fugou', (float)$user_sign_fugou['number'])
221
+//                        ->update();
222
+//                    Db::name('user_sign_fugou')
223
+//                        ->where('status', -1)
224
+//                        ->where('id', $user_sign_fugou['id'])
225
+//                        ->update([
226
+//                            'status'          => 1,
227
+//                            'settlement_time' => date('Y-m-d H:i:s')
228
+//                        ]);
229
+//                }
230
+//
231
+//
232
+//                // 结算贡献值
233
+//                $user_sign_gongxian_list = Db::name('user_sign_gongxian')
234
+//                    ->where('status', -1)
235
+//                    ->where('order_id', $online['order_id'])
236
+//                    ->whereIn('type', [1, 2])
237
+//                    ->select()
238
+//                    ->toArray();
239
+//                foreach ($user_sign_gongxian_list as $user_sign_gongxian) {
240
+//                    // 修改贡献值
241
+//                    Db::name('user')
242
+//                        ->where('uid', $user_sign_gongxian['uid'])
243
+//                        ->inc('contribute', (float)$user_sign_gongxian['number'])
244
+//                        ->update();
245
+//                    Db::name('user_sign_gongxian')
246
+//                        ->where('status', -1)
247
+//                        ->where('id', $user_sign_gongxian['id'])
248
+//                        ->update([
249
+//                            'status'          => 1,
250
+//                            'settlement_time' => date('Y-m-d H:i:s')
251
+//                        ]);
252
+//                }
253
+//
254
+//
255
+//                // 结算佣金
256
+//                $user_bill_list = Db::name('user_bill')
257
+//                    ->where('status', 0)
258
+//                    ->where('commission_type','<>', 5)
259
+//                    ->where([ 'link_id' => $online['order_id'], 'order_sn' => $online['order_sn']])
260
+//                    ->select()
261
+//                    ->toArray();
262
+//                foreach ($user_bill_list as $user_bill) {
263
+//                    if ($user_bill['commission_type'] != 5) {
264
+//                        if (in_array($user_bill['commission_type'], [7,12,19])) {
265
+//                            Db::name("merchant")
266
+//                                ->where("mer_id", $user_bill['mer_id'])
267
+//                                ->inc('brokerage_price', (float)$user_bill['number'])
268
+//                                ->update();
269
+//                        } else {
270
+//                            Db::name('user')
271
+//                                ->where('uid', $user_bill['uid'])
272
+//                                ->inc('brokerage_price', (float)$user_bill['number'])
273
+//                                ->update();
274
+//                        }
275
+//                    }
276
+//                    if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
277
+//                    } else {
278
+//                        // 虚拟支付的养老金不修改不结算
279
+//                        Db::name('user_bill')
280
+//                            ->where('status', 0)
281
+//                            ->where('bill_id', $user_bill['bill_id'])
282
+//                            ->update([
283
+//                                'status'    => 1,
284
+//                                'take_time' => time()
285
+//                            ]);
286
+//                    }
287
+//                    if ($online['pay_type'] == 0 && $user_bill['commission_type'] == 5) {
288
+//                    } else {
289
+//                        // 虚拟支付的养老金不修改不结算
290
+//                        Db::name('user_bill')
291
+//                            ->where('status', 0)
292
+//                            ->where('bill_id', $user_bill['bill_id'])
293
+//                            ->update([
294
+//                                'status'    => 1,
295
+//                                'take_time' => time()
296
+//                            ]);
297
+//                    }
298
+//                }
299
+//                //按角色结算
300
+//                $StoreOrderRepository->close_order($online['order_id']);
301
+//            });
302
+//        }
303
+    }
304
+
305
+}

+ 102 - 0
app/common/repositories/store/order/StoreOrderRepository.php

@@ -10499,5 +10499,107 @@ class StoreOrderRepository extends BaseRepository
10499
         Db::name("old_user_log")->insert($inser_data);
10499
         Db::name("old_user_log")->insert($inser_data);
10500
     }
10500
     }
10501
 
10501
 
10502
+    /**
10503
+     * 预分账处理
10504
+     *
10505
+     * @param $order
10506
+     * @param $group_order
10507
+     * @param $merchant
10508
+     * @param $merchant_profit
10509
+     * @param $platform_profit
10510
+     * @return bool
10511
+     * @throws DbException
10512
+     */
10513
+    public function prepareExecuteIndependentAccount($order, $group_order, $merchant, $merchant_profit, $platform_profit, $platform_mer_id = 496)
10514
+    {
10515
+        if ($order['pay_type'] == 1) {
10516
+            //微信
10517
+            if ($group_order['pay_wx'] == 3) {
10518
+                $hf_member_id = Db::name('merchant_member')->where('mer_id', $order['mer_id'])->where("type", 3)->value('member_id');
10519
+                $api_key = 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871';
10520
+                $type = 3;
10521
+            } else {
10522
+                $hf_member_id = Db::name('merchant_member')->where('mer_id', $order['mer_id'])->where("type", 2)->value('member_id');
10523
+                $api_key = 'api_live_90ef02e4-54ac-41ee-adf1-e4927d23d58f';
10524
+                $type = 2;
10525
+            }
10526
+
10527
+            // 平台特殊账户
10528
+            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $platform_mer_id, 'type' => $type])->value('member_id');
10529
+        } else {
10530
+            //支付宝
10531
+            $hf_member_id = $merchant['hf_member_id'];
10532
+            $api_key = 'api_live_b23537ed-64b8-45d6-832e-fb228aa9e0a9';
10533
+
10534
+            // 平台特殊账户
10535
+            $hf_platform_member_id = Db::name('merchant_member')->where(['mer_id' => $platform_mer_id, 'type' => 1])->value('member_id');
10536
+        }
10537
+
10538
+        $members[] = [
10539
+            'member_id' => $hf_member_id,
10540
+            'amount' => $merchant_profit,
10541
+            'fee_flag' => 'Y'
10542
+        ];
10543
+        if ($platform_profit > 0) {
10544
+            $members[] = [
10545
+                'member_id' => $hf_platform_member_id,
10546
+                'amount' => $platform_profit,
10547
+                'fee_flag' => 'N'
10548
+            ];
10549
+        }
10502
 
10550
 
10551
+        $requestId = ShouxinyiApiRequest::getOrderNumber('split');
10552
+        $params = [
10553
+            'payment_id' => $group_order['hf_pay_id'],
10554
+            'order_no' => $requestId,
10555
+            'confirm_amt' => $group_order['pay_price'],
10556
+            'div_members' => $members
10557
+        ];
10558
+
10559
+        $params['api_key'] = $api_key;
10560
+
10561
+        // 记录分账订单
10562
+        $this->recordIndependentAccount($order['mer_id'], $platform_mer_id, $group_order, $requestId, $merchant_profit, $platform_profit);
10563
+
10564
+        Db::name("log")->insert(array('data' => '汇付分账参数-----' . json_encode($params, JSON_FORCE_OBJECT)));//日志记录
10565
+        $res = $this->payment_confirm_create_traits($params);
10566
+        Db::name("log")->insert(array('data' => '汇付分账结果-----' . json_encode($res, JSON_FORCE_OBJECT)));//日志记录
10567
+        if ($res['status'] == 'succeeded') {
10568
+            Db::name("store_split_order")->where('requestId', $requestId)->update(['status' => 1, 'hf_pay_id' => $res['id']]);
10569
+            return true;
10570
+        }
10571
+        return false;
10572
+    }
10573
+
10574
+    /**
10575
+     * 记录分账订单
10576
+     *
10577
+     * @param $member_id
10578
+     * @param $platform_member_id
10579
+     * @param $group_order
10580
+     * @param $requestId
10581
+     * @param $merchant_profit
10582
+     * @param $platform_profit
10583
+     * @return void
10584
+     */
10585
+    private function recordIndependentAccount($member_id, $platform_member_id, $group_order, $requestId, $merchant_profit, $platform_profit)
10586
+    {
10587
+        $insertData = [
10588
+            [
10589
+                'mid' => $member_id,
10590
+                'requestId' => $requestId,
10591
+                'group_order_id' => $group_order['group_order_sn'],
10592
+                'splitAmount' => $merchant_profit,
10593
+                'status' => 0
10594
+            ],
10595
+            [
10596
+                'mid' => $platform_member_id,
10597
+                'requestId' => $requestId,
10598
+                'group_order_id' => $group_order['group_order_sn'],
10599
+                'splitAmount' => $platform_profit,
10600
+                'status' => 0
10601
+            ],
10602
+        ];
10603
+        Db::name("store_split_order")->insertAll($insertData);
10604
+    }
10503
 }
10605
 }