addCard(['card_no' => $car_no,'card_key' => $card_key,'card_type' => $card_type,'amount' => $amount]); } public function testReadCSVFile() { $items = [ ['file' => 'phone-100.csv','card_type' => mtopcard\PhoneCardPaper, 'value' => 100], ['file' => 'oil-100.csv','card_type' => mtopcard\OilCardPaper, 'value' => 100], ['file' => 'oil-200.csv','card_type' => mtopcard\OilCardPaper, 'value' => 200], ['file' => 'oil-500.csv','card_type' => mtopcard\OilCardPaper, 'value' => 500], ['file' => 'oil-1000.csv','card_type' => mtopcard\OilCardPaper, 'value' => 1000], ]; $mod_card = Model('card_key'); foreach ($items as $item) { $file = BASE_DATA_PATH . '/cards/' . $item['file']; $amount = $item['value']; $card_type = $item['card_type']; $fn = fopen($file,"r"); if(empty($fn)) { Log::record("Open File {$item['file']} error.",Log::ERR); break; } else { Log::record("{$item['file']} start woring",Log::DEBUG); } while(! feof($fn)) { $line = trim(fgets($fn)); $results = explode(',',$line); if(empty($results)) { continue; } elseif(count($results) == 2) { $card_no = trim($results[0]); $card_key = trim($results[1]); } elseif(count($results) == 3) { $card_no = trim($results[1]); $card_key = trim($results[2]); } else { Log::record("分析数据失败:{$line}",Log::ERR); continue; } $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount); } fclose($fn); } Log::record("All cards has been add to database.",Log::DEBUG); } private function addFile($file) { $filename = BASE_DATA_PATH . "/cards/{$file}"; if(!file_exists($filename)) { Log::record("{$file} not exists.",Log::ERR); return false; } $mod_card = Model('card_key'); $fileType = PHPExcel_IOFactory::identify($filename); $objReader = PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $objReader->load($filename); Log::record("{$file} begin....",Log::DEBUG); foreach ($objPHPExcel->getWorkSheetIterator() as $sheet) { $title = strtolower($sheet->getTitle()); Log::record("{$file} - {$title} start....",Log::DEBUG); [$type,$amount,$id] = explode('-',$title); Log::record("type = {$type} amount = {$amount}",Log::DEBUG); if($type == 'p') { $card_type = mtopcard\PhoneCardPaper; } elseif($type == 'o') { $card_type = mtopcard\OilCardPaper; } else { Log::record("{$filename} sheet {$title} name type error.",Log::ERR); return false; } if($amount <= 0) { Log::record("{$filename} sheet {$title} name amount error.",Log::ERR); return false; } foreach ($sheet->getRowIterator() as $row) { $index = $row->getRowIndex(); if ($index < 2) continue; $datas = []; foreach ($row->getCellIterator() as $cell) { $data = $cell->getValue(); $datas[] = $data; } if(empty($datas[1]) || empty($datas[2])) { continue; } $card_no = trim($datas[1]); $card_key = trim($datas[2]); if($this->check_cardno($card_no) && $this->check_cardkey($card_key)) { $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount); } else { Log::record("{$card_no} {$card_key}",Log::ERR); } } } Log::record("{$file} end....",Log::DEBUG); return true; } private function check_cardno($card_no) { $count = self::CardNoLength; return preg_match("/^\d{{$count}}$/i",$card_no) > 0; } private function check_cardkey($card_no) { $count = self::CardKeyLength; return preg_match("/^[a-z0-9]{{$count}}$/",$card_no) > 0; } public function testAddCradFromExecl() { $files = ['lm-1.xlsx','by-1.xlsx']; foreach ($files as $file) { $this->addFile($file); } } public function testCancelOrder() { //2020-08-20 处理了一些订单。 $card_nos = ['1900000000668434', '1900000000668403', '1900000000668402', '1900000000668400', '1900000000668398', '1900000000668399', '1900000000668397', '1900000000668396', '1900000000668395', '1900000000668394', '1900000000668392', '1900000000668390', '1900000000668393', '1900000000668387', '1900000000668384', '1900000000668327', '1900000000040001', '1900000000569005', '1900000000264501', '1900000000569004']; $this->cancel_order($card_nos); } private function cancel_order($card_nos) { $mod_card = Model('card_key'); foreach ($card_nos as $card_no) { $items = $mod_card->field('*')->where(['card_no' => $card_no])->select(); $length = count($items); if($length > 1) { Log::record("有{$length}张卡号为{$card_no}的卡",Log::ERR); } elseif(empty($items)) { Log::record("未找到{$card_no}的卡号",Log::ERR); } else { $order_id = intval($items[0]['order_id']); $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]); $vr_logic = Logic('vr_order'); $result = $vr_logic->changeOrderStateCancel($order_info,'','管理员申请退款.'); Log::record("{$card_no} : {$result['state']}",Log::DEBUG); } } } public function testCardSequence() { $card_key = Model('card_key'); $count = 1000; $i = 0; $pairs = []; $pair = ['start' => 0,'end' => 0]; while (true) { $start = $i * $count; $items = $card_key->field('*')->order('card_no asc')->limit("{$start},{$count}")->select(); if(empty($items)) { break; } $i++; foreach ($items as $item) { try { $card_no = intval($item['card_no']); if($pair['end'] + 1 == $card_no) { $pair['end'] = $card_no; } else { $pairs[] = $pair; $pair = ['start' => $card_no,'end' => $card_no]; } } catch (Exception $ex) { Log::record($ex->getMessage(),Log::DEBUG); } } } foreach ($pairs as $pair) { $count = $pair['end'] - $pair['start'] + 1; Log::record("{$pair['start']}------{$pair['end']} count = {$count}",Log::DEBUG); } } public function testCardCheck() { $card_no = 1000113300023292731; $topcard = Model('topcard'); $items = $topcard->get_cards($card_no - 500,$card_no + 500); } private function find_card($file,$card_no) { $fileType = PHPExcel_IOFactory::identify($file); $objReader = PHPExcel_IOFactory::createReader($fileType); $objPHPExcel = $objReader->load($file); foreach ($objPHPExcel->getWorkSheetIterator() as $sheet) { foreach ($sheet->getRowIterator() as $row) { $index = $row->getRowIndex(); if ($index < 2) continue; $datas = []; foreach ($row->getCellIterator() as $cell) { $data = $cell->getValue(); $datas[] = $data; } if(empty($datas[1]) || empty($datas[2])) { continue; } $no = trim($datas[1]); if($no == $card_no) { return true; } } } return false; } public function testFindCard() { $finder = function ($card_no) { $dir = BASE_DATA_PATH . "/cards/old/"; if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $file_name = $dir . $file; $ext = pathinfo($file_name)['extension']; if($ext == 'xlsx') { $ret = $this->find_card($file_name,$card_no); if($ret) { Log::record("{$card_no} in {$file}",Log::DEBUG); break; } } } closedir($dh); } }; $cards = ['1900000000669244','1900000000669294','1900000000669344','1900000000669094','1900000000669144', '1900000000669194','1900000000669394','1900000000669044','1900000000669994']; foreach ($cards as $card_no) { Log::record("{$card_no} is finding....",Log::DEBUG); $finder($card_no); Log::record("{$card_no} over.",Log::DEBUG); } } public function testModifyCardno() { $changer = function ($start,$end) { $card_key = Model('card_key'); $cond = ['_multi' => true, ['egt', $start], ['lt', $end]]; $items = $card_key->field('*')->where(['card_no&card_no' => $cond])->order('card_no asc')->select(); return $items; }; $cards = $changer('1900000000699000','1900000000700000'); $card_key = Model('card_key'); foreach ($cards as $card) { $card_id = intval($card['card_id']); $card_no = intval($card['card_no']) - 30000; $ret = $card_key->where(['card_id' => $card_id])->update(['card_no' => "{$card_no}"]); Log::record("{$card_no} ret = {$ret}",Log::DEBUG); } } public function testAddCard() { $finder = function () { $files = []; $dir = BASE_DATA_PATH . "/cards/"; $dh = opendir($dir); if($dh) { while (($file = readdir($dh)) !== false) { $file_name = $dir . $file; $ext = pathinfo($file_name)['extension']; if($ext == 'xlsx') { $files[] = $file; } } closedir($dh); } return $files; }; $files = $finder(); foreach ($files as $file) { $this->addFile($file); } } } //docker-compose -f ./docker-compose-dev.yml run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testCancelOrder)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test //docker-compose -f ./docker-compose-dev.yml run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20200821_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testAddCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testModifyCardno)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefill::testSuhcQuery)( .*)?$/" --test-suffix TestRefill.php /var/www/html/test