TestAddData.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. declare(strict_types=1);
  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. $mod_card = Model('card_key');
  78. $filename = BASE_DATA_PATH . "/cards/{$file}";
  79. $fileType = PHPExcel_IOFactory::identify($filename);
  80. $objReader = PHPExcel_IOFactory::createReader($fileType);
  81. $objPHPExcel = $objReader->load($filename);
  82. Log::record("{$file} begin....",Log::DEBUG);
  83. foreach ($objPHPExcel->getWorkSheetIterator() as $sheet)
  84. {
  85. $title = strtolower($sheet->getTitle());
  86. Log::record("{$file} - {$title} start....",Log::DEBUG);
  87. [$type,$amount,$id] = explode('-',$title);
  88. Log::record("type = {$type} amount = {$amount}",Log::DEBUG);
  89. if($type == 'p') {
  90. $card_type = mtopcard\PhoneCardPaper;
  91. }
  92. elseif($type == 'o') {
  93. $card_type = mtopcard\OilCardPaper;
  94. }
  95. else {
  96. Log::record("{$filename} sheet {$title} name type error.",Log::ERR);
  97. return ;
  98. }
  99. if($amount <= 0) {
  100. Log::record("{$filename} sheet {$title} name amount error.",Log::ERR);
  101. }
  102. foreach ($sheet->getRowIterator() as $row)
  103. {
  104. $index = $row->getRowIndex();
  105. if ($index < 2) continue;
  106. $datas = [];
  107. foreach ($row->getCellIterator() as $cell) {
  108. $data = $cell->getValue();
  109. $datas[] = $data;
  110. }
  111. if(empty($datas[1]) || empty($datas[2])) {
  112. continue;
  113. }
  114. $card_no = trim($datas[1]);
  115. $card_key = trim($datas[2]);
  116. if($this->check_cardno($card_no) && $this->check_cardkey($card_key)) {
  117. $this->addCard($mod_card,$card_no,$card_key,$card_type,$amount);
  118. }
  119. else {
  120. Log::record("{$card_no} {$card_key}",Log::ERR);
  121. }
  122. }
  123. }
  124. Log::record("{$file} end....",Log::DEBUG);
  125. }
  126. private function check_cardno($card_no)
  127. {
  128. $count = self::CardNoLength;
  129. return preg_match("/^\d{{$count}}$/i",$card_no) > 0;
  130. }
  131. private function check_cardkey($card_no)
  132. {
  133. $count = self::CardKeyLength;
  134. return preg_match("/^[a-z0-9]{{$count}}$/",$card_no) > 0;
  135. }
  136. public function testAddCradFromExecl()
  137. {
  138. $files = ['lm-1.xlsx','by-1.xlsx'];
  139. foreach ($files as $file) {
  140. $this->addFile($file);
  141. }
  142. }
  143. public function testCancelOrder()
  144. {
  145. //2020-08-20 处理了一些订单。
  146. $card_nos = ['1900000000668434',
  147. '1900000000668403',
  148. '1900000000668402',
  149. '1900000000668400',
  150. '1900000000668398',
  151. '1900000000668399',
  152. '1900000000668397',
  153. '1900000000668396',
  154. '1900000000668395',
  155. '1900000000668394',
  156. '1900000000668392',
  157. '1900000000668390',
  158. '1900000000668393',
  159. '1900000000668387',
  160. '1900000000668384',
  161. '1900000000668327',
  162. '1900000000040001',
  163. '1900000000569005',
  164. '1900000000264501',
  165. '1900000000569004'];
  166. $this->cancel_order($card_nos);
  167. }
  168. private function cancel_order($card_nos)
  169. {
  170. $mod_card = Model('card_key');
  171. foreach ($card_nos as $card_no)
  172. {
  173. $items = $mod_card->field('*')->where(['card_no' => $card_no])->select();
  174. $length = count($items);
  175. if($length > 1) {
  176. Log::record("有{$length}张卡号为{$card_no}的卡",Log::ERR);
  177. }
  178. elseif(empty($items)) {
  179. Log::record("未找到{$card_no}的卡号",Log::ERR);
  180. }
  181. else {
  182. $order_id = intval($items[0]['order_id']);
  183. $order_info = Model('vr_order')->getOrderInfo(['order_id' => $order_id]);
  184. $vr_logic = Logic('vr_order');
  185. $result = $vr_logic->changeOrderStateCancel($order_info,'','管理员申请退款.');
  186. Log::record("{$card_no} : {$result['state']}",Log::DEBUG);
  187. }
  188. }
  189. }
  190. public function test20200821_addCard()
  191. {
  192. $files = ['fen-p-200.xlsx','fen-p-500.xlsx'];
  193. foreach ($files as $file) {
  194. $this->addFile($file);
  195. }
  196. }
  197. public function test20200902_addCard()
  198. {
  199. $files = ['fen-p-100-1.xlsx','fen-p-100-2.xlsx'];
  200. foreach ($files as $file) {
  201. $this->addFile($file);
  202. }
  203. }
  204. public function test20200902_1_addCard()
  205. {
  206. $files = ['100-494-643.xlsx','200-779144-193.xlsx','200-779294-343.xlsx','500-794-843.xlsx'];
  207. foreach ($files as $file) {
  208. $this->addFile($file);
  209. }
  210. }
  211. public function test20200903_addCard()
  212. {
  213. $files = ['100-895-943.xlsx'];
  214. foreach ($files as $file) {
  215. $this->addFile($file);
  216. }
  217. }
  218. public function test20200904_addCard()
  219. {
  220. $files = ['100-644-743.xlsx','100-844-893.xlsx'];
  221. foreach ($files as $file) {
  222. $this->addFile($file);
  223. }
  224. }
  225. public function test20200904_1_addCard()
  226. {
  227. $files = ['200-344-353.xlsx'];
  228. foreach ($files as $file) {
  229. $this->addFile($file);
  230. }
  231. }
  232. public function test20200904_2_addCard()
  233. {
  234. $files = ['200-344-393.xlsx'];
  235. foreach ($files as $file) {
  236. $this->addFile($file);
  237. }
  238. }
  239. }
  240. //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
  241. //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
  242. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAddData::test20200904_2_addCard)( .*)?$/" --test-suffix TestAddData.php /var/www/html/test