stat_refill.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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);
  61. if ($order_count > 0) {
  62. $this->merchant_stat($start_tm);
  63. $this->provider_stat($start_tm);
  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. $cond = [
  75. 'refill_order.inner_status' => 0,
  76. "refill_order.{$order_time_type}&refill_order.{$order_time_type}" => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]]
  77. ];
  78. $items = Model('')->table('refill_order,vr_order')
  79. ->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')
  80. ->join('inner')
  81. ->on('refill_order.order_id=vr_order.order_id')
  82. ->where($cond)
  83. ->group('order_state')
  84. ->select();
  85. $params = [];
  86. $params['time_text'] = date("Y-m-d" , $day_time);
  87. $params['time_stamp'] = $day_time;
  88. $params['type'] = 'system';
  89. $params['cid'] = 0;
  90. $params['cname'] = 'system';
  91. $params['order_time_type'] = $order_time_type;
  92. $order_count = 0;
  93. foreach ($items as $item)
  94. {
  95. $order_state = $item['order_state'];
  96. if($order_state == ORDER_STATE_SUCCESS) {
  97. $params['success_count'] = $item['order_count'];
  98. $params['success_refill_amounts'] = $item['refill_amounts'];
  99. $params['success_channel_amounts'] = $item['channel_amounts'];
  100. $params['success_mch_amounts'] = $item['mch_amounts'];
  101. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  102. }
  103. elseif($order_state == ORDER_STATE_CANCEL) {
  104. $params['cancel_count'] = $item['order_count'];
  105. }
  106. else {
  107. $params['send_count'] = $item['order_count'];
  108. }
  109. $order_count += $item['order_count'];
  110. }
  111. if($order_count <= 0) return 0;
  112. $params['order_count'] = $order_count;
  113. $order_count = intval($order_count) == 0 ? 1 : $order_count;
  114. $success_cout = intval($params['success_count']);
  115. $params['success_ratio'] = ncPriceFormat($success_cout * 100 / $order_count);
  116. if (defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY')
  117. {
  118. $amounts = $params['success_refill_amounts'];
  119. if ($amounts > 0 && $amounts <= 15000000) {
  120. $params['service_amounts'] = ncPriceFormat($amounts * 0.001);
  121. } elseif ($amounts > 15000000 && $amounts <= 30000000) {
  122. $params['service_amounts'] = 15000;
  123. } else {
  124. $params['service_amounts'] = ncPriceFormat($amounts * 0.0005);
  125. }
  126. }
  127. Model('')->table('refill_stats')->insert($params);
  128. return $order_count;
  129. }
  130. public function merchant_stat($day_time,$cur_mchid = 0,$order_time_type='notify_time')
  131. {
  132. $cond = [
  133. 'refill_order.inner_status' => 0,
  134. 'vr_order.order_state' => ORDER_STATE_SUCCESS,
  135. "refill_order.{$order_time_type}&refill_order.{$order_time_type}" => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]],
  136. ];
  137. $items = Model('')->table('refill_order,vr_order')
  138. ->field('mchid, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
  139. ->join('inner')
  140. ->on('refill_order.order_id=vr_order.order_id')
  141. ->where($cond)
  142. ->group('mchid')
  143. ->select();
  144. foreach ($items as $item)
  145. {
  146. $params = [];
  147. $params['time_text'] = date("Y-m-d" , $day_time);
  148. $params['time_stamp'] = $day_time;
  149. $params['type'] = 'merchant';
  150. $params['order_time_type'] = $order_time_type;
  151. $mchid = intval($item['mchid']);
  152. if($mchid <= 0) continue;
  153. if($cur_mchid != 0 && $cur_mchid != $mchid) continue;
  154. $params['cid'] = $mchid;
  155. if(!array_key_exists($mchid,$this->mMerchantNames)) continue;
  156. $params['cname'] = $this->mMerchantNames[$mchid];
  157. $params['success_count'] = $item['order_count'];
  158. $params['success_refill_amounts'] = $item['refill_amounts'];
  159. $params['success_channel_amounts'] = $item['channel_amounts'];
  160. $params['success_mch_amounts'] = $item['mch_amounts'];
  161. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  162. if($cur_mchid != 0 && $cur_mchid == $mchid) {
  163. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_mchid, 'type' => 'merchant', 'order_time_type' => $order_time_type])->update($params);
  164. }else{
  165. Model('')->table('refill_stats')->insert($params);
  166. }
  167. }
  168. }
  169. public function provider_stat($day_time,$cur_storeid = 0,$order_time_type='notify_time')
  170. {
  171. $cond = [
  172. 'refill_order.inner_status' => 0,
  173. 'vr_order.order_state' => ORDER_STATE_SUCCESS,
  174. "refill_order.{$order_time_type}&refill_order.{$order_time_type}" => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]],
  175. ];
  176. $items = Model('')->table('refill_order,vr_order')
  177. ->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')
  178. ->join('inner')
  179. ->on('refill_order.order_id=vr_order.order_id')
  180. ->where($cond)
  181. ->group('vr_order.store_id')
  182. ->select();
  183. foreach ($items as $item)
  184. {
  185. $params = [];
  186. $params['time_text'] = date("Y-m-d" , $day_time);
  187. $params['time_stamp'] = $day_time;
  188. $params['type'] = 'provider';
  189. $params['order_time_type'] = $order_time_type;
  190. $store_id = intval($item['store_id']);
  191. if($store_id <= 0) continue;
  192. if($cur_storeid != 0 && $cur_storeid != $store_id) continue;
  193. $params['cid'] = $store_id;
  194. if(!array_key_exists($store_id,$this->mProviderNames)) continue;
  195. $params['cname'] = $this->mProviderNames[$store_id];
  196. $params['success_count'] = $item['order_count'];
  197. $params['success_refill_amounts'] = $item['refill_amounts'];
  198. $params['success_channel_amounts'] = $item['channel_amounts'];
  199. $params['success_mch_amounts'] = $item['mch_amounts'];
  200. $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
  201. if($cur_storeid != 0 && $cur_storeid == $store_id) {
  202. Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_storeid, 'type' => 'provider', 'order_time_type' => $order_time_type])->update($params);
  203. }else{
  204. Model('')->table('refill_stats')->insert($params);
  205. }
  206. }
  207. }
  208. }