GzcAddByBillCommand.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $output->writeln('定时任务执行完成');
  19. }
  20. public function billGzcAdd()
  21. {
  22. $testUserIdList = Db::name('user')
  23. ->where('test_user', 1)
  24. ->column('uid');
  25. // 1. 使用chunk分批处理
  26. Db::name('user_bill')
  27. ->where('status', 1)
  28. ->where('is_gzc', 0)
  29. ->where('commission_type', 5)
  30. ->whereNotIn('uid', $testUserIdList)
  31. ->chunk(100, function ($bills) {
  32. $this->processBills($bills);
  33. });
  34. }
  35. protected function processBills($bills)
  36. {
  37. // 2. 预加载用户实名认证信息
  38. $userIds = array_unique(array_column($bills->toArray(), 'uid'));
  39. $certifications = Db::name('user_certification')
  40. ->whereIn('uid', $userIds)
  41. ->column('user_name,mobile,user_card', 'uid');
  42. // 3. 订单类型映射配置
  43. $orderMap = [
  44. 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]],
  45. 1 => ['table' => 'rrx_order_pdd'],
  46. 2 => ['table' => 'rrx_order_vip'],
  47. 3 => ['table' => 'rrx_order_su'],
  48. 5 => ['table' => 'rrx_order_tb'],
  49. 6 => ['table' => 'rrx_order_jd'],
  50. 9 => ['table' => 'rrx_order_huafei'],
  51. 10 => [
  52. 'table' => 'rrx_order_merchant',
  53. 'condition' => [['paid', '=', 1], ['status', '=', 1]],
  54. 'field_map' => ['order_sn' => 'out_trade_no']
  55. ],
  56. ];
  57. foreach ($bills as $bill) {
  58. $typeShop = $bill['type_shop'] ?? 0;
  59. // 4. 跳过无效类型
  60. if (in_array($typeShop, [4, 7, 8])) continue;
  61. // 5. 使用映射配置查询订单
  62. $online = $this->getOrderData($orderMap, $typeShop, $bill);
  63. if (!$online) {
  64. Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在");
  65. continue;
  66. }
  67. // 6. 检查实名认证
  68. if (!isset($certifications[$bill['uid']])) {
  69. Log::info("用户{$bill['uid']}未实名认证");
  70. continue;
  71. }
  72. // 7. 验证养老金金额
  73. $pension = $bill['number'] ?? 0;
  74. if ($pension <= 0) {
  75. Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}");
  76. continue;
  77. }
  78. // 8. 事务处理
  79. Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) {
  80. // 检查是否已存在记录
  81. $exists = Db::name("gzc_logs")
  82. ->where("link_id", $bill['bill_id'])
  83. ->where("order_type", $typeShop)
  84. ->count();
  85. if ($exists) return;
  86. // 创建唯一订单ID
  87. $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  88. Db::name("gzc_logs")->insert([
  89. 'uid' => $bill['uid'],
  90. 'platform_no' => $orderId,
  91. 'account_type' => 1,
  92. 'user_name' => $certifications[$bill['uid']]['user_name'],
  93. 'mobile' => $certifications[$bill['uid']]['mobile'],
  94. 'user_card' => $certifications[$bill['uid']]['user_card'],
  95. 'pension' => $pension,
  96. 'order_type' => $typeShop,
  97. 'out_trade_no' => $online['order_sn'],
  98. 'link_id' => $bill['bill_id'],
  99. 'create_time' => date('Y-m-d H:i:s'),
  100. 'update_time' => date('Y-m-d H:i:s')
  101. ]);
  102. Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([
  103. 'is_gzc' => 1,
  104. 'gzc' => 1
  105. ]);
  106. });
  107. }
  108. }
  109. protected function getOrderData($orderMap, $typeShop, $bill)
  110. {
  111. $config = $orderMap[$typeShop] ?? $orderMap[0];
  112. $query = Db::table($config['table'])
  113. ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']);
  114. if (!empty($config['condition'])) {
  115. $query->where($config['condition']);
  116. }
  117. $result = $query->find();
  118. // 字段映射处理
  119. if ($result && isset($config['field_map'])) {
  120. foreach ($config['field_map'] as $src => $dest) {
  121. $result[$src] = $result[$dest] ?? null;
  122. }
  123. }
  124. return $result;
  125. }
  126. }