123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- declare(strict_types=1);
- 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');
- use PHPUnit\Framework\TestCase;
- 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 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)
- {
- $mod_card = Model('card_key');
- $filename = BASE_DATA_PATH . "/cards/{$file}";
- $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 ;
- }
- if($amount <= 0) {
- Log::record("{$filename} sheet {$title} name amount error.",Log::ERR);
- }
- 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);
- }
- 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 test20200821_addCard()
- {
- $files = ['fen-p-200.xlsx','fen-p-500.xlsx'];
- foreach ($files as $file) {
- $this->addFile($file);
- }
- }
- public function test20200902_addCard()
- {
- $files = ['fen-p-100-1.xlsx','fen-p-100-2.xlsx'];
- foreach ($files as $file) {
- $this->addFile($file);
- }
- }
- public function test20200902_1_addCard()
- {
- $files = ['100-494-643.xlsx','200-779144-193.xlsx','200-779294-343.xlsx','500-794-843.xlsx'];
- foreach ($files as $file) {
- $this->addFile($file);
- }
- }
- public function test20200903_addCard()
- {
- $files = ['100-895-943.xlsx'];
- foreach ($files as $file) {
- $this->addFile($file);
- }
- }
- public function test20200904_addCard()
- {
- $files = ['100-644-743.xlsx','100-844-893.xlsx'];
- foreach ($files as $file) {
- $this->addFile($file);
- }
- }
- public function test20200904_1_addCard()
- {
- $files = ['200-344-353.xlsx'];
- foreach ($files as $file) {
- $this->addFile($file);
- }
- }
- public function test20200904_2_addCard()
- {
- $files = ['200-344-393.xlsx'];
- 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::test20200904_2_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
|