orderstats.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. require_once(BASE_HELPER_PATH . '/task/task_helper.php');
  3. class orderstatsControl extends SystemControl
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. }
  9. public function indexOp()
  10. {
  11. $type = $_GET['type'] ?? 'system';
  12. $page = "{$type}.order.stats";
  13. $_GET['query_start_time'] = $_GET['query_start_time'] ?? date("Y-m-d 00:00:00", strtotime("-1 day"));
  14. $model_refill_order = Model('refill_order');
  15. $condition = $this->ct_cond($_GET, 'time_stamp');
  16. if (!empty($_GET['order_time_type'])) {
  17. $condition['order_time_type'] = $_GET['order_time_type'];
  18. }
  19. if(!empty($_GET['reload'])) {
  20. $stats_list = $model_refill_order->getAllRefillStats($condition);
  21. $this->stats_reload($stats_list);
  22. $json_str = json_encode($condition);
  23. $this->log("对账管理,批量重新统计,条件:{$json_str}", 1);
  24. showMessage('操作完成');
  25. }
  26. $stats_list = $model_refill_order->getOrderStatsList($condition, 50, '*', 'time_stamp desc, cname asc');
  27. if($type == 'merchant') {
  28. foreach ($stats_list as $key => $stats) {
  29. $time = date("Y-m-d",$stats['time_stamp']+86400);
  30. $mch_cache = rcache("merchant_balance_{$time}", 'refill-');
  31. $caches = empty($mch_cache['data']) ? [] : unserialize($mch_cache['data']);
  32. if(empty($caches)) continue;
  33. $stats_list[$key]['available'] = ncPriceFormat($caches[$stats['cid']]) ?? '';
  34. }
  35. }
  36. $total_stats = $this->stats($condition);
  37. $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
  38. Tpl::output('total_stats', $total_stats);
  39. Tpl::output('stats_list', $stats_list);
  40. Tpl::output('order_time_type_text', $order_time_type_text);
  41. Tpl::output('show_page', $model_refill_order->showpage());
  42. Tpl::showpage($page);
  43. }
  44. private function stats_reload($stats_list)
  45. {
  46. foreach ($stats_list as $stats)
  47. {
  48. if($stats['send_count'] == 0) continue;
  49. $cond = [
  50. 'type' => $stats['type'],
  51. 'time_stamp' => $stats['time_stamp'],
  52. 'cid' => $stats['cid'],
  53. 'order_time_type' => $stats['order_time_type']
  54. ];
  55. $manager = new task\manager();
  56. $manager->add_task('order_stat_reload',$cond,0,3600);
  57. }
  58. }
  59. private function ct_cond($input,$time_stamp)
  60. {
  61. $condition['type'] = $input['type'] ?? 'system';
  62. if (!empty($input['cid'])) {
  63. $condition['cid'] = ['in', $input['cid']];
  64. }
  65. $start_unixtime = intval(strtotime($input['query_start_time']));
  66. $end_unixtime = intval(strtotime($input['query_end_time']));
  67. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  68. $condition[$time_stamp] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  69. } elseif ($start_unixtime > 0) {
  70. $condition[$time_stamp] = ['egt', $start_unixtime];
  71. } elseif ($end_unixtime > 0) {
  72. $condition[$time_stamp] = ['lt', $end_unixtime];
  73. }
  74. return $condition;
  75. }
  76. public function ExportDataOp()
  77. {
  78. $type = $_GET['type'] ?? 'system';
  79. $model_refill_order = Model('refill_order');
  80. $condition['type'] = $type;
  81. if (!empty($_GET['cid'])) {
  82. $condition['cid'] = ['in', $_GET['cid']];
  83. }
  84. if (!empty($_GET['order_time_type'])) {
  85. $condition['order_time_type'] = $_GET['order_time_type'];
  86. }
  87. $start_unixtime = intval($_GET['query_start_time']);
  88. $end_unixtime = intval($_GET['query_end_time']);
  89. if ($start_unixtime > 0 && $end_unixtime > $start_unixtime) {
  90. $condition['time_stamp'] = [['egt', $start_unixtime], ['lt', $end_unixtime], 'and'];
  91. } elseif ($start_unixtime > 0) {
  92. $condition['time_stamp'] = ['egt', $start_unixtime];
  93. } elseif ($end_unixtime > 0) {
  94. $condition['time_stamp'] = ['lt', $end_unixtime];
  95. }
  96. $stats_list = $this->all_orderstats_data($condition);
  97. $total_stats = $this->stats($condition);
  98. $stats = $total_stats[$_GET['order_time_type']];
  99. if(empty($stats)) {
  100. $stats = $total_stats['notify_time'];
  101. }
  102. $result['data'] = $stats_list;
  103. $result['total_stats'] = $stats;
  104. echo(json_encode($result));
  105. }
  106. private function stats($condition)
  107. {
  108. $order_time_type = ['notify_time','order_time'];
  109. foreach ($order_time_type as $type){
  110. $condition['order_time_type'] = $type;
  111. $stats = Model('')->table('refill_stats')
  112. ->field('sum(success_count) as success_count,sum(success_refill_amounts) as success_refill_amounts,sum(success_mch_amounts) as success_mch_amounts,
  113. sum(success_channel_amounts) as success_channel_amounts,sum(profit_amounts) as profit_amounts,sum(send_count) as send_count')
  114. ->where($condition)
  115. ->find();
  116. $total_stats[$type] = [
  117. 'success_count_total' => $stats['success_count'],
  118. 'success_refill_amounts_total' => ncPriceFormat($stats['success_refill_amounts']),
  119. 'success_mch_amounts_toatl' => ncPriceFormat($stats['success_mch_amounts']),
  120. 'success_channel_amounts_total' => ncPriceFormat($stats['success_channel_amounts']),
  121. 'profit_amounts_total' => ncPriceFormat($stats['profit_amounts']),
  122. 'send_count_total' => $stats['send_count'],
  123. ];
  124. }
  125. return $total_stats;
  126. }
  127. private function all_orderstats_data($condition): array
  128. {
  129. $i = 0;
  130. $result = [];
  131. while (true) {
  132. $start = $i * 1000;
  133. $list = Model('')->table('refill_stats')->field('*')
  134. ->where($condition)->order('time_stamp desc')->limit("{$start},1000")->select();
  135. if (empty($list)) {
  136. break;
  137. }
  138. $i++;
  139. foreach ($list as $value) {
  140. $result[] = $value;
  141. }
  142. }
  143. return $result;
  144. }
  145. public function refill_balanceOp()
  146. {
  147. $mod = Model('refill_balance');
  148. $type = $_GET['type'] ?? 'system';
  149. $page = "{$type}.refill.balance";
  150. $cond = $this->ct_cond($_GET, 'end_stamp');
  151. if (!empty($_GET['order_time_type'])) {
  152. $cond['time_type'] = $_GET['order_time_type'];
  153. }
  154. $balance_list = $mod->getBalanceList($cond, 200);
  155. $order_time_type_text = ['notify_time' => '回调时间', 'order_time' => '下单时间'];
  156. Tpl::output('stats_list', $balance_list);
  157. Tpl::output('order_time_type_text', $order_time_type_text);
  158. Tpl::output('show_page', $mod->showpage());
  159. Tpl::showpage($page);
  160. }
  161. public function refill_balance_stat_allOp()
  162. {
  163. $end_unixtime = intval(strtotime($_GET['selectEndDate']));
  164. if(empty($end_unixtime)) {
  165. exit(json_encode(['code' => false, 'msg' => '结束日期不能为空!']));
  166. }
  167. $condition = ['end' => $end_unixtime];
  168. $manager = new task\manager();
  169. $task = $manager->add_task('refill_balance_stat_all',$condition);
  170. if ($task->completed() && $task->success()) {
  171. exit(json_encode(['code' => true, 'msg' => '操作成功,此记录已新建成功。']));
  172. } else {
  173. exit(json_encode(['code' => true, 'msg' => '操作成功,后台任务已开始新建,请稍后查看新数据']));
  174. }
  175. }
  176. public function refill_balance_createOp()
  177. {
  178. $balance_id = $_GET['balance_id'];
  179. $mod = Model('refill_balance');
  180. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  181. if(empty($balance )) {
  182. exit(json_encode(['code' => false, 'msg' => '记录不存在!']));
  183. }
  184. $start_unixtime = intval(strtotime($_GET['selectStartDate']));
  185. $end_unixtime = intval(strtotime($_GET['selectEndDate']));
  186. if(empty($start_unixtime)) {
  187. $start_unixtime = $balance['end_stamp'];
  188. }
  189. if(empty($end_unixtime)) {
  190. exit(json_encode(['code' => false, 'msg' => '结束日期不能为空!']));
  191. }
  192. $condition = [
  193. 'type' => $balance['type'], 'cid' => $balance['cid'], 'start' => $start_unixtime,
  194. 'end' => $end_unixtime, 'time_type' => $balance['time_type'], 'parent_id' => $balance_id
  195. ];
  196. $manager = new task\manager();
  197. $task = $manager->add_task('refill_balance_create',$condition,0);
  198. if ($task->completed() && $task->success()) {
  199. exit(json_encode(['code' => true, 'msg' => '操作成功,此记录已新建成功。']));
  200. } else {
  201. exit(json_encode(['code' => true, 'msg' => '操作成功,后台任务已开始新建,请稍后查看新数据']));
  202. }
  203. }
  204. public function refill_balance_rebuildOp()
  205. {
  206. $balance_id = $_GET['balance_id'];
  207. $mod = Model('refill_balance');
  208. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  209. if(empty($balance )) {
  210. showMessage('记录不存在!');
  211. }
  212. $condition['balance_id'] = $balance_id;
  213. $manager = new task\manager();
  214. $task = $manager->add_task('refill_balance_rebuild',$condition,0);
  215. if ($task->completed() && $task->success()) {
  216. showMessage('操作成功,此记录已重新生成!');
  217. } else {
  218. showMessage('操作成功,后台任务已开始重新生成,请稍后查看新数据!');
  219. }
  220. }
  221. public function refill_balance_saveOp()
  222. {
  223. showMessage('操作成功!');
  224. }
  225. public function refill_balance_confirmOp()
  226. {
  227. $balance_id = $_GET['balance_id'];
  228. $mod = Model('refill_balance');
  229. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  230. if(empty($balance )) {
  231. showMessage('记录不存在!');
  232. }
  233. $ret = $mod->confirm($balance_id);
  234. if ($ret) {
  235. showMessage('操作成功!');
  236. } else {
  237. showMessage('操作失败!');
  238. }
  239. }
  240. public function refill_balance_editOp()
  241. {
  242. $balance_id = $_POST['balance_id'];
  243. $mod = Model('refill_balance');
  244. $balance = $mod->getBalance(['balance_id' => $balance_id]);
  245. if(empty($balance )) {
  246. showMessage('记录不存在!');
  247. }
  248. $start_stamp = strtotime($_POST['start_stamp']);
  249. if(empty($start_stamp)) {
  250. $start_stamp = 0;
  251. }
  252. $end_text = $_POST['end_text'];
  253. if (!empty($end_text)) {
  254. $end_stamp = strtotime($end_text);
  255. } else {
  256. $end_stamp = 0;
  257. }
  258. if($start_stamp > $end_stamp){
  259. showMessage('统计日期有误!');
  260. }
  261. $success_count = $_POST['success_count'] ?? 0;
  262. $refill_amount = $_POST['refill_amount'] ?? 0;
  263. $mch_amount = $_POST['mch_amount'] ?? 0;
  264. $channel_amount = $_POST['channel_amount'] ?? 0;
  265. $profit_amount = $_POST['profit_amount'] ?? 0;
  266. $refund_amount = $_POST['refund_amount'] ?? 0;
  267. $except_amount = $_POST['except_amount'] ?? 0;
  268. $accumulate_balance = $_POST['accumulate_balance'] ?? 0;
  269. $balance = $_POST['balance'] ?? 0;
  270. $remark = $_POST['remark'] ?? '';
  271. $update = [
  272. 'start_stamp' => $start_stamp, 'end_stamp' => $end_stamp, 'end_text' => $end_text, 'success_count' => $success_count,
  273. 'refill_amount' => $refill_amount, 'mch_amount' => $mch_amount, 'channel_amount' => $channel_amount,
  274. 'profit_amount' => $profit_amount, 'refund_amount' => $refund_amount, 'except_amount' => $except_amount,
  275. 'accumulate_balance' => $accumulate_balance, 'balance' => $balance, 'remark' => $remark
  276. ];
  277. $resp = $mod->editBalance($balance_id, $update);
  278. if($resp) {
  279. showMessage('编辑成功!');
  280. }else{
  281. showMessage('操作失败!');
  282. }
  283. }
  284. public function refill_balance_exportOp()
  285. {
  286. $balance_ids = $_GET['balance_ids'];
  287. showMessage('操作成功!');
  288. }
  289. public function refill_balance_stat_cfgOp()
  290. {
  291. $type = $_POST['type'];
  292. $items = $result = [];
  293. if ($type === 'provider') {
  294. $items = $this->providers();
  295. $cid_field = 'store_id';
  296. $cache_field = 'provider';
  297. } elseif ($type === 'merchant') {
  298. $items = $this->merchants();
  299. $cid_field = 'mchid';
  300. $cache_field = 'merchant';
  301. } else {
  302. echo json_encode(['status' => 'fail','message' => '类型错误']);
  303. }
  304. $statType = $_POST['statType'];
  305. if(empty($statType)) {
  306. echo json_encode(['status' => 'success','message' => '操作成功']);
  307. }
  308. foreach ($items as $item)
  309. {
  310. $cid = $item[$cid_field];
  311. if (!empty($statType[$cid]) && array_key_exists($cid, $statType)) {
  312. foreach ($statType[$cid] as $value) {
  313. $result[$cid][] = $value;
  314. }
  315. }
  316. }
  317. wcache('balance-cfg', [$cache_field => serialize($result)], 'refill-');
  318. echo json_encode(['status' => 'success','message' => '操作成功']);
  319. }
  320. public function balance_cfg_dataOp()
  321. {
  322. $type = $_GET['type'];
  323. $result = [];
  324. if ($type === 'provider')
  325. {
  326. $items = $this->providers();
  327. $cfgs = $this->read_balance_cfg('balance-cfg','provider');
  328. foreach ($items as $item)
  329. {
  330. $data['name'] = $item['store_name'] ?? $item['name'];
  331. $data['value'] = $item['store_id'];
  332. if (!empty($cfgs[$item['store_id']]) && array_key_exists($item['store_id'], $cfgs)) {
  333. $data['order_time_type'] = $cfgs[$item['store_id']];
  334. } else {
  335. $data['order_time_type'] = [];
  336. }
  337. $result[] = $data;
  338. }
  339. }
  340. elseif ($type === 'merchant')
  341. {
  342. $items = $this->merchants();
  343. $cfgs = $this->read_balance_cfg('balance-cfg','merchant');
  344. foreach ($items as $item)
  345. {
  346. $data['name'] = $item['company_name'] ?? $item['name'];
  347. $data['value'] = $item['mchid'];
  348. if (!empty($cfgs[$item['mchid']]) && array_key_exists($item['mchid'], $cfgs)) {
  349. $data['order_time_type'] = $cfgs[$item['mchid']];
  350. } else {
  351. $data['order_time_type'] = [];
  352. }
  353. $result[] = $data;
  354. }
  355. }
  356. echo json_encode($result);
  357. }
  358. private function read_balance_cfg($name,$field)
  359. {
  360. $data = rcache($name, 'refill-',$field);
  361. $val = $data[$field] ?? serialize([]);
  362. return unserialize($val);
  363. }
  364. }