TestAddData.php 14 KB

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