TestAddData.php 15 KB

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