handler.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace task;
  3. require_once(BASE_HELPER_PATH . '/stat_helper.php');
  4. require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
  5. use Exception;
  6. use mtopcard;
  7. use PHPExcel;
  8. use PHPExcel_IOFactory;
  9. use statistics\stat_refill;
  10. class handler
  11. {
  12. public function refill_order_stat($condition)
  13. {
  14. try
  15. {
  16. $items = Model('')->table('refill_order,vr_order')
  17. ->field('count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
  18. ->join('inner')
  19. ->on('refill_order.order_id=vr_order.order_id')
  20. ->where($condition)
  21. ->group('order_state')
  22. ->select();
  23. $all = [];
  24. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  25. $sending = $success = $cancel = $data;
  26. foreach ($items as $item)
  27. {
  28. if ($item['order_state'] == ORDER_STATE_SEND) {
  29. $sending = $item;
  30. } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
  31. $success = $item;
  32. } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
  33. $cancel = $item;
  34. }
  35. $all['order_count'] += $item['order_count'];
  36. $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  37. $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  38. $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  39. }
  40. $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  41. return [true,$result];
  42. }
  43. catch (Exception $ex)
  44. {
  45. return [false,false];
  46. }
  47. }
  48. public function refill_order_stat_title($condition)
  49. {
  50. return md5("refill_order_stat-".serialize($condition));
  51. }
  52. public function refill_order_export($cond)
  53. {
  54. $tmcond_gen = function ($time_type,$time_scope,$cur_start,$cur_end)
  55. {
  56. if($time_type == 'notify_time') {
  57. [$start, $end] = $time_scope['order_time'];
  58. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $end], 'and'];
  59. [$start, $end] = $time_scope['add_time'];
  60. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
  61. $cond['refill_order.notify_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  62. } else {
  63. $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  64. [$start, $end] = $time_scope['add_time'];
  65. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
  66. }
  67. return $cond;
  68. };
  69. $normal_cond = $cond['normal'];
  70. $time_scope = $cond['time_scope'];
  71. $order_reader = function ($normal_cond,$time_scope) use ($tmcond_gen)
  72. {
  73. if(isset($time_scope['notify_time'])) {
  74. $time_type = 'notify_time';
  75. [$start,$end] = $time_scope['notify_time'];
  76. } else {
  77. $time_type = 'order_time';
  78. [$start,$end] = $time_scope['order_time'];
  79. }
  80. for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
  81. {
  82. if($cur_start + 3600 >= $end) {
  83. $cur_end = $end;
  84. } else {
  85. $cur_end = $cur_start + 3600;
  86. }
  87. $tmcond = $tmcond_gen($time_type,$time_scope,$cur_start,$cur_end);
  88. $cond = array_merge($normal_cond,$tmcond);
  89. $mod = Model();
  90. $len = 1000;
  91. $i = 0;
  92. while (true)
  93. {
  94. $start = $i * $len;
  95. $items = $mod->table('refill_order,vr_order')
  96. ->field('refill_order.*,vr_order.order_state')
  97. ->join('inner')
  98. ->on('refill_order.order_id=vr_order.order_id')
  99. ->where($cond)
  100. ->order('refill_order.order_id asc')
  101. ->limit("{$start},{$len}")
  102. ->select();
  103. $i++;
  104. if(empty($items)) break;
  105. foreach ($items as $item) {
  106. yield $item;
  107. }
  108. }
  109. }
  110. };
  111. $merchants = [];
  112. $column_values = ['商户号', '商户订单号', '平台单号','面额', '充值卡号', '充值卡类型', '是否转网', '下单日期', '完成日期', '订单状态', '扣款金额'];
  113. $data_keys = ['mchid', 'mch_order', 'order_sn', 'refill_amount', 'card_no', 'card_type_text', 'is_transfer_text', 'order_time_text', 'notify_time_text','order_state_text', 'mch_amount'];
  114. $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
  115. foreach ($merchant_list as $value) {
  116. $merchants[$value['mchid']] = $value;
  117. }
  118. $column_key = 'A';
  119. for($index=0;$index<count($column_values);$index++){
  120. $column_keys[] = $column_key;
  121. $column_key++;
  122. }
  123. $objPHPExcel = new PHPExcel();
  124. $objPHPExcel->setActiveSheetIndex(0);
  125. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  126. foreach ($column_keys as $key => $column_key) {
  127. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
  128. $cell_value = $column_key . 1;
  129. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  130. }
  131. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  132. $orders = $order_reader($normal_cond,$time_scope);
  133. $index = 0;
  134. foreach ($orders as $order)
  135. {
  136. if(!empty($merchants)) {
  137. $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
  138. }
  139. $order['card_type_text'] = $card_type_texts[$order['card_type']];
  140. $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  141. $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  142. $order['order_state_text'] = orderState($order);
  143. if($order['is_transfer'] == 1) {
  144. $order['is_transfer_text'] = '是';
  145. }else{
  146. $order['is_transfer_text'] = '否';
  147. }
  148. foreach ($column_keys as $key => $column_key) {
  149. $field = $column_key . ($index + 2);
  150. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
  151. }
  152. $index += 1;
  153. }
  154. try {
  155. $path = BASE_ROOT_PATH . "/data/upload/task/";
  156. if (!is_dir($path)) {
  157. mkdir($path, 0755);
  158. }
  159. $filename = date('YmdHis', time()) . "-订单导出.xlsx";
  160. $file_path = $path . $filename;
  161. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  162. $objWriter->save($file_path);
  163. return [true, $filename];
  164. } catch (Exception $e) {
  165. return [false, false];
  166. }
  167. }
  168. public function refill_order_export_title($condition)
  169. {
  170. return md5("refill_order_export-".serialize($condition));
  171. }
  172. public function order_stat_reload($condition)
  173. {
  174. $refill = new stat_refill();
  175. $type = $condition['type'];
  176. $time_stamp = $condition['time_stamp'];
  177. $cid = $condition['cid'];
  178. $order_time_type = $condition['order_time_type'];
  179. if ($type == 'merchant') {
  180. $refill->merchant_stat($time_stamp, $cid, $order_time_type);
  181. } elseif ($type == 'provider') {
  182. $refill->provider_stat($time_stamp, $cid, $order_time_type);
  183. }
  184. return [true, 'success'];
  185. }
  186. public function order_stat_reload_title($condition)
  187. {
  188. return md5("order_stat_reload-".serialize($condition));
  189. }
  190. }