load($filename); foreach ($objPHPExcel->getWorkSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { $items = []; foreach ($row->getCellIterator() as $cell) { $items[] = $cell->getValue(); } yield $items; } } }; $cardno_checker = function ($card_no) { $card_no = trim($card_no); $ret = preg_match("/^\d{11}$/i",$card_no) > 0; if($ret) { return $card_no; } else { return false; } }; $amount_checker = function ($amount) { $amount = floatval($amount) + 0.0005; $amount = intval($amount); if($amount > 0) { return $amount; } else { return false; } }; $mchid = 10239; $buyer_id = 66221; $sender = function ($card_no, $amount, $order_sn) use ($mchid, $buyer_id) { $params = [ 'mchid' => $mchid, 'buyer_id' => $buyer_id, 'amount' => $amount, 'mch_order' => $order_sn, 'card_no' => $card_no ]; $ret = refill\util::push_add($params); return $ret; }; $record_csv = fopen(BASE_ROOT_PATH . "/data/upload/refill/record.csv", 'a+'); $items = $field_reader($filename); foreach ($items as $item) { Log::record("{$item[0]}:{$item[1]}", Log::DEBUG); $card_no = $cardno_checker($item[0]); $amount = $amount_checker($item[1]); if ($card_no != false && $amount != false) { $order_sn = $this->make_sn(); $ret = $sender($card_no, $amount, $order_sn); Log::record("{$order_sn}:{$card_no}:{$amount}:{$ret}", Log::DEBUG); $order_time = date('Y-m-d H:i:S',time()); $row = [$order_sn,$card_no, $amount,$order_time]; if($ret) { $row[] = 'SUCC'; } else { $row[] = 'FAIL'; } fputcsv($record_csv,$row); } sleep(1); } fclose($record_csv); } private function make_sn() { return mt_rand(1000, 9999) . sprintf('%010d', time()) . sprintf('%06d', (float)microtime() * 1000000); } public function testDiffFromExecl() { $field_reader = function ($filename) { $fileType = PHPExcel_IOFactory::identify($filename); $objReader = PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $objReader->load($filename); foreach ($objPHPExcel->getWorkSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { $items = []; foreach ($row->getCellIterator() as $cell) { $items[] = $cell->getValue(); } yield $items; } } }; $cardno_checker = function ($card_no) { $card_no = trim($card_no); $ret = preg_match("/^\d{11}$/i",$card_no) > 0; if($ret) { return $card_no; } else { return false; } }; $amount_checker = function ($amount) { $amount = floatval($amount) + 0.0005; $amount = intval($amount); if($amount > 0) { return $amount; } else { return false; } }; $differ = function ($left,$right) { $result = []; $ll = count($left); $lr = count($right); $i = $j = 0; while ($i < $ll && $j < $lr) { if ($left[$i] > $right[$j]) { $j += 1; } elseif ($left[$i] === $right[$j]) { $i += 1; $j += 1; } else { $result[] = $left[$i]; $i += 1; } } for (; $i < $ll; $i++) { $result[] = $left[$i]; } return $result; }; $refill_reader = function ($file) use ($field_reader,$cardno_checker,$amount_checker) { if(!file_exists($file)) { Log::record("{$file} not exists.",Log::ERR); return []; } $result = []; $items = $field_reader($file); foreach ($items as $item) { $no = $cardno_checker($item[0]); $amount = $amount_checker($item[1]); if ($no != false && $amount != false) { $result[] = "{$no}-{$amount}"; } } return $result; }; $all = $refill_reader(BASE_ROOT_PATH . "/data/upload/refill/all.xls"); $succ = $refill_reader(BASE_ROOT_PATH . "/data/upload/refill/succ.xls"); sort($all); sort($succ); $result = $differ($all,$succ); $csv = fopen(BASE_ROOT_PATH . "/data/upload/refill/diff.csv", 'a+'); foreach ($result as $item) { $row = explode('-',$item); fputcsv($csv,$row); } fclose($csv); } public function testWriteCard() { refill\util::write_card('1000113300017553895',mtopcard\SinopecCard,'13911129867'); } }