|
|
@@ -2,166 +2,147 @@
|
|
2
|
2
|
|
|
3
|
3
|
namespace app\command;
|
|
4
|
4
|
|
|
5
|
|
-use app\common\model\user\UserCertification;
|
|
6
|
|
-use app\common\repositories\merchant\order\OrderGzcRepository;
|
|
7
|
|
-use app\common\repositories\merchant\order\OrderMerchantRepository;
|
|
8
|
|
-use app\common\repositories\store\order\StoreOrderRepository;
|
|
9
|
|
-use app\common\repositories\user\UserRepository;
|
|
10
|
5
|
use think\console\Command;
|
|
11
|
6
|
use think\console\Input;
|
|
12
|
7
|
use think\console\Output;
|
|
13
|
|
-use think\db\exception\DataNotFoundException;
|
|
14
|
|
-use think\db\exception\DbException;
|
|
15
|
|
-use think\db\exception\ModelNotFoundException;
|
|
16
|
|
-use think\facade\Db;
|
|
17
|
|
-use think\facade\Log;
|
|
|
8
|
+use think\facade\{Db, Log};
|
|
18
|
9
|
|
|
19
|
10
|
class GzcAddByBillCommand extends Command
|
|
20
|
11
|
{
|
|
21
|
12
|
protected function configure()
|
|
22
|
13
|
{
|
|
23
|
|
- // 配置命令名称及描述
|
|
24
|
14
|
$this->setName('gzcAddByBill')
|
|
25
|
15
|
->setDescription('从bill表中查询未提交到公证处的养老金记录,并提交公证处');
|
|
26
|
16
|
}
|
|
27
|
17
|
|
|
28
|
18
|
protected function execute(Input $input, Output $output)
|
|
29
|
19
|
{
|
|
30
|
|
- // 在这里执行任务的具体内容
|
|
31
|
20
|
$output->writeln('定时任务执行中...');
|
|
32
|
|
-
|
|
33
|
|
- $nowTime = time();
|
|
34
|
|
- $this->billGzcAdd($nowTime);
|
|
35
|
|
-
|
|
|
21
|
+ $this->billGzcAdd();
|
|
36
|
22
|
$output->writeln('定时任务执行完成');
|
|
37
|
23
|
}
|
|
38
|
24
|
|
|
39
|
|
- /**
|
|
40
|
|
- * 线下支付订单公证处IN10入账
|
|
41
|
|
- * @param $nowTime
|
|
42
|
|
- * author: php迷途小书童
|
|
43
|
|
- * created: 2021/3/20 9:26
|
|
44
|
|
- */
|
|
45
|
|
- public function billGzcAdd($nowTime)
|
|
|
25
|
+ public function billGzcAdd()
|
|
46
|
26
|
{
|
|
47
|
|
- $OrderGzcRepository = app()->make(OrderGzcRepository::class);
|
|
48
|
|
- $OrderMerchantRepository = app()->make(OrderMerchantRepository::class);
|
|
49
|
|
- $StoreOrderRepository = app()->make(StoreOrderRepository::class);
|
|
50
|
|
- $testUserIdList = [];
|
|
51
|
|
- /** @var UserRepository $userRepository */
|
|
52
|
|
- $userRepository = app()->make(UserRepository::class);
|
|
53
|
|
- try {
|
|
54
|
|
- $userIdData = $userRepository->selectWhere(['test_user' => 1], 'uid')->toArray();
|
|
55
|
|
- if (!empty($userIdData)) {
|
|
56
|
|
- $testUserIdList = array_column($userIdData, 'uid');
|
|
|
27
|
+ $testUserIdList = Db::name('user')
|
|
|
28
|
+ ->where('test_user', 1)
|
|
|
29
|
+ ->column('uid');
|
|
|
30
|
+ // 1. 使用chunk分批处理
|
|
|
31
|
+ Db::name('user_bill')
|
|
|
32
|
+ ->where('status', 1)
|
|
|
33
|
+ ->where('is_gzc', 0)
|
|
|
34
|
+ ->where('commission_type', 5)
|
|
|
35
|
+ ->whereNotIn('uid', $testUserIdList)
|
|
|
36
|
+ ->chunk(100, function ($bills) {
|
|
|
37
|
+ $this->processBills($bills);
|
|
|
38
|
+ });
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
|
41
|
+ protected function processBills($bills)
|
|
|
42
|
+ {
|
|
|
43
|
+ // 2. 预加载用户实名认证信息
|
|
|
44
|
+ $userIds = array_unique(array_column($bills->toArray(), 'uid'));
|
|
|
45
|
+ $certifications = Db::name('user_certification')
|
|
|
46
|
+ ->whereIn('uid', $userIds)
|
|
|
47
|
+ ->column('user_name,mobile,user_card', 'uid');
|
|
|
48
|
+
|
|
|
49
|
+ // 3. 订单类型映射配置
|
|
|
50
|
+ $orderMap = [
|
|
|
51
|
+ 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]],
|
|
|
52
|
+ 1 => ['table' => 'rrx_order_pdd'],
|
|
|
53
|
+ 2 => ['table' => 'rrx_order_vip'],
|
|
|
54
|
+ 3 => ['table' => 'rrx_order_su'],
|
|
|
55
|
+ 5 => ['table' => 'rrx_order_tb'],
|
|
|
56
|
+ 6 => ['table' => 'rrx_order_jd'],
|
|
|
57
|
+ 9 => ['table' => 'rrx_order_huafei'],
|
|
|
58
|
+ 10 => [
|
|
|
59
|
+ 'table' => 'rrx_order_merchant',
|
|
|
60
|
+ 'condition' => [['paid', '=', 1], ['status', '=', 1]],
|
|
|
61
|
+ 'field_map' => ['order_sn' => 'out_trade_no']
|
|
|
62
|
+ ],
|
|
|
63
|
+ ];
|
|
|
64
|
+
|
|
|
65
|
+ foreach ($bills as $bill) {
|
|
|
66
|
+ $typeShop = $bill['type_shop'] ?? 0;
|
|
|
67
|
+
|
|
|
68
|
+ // 4. 跳过无效类型
|
|
|
69
|
+ if (in_array($typeShop, [4, 7, 8])) continue;
|
|
|
70
|
+
|
|
|
71
|
+ // 5. 使用映射配置查询订单
|
|
|
72
|
+ $online = $this->getOrderData($orderMap, $typeShop, $bill);
|
|
|
73
|
+
|
|
|
74
|
+ if (!$online) {
|
|
|
75
|
+ Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在");
|
|
|
76
|
+ continue;
|
|
57
|
77
|
}
|
|
58
|
|
- } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
|
|
59
|
78
|
|
|
60
|
|
- }
|
|
|
79
|
+ // 6. 检查实名认证
|
|
|
80
|
+ if (!isset($certifications[$bill['uid']])) {
|
|
|
81
|
+ Log::info("用户{$bill['uid']}未实名认证");
|
|
|
82
|
+ continue;
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ // 7. 验证养老金金额
|
|
|
86
|
+ $pension = $bill['number'] ?? 0;
|
|
|
87
|
+ if ($pension <= 0) {
|
|
|
88
|
+ Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}");
|
|
|
89
|
+ continue;
|
|
|
90
|
+ }
|
|
61
|
91
|
|
|
62
|
|
- try {
|
|
63
|
|
- $cursor = Db::name('user_bill')
|
|
64
|
|
- ->where('status', 1)
|
|
65
|
|
- ->where('is_gzc', 0)
|
|
66
|
|
- ->where('commission_type', 5)
|
|
67
|
|
- ->whereNotIn('uid', $testUserIdList)
|
|
68
|
|
- ->select();
|
|
69
|
|
- } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
|
|
|
92
|
+ // 8. 事务处理
|
|
|
93
|
+ Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) {
|
|
|
94
|
+ // 检查是否已存在记录
|
|
|
95
|
+ $exists = Db::name("gzc_logs")
|
|
|
96
|
+ ->where("link_id", $bill['bill_id'])
|
|
|
97
|
+ ->where("order_type", $typeShop)
|
|
|
98
|
+ ->count();
|
|
70
|
99
|
|
|
71
|
|
- }
|
|
|
100
|
+ if ($exists) return;
|
|
72
|
101
|
|
|
73
|
|
- if(!empty($cursor)) {
|
|
74
|
|
- foreach ($cursor as $bill) {
|
|
75
|
|
- $type_shop = $bill['type_shop'] ?? 0;
|
|
76
|
|
- switch ($type_shop){
|
|
77
|
|
- case "0":
|
|
78
|
|
- $online = Db::table('rrx_store_order')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->where('paid', 1)->find();
|
|
79
|
|
- break;
|
|
80
|
|
- case "1":
|
|
81
|
|
- $online = Db::table('rrx_order_pdd')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->find();
|
|
82
|
|
- break;
|
|
83
|
|
- case "2":
|
|
84
|
|
- $online = Db::table('rrx_order_vip')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->find();
|
|
85
|
|
- break;
|
|
86
|
|
- case "3":
|
|
87
|
|
- $online = Db::table('rrx_order_su')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->find();
|
|
88
|
|
- break;
|
|
89
|
|
- case "4":
|
|
90
|
|
- $online = '';
|
|
91
|
|
- break;
|
|
92
|
|
- case "5":
|
|
93
|
|
- $online = Db::table('rrx_order_tb')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->find();
|
|
94
|
|
- break;
|
|
95
|
|
- case "6":
|
|
96
|
|
- $online = Db::table('rrx_order_jd')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->find();
|
|
97
|
|
- break;
|
|
98
|
|
- case "7":
|
|
99
|
|
- $online = '';
|
|
100
|
|
- break;
|
|
101
|
|
- case "8":
|
|
102
|
|
- $online = '';
|
|
103
|
|
- break;
|
|
104
|
|
- case "9":
|
|
105
|
|
- $online = Db::table('rrx_order_huafei')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->find();
|
|
106
|
|
- break;
|
|
107
|
|
- case "10":
|
|
108
|
|
- $online = Db::table('rrx_order_merchant')
|
|
109
|
|
- ->where('uid', $bill['uid'])
|
|
110
|
|
- ->where('out_trade_no', $bill['order_sn'])
|
|
111
|
|
- ->where('paid', 1)
|
|
112
|
|
- ->where('status', 1)
|
|
113
|
|
- ->find();
|
|
114
|
|
- if (!empty($online)) {
|
|
115
|
|
- $online['order_sn'] = $online['out_trade_no'];
|
|
116
|
|
- }
|
|
117
|
|
- break;
|
|
118
|
|
- default:
|
|
119
|
|
- $online = Db::table('rrx_store_order')->where('uid', $bill['uid'])->where('order_sn', $bill['order_sn'])->where('paid', 1)->find();
|
|
120
|
|
- }
|
|
121
|
|
- if (!$online) {
|
|
122
|
|
- Log::info($bill['uid'] . '查询不到订单');
|
|
123
|
|
- continue;
|
|
124
|
|
- }
|
|
125
|
|
-
|
|
126
|
|
- $certification = UserCertification::getInstance()->where('uid', $bill['uid'])->find();
|
|
127
|
|
- if (!$certification) {
|
|
128
|
|
- Log::info($online['uid'] . '没有实名认证');
|
|
129
|
|
- continue;
|
|
130
|
|
- }
|
|
131
|
|
-
|
|
132
|
|
- $pension = $bill['number'] ?? 0;
|
|
133
|
|
- if ($pension <= 0) {
|
|
134
|
|
- Log::info($online['order_sn'] . 'pension' . $pension);
|
|
135
|
|
- continue;
|
|
136
|
|
- }
|
|
137
|
|
-
|
|
138
|
|
- //新增入账明细记录
|
|
139
|
|
- $orderId = $OrderMerchantRepository->getNewOrderId('IN10');
|
|
140
|
|
- $create = [
|
|
141
|
|
- 'uid' => $online['uid'],
|
|
142
|
|
- 'platform_no' => $orderId,
|
|
|
102
|
+ // 创建唯一订单ID
|
|
|
103
|
+ $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
|
|
|
104
|
+
|
|
|
105
|
+ Db::name("gzc_logs")->insert([
|
|
|
106
|
+ 'uid' => $bill['uid'],
|
|
|
107
|
+ 'platform_no' => $orderId,
|
|
143
|
108
|
'account_type' => 1,
|
|
144
|
|
- 'user_name' => $certification['user_name'],
|
|
145
|
|
- 'mobile' => $certification['mobile'],
|
|
146
|
|
- 'user_card' => $certification['user_card'],
|
|
147
|
|
- 'pension' => $pension,
|
|
148
|
|
- 'order_type' => $type_shop,
|
|
|
109
|
+ 'user_name' => $certifications[$bill['uid']]['user_name'],
|
|
|
110
|
+ 'mobile' => $certifications[$bill['uid']]['mobile'],
|
|
|
111
|
+ 'user_card' => $certifications[$bill['uid']]['user_card'],
|
|
|
112
|
+ 'pension' => $pension,
|
|
|
113
|
+ 'order_type' => $typeShop,
|
|
149
|
114
|
'out_trade_no' => $online['order_sn'],
|
|
150
|
|
- 'link_id'=> $bill['bill_id']
|
|
151
|
|
- ];
|
|
152
|
|
- $test_user = Db::name('user')->where('uid', $online['uid'])->value('test_user');
|
|
153
|
|
- $check = Db::name("gzc_logs")->where("link_id", $bill['bill_id'])->where("order_type", $type_shop)->count();
|
|
154
|
|
- if ($test_user == 0 && $check <= 0) {
|
|
155
|
|
- $OrderGzcRepository->create($create);
|
|
156
|
|
- Db::name('user_bill')
|
|
157
|
|
- ->where('bill_id', $bill['bill_id'])
|
|
158
|
|
- ->where('commission_type', 5)
|
|
159
|
|
- ->update([
|
|
160
|
|
- 'is_gzc' => 1,
|
|
161
|
|
- 'gzc' => 1
|
|
162
|
|
- ]);
|
|
163
|
|
- }
|
|
|
115
|
+ 'link_id' => $bill['bill_id']
|
|
|
116
|
+ ]);
|
|
|
117
|
+
|
|
|
118
|
+ Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([
|
|
|
119
|
+ 'is_gzc' => 1,
|
|
|
120
|
+ 'gzc' => 1
|
|
|
121
|
+ ]);
|
|
|
122
|
+ });
|
|
|
123
|
+ }
|
|
|
124
|
+ }
|
|
|
125
|
+
|
|
|
126
|
+ protected function getOrderData($orderMap, $typeShop, $bill)
|
|
|
127
|
+ {
|
|
|
128
|
+ $config = $orderMap[$typeShop] ?? $orderMap[0];
|
|
|
129
|
+ $query = Db::table($config['table'])
|
|
|
130
|
+ ->where('uid', $bill['uid'])
|
|
|
131
|
+ ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']);
|
|
|
132
|
+
|
|
|
133
|
+ if (!empty($config['condition'])) {
|
|
|
134
|
+ $query->where($config['condition']);
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ $result = $query->find();
|
|
|
138
|
+
|
|
|
139
|
+ // 字段映射处理
|
|
|
140
|
+ if ($result && isset($config['field_map'])) {
|
|
|
141
|
+ foreach ($config['field_map'] as $src => $dest) {
|
|
|
142
|
+ $result[$src] = $result[$dest] ?? null;
|
|
164
|
143
|
}
|
|
165
|
144
|
}
|
|
|
145
|
+
|
|
|
146
|
+ return $result;
|
|
166
|
147
|
}
|
|
167
|
148
|
}
|