TestExportOrder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. use statistics\stat_refill;
  4. define('APP_ID', 'test');
  5. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  6. require_once(BASE_ROOT_PATH . '/global.php');
  7. require_once(BASE_CORE_PATH . '/lrlz.php');
  8. require_once(BASE_ROOT_PATH . '/fooder.php');
  9. require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
  10. class TestExportOrder extends TestCase
  11. {
  12. public static function setUpBeforeClass() : void
  13. {
  14. Base::run_util();
  15. }
  16. public function testXingzhiyu()
  17. {
  18. $end_time = strtotime("2021-06-15");
  19. $this->exoprt(10132,null,$end_time);
  20. }
  21. public function exoprt($mchid,$start_time = null,$end_time = null)
  22. {
  23. $cond['mchid'] = $mchid;
  24. $cond['vr_order.order_state'] = 40;
  25. if(!is_null($start_time) && !is_null($end_time)) {
  26. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $start_time], ['lt', $end_time]];
  27. }
  28. elseif(!is_null($start_time)) {
  29. $cond["refill_order.notify_time"] = ['egt', $start_time];
  30. }
  31. elseif(!is_null($end_time)) {
  32. $cond["refill_order.notify_time"] = ['lt', $end_time];
  33. }
  34. $path = BASE_DATA_PATH . "/log/{$mchid}.csv";
  35. $fp = fopen($path, 'w');
  36. fputcsv($fp,['序号','商户订单号','卡号','面额','流水号',"下单时间","回调时间"]);
  37. $count = 1000;
  38. $i = 0;
  39. $index = 0;
  40. while (true)
  41. {
  42. $start = $i * $count;
  43. $items = Model('')->table('refill_order,vr_order')
  44. ->field('mch_order,refill_order.card_no,refill_amount,mch_amount,official_sn,from_unixtime(order_time) as sorder_time,from_unixtime(notify_time) as snotify_time')
  45. ->join('inner')
  46. ->on('refill_order.order_id=vr_order.order_id')
  47. ->where($cond)
  48. ->limit("{$start},{$count}")
  49. ->select();
  50. if(empty($items)) {
  51. break;
  52. }
  53. $i++;
  54. foreach ($items as $item)
  55. {
  56. $index++;
  57. $data = [$index,$item['mch_order'],$item['card_no'],$item['refill_amount'],$item['mch_amount'],$item['official_sn'],$item['sorder_time'],$item['snotify_time']];
  58. fputcsv($fp,$data);
  59. }
  60. }
  61. fclose($fp);
  62. }
  63. public function testRefillTask()
  64. {
  65. $model_refill_task = Model('refill_task');
  66. $task = $model_refill_task->UnDispose('order_export');
  67. if (empty($task)) return false;
  68. $task_id = $task['task_id'];
  69. $task_condition = unserialize($task['task_condition']);
  70. if (empty($task_condition)) {
  71. Log::record("refill task condition unserialize err, task_id:{$task_id}");
  72. return false;
  73. }
  74. $type = $task['type'];
  75. $cid = $task['cid'];
  76. if ($type === 'merchant') {
  77. $condition['refill_order.mchid'] = $cid;
  78. } elseif ($type === 'provider') {
  79. $condition['vr_order.store_id'] = $cid;
  80. } else {
  81. Log::record("refill task type err, task_id:{$task_id}");
  82. return false;
  83. }
  84. $order_time_type = $task_condition['order_time_type'];
  85. $start_unixtime = $task_condition['start_unixtime'];
  86. $end_unixtime = $task_condition['end_unixtime'];
  87. $order_state = $task_condition['order_state'];
  88. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  89. $condition["refill_order.{$order_time_type}"] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  90. $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
  91. } elseif ($start_unixtime > 0) {
  92. $condition["refill_order.{$order_time_type}"] = ['egt', $start_unixtime];
  93. $condition['vr_order.add_time'] = ['egt', $start_unixtime-1];
  94. }
  95. if (in_array($order_state, ['0', '10', '20', '30', '40'], true)) {
  96. $condition['order_state'] = $order_state;
  97. }
  98. $model_refill_task->Dispose($task_id);
  99. [$state, $task_result] = $this->order_export($condition, $type, $task_id);
  100. if ($state) {
  101. $model_refill_task->DisposeFinish($task_id, $task_result);
  102. } else {
  103. $model_refill_task->DisposeErr($task_id, $task_result);
  104. }
  105. return true;
  106. }
  107. /**
  108. * @throws PHPExcel_Exception
  109. */
  110. private function order_export($condition, $type, $task_id)
  111. {
  112. $orders = $this->getAllOrders($condition);
  113. if(empty($orders)) {
  114. return [false, '统计数据为空'];
  115. }
  116. $orders = $this->orderFormat($orders);
  117. $objPHPExcel = new PHPExcel();
  118. if ($type === 'merchant') {
  119. $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
  120. $column_values = ['商户号', '客户订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
  121. $data_keys = ['mchid', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'mch_amount'];
  122. } elseif ($type === 'provider') {
  123. $column_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'];
  124. $column_values = ['上游订单号', '平台单号', '面额', '充值卡号', '充值卡类型', '下单日期', '完成日期', '官方流水号', '订单状态', '扣款金额'];
  125. $data_keys = ['ch_trade_no', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'order_time_text', 'notify_time_text', 'official_sn', 'order_state_text', 'channel_amount'];
  126. } else {
  127. return [false, '主体类型错误'];
  128. }
  129. $objPHPExcel->setActiveSheetIndex(0);
  130. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  131. foreach ($column_keys as $key => $column_key) {
  132. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(25);
  133. $cell_value = $column_key . 1;
  134. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  135. }
  136. foreach ($orders as $k => $order) {
  137. foreach ($column_keys as $key => $column_key) {
  138. $field = $column_key.($k+2);
  139. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]],
  140. PHPExcel_Cell_DataType::TYPE_STRING);
  141. }
  142. }
  143. try {
  144. $path = BASE_ROOT_PATH . "/data/upload/task/";
  145. if(!is_dir($path)){
  146. mkdir($path, 0755);
  147. }
  148. $filename = date('YmdHis',time())."-任务导出-任务ID:{$task_id}.xlsx";
  149. $file_path = $path . $filename;
  150. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  151. $objWriter->save($file_path);
  152. return [true, $filename];
  153. } catch (Exception $e) {
  154. return [false, $e->getMessage()];
  155. }
  156. }
  157. private function getAllOrders($condition): array
  158. {
  159. $len = 1000;
  160. $i = 0;
  161. $orders = [];
  162. while (true)
  163. {
  164. $start = $i * $len;
  165. $items = Model('')->table('refill_order,vr_order')
  166. ->field('refill_order.*,vr_order.order_state')
  167. ->join('inner')
  168. ->on('refill_order.order_id=vr_order.order_id')
  169. ->where($condition)
  170. ->order('refill_order.order_time desc')
  171. ->limit("{$start},{$len}")
  172. ->select();
  173. $orders = array_merge($orders,$items);
  174. if (empty($items) || count($items) < $len) {
  175. break;
  176. }
  177. $i++;
  178. }
  179. return $orders;
  180. }
  181. private function orderFormat($orders): array
  182. {
  183. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  184. $orderState = function ($order_info) {
  185. $is_retrying = $order_info['is_retrying'];
  186. switch ($order_info['order_state']) {
  187. case ORDER_STATE_CANCEL:
  188. if ($is_retrying) {
  189. $order_state = '重试中';
  190. } else {
  191. $order_state = '已取消';
  192. }
  193. break;
  194. case ORDER_STATE_NEW:
  195. $order_state = '待付款';
  196. break;
  197. case ORDER_STATE_PAY:
  198. $order_state = '待发货';
  199. break;
  200. case ORDER_STATE_SEND:
  201. $order_state = '待收货';
  202. break;
  203. case ORDER_STATE_SUCCESS:
  204. $order_state = '交易完成';
  205. break;
  206. default :
  207. $order_state = '未知状态';
  208. }
  209. return $order_state;
  210. };
  211. foreach($orders as $key => $order)
  212. {
  213. $orders[$key]['card_type_text'] = $card_type_texts[$order['card_type']];
  214. $orders[$key]['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  215. $orders[$key]['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  216. $orders[$key]['order_state_text'] = $orderState($order);
  217. }
  218. return $orders;
  219. }
  220. public function testStrSer()
  221. {
  222. $str = 'a:4:{s:9:"qualities";a:3:{i:1;a:3:{s:8:"day_secs";i:3600;s:10:"night_secs";i:1800;s:5:"times";i:50;}i:2;a:3:{s:8:"day_secs";i:600;s:10:"night_secs";i:300;s:5:"times";i:10;}i:3;a:3:{s:8:"day_secs";i:900;s:10:"night_secs";i:600;s:5:"times";i:1;}}s:11:"lower_ratio";a:2:{s:5:"ratio";d:0.9;s:6:"period";i:3600;}s:12:"profit_ratio";d:0.03;s:6:"opened";b:1;}';
  223. $str = 'a:3:{s:11:"is_transfer";b:1;s:11:"card_states";a:2:{i:0;i:0;i:1;i:5;}s:10:"card_types";a:1:{i:0;i:4;}}';
  224. $arr = unserialize($str);
  225. }
  226. }
  227. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestExportOrder::testXingzhiyu)( .*)?$/" --test-suffix TestExportOrder.php /var/www/html/test