orderstats.php 16 KB

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