handler.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. namespace task;
  3. require_once(BASE_HELPER_PATH . '/stat_helper.php');
  4. require_once(BASE_HELPER_PATH . '/PHPExcel/PHPExcel.php');
  5. require_once(BASE_HELPER_PATH . '/statistics/refill_balance.php');
  6. use Exception;
  7. use message\pubevent;
  8. use mtopcard;
  9. use PHPExcel;
  10. use PHPExcel_IOFactory;
  11. use statistics\refill_balance;
  12. use statistics\stat_refill;
  13. use Log;
  14. class handler
  15. {
  16. private $mTtaskId = 0;
  17. public function set_task_id($task_id)
  18. {
  19. $this->mTtaskId = $task_id;
  20. }
  21. public function refill_order_stat($condition)
  22. {
  23. try
  24. {
  25. $items = Model('')->table('refill_order,vr_order')
  26. ->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')
  27. ->join('inner')
  28. ->on('refill_order.order_id=vr_order.order_id')
  29. ->where($condition)
  30. ->group('order_state')
  31. ->select();
  32. $all = [];
  33. $data['order_count'] = $data['refill_amounts'] = $data['channel_amounts'] = $data['mch_amounts'] = 0;
  34. $sending = $success = $cancel = $data;
  35. foreach ($items as $item)
  36. {
  37. if ($item['order_state'] == ORDER_STATE_SEND) {
  38. $sending = $item;
  39. } elseif ($item['order_state'] == ORDER_STATE_SUCCESS) {
  40. $success = $item;
  41. } elseif ($item['order_state'] == ORDER_STATE_CANCEL) {
  42. $cancel = $item;
  43. }
  44. $all['order_count'] += $item['order_count'];
  45. $all['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  46. $all['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  47. $all['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  48. }
  49. $result = ['all' => $all, 'sending' => $sending, 'success' => $success, 'cancel' => $cancel];
  50. return [true,$result];
  51. }
  52. catch (Exception $ex)
  53. {
  54. return [false,false];
  55. }
  56. }
  57. public function refill_order_stat_ex($cond)
  58. {
  59. $tmcond_gen = function ($cur_start,$cur_end)
  60. {
  61. $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  62. $end = $cur_end + 86400*2;
  63. $cond['vr_order.add_time'] = [['egt', $cur_start], ['elt', $end], 'and'];
  64. return $cond;
  65. };
  66. $normal_cond = $cond['normal'];
  67. $time_scope = $cond['time_scope'];
  68. $order_reader = function ($normal_cond, $time_scope) use ($tmcond_gen)
  69. {
  70. [$start, $end] = $time_scope['order_time'];
  71. for ($cur_start = $start; $cur_start < $end; $cur_start += 86400)
  72. {
  73. if ($cur_start + 86400 >= $end) {
  74. $cur_end = $end;
  75. } else {
  76. $cur_end = $cur_start + 86400;
  77. }
  78. $tmcond = $tmcond_gen($cur_start, $cur_end);
  79. $cond = array_merge($normal_cond, $tmcond);
  80. $mod = Model();
  81. $items = $mod->table('refill_order,vr_order')
  82. ->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')
  83. ->join('inner')
  84. ->on('refill_order.order_id=vr_order.order_id')
  85. ->where($cond)
  86. ->group('order_state')
  87. ->select();
  88. if(empty($items)) continue;
  89. yield $items;
  90. }
  91. };
  92. $summer = function ($items,&$result)
  93. {
  94. foreach ($items as $item)
  95. {
  96. $order_state = $item['order_state'];
  97. $result[$order_state]['order_count'] += $item['order_count'];
  98. $result[$order_state]['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  99. $result[$order_state]['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  100. $result[$order_state]['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  101. }
  102. };
  103. $initor = function (&$records)
  104. {
  105. $order_states = [ORDER_STATE_CANCEL, ORDER_STATE_SEND, ORDER_STATE_SUCCESS];
  106. foreach ($order_states as $state) {
  107. $val = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0];
  108. $records[$state] = $val;
  109. }
  110. };
  111. $all_summer = function ($records)
  112. {
  113. $result = ['order_count' => 0, 'refill_amounts' => 0, 'channel_amounts' => 0, 'mch_amounts' => 0];
  114. foreach ($records as $item) {
  115. $result['order_count'] += $item['order_count'];
  116. $result['refill_amounts'] += ncPriceFormat($item['refill_amounts']);
  117. $result['channel_amounts'] += ncPriceFormat($item['channel_amounts']);
  118. $result['mch_amounts'] += ncPriceFormat($item['mch_amounts']);
  119. }
  120. return $result;
  121. };
  122. try
  123. {
  124. $records = [];
  125. $initor($records);
  126. $stats = $order_reader($normal_cond, $time_scope);
  127. foreach ($stats as $items) {
  128. $summer($items,$records);
  129. }
  130. $all = $all_summer($records);
  131. $result = ['all' => $all, 'sending' => $records[ORDER_STATE_SEND], 'success' => $records[ORDER_STATE_SUCCESS], 'cancel' => $records[ORDER_STATE_CANCEL]];
  132. return [true, $result];
  133. } catch (Exception $ex) {
  134. return [false, false];
  135. }
  136. }
  137. public function refill_order_stat_title($condition)
  138. {
  139. return md5("refill_order_stat-".serialize($condition));
  140. }
  141. public function refill_order_stat_ex_title($condition)
  142. {
  143. return md5("refill_order_stat_ex-".serialize($condition));
  144. }
  145. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  146. public function refill_order_export($cond)
  147. {
  148. $tmcond_gen = function ($time_type,$time_scope,$cur_start,$cur_end)
  149. {
  150. if($time_type == 'notify_time') {
  151. $start = $cur_start - 86400 * 2;
  152. $end = $cur_end + 86400 * 2;
  153. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $cur_end], 'and'];
  154. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
  155. $cond['refill_order.notify_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  156. } else {
  157. $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  158. $end = $cur_end + 86400 * 2;
  159. $cond['vr_order.add_time'] = [['egt', $cur_start], ['elt', $end], 'and'];
  160. }
  161. return $cond;
  162. };
  163. $normal_cond = $cond['normal'];
  164. $time_scope = $cond['time_scope'];
  165. $export_type = $cond['export_type'];
  166. $order_reader = function ($normal_cond,$time_scope) use ($tmcond_gen)
  167. {
  168. if(isset($time_scope['notify_time'])) {
  169. $time_type = 'notify_time';
  170. [$start,$end] = $time_scope['notify_time'];
  171. } else {
  172. $time_type = 'order_time';
  173. [$start,$end] = $time_scope['order_time'];
  174. }
  175. $total_stage = ceil(($end - $start) / 3600);
  176. $cur_stage = 1;
  177. for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
  178. {
  179. if($cur_start + 3600 >= $end) {
  180. $cur_end = $end;
  181. } else {
  182. $cur_end = $cur_start + 3600;
  183. }
  184. $tmcond = $tmcond_gen($time_type,$time_scope,$cur_start,$cur_end);
  185. $cond = array_merge($normal_cond,$tmcond);
  186. $mod = Model();
  187. $order_id = 0;
  188. while (true)
  189. {
  190. $cond['refill_order.order_id'] = ['gt', $order_id];
  191. $items = $mod->table('refill_order,vr_order')
  192. ->field('refill_order.*,vr_order.order_state,vr_order.store_name')
  193. ->join('inner')
  194. ->on('refill_order.order_id=vr_order.order_id')
  195. ->where($cond)
  196. ->order('refill_order.order_id asc')
  197. ->limit("0,1000")
  198. ->select();
  199. if(empty($items)) break;
  200. $order_id = intval(end($items)['order_id']);
  201. yield [$items,$total_stage,$cur_stage];
  202. }
  203. $cur_stage++;
  204. }
  205. };
  206. $merchants = [];
  207. $column_values = ['平台单号','面额', '充值卡号', '充值卡类型', '是否转网', '下单日期', '完成日期', '订单状态', '流水号'];
  208. $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'];
  209. if($export_type === 'merchant') {
  210. $column_values = array_merge(['商户号', '商户订单号'], $column_values, ['扣款金额']);
  211. $data_keys = array_merge(['mchid', 'mch_order'], $data_keys, ['mch_amount']);
  212. }elseif ($export_type === 'provider') {
  213. $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']);
  214. $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['channel_amount']);
  215. }
  216. $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
  217. foreach ($merchant_list as $value) {
  218. $merchants[$value['mchid']] = $value;
  219. }
  220. $column_key = 'A';
  221. for($execl_index=0;$execl_index<count($column_values);$execl_index++){
  222. $column_keys[] = $column_key;
  223. $column_key++;
  224. }
  225. $objPHPExcel = new PHPExcel();
  226. $objPHPExcel->setActiveSheetIndex(0);
  227. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  228. foreach ($column_keys as $key => $column_key) {
  229. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
  230. $cell_value = $column_key . 1;
  231. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  232. }
  233. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  234. $execl_writer = function ($order,$index) use ($card_type_texts, $column_keys,$objPHPExcel,$data_keys)
  235. {
  236. if(!empty($merchants)) {
  237. $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
  238. }
  239. $order['card_type_text'] = $card_type_texts[$order['card_type']];
  240. $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  241. $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  242. $order['order_state_text'] = orderState($order);
  243. if($order['is_transfer'] == 1) {
  244. $order['is_transfer_text'] = '是';
  245. }else{
  246. $order['is_transfer_text'] = '否';
  247. }
  248. foreach ($column_keys as $key => $column_key) {
  249. $field = $column_key . ($index + 2);
  250. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
  251. }
  252. };
  253. $start_time = time();
  254. $percentor = function ($total_stage, $cur_stage) use ($start_time)
  255. {
  256. $task_id = $this->mTtaskId;
  257. $total_used = time() - $start_time;
  258. $remain = $total_used * ($total_stage - $cur_stage) / $cur_stage;
  259. $remain = intval($remain);
  260. $expected_time = date("H:i:s", (time() + $remain));
  261. $mod_task = Model('task');
  262. $stage = "导出进度:{$cur_stage}/{$total_stage}, 预计完成时间:{$expected_time}";
  263. $mod_task->where(['task_id' => $task_id])->update(['stage' => $stage]);
  264. };
  265. $stage = 0;
  266. $execl_index = 0;
  267. $reader = $order_reader($normal_cond,$time_scope);
  268. foreach ($reader as $result)
  269. {
  270. [$items,$total_stage,$cur_stage] = $result;
  271. foreach ($items as $order)
  272. {
  273. $execl_writer($order,$execl_index);
  274. $execl_index += 1;
  275. }
  276. Log::record("total_stage={$total_stage} cur_stage={$cur_stage}",Log::DEBUG);
  277. if($stage == 0) {
  278. $stage = $cur_stage;
  279. }
  280. elseif($stage != $cur_stage) {
  281. $percentor($total_stage,$stage);
  282. $stage = $cur_stage;
  283. }
  284. }
  285. try {
  286. $path = BASE_ROOT_PATH . "/data/upload/task/";
  287. if (!is_dir($path)) {
  288. mkdir($path, 0755);
  289. }
  290. $filename = date('YmdHis', time()) . "-订单导出.xlsx";
  291. $file_path = $path . $filename;
  292. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  293. $objWriter->save($file_path);
  294. return [true, $filename];
  295. } catch (Exception $e) {
  296. Log::record($e->getMessage(),Log::ERR);
  297. return [false, false];
  298. }
  299. }
  300. public function refill_order_export_title($condition)
  301. {
  302. return md5("refill_order_export-".serialize($condition));
  303. }
  304. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  305. public function order_stat_reload($condition)
  306. {
  307. $refill = new stat_refill();
  308. $type = $condition['type'];
  309. $time_stamp = $condition['time_stamp'];
  310. $cid = $condition['cid'];
  311. $order_time_type = $condition['order_time_type'];
  312. if ($type == 'merchant') {
  313. $refill->merchant_stat($time_stamp, $cid, $order_time_type);
  314. } elseif ($type == 'provider') {
  315. $refill->provider_stat($time_stamp, $cid, $order_time_type);
  316. } elseif ($type == 'system') {
  317. $refill->system_stat($time_stamp, $order_time_type, 'reload');
  318. }
  319. return [true, 'success'];
  320. }
  321. public function order_stat_reload_title($condition)
  322. {
  323. return md5("order_stat_reload-".serialize($condition));
  324. }
  325. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  326. public function order_stat_reload_some($condes)
  327. {
  328. foreach ($condes as $cond) {
  329. $this->order_stat_reload($cond);
  330. }
  331. return [true, 'success'];
  332. }
  333. public function order_stat_reload_some_title($condition)
  334. {
  335. return md5("order_stat_reload-some".serialize($condition));
  336. }
  337. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  338. public function refill_balance_create($condition)
  339. {
  340. $type = $condition['type'];
  341. $cid = $condition['cid'];
  342. $start = $condition['start'];
  343. $end = $condition['end'];
  344. $time_type = $condition['time_type'];
  345. $parent_balance = $condition['parent_id'];
  346. $refill_balance = new refill_balance();
  347. $refill_balance->add_balance($type, $cid, $start, $end, $time_type, $parent_balance);
  348. return [true, 'success'];
  349. }
  350. public function refill_balance_create_title($condition)
  351. {
  352. return md5("refill_balance_create-".serialize($condition));
  353. }
  354. public function refill_balance_rebuild($condition)
  355. {
  356. $balance_id = $condition['balance_id'];
  357. $refill_balance = new refill_balance();
  358. $refill_balance->rebuild_balance($balance_id);
  359. return [true, 'success'];
  360. }
  361. public function refill_balance_rebuild_title($condition)
  362. {
  363. return md5("refill_balance_rebuild-".serialize($condition));
  364. }
  365. public function refill_balance_stat_all($condition)
  366. {
  367. $end = $condition['end'];
  368. $refill_balance = new refill_balance();
  369. $refill_balance->stat_all($end);
  370. return [true, 'success'];
  371. }
  372. public function refill_balance_stat_all_title($condition)
  373. {
  374. return md5("refill_balance_stat_all-".serialize($condition));
  375. }
  376. }