TestAddData.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 testCardSequence()
  370. {
  371. $card_key = Model('card_key');
  372. $count = 1000;
  373. $i = 0;
  374. $pairs = [];
  375. $pair = ['start' => 0,'end' => 0];
  376. while (true)
  377. {
  378. $start = $i * $count;
  379. $items = $card_key->field('*')->order('card_no asc')->limit("{$start},{$count}")->select();
  380. if(empty($items)) {
  381. break;
  382. }
  383. $i++;
  384. foreach ($items as $item)
  385. {
  386. try {
  387. $card_no = intval($item['card_no']);
  388. if($pair['end'] + 1 == $card_no) {
  389. $pair['end'] = $card_no;
  390. }
  391. else {
  392. $pairs[] = $pair;
  393. $pair = ['start' => $card_no,'end' => $card_no];
  394. }
  395. }
  396. catch (Exception $ex) {
  397. Log::record($ex->getMessage(),Log::DEBUG);
  398. }
  399. }
  400. }
  401. foreach ($pairs as $pair) {
  402. $count = $pair['end'] - $pair['start'] + 1;
  403. Log::record("{$pair['start']}------{$pair['end']} count = {$count}",Log::DEBUG);
  404. }
  405. }
  406. public function testCardCheck()
  407. {
  408. $card_no = 1000113300023292731;
  409. $topcard = Model('topcard');
  410. $items = $topcard->get_cards($card_no - 500,$card_no + 500);
  411. }
  412. private function find_card($file,$card_no)
  413. {
  414. $fileType = PHPExcel_IOFactory::identify($file);
  415. $objReader = PHPExcel_IOFactory::createReader($fileType);
  416. $objPHPExcel = $objReader->load($file);
  417. foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
  418. {
  419. foreach ($sheet->getRowIterator() as $row)
  420. {
  421. $index = $row->getRowIndex();
  422. if ($index < 2) continue;
  423. $datas = [];
  424. foreach ($row->getCellIterator() as $cell) {
  425. $data = $cell->getValue();
  426. $datas[] = $data;
  427. }
  428. if(empty($datas[1]) || empty($datas[2])) {
  429. continue;
  430. }
  431. $no = trim($datas[1]);
  432. if($no == $card_no) {
  433. return true;
  434. }
  435. }
  436. }
  437. return false;
  438. }
  439. public function testFindCard()
  440. {
  441. $finder = function ($card_no) {
  442. $dir = BASE_DATA_PATH . "/cards/old/";
  443. if ($dh = opendir($dir))
  444. {
  445. while (($file = readdir($dh)) !== false)
  446. {
  447. $file_name = $dir . $file;
  448. $ext = pathinfo($file_name)['extension'];
  449. if($ext == 'xlsx') {
  450. $ret = $this->find_card($file_name,$card_no);
  451. if($ret) {
  452. Log::record("{$card_no} in {$file}",Log::DEBUG);
  453. break;
  454. }
  455. }
  456. }
  457. closedir($dh);
  458. }
  459. };
  460. $cards = ['1900000000669244','1900000000669294','1900000000669344','1900000000669094','1900000000669144',
  461. '1900000000669194','1900000000669394','1900000000669044','1900000000669994'];
  462. foreach ($cards as $card_no) {
  463. Log::record("{$card_no} is finding....",Log::DEBUG);
  464. $finder($card_no);
  465. Log::record("{$card_no} over.",Log::DEBUG);
  466. }
  467. }
  468. }
  469. //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
  470. //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
  471. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20200917_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test