handler.php 16 KB

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