stat_refill.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace statistics;
  3. use Exception;
  4. class stat_refill
  5. {
  6. const DaySecs = 86400;
  7. private $mMerchantNames = [];
  8. private $mProviderNames = [];
  9. public function __construct()
  10. {
  11. $mod_merchant = Model('merchant');
  12. $items = $mod_merchant->getMerchantList(['mchid' => ['gt',0]]);
  13. foreach ($items as $item) {
  14. $mchid = intval($item['mchid']);
  15. $this->mMerchantNames[$mchid] = !empty($item['company_name']) ? $item['company_name'] : $item['name'];
  16. }
  17. $items = Model('')->table('refill_provider,store')
  18. ->field('refill_provider.store_id,store.store_name')->join('inner')
  19. ->on('store.store_id=refill_provider.store_id')
  20. ->where(['refill_provider.provider_id' => ['gt',0]])
  21. ->select();
  22. foreach ($items as $item) {
  23. $store_id = intval($item['store_id']);
  24. $this->mProviderNames[$store_id] = $item['store_name'];
  25. }
  26. }
  27. private function lastest_day()
  28. {
  29. $mod_stat = Model('refill_stats');
  30. $item = $mod_stat->latest_record_time();
  31. if(empty($item))
  32. {
  33. $mod_refill = Model('refill_order');
  34. $item = $mod_refill->first_item();
  35. if(empty($item)) {
  36. throw new Exception("refill_order table is empty");
  37. }
  38. else {
  39. $time_stamp = intval($item['order_time']);
  40. }
  41. }
  42. else {
  43. $time_stamp = intval($item['time_stamp']) + 86400; //time_stamp那天已经统计好数据了
  44. }
  45. $date = date('Ymd',$time_stamp);
  46. $time_stamp = strtotime($date);
  47. return $time_stamp;
  48. }
  49. private function end_day()
  50. {
  51. $date = date('Ymd',time());
  52. $time_stamp = strtotime($date);
  53. return $time_stamp;
  54. }
  55. public function run()
  56. {
  57. $end_tm = $this->end_day();
  58. for($start_tm = $this->lastest_day(); $start_tm < $end_tm; $start_tm += stat_refill::DaySecs)
  59. {
  60. $order_count = $this->system_stat($start_tm,'notify_time');
  61. if ($order_count > 0) {
  62. $this->merchant_stat($start_tm,'notify_time');
  63. $this->provider_stat($start_tm,'notify_time');
  64. }
  65. $order_count = $this->system_stat($start_tm,'order_time');
  66. if ($order_count > 0) {
  67. $this->merchant_stat($start_tm,0,'order_time');
  68. $this->provider_stat($start_tm,0,'order_time');
  69. }
  70. }
  71. }
  72. public function restat($dates)
  73. {
  74. $deleter = function ($daystamp)
  75. {
  76. Model('')->table('refill_stats')->where(['time_stamp' => $daystamp])->delete();
  77. };
  78. $flag = strtotime('2021-01-01');
  79. foreach ($dates as $date)
  80. {
  81. $time = strtotime($date);
  82. $date = date('Ymd',$time);
  83. $daystamp = strtotime($date);
  84. if($daystamp < $flag) continue;
  85. $deleter($daystamp);
  86. $order_count = $this->system_stat($daystamp,'notify_time');
  87. if ($order_count > 0) {
  88. $this->merchant_stat($daystamp,'notify_time');
  89. $this->provider_stat($daystamp,'notify_time');
  90. }
  91. $order_count = $this->system_stat($daystamp,'order_time');
  92. if ($order_count > 0) {
  93. $this->merchant_stat($daystamp,0,'order_time');
  94. $this->provider_stat($daystamp,0,'order_time');
  95. }
  96. }
  97. }
  98. public function system_stat($day_time, $order_time_type='notify_time', $stat_type='create')
  99. {
  100. $end_time = $day_time + stat_refill::DaySecs;
  101. $cond = [
  102. 'refill_order.inner_status' => 0,
  103. ];
  104. if($order_time_type == 'notify_time') {
  105. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  106. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  107. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  108. }
  109. else {
  110. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  111. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]];
  112. }
  113. $items = Model('')->table('refill_order,vr_order')
  114. ->field('order_state, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  115. ->join('inner')
  116. ->on('refill_order.order_id=vr_order.order_id')
  117. ->where($cond)
  118. ->group('order_state')
  119. ->select();
  120. $params = [];
  121. $params['time_text'] = date("Y-m-d" , $day_time);
  122. $params['time_stamp'] = $day_time;
  123. $params['type'] = 'system';
  124. $params['cid'] = 0;
  125. $params['cname'] = 'system';
  126. $params['order_time_type'] = $order_time_type;
  127. $params = $this->init_count($params);
  128. $order_count = 0;
  129. foreach ($items as $item)
  130. {
  131. $order_state = $item['order_state'];
  132. if($order_state == ORDER_STATE_SUCCESS) {
  133. $params['success_count'] = $item['order_count'];
  134. $params['success_refill_amounts'] = $item['refill_amounts'];
  135. $params['success_channel_amounts'] = $item['channel_amounts'];
  136. $params['success_mch_amounts'] = $item['mch_amounts'];
  137. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  138. }
  139. elseif($order_state == ORDER_STATE_CANCEL) {
  140. $params['cancel_count'] = $item['order_count'];
  141. }
  142. else {
  143. $params['send_count'] = $item['order_count'];
  144. }
  145. $order_count += $item['order_count'];
  146. }
  147. if($order_count <= 0) return 0;
  148. $params['order_count'] = $order_count;
  149. $order_count = intval($order_count) == 0 ? 1 : $order_count;
  150. $success_cout = intval($params['success_count']);
  151. $params['success_ratio'] = ncPriceFormat($success_cout * 100 / $order_count);
  152. if (defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY')
  153. {
  154. $amounts = $params['success_refill_amounts'];
  155. if ($amounts > 0 && $amounts <= 15000000) {
  156. $params['service_amounts'] = ncPriceFormat($amounts * 0.001);
  157. } elseif ($amounts > 15000000 && $amounts <= 30000000) {
  158. $params['service_amounts'] = 15000;
  159. } else {
  160. $params['service_amounts'] = ncPriceFormat($amounts * 0.0005);
  161. }
  162. }
  163. if ($stat_type == 'reload') {
  164. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'type' => 'system', 'order_time_type' => $order_time_type])->update($params);
  165. } elseif ($stat_type == 'create') {
  166. Model('')->table('refill_stats')->insert($params);
  167. }
  168. return $order_count;
  169. }
  170. public function merchant_stat($day_time,$cur_mchid = 0,$order_time_type='notify_time')
  171. {
  172. $end_time = $day_time + stat_refill::DaySecs;
  173. $cond = [
  174. 'refill_order.inner_status' => 0,
  175. ];
  176. if($order_time_type == 'notify_time') {
  177. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  178. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  179. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  180. }
  181. else {
  182. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  183. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]];
  184. }
  185. foreach ($this->mMerchantNames as $mchid => $cname)
  186. {
  187. $mchid = intval($mchid);
  188. if($mchid <= 0) continue;
  189. if($cur_mchid != 0 && $cur_mchid != $mchid) continue;
  190. $cond['refill_order.mchid'] = $mchid;
  191. $items = Model('')->table('refill_order,vr_order')
  192. ->field('mchid, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
  193. ->join('inner')
  194. ->on('refill_order.order_id=vr_order.order_id')
  195. ->where($cond)
  196. ->group('order_state')
  197. ->select();
  198. if(empty($items)) continue;
  199. $params = [];
  200. $params['time_text'] = date("Y-m-d" , $day_time);
  201. $params['time_stamp'] = $day_time;
  202. $params['type'] = 'merchant';
  203. $params['order_time_type'] = $order_time_type;
  204. $params['cid'] = $mchid;
  205. $params['cname'] = $cname;
  206. $params = $this->init_count($params);
  207. foreach ($items as $item)
  208. {
  209. $order_state = $item['order_state'];
  210. if ($order_state == ORDER_STATE_SUCCESS) {
  211. $params['success_count'] = $item['order_count'];
  212. $params['success_refill_amounts'] = $item['refill_amounts'];
  213. $params['success_channel_amounts'] = $item['channel_amounts'];
  214. $params['success_mch_amounts'] = $item['mch_amounts'];
  215. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  216. } elseif ($order_state == ORDER_STATE_CANCEL) {
  217. $params['cancel_count'] = $item['order_count'];
  218. } else {
  219. $params['send_count'] = $item['order_count'];
  220. }
  221. }
  222. if ($cur_mchid != 0 && $cur_mchid == $mchid) {
  223. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_mchid, 'type' => 'merchant', 'order_time_type' => $order_time_type])->update($params);
  224. } elseif (!empty($params['success_count'])) {
  225. Model('')->table('refill_stats')->insert($params);
  226. } else {
  227. }
  228. }
  229. }
  230. public function provider_stat($day_time,$cur_storeid = 0,$order_time_type='notify_time')
  231. {
  232. $end_time = $day_time + stat_refill::DaySecs;
  233. $cond = [
  234. 'refill_order.inner_status' => 0,
  235. ];
  236. if($order_time_type == 'notify_time') {
  237. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  238. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  239. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  240. }
  241. else {
  242. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  243. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]];
  244. }
  245. foreach ($this->mProviderNames as $store_id => $cname)
  246. {
  247. $store_id = intval($store_id);
  248. if($store_id <= 0) continue;
  249. if($cur_storeid != 0 && $cur_storeid != $store_id) continue;
  250. $cond['vr_order.store_id'] = $store_id;
  251. $items = Model('')->table('refill_order,vr_order')
  252. ->field('vr_order.store_id, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts, order_state')
  253. ->join('inner')
  254. ->on('refill_order.order_id=vr_order.order_id')
  255. ->where($cond)
  256. ->group('order_state')
  257. ->select();
  258. if(empty($items)) continue;
  259. $params = [];
  260. $params['time_text'] = date("Y-m-d" , $day_time);
  261. $params['time_stamp'] = $day_time;
  262. $params['type'] = 'provider';
  263. $params['order_time_type'] = $order_time_type;
  264. $params['cid'] = $store_id;
  265. $params['cname'] = $cname;
  266. $params = $this->init_count($params);
  267. foreach ($items as $item)
  268. {
  269. $order_state = $item['order_state'];
  270. if ($order_state == ORDER_STATE_SUCCESS) {
  271. $params['success_count'] = $item['order_count'];
  272. $params['success_refill_amounts'] = $item['refill_amounts'];
  273. $params['success_channel_amounts'] = $item['channel_amounts'];
  274. $params['success_mch_amounts'] = $item['mch_amounts'];
  275. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  276. } elseif ($order_state == ORDER_STATE_CANCEL) {
  277. $params['cancel_count'] = $item['order_count'];
  278. } else {
  279. $params['send_count'] = $item['order_count'];
  280. }
  281. }
  282. if ($cur_storeid != 0 && $cur_storeid == $store_id) {
  283. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_storeid, 'type' => 'provider', 'order_time_type' => $order_time_type])->update($params);
  284. } elseif (!empty($params['success_count'])) {
  285. Model('')->table('refill_stats')->insert($params);
  286. } else {
  287. }
  288. }
  289. }
  290. private function init_count($params)
  291. {
  292. $params['success_count'] = 0;
  293. $params['success_refill_amounts'] = 0;
  294. $params['success_channel_amounts'] = 0;
  295. $params['success_mch_amounts'] = 0;
  296. $params['profit_amounts'] = 0;
  297. $params['cancel_count'] = 0;
  298. $params['send_count'] = 0;
  299. return $params;
  300. }
  301. }