stat_refill.php 8.8 KB

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