stat_refill.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. private function system_stat($day_time, $order_time_type='notify_time')
  73. {
  74. $end_time = $day_time + stat_refill::DaySecs;
  75. $cond = [
  76. 'refill_order.inner_status' => 0,
  77. ];
  78. if($order_time_type == 'notify_time') {
  79. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  80. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  81. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  82. }
  83. else {
  84. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  85. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]];
  86. }
  87. $items = Model('')->table('refill_order,vr_order')
  88. ->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')
  89. ->join('inner')
  90. ->on('refill_order.order_id=vr_order.order_id')
  91. ->where($cond)
  92. ->group('order_state')
  93. ->select();
  94. $params = [];
  95. $params['time_text'] = date("Y-m-d" , $day_time);
  96. $params['time_stamp'] = $day_time;
  97. $params['type'] = 'system';
  98. $params['cid'] = 0;
  99. $params['cname'] = 'system';
  100. $params['order_time_type'] = $order_time_type;
  101. $order_count = 0;
  102. foreach ($items as $item)
  103. {
  104. $order_state = $item['order_state'];
  105. if($order_state == ORDER_STATE_SUCCESS) {
  106. $params['success_count'] = $item['order_count'];
  107. $params['success_refill_amounts'] = $item['refill_amounts'];
  108. $params['success_channel_amounts'] = $item['channel_amounts'];
  109. $params['success_mch_amounts'] = $item['mch_amounts'];
  110. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  111. }
  112. elseif($order_state == ORDER_STATE_CANCEL) {
  113. $params['cancel_count'] = $item['order_count'];
  114. }
  115. else {
  116. $params['send_count'] = $item['order_count'];
  117. }
  118. $order_count += $item['order_count'];
  119. }
  120. if($order_count <= 0) return 0;
  121. $params['order_count'] = $order_count;
  122. $order_count = intval($order_count) == 0 ? 1 : $order_count;
  123. $success_cout = intval($params['success_count']);
  124. $params['success_ratio'] = ncPriceFormat($success_cout * 100 / $order_count);
  125. if (defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY')
  126. {
  127. $amounts = $params['success_refill_amounts'];
  128. if ($amounts > 0 && $amounts <= 15000000) {
  129. $params['service_amounts'] = ncPriceFormat($amounts * 0.001);
  130. } elseif ($amounts > 15000000 && $amounts <= 30000000) {
  131. $params['service_amounts'] = 15000;
  132. } else {
  133. $params['service_amounts'] = ncPriceFormat($amounts * 0.0005);
  134. }
  135. }
  136. Model('')->table('refill_stats')->insert($params);
  137. return $order_count;
  138. }
  139. public function merchant_stat($day_time,$cur_mchid = 0,$order_time_type='notify_time')
  140. {
  141. $end_time = $day_time + stat_refill::DaySecs;
  142. $cond = [
  143. 'refill_order.inner_status' => 0,
  144. ];
  145. if($order_time_type == 'notify_time') {
  146. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  147. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  148. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  149. }
  150. else {
  151. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  152. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]];
  153. }
  154. foreach ($this->mMerchantNames as $mchid => $cname)
  155. {
  156. $mchid = intval($mchid);
  157. if($mchid <= 0) continue;
  158. if($cur_mchid != 0 && $cur_mchid != $mchid) continue;
  159. $cond['refill_order.mchid'] = $mchid;
  160. $items = Model('')->table('refill_order,vr_order')
  161. ->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')
  162. ->join('inner')
  163. ->on('refill_order.order_id=vr_order.order_id')
  164. ->where($cond)
  165. ->group('order_state')
  166. ->select();
  167. if(empty($items)) continue;
  168. $params = [];
  169. $params['time_text'] = date("Y-m-d" , $day_time);
  170. $params['time_stamp'] = $day_time;
  171. $params['type'] = 'merchant';
  172. $params['order_time_type'] = $order_time_type;
  173. $params['cid'] = $mchid;
  174. $params['cname'] = $cname;
  175. foreach ($items as $item)
  176. {
  177. $order_state = $item['order_state'];
  178. if ($order_state == ORDER_STATE_SUCCESS) {
  179. $params['success_count'] = $item['order_count'];
  180. $params['success_refill_amounts'] = $item['refill_amounts'];
  181. $params['success_channel_amounts'] = $item['channel_amounts'];
  182. $params['success_mch_amounts'] = $item['mch_amounts'];
  183. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  184. } elseif ($order_state == ORDER_STATE_CANCEL) {
  185. $params['cancel_count'] = $item['order_count'];
  186. } else {
  187. $params['send_count'] = $item['order_count'];
  188. }
  189. }
  190. if ($cur_mchid != 0 && $cur_mchid == $mchid) {
  191. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_mchid, 'type' => 'merchant', 'order_time_type' => $order_time_type])->update($params);
  192. } elseif (!empty($params['success_count'])) {
  193. Model('')->table('refill_stats')->insert($params);
  194. } else {
  195. }
  196. }
  197. }
  198. public function provider_stat($day_time,$cur_storeid = 0,$order_time_type='notify_time')
  199. {
  200. $end_time = $day_time + stat_refill::DaySecs;
  201. $cond = [
  202. 'refill_order.inner_status' => 0,
  203. ];
  204. if($order_time_type == 'notify_time') {
  205. $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  206. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  207. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]];
  208. }
  209. else {
  210. $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]];
  211. $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]];
  212. }
  213. foreach ($this->mProviderNames as $store_id => $cname)
  214. {
  215. $store_id = intval($store_id);
  216. if($store_id <= 0) continue;
  217. if($cur_storeid != 0 && $cur_storeid != $store_id) continue;
  218. $cond['vr_order.store_id'] = $store_id;
  219. $items = Model('')->table('refill_order,vr_order')
  220. ->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')
  221. ->join('inner')
  222. ->on('refill_order.order_id=vr_order.order_id')
  223. ->where($cond)
  224. ->group('order_state')
  225. ->select();
  226. if(empty($items)) continue;
  227. $params = [];
  228. $params['time_text'] = date("Y-m-d" , $day_time);
  229. $params['time_stamp'] = $day_time;
  230. $params['type'] = 'provider';
  231. $params['order_time_type'] = $order_time_type;
  232. $params['cid'] = $store_id;
  233. $params['cname'] = $cname;
  234. foreach ($items as $item)
  235. {
  236. $order_state = $item['order_state'];
  237. if ($order_state == ORDER_STATE_SUCCESS) {
  238. $params['success_count'] = $item['order_count'];
  239. $params['success_refill_amounts'] = $item['refill_amounts'];
  240. $params['success_channel_amounts'] = $item['channel_amounts'];
  241. $params['success_mch_amounts'] = $item['mch_amounts'];
  242. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  243. } elseif ($order_state == ORDER_STATE_CANCEL) {
  244. $params['cancel_count'] = $item['order_count'];
  245. } else {
  246. $params['send_count'] = $item['order_count'];
  247. }
  248. }
  249. if ($cur_storeid != 0 && $cur_storeid == $store_id) {
  250. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_storeid, 'type' => 'provider', 'order_time_type' => $order_time_type])->update($params);
  251. } elseif (!empty($params['success_count'])) {
  252. Model('')->table('refill_stats')->insert($params);
  253. } else {
  254. }
  255. }
  256. }
  257. }