stat_refill.php 12 KB

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