GzcAddByBillCommand.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. $delAllIdList = [];
  72. foreach ($gzcLogMap as $gzcLogArr) {
  73. $gzcIdList = [];
  74. $allIds = [];
  75. // 单次遍历同时收集所有ID和需要保留的ID
  76. foreach ($gzcLogArr as $gzcLog) {
  77. $allIds[] = $gzcLog['id'];
  78. if ($gzcLog['gzc'] != 0) {
  79. $gzcIdList[] = $gzcLog['id'];
  80. }
  81. }
  82. // 确定需要删除的ID列表
  83. if (!empty($gzcIdList)) {
  84. // 保留有gzc标志的记录,删除其他
  85. $delIdList = array_diff($allIds, $gzcIdList);
  86. } else {
  87. // 保留第一条记录,删除其他
  88. $delIdList = array_slice($allIds, 1);
  89. }
  90. $delAllIdList = array_merge($delAllIdList, $delIdList);
  91. }
  92. // 如果后续需要索引连续
  93. $delAllIdList = array_values($delAllIdList);
  94. $res = Db::name("gzc_logs")
  95. ->whereIn('id', $delAllIdList)
  96. ->delete();
  97. echo implode('、', $delAllIdList);
  98. echo 'deleteNum' . $res;
  99. $output->writeln('定时任务执行完成');
  100. }
  101. public function billGzcAdd()
  102. {
  103. $testUserIdList = Db::name('user')
  104. ->where('test_user', 1)
  105. ->column('uid');
  106. // 1. 使用chunk分批处理
  107. Db::name('user_bill')
  108. ->where('status', 1)
  109. ->where('gzc', 0)
  110. ->where('commission_type', 5)
  111. ->whereNotIn('uid', $testUserIdList)
  112. ->chunk(100, function ($bills) {
  113. $this->processBills($bills);
  114. });
  115. }
  116. protected function processBills($bills)
  117. {
  118. // 2. 预加载用户实名认证信息
  119. $userIds = array_unique(array_column($bills->toArray(), 'uid'));
  120. $certifications = Db::name('user_certification')
  121. ->whereIn('uid', $userIds)
  122. ->column('user_name,mobile,user_card', 'uid');
  123. // 3. 订单类型映射配置
  124. $orderMap = [
  125. 0 => ['table' => 'rrx_store_order', 'condition' => [['paid', '=', 1]]],
  126. 1 => ['table' => 'rrx_order_pdd'],
  127. 2 => ['table' => 'rrx_order_vip'],
  128. 3 => ['table' => 'rrx_order_su'],
  129. 5 => ['table' => 'rrx_order_tb'],
  130. 6 => ['table' => 'rrx_order_jd'],
  131. 9 => ['table' => 'rrx_order_huafei'],
  132. 10 => [
  133. 'table' => 'rrx_order_merchant',
  134. 'condition' => [['paid', '=', 1], ['status', '=', 1]],
  135. 'field_map' => ['order_sn' => 'out_trade_no']
  136. ],
  137. ];
  138. foreach ($bills as $bill) {
  139. $typeShop = $bill['type_shop'] ?? 0;
  140. // 4. 跳过无效类型
  141. if (in_array($typeShop, [4, 7, 8])) continue;
  142. // 5. 使用映射配置查询订单
  143. $online = $this->getOrderData($orderMap, $typeShop, $bill);
  144. if (!$online) {
  145. Log::info("用户{$bill['uid']}订单{$bill['order_sn']}不存在");
  146. continue;
  147. }
  148. // 6. 检查实名认证
  149. if (!isset($certifications[$bill['uid']])) {
  150. Log::info("用户{$bill['uid']}未实名认证");
  151. continue;
  152. }
  153. // 7. 验证养老金金额
  154. $pension = $bill['number'] ?? 0;
  155. if ($pension <= 0) {
  156. Log::info("订单{$online['order_sn']}养老金金额无效: {$pension}");
  157. continue;
  158. }
  159. // 8. 事务处理
  160. Db::transaction(function () use ($bill, $online, $certifications, $pension, $typeShop) {
  161. // 检查是否已存在记录
  162. $exists = Db::name("gzc_logs")
  163. ->where("link_id", $bill['bill_id'])
  164. ->where("order_type", $typeShop)
  165. ->count();
  166. if ($exists) return;
  167. // 创建唯一订单ID
  168. $orderId = 'IN10' . (int)(microtime(true) * 1000) . mt_rand(10000, 99999);
  169. Db::name("gzc_logs")->insert([
  170. 'uid' => $bill['uid'],
  171. 'platform_no' => $orderId,
  172. 'account_type' => 1,
  173. 'user_name' => $certifications[$bill['uid']]['user_name'],
  174. 'mobile' => $certifications[$bill['uid']]['mobile'],
  175. 'user_card' => $certifications[$bill['uid']]['user_card'],
  176. 'pension' => $pension,
  177. 'order_type' => $typeShop,
  178. 'out_trade_no' => $online['order_sn'],
  179. 'link_id' => $bill['bill_id'],
  180. 'create_time' => date('Y-m-d H:i:s'),
  181. 'update_time' => date('Y-m-d H:i:s')
  182. ]);
  183. Db::name('user_bill')->where('bill_id', $bill['bill_id'])->update([
  184. 'is_gzc' => 1,
  185. 'gzc' => 1
  186. ]);
  187. });
  188. }
  189. }
  190. protected function getOrderData($orderMap, $typeShop, $bill)
  191. {
  192. $config = $orderMap[$typeShop] ?? $orderMap[0];
  193. $query = Db::table($config['table'])
  194. ->where($config['field_map']['order_sn'] ?? 'order_sn', $bill['order_sn']);
  195. if (!empty($config['condition'])) {
  196. $query->where($config['condition']);
  197. }
  198. $result = $query->find();
  199. // 字段映射处理
  200. if ($result && isset($config['field_map'])) {
  201. foreach ($config['field_map'] as $src => $dest) {
  202. $result[$src] = $result[$dest] ?? null;
  203. }
  204. }
  205. return $result;
  206. }
  207. }