stat_refill.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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] = $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. ->limit(100)
  22. ->select();
  23. foreach ($items as $item) {
  24. $store_id = intval($item['store_id']);
  25. $this->mProviderNames[$store_id] = $item['store_name'];
  26. }
  27. }
  28. private function lastest_day()
  29. {
  30. $mod_stat = Model('refill_stats');
  31. $item = $mod_stat->latest_record_time();
  32. if(empty($item))
  33. {
  34. $mod_refill = Model('refill_order');
  35. $item = $mod_refill->first_item();
  36. if(empty($item)) {
  37. throw new Exception("refill_order table is empty");
  38. }
  39. else {
  40. $time_stamp = intval($item['order_time']);
  41. }
  42. }
  43. else {
  44. $time_stamp = intval($item['time_stamp']) + 86400; //time_stamp那天已经统计好数据了
  45. }
  46. $date = date('Ymd',$time_stamp);
  47. $time_stamp = strtotime($date);
  48. return $time_stamp;
  49. }
  50. private function end_day()
  51. {
  52. $date = date('Ymd',time());
  53. $time_stamp = strtotime($date);
  54. return $time_stamp;
  55. }
  56. public function run()
  57. {
  58. $end_tm = $this->end_day();
  59. for($start_tm = $this->lastest_day(); $start_tm < $end_tm; $start_tm += stat_refill::DaySecs)
  60. {
  61. $order_count = $this->system_stat($start_tm);
  62. if ($order_count > 0) {
  63. $this->merchant_stat($start_tm);
  64. $this->provider_stat($start_tm);
  65. }
  66. }
  67. }
  68. private function system_stat($day_time)
  69. {
  70. $cond = [
  71. 'refill_order.inner_status' => 0,
  72. 'refill_order.order_time&refill_order.order_time' => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]]
  73. ];
  74. $items = Model('')->table('refill_order,vr_order')
  75. ->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')
  76. ->join('inner')
  77. ->on('refill_order.order_id=vr_order.order_id')
  78. ->where($cond)
  79. ->group('order_state')
  80. ->select();
  81. $params = [];
  82. $params['time_text'] = date("Y-m-d" , $day_time);
  83. $params['time_stamp'] = $day_time;
  84. $params['type'] = 'system';
  85. $params['cid'] = 0;
  86. $params['cname'] = 'system';
  87. $order_count = 0;
  88. foreach ($items as $item)
  89. {
  90. $order_state = $item['order_state'];
  91. if($order_state == ORDER_STATE_SUCCESS) {
  92. $params['success_count'] = $item['order_count'];
  93. $params['success_refill_amounts'] = $item['refill_amounts'];
  94. $params['success_channel_amounts'] = $item['channel_amounts'];
  95. $params['success_mch_amounts'] = $item['mch_amounts'];
  96. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  97. }
  98. elseif($order_state == ORDER_STATE_CANCEL) {
  99. $params['cancel_count'] = $item['order_count'];
  100. }
  101. else {
  102. $params['send_count'] = $item['order_count'];
  103. }
  104. $order_count += $item['order_count'];
  105. }
  106. if($order_count <= 0) return 0;
  107. $params['order_count'] = $order_count;
  108. $order_count = intval($order_count) == 0 ? 1 : $order_count;
  109. $success_cout = intval($params['success_count']);
  110. $params['success_ratio'] = ncPriceFormat($success_cout * 100 / $order_count);
  111. if (defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY')
  112. {
  113. $amounts = $params['success_refill_amounts'];
  114. if ($amounts > 0 && $amounts <= 15000000) {
  115. $params['service_amounts'] = ncPriceFormat($amounts * 0.001);
  116. } elseif ($amounts > 15000000 && $amounts <= 30000000) {
  117. $params['service_amounts'] = 15000;
  118. } else {
  119. $params['service_amounts'] = ncPriceFormat($amounts * 0.0005);
  120. }
  121. }
  122. Model('')->table('refill_stats')->insert($params);
  123. return $order_count;
  124. }
  125. private function merchant_stat($day_time)
  126. {
  127. $cond = [
  128. 'refill_order.inner_status' => 0,
  129. 'vr_order.order_state' => ORDER_STATE_SUCCESS,
  130. 'refill_order.order_time&refill_order.order_time' => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]],
  131. ];
  132. $items = Model('')->table('refill_order,vr_order')
  133. ->field('mchid, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  134. ->join('inner')
  135. ->on('refill_order.order_id=vr_order.order_id')
  136. ->where($cond)
  137. ->group('mchid')
  138. ->select();
  139. foreach ($items as $item)
  140. {
  141. $params = [];
  142. $params['time_text'] = date("Y-m-d" , $day_time);
  143. $params['time_stamp'] = $day_time;
  144. $params['type'] = 'merchant';
  145. $mchid = intval($item['mchid']);
  146. if($mchid <= 0) continue;
  147. $params['cid'] = $mchid;
  148. if(!array_key_exists($mchid,$this->mMerchantNames)) continue;
  149. $params['cname'] = $this->mMerchantNames[$mchid];
  150. $params['success_count'] = $item['order_count'];
  151. $params['success_refill_amounts'] = $item['refill_amounts'];
  152. $params['success_channel_amounts'] = $item['channel_amounts'];
  153. $params['success_mch_amounts'] = $item['mch_amounts'];
  154. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  155. Model('')->table('refill_stats')->insert($params);
  156. }
  157. }
  158. private function provider_stat($day_time)
  159. {
  160. $cond = [
  161. 'refill_order.inner_status' => 0,
  162. 'vr_order.order_state' => ORDER_STATE_SUCCESS,
  163. 'vr_order.add_time&vr_order.add_time' => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]],
  164. ];
  165. $items = Model('')->table('refill_order,vr_order')
  166. ->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')
  167. ->join('inner')
  168. ->on('refill_order.order_id=vr_order.order_id')
  169. ->where($cond)
  170. ->group('vr_order.store_id')
  171. ->select();
  172. foreach ($items as $item)
  173. {
  174. $params = [];
  175. $params['time_text'] = date("Y-m-d" , $day_time);
  176. $params['time_stamp'] = $day_time;
  177. $params['type'] = 'provider';
  178. $store_id = intval($item['store_id']);
  179. if($store_id <= 0) continue;
  180. $params['cid'] = $store_id;
  181. if(!array_key_exists($store_id,$this->mProviderNames)) continue;
  182. $params['cname'] = $this->mProviderNames[$store_id];
  183. $params['success_count'] = $item['order_count'];
  184. $params['success_refill_amounts'] = $item['refill_amounts'];
  185. $params['success_channel_amounts'] = $item['channel_amounts'];
  186. $params['success_mch_amounts'] = $item['mch_amounts'];
  187. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  188. Model('')->table('refill_stats')->insert($params);
  189. }
  190. }
  191. }