123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- declare(strict_types=0);
- define('APP_ID', 'test');
- define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
- require_once(BASE_ROOT_PATH . '/global.php');
- require_once(BASE_CORE_PATH . '/lrlz.php');
- require_once(BASE_ROOT_PATH . '/fooder.php');
- require_once(BASE_HELPER_PATH . '/mcard/mcard.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- require_once(BASE_HELPER_PATH . '/util_helper.php');
- require_once(BASE_HELPER_PATH . '/order_helper.php');
- require_once(BASE_HELPER_PATH . '/bonus_helper.php');
- require_once(BASE_HELPER_PATH . '/vrorder_helper.php');
- require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
- require_once (BASE_CORE_PATH . '/framework/function/http.php');
- use PHPUnit\Framework\TestCase;
- use const mtopcard\ProvinceList;
- class TestAddData extends TestCase
- {
- const CardNoLength = 16;
- const CardKeyLength = 12;
- public static function setUpBeforeClass(): void
- {
- Base::run_util();
- }
- private function addCard($mod_card,$car_no,$card_key,$card_type,$amount)
- {
- $mod_card->addCard(['card_no' => $car_no,'card_key' => $card_key,'card_type' => $card_type,'amount' => $amount]);
- }
- public function testRegin()
- {
- $card_type = mtopcard\card_type('13911129867',$regin);
- }
- public function testCardtype()
- {
- $url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13911129867";
- $resp = http_request($url); //获取API返回 的数据
- if(empty($resp)) return false;
- $resp = mb_convert_encoding($resp, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5'); //解决中文乱码
- $datas = explode('__GetZoneResult_', $resp);
- if(count($datas) == 2)
- {
- $body = trim($datas[0]);
- $res = json_decode($body,true);
- return $res;
- }
- }
- public function testFSheng()
- {
- $data = 'a:3:{i:4;s:17:"10,11,15,16,25,28";i:5;i:-1;i:6;i:-1;}';
- $regins = [];
- $tmp = unserialize($data);
- if(!empty($tmp))
- {
- foreach ($tmp as $card_type => $sregions)
- {
- $regins = explode(',',$sregions);
- if(!empty($regins)) {
- $regins[$card_type] = $regins;
- }
- }
- }
- }
- 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
|