handler.php 15 KB

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