handler.php 17 KB

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