TestAddData.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. declare(strict_types=0);
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. require_once(BASE_HELPER_PATH . '/mcard/mcard.php');
  9. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  10. require_once(BASE_HELPER_PATH . '/util_helper.php');
  11. require_once(BASE_HELPER_PATH . '/order_helper.php');
  12. require_once(BASE_HELPER_PATH . '/bonus_helper.php');
  13. require_once(BASE_HELPER_PATH . '/vrorder_helper.php');
  14. require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
  15. use PHPUnit\Framework\TestCase;
  16. class TestAddData extends TestCase
  17. {
  18. const CardNoLength = 16;
  19. const CardKeyLength = 12;
  20. public static function setUpBeforeClass(): void
  21. {
  22. Base::run_util();
  23. }
  24. private function addCard($mod_card,$car_no,$card_key,$card_type,$amount)
  25. {
  26. $mod_card->addCard(['card_no' => $car_no,'card_key' => $card_key,'card_type' => $card_type,'amount' => $amount]);
  27. }
  28. public function testReadCSVFile()
  29. {
  30. $items = [
  31. ['file' => 'phone-100.csv','card_type' => mtopcard\PhoneCardPaper, 'value' => 100],
  32. ['file' => 'oil-100.csv','card_type' => mtopcard\OilCardPaper, 'value' => 100],
  33. ['file' => 'oil-200.csv','card_type' => mtopcard\OilCardPaper, 'value' => 200],
  34. ['file' => 'oil-500.csv','card_type' => mtopcard\OilCardPaper, 'value' => 500],
  35. ['file' => 'oil-1000.csv','card_type' => mtopcard\OilCardPaper, 'value' => 1000],
  36. ];
  37. $mod_card = Model('card_key');
  38. foreach ($items as $item)
  39. {
  40. $file = BASE_DATA_PATH . '/cards/' . $item['file'];
  41. $amount = $item['value'];
  42. $card_type = $item['card_type'];
  43. $fn = fopen($file,"r");
  44. if(empty($fn)) {
  45. Log::record("Open File {$item['file']} error.",Log::ERR);
  46. break;
  47. }
  48. else {
  49. Log::record("{$item['file']} start woring",Log::DEBUG);
  50. }
  51. while(! feof($fn)) {
  52. $line = trim(fgets($fn));
  53. $results = explode(',',$line);
  54. if(empty($results)) {
  55. continue;
  56. }
  57. elseif(count($results) == 2) {
  58. $card_no = trim($results[0]);
  59. $card_key = trim($results[1]);
  60. }
  61. elseif(count($results) == 3) {
  62. $card_no = trim($results[1]);
  63. $card_key = trim($results[2]);
  64. }
  65. else {
  66. Log::record("分析数据失败:{$line}",Log::ERR);
  67. continue;
  68. }
  69. $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount);
  70. }
  71. fclose($fn);
  72. }
  73. Log::record("All cards has been add to database.",Log::DEBUG);
  74. }
  75. private function addFile($file)
  76. {
  77. $filename = BASE_DATA_PATH . "/cards/{$file}";
  78. if(!file_exists($filename)) {
  79. Log::record("{$file} not exists.",Log::ERR);
  80. return false;
  81. }
  82. $mod_card = Model('card_key');
  83. $fileType = PHPExcel_IOFactory::identify($filename);
  84. $objReader = PHPExcel_IOFactory::createReader($fileType);
  85. $objPHPExcel = $objReader->load($filename);
  86. Log::record("{$file} begin....",Log::DEBUG);
  87. foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
  88. {
  89. $title = strtolower($sheet->getTitle());
  90. Log::record("{$file} - {$title} start....",Log::DEBUG);
  91. [$type,$amount,$id] = explode('-',$title);
  92. Log::record("type = {$type} amount = {$amount}",Log::DEBUG);
  93. if($type == 'p') {
  94. $card_type = mtopcard\PhoneCardPaper;
  95. }
  96. elseif($type == 'o') {
  97. $card_type = mtopcard\OilCardPaper;
  98. }
  99. else {
  100. Log::record("{$filename} sheet {$title} name type error.",Log::ERR);
  101. return false;
  102. }
  103. if($amount <= 0) {
  104. Log::record("{$filename} sheet {$title} name amount error.",Log::ERR);
  105. return false;
  106. }
  107. foreach ($sheet->getRowIterator() as $row)
  108. {
  109. $index = $row->getRowIndex();
  110. if ($index < 2) continue;
  111. $datas = [];
  112. foreach ($row->getCellIterator() as $cell) {
  113. $data = $cell->getValue();
  114. $datas[] = $data;
  115. }
  116. if(empty($datas[1]) || empty($datas[2])) {
  117. continue;
  118. }
  119. $card_no = trim($datas[1]);
  120. $card_key = trim($datas[2]);
  121. if($this->check_cardno($card_no) && $this->check_cardkey($card_key)) {
  122. $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount);
  123. }
  124. else {
  125. Log::record("{$card_no} {$card_key}",Log::ERR);
  126. }
  127. }
  128. }
  129. Log::record("{$file} end....",Log::DEBUG);
  130. return true;
  131. }
  132. private function check_cardno($card_no)
  133. {
  134. $count = self::CardNoLength;
  135. return preg_match("/^\d{{$count}}$/i",$card_no) > 0;
  136. }
  137. private function check_cardkey($card_no)
  138. {
  139. $count = self::CardKeyLength;
  140. return preg_match("/^[a-z0-9]{{$count}}$/",$card_no) > 0;
  141. }
  142. public function testAddCradFromExecl()
  143. {
  144. $files = ['lm-1.xlsx','by-1.xlsx'];
  145. foreach ($files as $file) {
  146. $this->addFile($file);
  147. }
  148. }
  149. public function testCancelOrder()
  150. {
  151. //2020-08-20 处理了一些订单。
  152. $card_nos = ['1900000000668434',
  153. '1900000000668403',
  154. '1900000000668402',
  155. '1900000000668400',
  156. '1900000000668398',
  157. '1900000000668399',
  158. '1900000000668397',
  159. '1900000000668396',
  160. '1900000000668395',
  161. '1900000000668394',
  162. '1900000000668392',
  163. '1900000000668390',
  164. '1900000000668393',
  165. '1900000000668387',
  166. '1900000000668384',
  167. '1900000000668327',
  168. '1900000000040001',
  169. '1900000000569005',
  170. '1900000000264501',
  171. '1900000000569004'];
  172. $this->cancel_order($card_nos);
  173. }
  174. private function cancel_order($card_nos)
  175. {
  176. $mod_card = Model('card_key');
  177. foreach ($card_nos as $card_no)
  178. {
  179. $items = $mod_card->field('*')->where(['card_no' => $card_no])->select();
  180. $length = count($items);
  181. if($length > 1) {
  182. Log::record("有{$length}张卡号为{$card_no}的卡",Log::ERR);
  183. }
  184. elseif(empty($items)) {
  185. Log::record("未找到{$card_no}的卡号",Log::ERR);
  186. }
  187. else {
  188. $order_id = intval($items[0]['order_id']);
  189. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  190. $vr_logic = Logic('vr_order');
  191. $result = $vr_logic->changeOrderStateCancel($order_info,'','管理员申请退款.');
  192. Log::record("{$card_no} : {$result['state']}",Log::DEBUG);
  193. }
  194. }
  195. }
  196. public function test20200821_addCard()
  197. {
  198. $files = ['fen-p-200.xlsx','fen-p-500.xlsx'];
  199. foreach ($files as $file) {
  200. $this->addFile($file);
  201. }
  202. }
  203. public function test20200902_addCard()
  204. {
  205. $files = ['fen-p-100-1.xlsx','fen-p-100-2.xlsx'];
  206. foreach ($files as $file) {
  207. $this->addFile($file);
  208. }
  209. }
  210. public function test20200902_1_addCard()
  211. {
  212. $files = ['100-494-643.xlsx','200-779144-193.xlsx','200-779294-343.xlsx','500-794-843.xlsx'];
  213. foreach ($files as $file) {
  214. $this->addFile($file);
  215. }
  216. }
  217. public function test20200903_addCard()
  218. {
  219. $files = ['100-895-943.xlsx'];
  220. foreach ($files as $file) {
  221. $this->addFile($file);
  222. }
  223. }
  224. public function test20200904_addCard()
  225. {
  226. $files = ['100-644-743.xlsx','100-844-893.xlsx'];
  227. foreach ($files as $file) {
  228. $this->addFile($file);
  229. }
  230. }
  231. public function test20200904_1_addCard()
  232. {
  233. $files = ['200-344-353.xlsx'];
  234. foreach ($files as $file) {
  235. $this->addFile($file);
  236. }
  237. }
  238. public function test20200904_2_addCard()
  239. {
  240. $files = ['200-344-393.xlsx'];
  241. foreach ($files as $file) {
  242. $this->addFile($file);
  243. }
  244. }
  245. public function test20200904_3_addCard()
  246. {
  247. $files = ['200-344-393.xlsx'];
  248. foreach ($files as $file) {
  249. $this->addFile($file);
  250. }
  251. }
  252. public function test20200904_4_addCard()
  253. {
  254. $files = ['100-344-443.xlsx'];
  255. foreach ($files as $file) {
  256. $this->addFile($file);
  257. }
  258. }
  259. public function test20200905_addCard()
  260. {
  261. $files = ['200-8344-8443.xlsx' , '100-144-243.xlsx'];
  262. foreach ($files as $file) {
  263. $this->addFile($file);
  264. }
  265. }
  266. public function test20200905_1_addCard()
  267. {
  268. $files = ['100-294-343.xlsx'];
  269. foreach ($files as $file) {
  270. $this->addFile($file);
  271. }
  272. }
  273. public function test20200905_2_addCard()
  274. {
  275. $files = ['200-244-343.xlsx'];
  276. foreach ($files as $file) {
  277. $this->addFile($file);
  278. }
  279. }
  280. public function test20200905_3_addCard()
  281. {
  282. $files = ['100-669944-669993-670044-670093.xlsx'];
  283. foreach ($files as $file) {
  284. $this->addFile($file);
  285. }
  286. }
  287. public function test20200906_addCard()
  288. {
  289. $files = ['200-807944-808043.xlsx'];
  290. foreach ($files as $file) {
  291. $this->addFile($file);
  292. }
  293. }
  294. public function test20200906_1_addCard()
  295. {
  296. $files = ['100-1900000000669994-1900000000670043.xlsx' ,
  297. '100-670844-893.xlsx' ,
  298. '200-1900000000808194-1900000000808243.xlsx' ,
  299. 'p100-670-644-693.xlsx' ,
  300. 'p100-670894-943.xlsx'];
  301. foreach ($files as $file) {
  302. $this->addFile($file);
  303. }
  304. }
  305. public function test20200907_addCard()
  306. {
  307. $files = ['p-100-670-544-593.xlsx',
  308. 'p-100-670-594-643.xlsx',
  309. 'p-100-670694-793.xlsx',
  310. 'p-100-670794-843.xlsx'];
  311. foreach ($files as $file) {
  312. $this->addFile($file);
  313. }
  314. }
  315. public function test20200907_1_addCard()
  316. {
  317. $files = ['p200-807944-808043.xlsx'];
  318. foreach ($files as $file) {
  319. $this->addFile($file);
  320. }
  321. }
  322. public function test20200908_addCard()
  323. {
  324. $files = ['话费100元670号段444-543.xlsx',
  325. '话费200元808系列094-143.xlsx',
  326. '话费200元808系列344-443.xlsx'];
  327. foreach ($files as $file) {
  328. $this->addFile($file);
  329. }
  330. }
  331. public function test20200909_addCard()
  332. {
  333. $files = ['200元话费808系列895-943.xlsx','话费100元669列594-693.xlsx','话费100元669号段794-943.xlsx','话费100元699系列444-543.xlsx'];
  334. foreach ($files as $file) {
  335. $this->addFile($file);
  336. }
  337. }
  338. public function test20200909_1_addCard()
  339. {
  340. $files = ['话费200元808系列444-543.xlsx'];
  341. foreach ($files as $file) {
  342. $this->addFile($file);
  343. }
  344. }
  345. public function test20200910_addCard()
  346. {
  347. $files = ['100元话费669系列394-443.xlsx','100元话费699系列544-593.xlsx','200元话费808系列693-743.xlsx',
  348. '200元话费808系列744-843zn.xlsx','200元话费808系列844-893.xlsx','话费100元669号段694-793.xlsx',
  349. '话费200元808系列544-643.xlsx','话费100元668号段094-193zn.xlsx','话费100元 668号段444-493zn.xlsx'];
  350. foreach ($files as $file) {
  351. $this->addFile($file);
  352. }
  353. }
  354. public function test20200916_addCard()
  355. {
  356. $files = ['100元668044——093.xlsx','话费100元670系列094-143.xlsx',
  357. '话费200元779244——779293.xlsx','话费200元808系列044-093.xlsx','话费200元808系列144-193.xlsx'];
  358. foreach ($files as $file) {
  359. $this->addFile($file);
  360. }
  361. }
  362. public function test20200917_addCard()
  363. {
  364. $files = ['加油1000元569202——250.xlsx','加油1000元569251——300.xlsx','话费100元670系列244-293.xlsx'];
  365. foreach ($files as $file) {
  366. $this->addFile($file);
  367. }
  368. }
  369. public function test20200918_addCard()
  370. {
  371. $files = ['200元810344——443.xlsx','加油1000元569301——500(200张).xlsx',
  372. '话费100元670系列244-293.xlsx',
  373. '话费200元809系列944-810系列143.xlsx','话费200元810系列144-343.xlsx'];
  374. foreach ($files as $file) {
  375. $this->addFile($file);
  376. }
  377. }
  378. public function test20200926_addCard()
  379. {
  380. $files = ['100元话费669系列144-293.xlsx','100元话费669系列294-393卡.xlsx',
  381. '100元话费669系列394-443 50张.xlsx','100元话费669系列544-593 50张.xlsx',
  382. '话费100元669系列444-543.xlsx','加油卡1000元569系列071-150.xlsx'];
  383. foreach ($files as $file) {
  384. $this->addFile($file);
  385. }
  386. }
  387. public function test20200927_addCard()
  388. {
  389. $files = ['加油卡1000元569系列51-70.xlsx'];
  390. foreach ($files as $file) {
  391. $this->addFile($file);
  392. }
  393. }
  394. public function test20200927_01_addCard()
  395. {
  396. $files = ['500油卡264851-950.xlsx'];
  397. foreach ($files as $file) {
  398. $this->addFile($file);
  399. }
  400. }
  401. public function test20200928_addCard()
  402. {
  403. $files = ['liumin500油卡264651-750.xlsx','加油卡500元264系列751-850.xlsx'];
  404. foreach ($files as $file) {
  405. $this->addFile($file);
  406. }
  407. }
  408. public function test20200928_01_addCard()
  409. {
  410. $files = ['668 944-043.xlsx','话费100元 669号段044-143.xlsx','加油卡200元备用卡124251——124300.xlsx'];
  411. foreach ($files as $file) {
  412. $this->addFile($file);
  413. }
  414. }
  415. public function testCardSequence()
  416. {
  417. $card_key = Model('card_key');
  418. $count = 1000;
  419. $i = 0;
  420. $pairs = [];
  421. $pair = ['start' => 0,'end' => 0];
  422. while (true)
  423. {
  424. $start = $i * $count;
  425. $items = $card_key->field('*')->order('card_no asc')->limit("{$start},{$count}")->select();
  426. if(empty($items)) {
  427. break;
  428. }
  429. $i++;
  430. foreach ($items as $item)
  431. {
  432. try {
  433. $card_no = intval($item['card_no']);
  434. if($pair['end'] + 1 == $card_no) {
  435. $pair['end'] = $card_no;
  436. }
  437. else {
  438. $pairs[] = $pair;
  439. $pair = ['start' => $card_no,'end' => $card_no];
  440. }
  441. }
  442. catch (Exception $ex) {
  443. Log::record($ex->getMessage(),Log::DEBUG);
  444. }
  445. }
  446. }
  447. foreach ($pairs as $pair) {
  448. $count = $pair['end'] - $pair['start'] + 1;
  449. Log::record("{$pair['start']}------{$pair['end']} count = {$count}",Log::DEBUG);
  450. }
  451. }
  452. public function testCardCheck()
  453. {
  454. $card_no = 1000113300023292731;
  455. $topcard = Model('topcard');
  456. $items = $topcard->get_cards($card_no - 500,$card_no + 500);
  457. }
  458. private function find_card($file,$card_no)
  459. {
  460. $fileType = PHPExcel_IOFactory::identify($file);
  461. $objReader = PHPExcel_IOFactory::createReader($fileType);
  462. $objPHPExcel = $objReader->load($file);
  463. foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
  464. {
  465. foreach ($sheet->getRowIterator() as $row)
  466. {
  467. $index = $row->getRowIndex();
  468. if ($index < 2) continue;
  469. $datas = [];
  470. foreach ($row->getCellIterator() as $cell) {
  471. $data = $cell->getValue();
  472. $datas[] = $data;
  473. }
  474. if(empty($datas[1]) || empty($datas[2])) {
  475. continue;
  476. }
  477. $no = trim($datas[1]);
  478. if($no == $card_no) {
  479. return true;
  480. }
  481. }
  482. }
  483. return false;
  484. }
  485. public function testFindCard()
  486. {
  487. $finder = function ($card_no) {
  488. $dir = BASE_DATA_PATH . "/cards/old/";
  489. if ($dh = opendir($dir))
  490. {
  491. while (($file = readdir($dh)) !== false)
  492. {
  493. $file_name = $dir . $file;
  494. $ext = pathinfo($file_name)['extension'];
  495. if($ext == 'xlsx') {
  496. $ret = $this->find_card($file_name,$card_no);
  497. if($ret) {
  498. Log::record("{$card_no} in {$file}",Log::DEBUG);
  499. break;
  500. }
  501. }
  502. }
  503. closedir($dh);
  504. }
  505. };
  506. $cards = ['1900000000669244','1900000000669294','1900000000669344','1900000000669094','1900000000669144',
  507. '1900000000669194','1900000000669394','1900000000669044','1900000000669994'];
  508. foreach ($cards as $card_no) {
  509. Log::record("{$card_no} is finding....",Log::DEBUG);
  510. $finder($card_no);
  511. Log::record("{$card_no} over.",Log::DEBUG);
  512. }
  513. }
  514. public function testModifyCardno()
  515. {
  516. $changer = function ($start,$end) {
  517. $card_key = Model('card_key');
  518. $cond = ['_multi' => true, ['egt', $start], ['lt', $end]];
  519. $items = $card_key->field('*')->where(['card_no&card_no' => $cond])->order('card_no asc')->select();
  520. return $items;
  521. };
  522. $cards = $changer('1900000000699000','1900000000700000');
  523. $card_key = Model('card_key');
  524. foreach ($cards as $card) {
  525. $card_id = intval($card['card_id']);
  526. $card_no = intval($card['card_no']) - 30000;
  527. $ret = $card_key->where(['card_id' => $card_id])->update(['card_no' => "{$card_no}"]);
  528. Log::record("{$card_no} ret = {$ret}",Log::DEBUG);
  529. }
  530. }
  531. public function test20201013_addCard()
  532. {
  533. $files = ['p100-671594-671643.xlsx','话费100元671794-671843.xlsx','话费100元671844-671943.xlsx'];
  534. foreach ($files as $file) {
  535. $this->addFile($file);
  536. }
  537. }
  538. public function test20201013_01_addCard()
  539. {
  540. $files = ['话费100元671系列444-493.xlsx','话费100元671系列494-593.xlsx','话费100元671系列644-693.xlsx','话费100元671系列694-793.xlsx'];
  541. foreach ($files as $file) {
  542. $this->addFile($file);
  543. }
  544. }
  545. }
  546. //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
  547. //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
  548. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20201013_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
  549. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testModifyCardno)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test