TestAddData.php 15 KB

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