recover($filename); } } private function recover($filename) { $reader = function ($fp) { while (!feof($fp)) { $logs = []; for ($index = 0; $index < 100 && !feof($fp); $index++) { $log = fgets($fp); $logs[] = $log; } yield $logs; } }; $filter = function ($lines) { $logs = []; foreach ($lines as $line) { $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P[^)]+)[)]{1}/u', $line, $matches); if ($ret) { $skey = $matches['key']; $svalue = $matches['value']; $keys = explode(',', $skey); $values = explode(',', $svalue); $conveter = function ($value) { $val = trim($value, '\''); return $val; }; $values = array_map($conveter, $values); $log = array_combine($keys, $values); $logs[] = $log; } } return $logs; }; $mod_log = Model(); $inserter = function ($logs) use ($mod_log) { // $mod_log->table('acclog')->insertAll($logs); foreach ($logs as $log) { $ret = $mod_log->table('acclog')->insert($log); if (!$ret) { Log::record("import error", Log::ERR); } } }; $file = fopen($filename, 'r'); $batches = $reader($file); foreach ($batches as $batch) { $logs = $filter($batch); $inserter($logs); } fclose($file); } public function testSQLMatch() { // $line =<<< line // [cordispatcher 38-123975 2021-12-28 15:22:10 0.261198] ERR: DbError eno=1062 msg=Duplicate entry '2147483647' for key 'lrlz_pd_log.PRIMARY' sql=INSERT INTO `lrlz_pd_log` (lg_member_id,lg_member_name,lg_add_time,lg_type,lg_order_sn,lg_av_amount,lg_desc,lg_available) VALUES ('66025','5d322c4ee98781023b16d9a3c21cbfb4',1640676130,'order_pay','6861760694020130235975',-48,'下单,支付预存款,订单号: 6861760694020130235975','954256.6700') // line; $line = <<< line [cordispatcher 29-126259 2021-12-28 15:22:10 0.836849] ERR: DbError eno=1062 msg=Duplicate entry '2147483647' for key 'lrlz_pd_log.PRIMARY' sql=INSERT INTO `lrlz_pd_log` (lg_member_id,lg_member_name,lg_add_time,lg_type,lg_order_sn,lg_av_amount,lg_freeze_amount,lg_desc,lg_available) VALUES ('66240','c27517c6553a6f783b8df974c3d2d219',1640676130,'order_cancel','9031520694020106890799',48,-48,'取消订单,解冻预存款,订单 号: 9031520694020106890799','42192.9500') line; $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P[^)]+)[)]{1}/u', $line, $matches); if ($ret) { $skey = $matches['key']; $svalue = $matches['value']; $keys = explode(',', $skey); $values = explode(',', $svalue); $conveter = function ($value) { $val = trim($value, '\''); return $val; }; $values = array_map($conveter, $values); $log = array_combine($keys, $values); } } public function testGetLatestSending() { $mod = Model(); $item = $mod->table('refill_detail')->field('min(order_time) as mintime')->where(['order_time' => ['egt', time() - 5 * 86400], 'order_state' => 30])->find(); } public function testLoadAcc() { $line = <<< line [crontab 1 2021-12-28 00:12:28] SQL: UPDATE `lrlz_member` SET available_predeposit='74496.9500',freeze_predeposit='0.0000' WHERE ( member_id = 66240 ) [ RunTime:0.000156s ] line; // $ret = preg_match('/lrlz_pd_log[^(]+[(]{1}(?P[^)]+)[)]{1}[\s]+VALUES[^(][(]{1}(?P[^)]+)[)]{1}/u', $line, $matches); $ret = preg_match('/2021-12-28[\s]+(?P