GzcAddByBillCommand.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\command;
  3. use think\console\Command;
  4. use think\console\Input;
  5. use think\console\Output;
  6. use think\facade\{Db, Log};
  7. class GzcAddByBillCommand extends Command
  8. {
  9. protected function configure()
  10. {
  11. $this->setName('gzcAddByBill')
  12. ->setDescription('从bill表中查询未提交到公证处的养老金记录,并提交公证处');
  13. }
  14. protected function execute(Input $input, Output $output)
  15. {
  16. $output->writeln('定时任务执行中...');
  17. // $this->billGzcAdd();
  18. $gzcLogList = Db::name("gzc_logs")
  19. ->where('link_id', '=', 0)
  20. ->select()
  21. ->toArray();
  22. $orderSnList = array_column($gzcLogList, 'out_trade_no');
  23. $userBillList = Db::name('user_bill')
  24. ->where('status', 1)
  25. ->where('commission_type', 5)
  26. ->whereIn('order_sn', $orderSnList)
  27. ->select()
  28. ->toArray();
  29. $userBillMap = [];
  30. foreach ($userBillList as $key => $value) {
  31. $userBillMap[$value['order_sn'] . '_' . $value['uid'] . '_' . $value['number']] = [
  32. 'bill_id' => $value['bill_id'],
  33. 'type_shop' => $value['type_shop']
  34. ];
  35. }
  36. unset($userBillList);
  37. $str = '';
  38. foreach ($gzcLogList as $value) {
  39. if (isset($userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']])) {
  40. $update = [
  41. 'link_id' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['bill_id'],
  42. 'order_type' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['type_shop'],
  43. ];
  44. if (empty($value['platform_no'])) {
  45. $update['platform_no'] = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  46. }
  47. Db::name("gzc_logs")
  48. ->where('id', '=', $value['id'])
  49. ->update($update);
  50. $str .= $value['id'] . '、';
  51. }
  52. }
  53. echo $str;
  54. // $gzcLogList = Db::query('SELECT out_trade_no, order_type, uid, pension FROM rrx_gzc_logs GROUP BY out_trade_no, order_type, uid, pension HAVING COUNT(*) > 1');
  55. $output->writeln('定时任务执行完成');
  56. }
  57. public function billGzcAdd()
  58. {
  59. $testUserIdList = Db::name('user')
  60. ->where('test_user', 1)
  61. ->column('uid');
  62. // 1. 使用chunk分批处理
  63. Db::name('user_bill')
  64. ->where('status', 1)
  65. ->where('gzc', 0)
  66. ->where('commission_type', 5)
  67. ->whereNotIn('uid', $testUserIdList)
  68. ->chunk(100, function ($bills) {
  69. $this->processBills($bills);
  70. });
  71. }
  72. protected function processBills($bills)
  73. {
  74. // 2. 预加载用户实名认证信息
  75. $userIds = array_unique(array_column($bills->toArray(), 'uid'));
  76. $certifications = Db::name('user_certification')
  77. ->whereIn('uid', $userIds)
  78. ->column('user_name,mobile,user_card', 'uid');
  79. // 3. 订单类型映射配置
  80. $orderMap = [
  81. 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]],
  82. 1 => ['table' => 'rrx_order_pdd'],
  83. 2 => ['table' => 'rrx_order_vip'],
  84. 3 => ['table' => 'rrx_order_su'],
  85. 5 => ['table' => 'rrx_order_tb'],
  86. 6 => ['table' => 'rrx_order_jd'],
  87. 9 => ['table' => 'rrx_order_huafei'],
  88. 10 => [
  89. 'table' => 'rrx_order_merchant',
  90. 'condition' => [['paid', '=', 1], ['status', '=', 1]],
  91. 'field_map' => ['order_sn' => 'out_trade_no']
  92. ],
  93. ];
  94. foreach ($bills as $bill) {
  95. $typeShop = $bill['type_shop'] ?? 0;
  96. // 4. 跳过无效类型
  97. if (in_array($typeShop, [4, 7, 8])) continue;
  98. // 5. 使用映射配置查询订单
  99. $online = $this->getOrderData($orderMap, $typeShop, $bill);
  100. if (!$online) {
  101. Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在");
  102. continue;
  103. }
  104. // 6. 检查实名认证
  105. if (!isset($certifications[$bill['uid']])) {
  106. Log::info("用户{$bill['uid']}未实名认证");
  107. continue;
  108. }
  109. // 7. 验证养老金金额
  110. $pension = $bill['number'] ?? 0;
  111. if ($pension <= 0) {
  112. Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}");
  113. continue;
  114. }
  115. // 8. 事务处理
  116. Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) {
  117. // 检查是否已存在记录
  118. $exists = Db::name("gzc_logs")
  119. ->where("link_id", $bill['bill_id'])
  120. ->where("order_type", $typeShop)
  121. ->count();
  122. if ($exists) return;
  123. // 创建唯一订单ID
  124. $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  125. Db::name("gzc_logs")->insert([
  126. 'uid' => $bill['uid'],
  127. 'platform_no' => $orderId,
  128. 'account_type' => 1,
  129. 'user_name' => $certifications[$bill['uid']]['user_name'],
  130. 'mobile' => $certifications[$bill['uid']]['mobile'],
  131. 'user_card' => $certifications[$bill['uid']]['user_card'],
  132. 'pension' => $pension,
  133. 'order_type' => $typeShop,
  134. 'out_trade_no' => $online['order_sn'],
  135. 'link_id' => $bill['bill_id'],
  136. 'create_time' => date('Y-m-d H:i:s'),
  137. 'update_time' => date('Y-m-d H:i:s')
  138. ]);
  139. Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([
  140. 'is_gzc' => 1,
  141. 'gzc' => 1
  142. ]);
  143. });
  144. }
  145. }
  146. protected function getOrderData($orderMap, $typeShop, $bill)
  147. {
  148. $config = $orderMap[$typeShop] ?? $orderMap[0];
  149. $query = Db::table($config['table'])
  150. ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']);
  151. if (!empty($config['condition'])) {
  152. $query->where($config['condition']);
  153. }
  154. $result = $query->find();
  155. // 字段映射处理
  156. if ($result && isset($config['field_map'])) {
  157. foreach ($config['field_map'] as $src => $dest) {
  158. $result[$src] = $result[$dest] ?? null;
  159. }
  160. }
  161. return $result;
  162. }
  163. }