handler.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. public function refill_order_export($cond)
  139. {
  140. $tmcond_gen = function ($time_type,$time_scope,$cur_start,$cur_end)
  141. {
  142. if($time_type == 'notify_time') {
  143. $start = $cur_start - 86400*2;
  144. $end = $cur_end + 86400*2;
  145. $cond['refill_order.order_time'] = [['egt', $start], ['lt', $cur_end], 'and'];
  146. $cond['vr_order.add_time'] = [['egt', $start], ['elt', $end], 'and'];
  147. $cond['refill_order.notify_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  148. } else {
  149. $cond['refill_order.order_time'] = [['egt', $cur_start], ['lt', $cur_end], 'and'];
  150. $end = $cur_end + 86400*2;
  151. $cond['vr_order.add_time'] = [['egt', $cur_start], ['elt', $end], 'and'];
  152. }
  153. return $cond;
  154. };
  155. $normal_cond = $cond['normal'];
  156. $time_scope = $cond['time_scope'];
  157. $export_type = $cond['export_type'];
  158. $order_reader = function ($normal_cond,$time_scope) use ($tmcond_gen)
  159. {
  160. if(isset($time_scope['notify_time'])) {
  161. $time_type = 'notify_time';
  162. [$start,$end] = $time_scope['notify_time'];
  163. } else {
  164. $time_type = 'order_time';
  165. [$start,$end] = $time_scope['order_time'];
  166. }
  167. for ($cur_start = $start; $cur_start < $end; $cur_start += 3600)
  168. {
  169. if($cur_start + 3600 >= $end) {
  170. $cur_end = $end;
  171. } else {
  172. $cur_end = $cur_start + 3600;
  173. }
  174. $tmcond = $tmcond_gen($time_type,$time_scope,$cur_start,$cur_end);
  175. $cond = array_merge($normal_cond,$tmcond);
  176. $mod = Model();
  177. $len = 1000;
  178. $i = 0;
  179. while (true)
  180. {
  181. $start = $i * $len;
  182. $items = $mod->table('refill_order,vr_order')
  183. ->field('refill_order.*,vr_order.order_state,vr_order.store_name')
  184. ->join('inner')
  185. ->on('refill_order.order_id=vr_order.order_id')
  186. ->where($cond)
  187. ->order('refill_order.order_id asc')
  188. ->limit("{$start},{$len}")
  189. ->select();
  190. $i++;
  191. if(empty($items)) break;
  192. foreach ($items as $item) {
  193. yield $item;
  194. }
  195. }
  196. }
  197. };
  198. $merchants = [];
  199. $column_values = ['平台单号','面额', '充值卡号', '充值卡类型', '是否转网', '下单日期', '完成日期', '订单状态', '流水号'];
  200. $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'];
  201. if($export_type === 'merchant') {
  202. $column_values = array_merge(['商户号', '商户订单号'], $column_values, ['扣款金额']);
  203. $data_keys = array_merge(['mchid', 'mch_order'], $data_keys, ['mch_amount']);
  204. }elseif ($export_type === 'provider') {
  205. $column_values = array_merge(['上游名称', '上游订单号'], $column_values, ['折扣金额']);
  206. $data_keys = array_merge(['store_name', 'ch_trade_no'], $data_keys, ['channel_amount']);
  207. }
  208. $merchant_list = Model('')->table('merchant')->limit(1000)->order('company_name asc')->select();
  209. foreach ($merchant_list as $value) {
  210. $merchants[$value['mchid']] = $value;
  211. }
  212. $column_key = 'A';
  213. for($index=0;$index<count($column_values);$index++){
  214. $column_keys[] = $column_key;
  215. $column_key++;
  216. }
  217. $objPHPExcel = new PHPExcel();
  218. $objPHPExcel->setActiveSheetIndex(0);
  219. $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
  220. foreach ($column_keys as $key => $column_key) {
  221. $objPHPExcel->getActiveSheet()->getColumnDimension($column_key)->setWidth(15);
  222. $cell_value = $column_key . 1;
  223. $objPHPExcel->getActiveSheet()->setCellValue($cell_value, $column_values[$key]);
  224. }
  225. $card_type_texts = [mtopcard\PetroChinaCard => '中石油', mtopcard\SinopecCard => '中石化', mtopcard\ChinaMobileCard => '中国移动', mtopcard\ChinaUnicomCard => '中国联通', mtopcard\ChinaTelecomCard => '中国电信'];
  226. $orders = $order_reader($normal_cond,$time_scope);
  227. $index = 0;
  228. foreach ($orders as $order)
  229. {
  230. if(!empty($merchants)) {
  231. $order['mch_name'] = $merchants[$order['mchid']]['company_name'];
  232. }
  233. $order['card_type_text'] = $card_type_texts[$order['card_type']];
  234. $order['order_time_text'] = $order['order_time'] ? date('Y-m-d H:i:s', $order['order_time']) : '';
  235. $order['notify_time_text'] = $order['notify_time'] ? date('Y-m-d H:i:s', $order['notify_time']) : '';
  236. $order['order_state_text'] = orderState($order);
  237. if($order['is_transfer'] == 1) {
  238. $order['is_transfer_text'] = '是';
  239. }else{
  240. $order['is_transfer_text'] = '否';
  241. }
  242. foreach ($column_keys as $key => $column_key) {
  243. $field = $column_key . ($index + 2);
  244. $objPHPExcel->getActiveSheet()->setCellValueExplicit($field, $order[$data_keys[$key]]);
  245. }
  246. $index += 1;
  247. }
  248. try {
  249. $path = BASE_ROOT_PATH . "/data/upload/task/";
  250. if (!is_dir($path)) {
  251. mkdir($path, 0755);
  252. }
  253. $filename = date('YmdHis', time()) . "-订单导出.xlsx";
  254. $file_path = $path . $filename;
  255. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  256. $objWriter->save($file_path);
  257. return [true, $filename];
  258. } catch (Exception $e) {
  259. return [false, false];
  260. }
  261. }
  262. public function refill_order_export_title($condition)
  263. {
  264. return md5("refill_order_export-".serialize($condition));
  265. }
  266. public function order_stat_reload($condition)
  267. {
  268. $refill = new stat_refill();
  269. $type = $condition['type'];
  270. $time_stamp = $condition['time_stamp'];
  271. $cid = $condition['cid'];
  272. $order_time_type = $condition['order_time_type'];
  273. if ($type == 'merchant') {
  274. $refill->merchant_stat($time_stamp, $cid, $order_time_type);
  275. } elseif ($type == 'provider') {
  276. $refill->provider_stat($time_stamp, $cid, $order_time_type);
  277. }
  278. return [true, 'success'];
  279. }
  280. public function order_stat_reload_title($condition)
  281. {
  282. return md5("order_stat_reload-".serialize($condition));
  283. }
  284. public function refill_balance_create($condition)
  285. {
  286. $type = $condition['type'];
  287. $cid = $condition['cid'];
  288. $start = $condition['start'];
  289. $end = $condition['end'];
  290. $time_type = $condition['time_type'];
  291. $parent_balance = $condition['parent_id'];
  292. $refill_balance = new refill_balance();
  293. $refill_balance->add_balance($type, $cid, $start, $end, $time_type, $parent_balance);
  294. return [true, 'success'];
  295. }
  296. public function refill_balance_create_title($condition)
  297. {
  298. return md5("refill_balance_create-".serialize($condition));
  299. }
  300. public function refill_balance_rebuild($condition)
  301. {
  302. $balance_id = $condition['balance_id'];
  303. $refill_balance = new refill_balance();
  304. $refill_balance->rebuild_balance($balance_id);
  305. return [true, 'success'];
  306. }
  307. public function refill_balance_rebuild_title($condition)
  308. {
  309. return md5("refill_balance_rebuild-".serialize($condition));
  310. }
  311. }