TestAddData.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. require_once (BASE_CORE_PATH . '/framework/function/http.php');
  16. use PHPUnit\Framework\TestCase;
  17. class TestAddData extends TestCase
  18. {
  19. const CardNoLength = 16;
  20. const CardKeyLength = 12;
  21. public static function setUpBeforeClass(): void
  22. {
  23. Base::run_util();
  24. }
  25. private function addCard($mod_card,$car_no,$card_key,$card_type,$amount)
  26. {
  27. $mod_card->addCard(['card_no' => $car_no,'card_key' => $card_key,'card_type' => $card_type,'amount' => $amount]);
  28. }
  29. public function testRegin()
  30. {
  31. $card_type = mtopcard\card_type('13911129867',$regin);
  32. }
  33. public function testReadCSVFile()
  34. {
  35. $items = [
  36. ['file' => 'phone-100.csv','card_type' => mtopcard\PhoneCardPaper, 'value' => 100],
  37. ['file' => 'oil-100.csv','card_type' => mtopcard\OilCardPaper, 'value' => 100],
  38. ['file' => 'oil-200.csv','card_type' => mtopcard\OilCardPaper, 'value' => 200],
  39. ['file' => 'oil-500.csv','card_type' => mtopcard\OilCardPaper, 'value' => 500],
  40. ['file' => 'oil-1000.csv','card_type' => mtopcard\OilCardPaper, 'value' => 1000],
  41. ];
  42. $mod_card = Model('card_key');
  43. foreach ($items as $item)
  44. {
  45. $file = BASE_DATA_PATH . '/cards/' . $item['file'];
  46. $amount = $item['value'];
  47. $card_type = $item['card_type'];
  48. $fn = fopen($file,"r");
  49. if(empty($fn)) {
  50. Log::record("Open File {$item['file']} error.",Log::ERR);
  51. break;
  52. }
  53. else {
  54. Log::record("{$item['file']} start woring",Log::DEBUG);
  55. }
  56. while(! feof($fn)) {
  57. $line = trim(fgets($fn));
  58. $results = explode(',',$line);
  59. if(empty($results)) {
  60. continue;
  61. }
  62. elseif(count($results) == 2) {
  63. $card_no = trim($results[0]);
  64. $card_key = trim($results[1]);
  65. }
  66. elseif(count($results) == 3) {
  67. $card_no = trim($results[1]);
  68. $card_key = trim($results[2]);
  69. }
  70. else {
  71. Log::record("分析数据失败:{$line}",Log::ERR);
  72. continue;
  73. }
  74. $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount);
  75. }
  76. fclose($fn);
  77. }
  78. Log::record("All cards has been add to database.",Log::DEBUG);
  79. }
  80. private function addFile($file)
  81. {
  82. $filename = BASE_DATA_PATH . "/cards/{$file}";
  83. if(!file_exists($filename)) {
  84. Log::record("{$file} not exists.",Log::ERR);
  85. return false;
  86. }
  87. $mod_card = Model('card_key');
  88. $fileType = PHPExcel_IOFactory::identify($filename);
  89. $objReader = PHPExcel_IOFactory::createReader($fileType);
  90. $objPHPExcel = $objReader->load($filename);
  91. Log::record("{$file} begin....",Log::DEBUG);
  92. foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
  93. {
  94. $title = strtolower($sheet->getTitle());
  95. Log::record("{$file} - {$title} start....",Log::DEBUG);
  96. [$type,$amount,$id] = explode('-',$title);
  97. Log::record("type = {$type} amount = {$amount}",Log::DEBUG);
  98. if($type == 'p') {
  99. $card_type = mtopcard\PhoneCardPaper;
  100. }
  101. elseif($type == 'o') {
  102. $card_type = mtopcard\OilCardPaper;
  103. }
  104. else {
  105. Log::record("{$filename} sheet {$title} name type error.",Log::ERR);
  106. return false;
  107. }
  108. if($amount <= 0) {
  109. Log::record("{$filename} sheet {$title} name amount error.",Log::ERR);
  110. return false;
  111. }
  112. foreach ($sheet->getRowIterator() as $row)
  113. {
  114. $index = $row->getRowIndex();
  115. if ($index < 2) continue;
  116. $datas = [];
  117. foreach ($row->getCellIterator() as $cell) {
  118. $data = $cell->getValue();
  119. $datas[] = $data;
  120. }
  121. if(empty($datas[1]) || empty($datas[2])) {
  122. continue;
  123. }
  124. $card_no = trim($datas[1]);
  125. $card_key = trim($datas[2]);
  126. if($this->check_cardno($card_no) && $this->check_cardkey($card_key)) {
  127. $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount);
  128. }
  129. else {
  130. Log::record("{$card_no} {$card_key}",Log::ERR);
  131. }
  132. }
  133. }
  134. Log::record("{$file} end....",Log::DEBUG);
  135. return true;
  136. }
  137. private function check_cardno($card_no)
  138. {
  139. $count = self::CardNoLength;
  140. return preg_match("/^\d{{$count}}$/i",$card_no) > 0;
  141. }
  142. private function check_cardkey($card_no)
  143. {
  144. $count = self::CardKeyLength;
  145. return preg_match("/^[a-z0-9]{{$count}}$/",$card_no) > 0;
  146. }
  147. public function testAddCradFromExecl()
  148. {
  149. $files = ['lm-1.xlsx','by-1.xlsx'];
  150. foreach ($files as $file) {
  151. $this->addFile($file);
  152. }
  153. }
  154. public function testCancelOrder()
  155. {
  156. //2020-08-20 处理了一些订单。
  157. $card_nos = ['1900000000668434',
  158. '1900000000668403',
  159. '1900000000668402',
  160. '1900000000668400',
  161. '1900000000668398',
  162. '1900000000668399',
  163. '1900000000668397',
  164. '1900000000668396',
  165. '1900000000668395',
  166. '1900000000668394',
  167. '1900000000668392',
  168. '1900000000668390',
  169. '1900000000668393',
  170. '1900000000668387',
  171. '1900000000668384',
  172. '1900000000668327',
  173. '1900000000040001',
  174. '1900000000569005',
  175. '1900000000264501',
  176. '1900000000569004'];
  177. $this->cancel_order($card_nos);
  178. }
  179. private function cancel_order($card_nos)
  180. {
  181. $mod_card = Model('card_key');
  182. foreach ($card_nos as $card_no)
  183. {
  184. $items = $mod_card->field('*')->where(['card_no' => $card_no])->select();
  185. $length = count($items);
  186. if($length > 1) {
  187. Log::record("有{$length}张卡号为{$card_no}的卡",Log::ERR);
  188. }
  189. elseif(empty($items)) {
  190. Log::record("未找到{$card_no}的卡号",Log::ERR);
  191. }
  192. else {
  193. $order_id = intval($items[0]['order_id']);
  194. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  195. $vr_logic = Logic('vr_order');
  196. $result = $vr_logic->changeOrderStateCancel($order_info,'','管理员申请退款.');
  197. Log::record("{$card_no} : {$result['state']}",Log::DEBUG);
  198. }
  199. }
  200. }
  201. public function testCardSequence()
  202. {
  203. $card_key = Model('card_key');
  204. $count = 1000;
  205. $i = 0;
  206. $pairs = [];
  207. $pair = ['start' => 0,'end' => 0];
  208. while (true)
  209. {
  210. $start = $i * $count;
  211. $items = $card_key->field('*')->order('card_no asc')->limit("{$start},{$count}")->select();
  212. if(empty($items)) {
  213. break;
  214. }
  215. $i++;
  216. foreach ($items as $item)
  217. {
  218. try {
  219. $card_no = intval($item['card_no']);
  220. if($pair['end'] + 1 == $card_no) {
  221. $pair['end'] = $card_no;
  222. }
  223. else {
  224. $pairs[] = $pair;
  225. $pair = ['start' => $card_no,'end' => $card_no];
  226. }
  227. }
  228. catch (Exception $ex) {
  229. Log::record($ex->getMessage(),Log::DEBUG);
  230. }
  231. }
  232. }
  233. foreach ($pairs as $pair) {
  234. $count = $pair['end'] - $pair['start'] + 1;
  235. Log::record("{$pair['start']}------{$pair['end']} count = {$count}",Log::DEBUG);
  236. }
  237. }
  238. public function testCardCheck()
  239. {
  240. $card_no = 1000113300023292731;
  241. $topcard = Model('topcard');
  242. $items = $topcard->get_cards($card_no - 500,$card_no + 500);
  243. }
  244. private function find_card($file,$card_no)
  245. {
  246. $fileType = PHPExcel_IOFactory::identify($file);
  247. $objReader = PHPExcel_IOFactory::createReader($fileType);
  248. $objPHPExcel = $objReader->load($file);
  249. foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
  250. {
  251. foreach ($sheet->getRowIterator() as $row)
  252. {
  253. $index = $row->getRowIndex();
  254. if ($index < 2) continue;
  255. $datas = [];
  256. foreach ($row->getCellIterator() as $cell) {
  257. $data = $cell->getValue();
  258. $datas[] = $data;
  259. }
  260. if(empty($datas[1]) || empty($datas[2])) {
  261. continue;
  262. }
  263. $no = trim($datas[1]);
  264. if($no == $card_no) {
  265. return true;
  266. }
  267. }
  268. }
  269. return false;
  270. }
  271. public function testFindCard()
  272. {
  273. $finder = function ($card_no) {
  274. $dir = BASE_DATA_PATH . "/cards/old/";
  275. if ($dh = opendir($dir))
  276. {
  277. while (($file = readdir($dh)) !== false)
  278. {
  279. $file_name = $dir . $file;
  280. $ext = pathinfo($file_name)['extension'];
  281. if($ext == 'xlsx') {
  282. $ret = $this->find_card($file_name,$card_no);
  283. if($ret) {
  284. Log::record("{$card_no} in {$file}",Log::DEBUG);
  285. break;
  286. }
  287. }
  288. }
  289. closedir($dh);
  290. }
  291. };
  292. $cards = ['1900000000669244','1900000000669294','1900000000669344','1900000000669094','1900000000669144',
  293. '1900000000669194','1900000000669394','1900000000669044','1900000000669994'];
  294. foreach ($cards as $card_no) {
  295. Log::record("{$card_no} is finding....",Log::DEBUG);
  296. $finder($card_no);
  297. Log::record("{$card_no} over.",Log::DEBUG);
  298. }
  299. }
  300. public function testModifyCardno()
  301. {
  302. $changer = function ($start,$end) {
  303. $card_key = Model('card_key');
  304. $cond = ['_multi' => true, ['egt', $start], ['lt', $end]];
  305. $items = $card_key->field('*')->where(['card_no&card_no' => $cond])->order('card_no asc')->select();
  306. return $items;
  307. };
  308. $cards = $changer('1900000000699000','1900000000700000');
  309. $card_key = Model('card_key');
  310. foreach ($cards as $card) {
  311. $card_id = intval($card['card_id']);
  312. $card_no = intval($card['card_no']) - 30000;
  313. $ret = $card_key->where(['card_id' => $card_id])->update(['card_no' => "{$card_no}"]);
  314. Log::record("{$card_no} ret = {$ret}",Log::DEBUG);
  315. }
  316. }
  317. public function testAddCard()
  318. {
  319. $finder = function ()
  320. {
  321. $files = [];
  322. $dir = BASE_DATA_PATH . "/cards/";
  323. $dh = opendir($dir);
  324. if($dh)
  325. {
  326. while (($file = readdir($dh)) !== false)
  327. {
  328. $file_name = $dir . $file;
  329. $ext = pathinfo($file_name)['extension'];
  330. if($ext == 'xlsx') {
  331. $files[] = $file;
  332. }
  333. }
  334. closedir($dh);
  335. }
  336. return $files;
  337. };
  338. $files = $finder();
  339. foreach ($files as $file) {
  340. $this->addFile($file);
  341. }
  342. }
  343. }
  344. //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
  345. //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
  346. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testAddCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
  347. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::testModifyCardno)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test
  348. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefill::testSuhcQuery)( .*)?$/" --test-suffix TestRefill.php /var/www/html/test