handler.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_ex($cond)
  49. {
  50. $tmcond_gen = function ($cur_start,$cur_end)
  51. {
  52. $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  53. $end = $cur_end + 86400*2;
  54. $cond['vr_order.add_time'] = [['egt', $cur_start], ['elt', $end], 'and'];
  55. return $cond;
  56. };
  57. $normal_cond = $cond['normal'];
  58. $time_scope = $cond['time_scope'];
  59. $order_reader = function ($normal_cond, $time_scope) use ($tmcond_gen)
  60. {
  61. [$start, $end] = $time_scope['order_time'];
  62. for ($cur_start = $start; $cur_start < $end; $cur_start += 86400)
  63. {
  64. if ($cur_start + 86400 >= $end) {
  65. $cur_end = $end;
  66. } else {
  67. $cur_end = $cur_start + 86400;
  68. }
  69. $tmcond = $tmcond_gen($cur_start, $cur_end);
  70. $cond = array_merge($normal_cond, $tmcond);
  71. $mod = Model();
  72. $items = $mod->table('refill_order,vr_order')
  73. ->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')
  74. ->join('inner')
  75. ->on('refill_order.order_id=vr_order.order_id')
  76. ->where($cond)
  77. ->group('order_state')
  78. ->select();
  79. if(empty($items)) continue;
  80. yield $items;
  81. }
  82. };
  83. $summer = function ($items,&$result)
  84. {
  85. foreach ($items as $item)
  86. {
  87. $order_state = $item['order_state'];
  88. $result[$order_state]['order_count'] += $item['order_count'];
  89. $result[$order_state]['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  90. $result[$order_state]['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  91. $result[$order_state]['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  92. }
  93. };
  94. $initor = function (&$records)
  95. {
  96. $order_states = [ORDER_STATE_CANCEL, ORDER_STATE_SEND, ORDER_STATE_SUCCESS];
  97. foreach ($order_states as $state) {
  98. $val = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0];
  99. $records[$state] = $val;
  100. }
  101. };
  102. $all_summer = function ($records)
  103. {
  104. $result = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0];
  105. foreach ($records as $item) {
  106. $result['order_count'] += $item['order_count'];
  107. $result['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  108. $result['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  109. $result['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  110. }
  111. return $result;
  112. };
  113. try
  114. {
  115. $records = [];
  116. $initor($records);
  117. $stats = $order_reader($normal_cond, $time_scope);
  118. foreach ($stats as $items) {
  119. $summer($items,$records);
  120. }
  121. $all = $all_summer($records);
  122. $result = ['all' => $all, 'sending' => $records[ORDER_STATE_SEND], 'success' => $records[ORDER_STATE_SUCCESS], 'cancel' => $records[ORDER_STATE_CANCEL]];
  123. return [true, $result];
  124. } catch (Exception $ex) {
  125. return [false, false];
  126. }
  127. }
  128. public function refill_order_stat_title($condition)
  129. {
  130. return md5("refill_order_stat-".serialize($condition));
  131. }
  132. public function refill_order_stat_ex_title($condition)
  133. {
  134. return md5("refill_order_stat_ex-".serialize($condition));
  135. }
  136. public function refill_order_export($cond)
  137. {
  138. $tmcond_gen = function ($time_type,$time_scope,$cur_start,$cur_end)
  139. {
  140. if($time_type == 'notify_time') {
  141. $start = $cur_start - 86400*2;
  142. $end = $cur_end + 86400*2;
  143. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $cur_end], 'and'];
  144. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
  145. $cond['refill_order.notify_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  146. } else {
  147. $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  148. $end = $cur_end + 86400*2;
  149. $cond['vr_order.add_time'] = [['egt', $cur_start], ['elt', $end], 'and'];
  150. }
  151. return $cond;
  152. };
  153. $normal_cond = $cond['normal'];
  154. $time_scope = $cond['time_scope'];
  155. $export_type = $cond['export_type'];
  156. $order_reader = function ($normal_cond,$time_scope) use ($tmcond_gen)
  157. {
  158. if(isset($time_scope['notify_time'])) {
  159. $time_type = 'notify_time';
  160. [$start,$end] = $time_scope['notify_time'];
  161. } else {
  162. $time_type = 'order_time';
  163. [$start,$end] = $time_scope['order_time'];
  164. }
  165. for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
  166. {
  167. if($cur_start + 3600 >= $end) {
  168. $cur_end = $end;
  169. } else {
  170. $cur_end = $cur_start + 3600;
  171. }
  172. $tmcond = $tmcond_gen($time_type,$time_scope,$cur_start,$cur_end);
  173. $cond = array_merge($normal_cond,$tmcond);
  174. $mod = Model();
  175. $len = 1000;
  176. $i = 0;
  177. while (true)
  178. {
  179. $start = $i * $len;
  180. $items = $mod->table('refill_order,vr_order')
  181. ->field('refill_order.*,vr_order.order_state,vr_order.store_name')
  182. ->join('inner')
  183. ->on('refill_order.order_id=vr_order.order_id')
  184. ->where($cond)
  185. ->order('refill_order.order_id asc')
  186. ->limit("{$start},{$len}")
  187. ->select();
  188. $i++;
  189. if(empty($items)) break;
  190. foreach ($items as $item) {
  191. yield $item;
  192. }
  193. }
  194. }
  195. };
  196. $merchants = [];
  197. $column_values = ['平台单号','面额', '充值卡号', '充值卡类型', '是否转网', '下单日期', '完成日期', '订单状态', '流水号'];
  198. $data_keys = ['order_sn', 'refill_amount', 'card_no', 'card_type_text', 'is_transfer_text', 'order_time_text', 'notify_time_text','order_state_text', 'official_sn'];
  199. if($export_type === 'merchant') {
  200. $column_values = array_merge(['商户号', '商户订单号'], $column_values, ['扣款金额']);
  201. $data_keys = array_merge(['mchid', 'mch_order'], $data_keys, ['mch_amount']);
  202. }elseif ($export_type === 'provider') {
  203. $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']);
  204. $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['channel_amount']);
  205. }
  206. $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
  207. foreach ($merchant_list as $value) {
  208. $merchants[$value['mchid']] = $value;
  209. }
  210. $column_key = 'A';
  211. for($index=0;$index<count($column_values);$index++){
  212. $column_keys[] = $column_key;
  213. $column_key++;
  214. }
  215. $objPHPExcel = new PHPExcel();
  216. $objPHPExcel->setActiveSheetIndex(0);
  217. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  218. foreach ($column_keys as $key => $column_key) {
  219. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
  220. $cell_value = $column_key . 1;
  221. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  222. }
  223. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  224. $orders = $order_reader($normal_cond,$time_scope);
  225. $index = 0;
  226. foreach ($orders as $order)
  227. {
  228. if(!empty($merchants)) {
  229. $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
  230. }
  231. $order['card_type_text'] = $card_type_texts[$order['card_type']];
  232. $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  233. $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  234. $order['order_state_text'] = orderState($order);
  235. if($order['is_transfer'] == 1) {
  236. $order['is_transfer_text'] = '是';
  237. }else{
  238. $order['is_transfer_text'] = '否';
  239. }
  240. foreach ($column_keys as $key => $column_key) {
  241. $field = $column_key . ($index + 2);
  242. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
  243. }
  244. $index += 1;
  245. }
  246. try {
  247. $path = BASE_ROOT_PATH . "/data/upload/task/";
  248. if (!is_dir($path)) {
  249. mkdir($path, 0755);
  250. }
  251. $filename = date('YmdHis', time()) . "-订单导出.xlsx";
  252. $file_path = $path . $filename;
  253. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  254. $objWriter->save($file_path);
  255. return [true, $filename];
  256. } catch (Exception $e) {
  257. return [false, false];
  258. }
  259. }
  260. public function refill_order_export_title($condition)
  261. {
  262. return md5("refill_order_export-".serialize($condition));
  263. }
  264. public function order_stat_reload($condition)
  265. {
  266. $refill = new stat_refill();
  267. $type = $condition['type'];
  268. $time_stamp = $condition['time_stamp'];
  269. $cid = $condition['cid'];
  270. $order_time_type = $condition['order_time_type'];
  271. if ($type == 'merchant') {
  272. $refill->merchant_stat($time_stamp, $cid, $order_time_type);
  273. } elseif ($type == 'provider') {
  274. $refill->provider_stat($time_stamp, $cid, $order_time_type);
  275. }
  276. return [true, 'success'];
  277. }
  278. public function order_stat_reload_title($condition)
  279. {
  280. return md5("order_stat_reload-".serialize($condition));
  281. }
  282. }