handler.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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)) {
  200. yield [$items,$total_stage,$cur_stage];
  201. break;
  202. }
  203. else {
  204. $last_item = end($items);
  205. $order_id = intval($last_item['order_id']);
  206. Log::record("handler order_id={$order_id}",Log::DEBUG);
  207. yield [$items,$total_stage,$cur_stage];
  208. }
  209. }
  210. $cur_stage++;
  211. }
  212. };
  213. $merchants = [];
  214. $column_values = ['平台单号','面额', '充值卡号', '充值卡类型', '是否转网', '下单日期', '完成日期', '订单状态', '流水号'];
  215. $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'];
  216. if($export_type === 'merchant') {
  217. $column_values = array_merge(['商户号', '商户订单号'], $column_values, ['扣款金额']);
  218. $data_keys = array_merge(['mchid', 'mch_order'], $data_keys, ['mch_amount']);
  219. }elseif ($export_type === 'provider') {
  220. $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']);
  221. $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['channel_amount']);
  222. }
  223. $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
  224. foreach ($merchant_list as $value) {
  225. $merchants[$value['mchid']] = $value;
  226. }
  227. $column_key = 'A';
  228. for($execl_index=0;$execl_index<count($column_values);$execl_index++){
  229. $column_keys[] = $column_key;
  230. $column_key++;
  231. }
  232. $objPHPExcel = new PHPExcel();
  233. $objPHPExcel->setActiveSheetIndex(0);
  234. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  235. foreach ($column_keys as $key => $column_key) {
  236. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
  237. $cell_value = $column_key . 1;
  238. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  239. }
  240. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  241. $execl_writer = function ($order,$index) use ($card_type_texts, $column_keys,$objPHPExcel,$data_keys)
  242. {
  243. if(!empty($merchants)) {
  244. $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
  245. }
  246. $order['card_type_text'] = $card_type_texts[$order['card_type']];
  247. $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  248. $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  249. $order['order_state_text'] = orderState($order);
  250. if($order['is_transfer'] == 1) {
  251. $order['is_transfer_text'] = '是';
  252. }else{
  253. $order['is_transfer_text'] = '否';
  254. }
  255. foreach ($column_keys as $key => $column_key) {
  256. $field = $column_key . ($index + 2);
  257. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
  258. }
  259. };
  260. $start_time = time();
  261. $percentor = function ($total_stage, $cur_stage) use ($start_time)
  262. {
  263. $task_id = $this->mTtaskId;
  264. if($total_stage > $cur_stage) {
  265. $total_used = time() - $start_time;
  266. $remain = $total_used * ($total_stage - $cur_stage) / $cur_stage;
  267. $remain = intval($remain);
  268. $expected_time = date("H:i:s", (time() + $remain));
  269. $stage = "导出进度:{$cur_stage}/{$total_stage}, 预计在{$expected_time}完成";
  270. } else {
  271. $stage = "已经完成";
  272. }
  273. $mod_task = Model('task');
  274. $mod_task->where(['task_id' => $task_id])->update(['stage' => $stage]);
  275. };
  276. $stage = 0;
  277. $execl_index = 0;
  278. $reader = $order_reader($normal_cond,$time_scope);
  279. foreach ($reader as $result)
  280. {
  281. [$items,$total_stage,$cur_stage] = $result;
  282. foreach ($items as $order)
  283. {
  284. Log::record("handler write order index={$execl_index}",Log::DEBUG);
  285. $execl_writer($order,$execl_index);
  286. $execl_index += 1;
  287. }
  288. Log::record("handler total_stage={$total_stage} cur_stage={$cur_stage}",Log::DEBUG);
  289. if($stage == 0) {
  290. $stage = $cur_stage;
  291. }
  292. elseif($stage != $cur_stage) {
  293. $percentor($total_stage,$stage);
  294. $stage = $cur_stage;
  295. }
  296. }
  297. try {
  298. $path = BASE_ROOT_PATH . "/data/upload/task/";
  299. if (!is_dir($path)) {
  300. mkdir($path, 0755);
  301. }
  302. $filename = date('YmdHis', time()) . "订单导出.xlsx";
  303. $file_path = "{$path}{$filename}";
  304. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  305. $objWriter->save($file_path);
  306. $percentor($total_stage,$total_stage);
  307. return [true, $filename];
  308. } catch (Exception $e) {
  309. Log::record("handler {$e->getMessage()}",Log::ERR);
  310. return [false, false];
  311. }
  312. }
  313. public function refill_order_export_title($condition)
  314. {
  315. return md5("refill_order_export-".serialize($condition));
  316. }
  317. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  318. public function order_stat_reload($condition)
  319. {
  320. $refill = new stat_refill();
  321. $type = $condition['type'];
  322. $time_stamp = $condition['time_stamp'];
  323. $cid = $condition['cid'];
  324. $order_time_type = $condition['order_time_type'];
  325. if ($type == 'merchant') {
  326. $refill->merchant_stat($time_stamp, $cid, $order_time_type);
  327. } elseif ($type == 'provider') {
  328. $refill->provider_stat($time_stamp, $cid, $order_time_type);
  329. } elseif ($type == 'system') {
  330. $refill->system_stat($time_stamp, $order_time_type, 'reload');
  331. }
  332. return [true, 'success'];
  333. }
  334. public function order_stat_reload_title($condition)
  335. {
  336. return md5("order_stat_reload-".serialize($condition));
  337. }
  338. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  339. public function order_stat_reload_some($condes)
  340. {
  341. foreach ($condes as $cond) {
  342. $this->order_stat_reload($cond);
  343. }
  344. return [true, 'success'];
  345. }
  346. public function order_stat_reload_some_title($condition)
  347. {
  348. return md5("order_stat_reload-some".serialize($condition));
  349. }
  350. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  351. public function refill_balance_create($condition)
  352. {
  353. $type = $condition['type'];
  354. $cid = $condition['cid'];
  355. $start = $condition['start'];
  356. $end = $condition['end'];
  357. $time_type = $condition['time_type'];
  358. $parent_balance = $condition['parent_id'];
  359. $refill_balance = new refill_balance();
  360. $refill_balance->add_balance($type, $cid, $start, $end, $time_type, $parent_balance);
  361. return [true, 'success'];
  362. }
  363. public function refill_balance_create_title($condition)
  364. {
  365. return md5("refill_balance_create-".serialize($condition));
  366. }
  367. public function refill_balance_rebuild($condition)
  368. {
  369. $balance_id = $condition['balance_id'];
  370. $refill_balance = new refill_balance();
  371. $refill_balance->rebuild_balance($balance_id);
  372. return [true, 'success'];
  373. }
  374. public function refill_balance_rebuild_title($condition)
  375. {
  376. return md5("refill_balance_rebuild-".serialize($condition));
  377. }
  378. public function refill_balance_stat_all($condition)
  379. {
  380. $end = $condition['end'];
  381. $refill_balance = new refill_balance();
  382. $refill_balance->stat_all($end);
  383. return [true, 'success'];
  384. }
  385. public function refill_balance_stat_all_title($condition)
  386. {
  387. return md5("refill_balance_stat_all-".serialize($condition));
  388. }
  389. }