GzcAddByBillCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. /** 修复 历史数据脚本 */
  19. // $gzcLogList = Db::name("gzc_logs")
  20. // ->where('link_id', '=', 0)
  21. // ->select()
  22. // ->toArray();
  23. // $orderSnList = array_column($gzcLogList, 'out_trade_no');
  24. // $userBillList = Db::name('user_bill')
  25. // ->where('status', 1)
  26. // ->where('commission_type', 5)
  27. // ->whereIn('order_sn', $orderSnList)
  28. // ->select()
  29. // ->toArray();
  30. // $userBillMap = [];
  31. // foreach ($userBillList as $key => $value) {
  32. // $userBillMap[$value['order_sn'] . '_' . $value['uid'] . '_' . $value['number']] = [
  33. // 'bill_id' => $value['bill_id'],
  34. // 'type_shop' => $value['type_shop']
  35. // ];
  36. // }
  37. // unset($userBillList);
  38. // $str = '';
  39. // foreach ($gzcLogList as $value) {
  40. // if (isset($userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']])) {
  41. // $update = [
  42. // 'link_id' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['bill_id'],
  43. // 'order_type' => $userBillMap[$value['out_trade_no'] . '_' . $value['uid'] . '_' . $value['pension']]['type_shop'],
  44. // ];
  45. // if (empty($value['platform_no'])) {
  46. // $update['platform_no'] = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  47. // }
  48. // Db::name("gzc_logs")
  49. // ->where('id', '=', $value['id'])
  50. // ->update($update);
  51. // $str .= $value['id'] . '、';
  52. // }
  53. // }
  54. // echo $str;
  55. // // 定时任务执行中...
  56. // // 181、274、275、277、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、336352、353、354、355、356、357、358、359、360、361、362、363、364、365、366、367、368、369、370、371、372、373、374、375、376、377、378、379、380、381、382、383、384、385、386、387、388、389、390、391、392、393、394、395、396、398、400、401、402、403、404、405、406、407、408、409、410、411、412、413、414、415431、432、433、434、435、436、437、438、439、440、441、442、443、444、445、446、447、448、449、450、451、452、453、454、455、456、457、458、459、460、461、462、463、464、465、466、467、468、469、470、471、472、473、474、475、476、477、478、479、480、481、482、483、484、485、486、487、488、489、490、491、492508、509、510、511、512、513、514、515、516、517、518、519、520、521、522、523、524、525、526、527、528、529、530、1591、1607、1608、1632、1633、1634、1649、1650、1651、1652、2108、2109、2110、2111、3687、3688、3689、3690、3691、3692、3693、3694、3695、3696、3697、3698、3699、3700、3701、3702、3703、3704、3705、3706、3707、3708、
  57. // // 定时任务执行完成
  58. // /** 删除重复数据脚本 */
  59. // $gzcLogList = Db::query('SELECT link_id FROM rrx_gzc_logs WHERE link_id != 0 GROUP BY link_id HAVING COUNT(*) > 1');
  60. // $linkIdList = array_column($gzcLogList, 'link_id');
  61. // $gzcLogList = Db::name("gzc_logs")
  62. // ->whereIn('link_id', $linkIdList)
  63. // ->field('id, gzc, success, link_id')
  64. // ->order('id ASC')
  65. // ->select()
  66. // ->toArray();
  67. // $gzcLogMap = [];
  68. // foreach ($gzcLogList as $item) {
  69. // $gzcLogMap[$item['link_id']][] = $item;
  70. // }
  71. //
  72. // $delAllIdList = [];
  73. // foreach ($gzcLogMap as $gzcLogArr) {
  74. // $gzcIdList = [];
  75. // $allIds = [];
  76. // // 单次遍历同时收集所有ID和需要保留的ID
  77. // foreach ($gzcLogArr as $gzcLog) {
  78. // $allIds[] = $gzcLog['id'];
  79. // if ($gzcLog['gzc'] != 0) {
  80. // $gzcIdList[] = $gzcLog['id'];
  81. // }
  82. // }
  83. // // 确定需要删除的ID列表
  84. // if (!empty($gzcIdList)) {
  85. // // 保留有gzc标志的记录,删除其他
  86. // $delIdList = array_diff($allIds, $gzcIdList);
  87. // } else {
  88. // // 保留第一条记录,删除其他
  89. // $delIdList = array_slice($allIds, 1);
  90. // }
  91. // $delAllIdList = array_merge($delAllIdList, $delIdList);
  92. // }
  93. //
  94. // // 如果后续需要索引连续
  95. // $delAllIdList = array_values($delAllIdList);
  96. // $res = Db::name("gzc_logs")
  97. // ->whereIn('id', $delAllIdList)
  98. // ->delete();
  99. // echo implode('、', $delAllIdList);
  100. // echo 'deleteNum' . $res;
  101. // // 定时任务执行中...
  102. // // 2327、2362、2375、2413、2414、2487、2536、2550、2564、320、2576、2578、2579、2580、2581、2588、2589、2599、2601、2602、2603、2604、2605、2606、2607、2608、2614、2638、2666、2680、2706、2707、2721、2729、2736、2739、2744、2748、2811、2859、2860、2861、2897、2928、2933、3264、3277、3279、3571、3572、3573、357、1590、3673、3675、1631、3678、3679、3680、3681、2106、3683、2107、3684、2112、3685、2113、3686
  103. // // deleteNum78
  104. // // 定时任务执行完成
  105. $output->writeln('定时任务执行完成');
  106. }
  107. public function billGzcAdd()
  108. {
  109. $testUserIdList = Db::name('user')
  110. ->where('test_user', 1)
  111. ->column('uid');
  112. // 1. 使用chunk分批处理
  113. Db::name('user_bill')
  114. ->where('status', 1)
  115. ->where('gzc', 0)
  116. ->where('commission_type', 5)
  117. ->whereNotIn('uid', $testUserIdList)
  118. ->chunk(100, function ($bills) {
  119. $this->processBills($bills);
  120. });
  121. }
  122. protected function processBills($bills)
  123. {
  124. // 2. 预加载用户实名认证信息
  125. $userIds = array_unique(array_column($bills->toArray(), 'uid'));
  126. $certifications = Db::name('user_certification')
  127. ->whereIn('uid', $userIds)
  128. ->column('user_name,mobile,user_card', 'uid');
  129. // 3. 订单类型映射配置
  130. $orderMap = [
  131. 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]],
  132. 1 => ['table' => 'rrx_order_pdd'],
  133. 2 => ['table' => 'rrx_order_vip'],
  134. 3 => ['table' => 'rrx_order_su'],
  135. 5 => ['table' => 'rrx_order_tb'],
  136. 6 => ['table' => 'rrx_order_jd'],
  137. 9 => ['table' => 'rrx_order_huafei'],
  138. 10 => [
  139. 'table' => 'rrx_order_merchant',
  140. 'condition' => [['paid', '=', 1], ['status', '=', 1]],
  141. 'field_map' => ['order_sn' => 'out_trade_no']
  142. ],
  143. ];
  144. foreach ($bills as $bill) {
  145. $typeShop = $bill['type_shop'] ?? 0;
  146. // 4. 跳过无效类型
  147. if (in_array($typeShop, [4, 7, 8])) continue;
  148. // 5. 使用映射配置查询订单
  149. $online = $this->getOrderData($orderMap, $typeShop, $bill);
  150. if (!$online) {
  151. Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在");
  152. continue;
  153. }
  154. // 6. 检查实名认证
  155. if (!isset($certifications[$bill['uid']])) {
  156. Log::info("用户{$bill['uid']}未实名认证");
  157. continue;
  158. }
  159. // 7. 验证养老金金额
  160. $pension = $bill['number'] ?? 0;
  161. if ($pension <= 0) {
  162. Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}");
  163. continue;
  164. }
  165. // 8. 事务处理
  166. Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) {
  167. // 检查是否已存在记录
  168. $exists = Db::name("gzc_logs")
  169. ->where("link_id", $bill['bill_id'])
  170. ->where("order_type", $typeShop)
  171. ->count();
  172. if ($exists) return;
  173. // 创建唯一订单ID
  174. $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  175. Db::name("gzc_logs")->insert([
  176. 'uid' => $bill['uid'],
  177. 'platform_no' => $orderId,
  178. 'account_type' => 1,
  179. 'user_name' => $certifications[$bill['uid']]['user_name'],
  180. 'mobile' => $certifications[$bill['uid']]['mobile'],
  181. 'user_card' => $certifications[$bill['uid']]['user_card'],
  182. 'pension' => $pension,
  183. 'order_type' => $typeShop,
  184. 'out_trade_no' => $online['order_sn'],
  185. 'link_id' => $bill['bill_id'],
  186. 'create_time' => date('Y-m-d H:i:s'),
  187. 'update_time' => date('Y-m-d H:i:s')
  188. ]);
  189. Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([
  190. 'is_gzc' => 1,
  191. 'gzc' => 1
  192. ]);
  193. });
  194. }
  195. }
  196. protected function getOrderData($orderMap, $typeShop, $bill)
  197. {
  198. $config = $orderMap[$typeShop] ?? $orderMap[0];
  199. $query = Db::table($config['table'])
  200. ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']);
  201. if (!empty($config['condition'])) {
  202. $query->where($config['condition']);
  203. }
  204. $result = $query->find();
  205. // 字段映射处理
  206. if ($result && isset($config['field_map'])) {
  207. foreach ($config['field_map'] as $src => $dest) {
  208. $result[$src] = $result[$dest] ?? null;
  209. }
  210. }
  211. return $result;
  212. }
  213. }