123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace statistics;
- use Exception;
- class stat_refill
- {
- const DaySecs = 86400;
- private $mMerchantNames = [];
- private $mProviderNames = [];
- public function __construct()
- {
- $mod_merchant = Model('merchant');
- $items = $mod_merchant->getMerchantList(['mchid' => ['gt',0]]);
- foreach ($items as $item) {
- $mchid = intval($item['mchid']);
- $this->mMerchantNames[$mchid] = !empty($item['company_name']) ? $item['company_name'] : $item['name'];
- }
- $items = Model('')->table('refill_provider,store')
- ->field('refill_provider.store_id,store.store_name')->join('inner')
- ->on('store.store_id=refill_provider.store_id')
- ->where(['refill_provider.provider_id' => ['gt',0]])
- ->select();
- foreach ($items as $item) {
- $store_id = intval($item['store_id']);
- $this->mProviderNames[$store_id] = $item['store_name'];
- }
- }
- private function lastest_day()
- {
- $mod_stat = Model('refill_stats');
- $item = $mod_stat->latest_record_time();
- if(empty($item))
- {
- $mod_refill = Model('refill_order');
- $item = $mod_refill->first_item();
- if(empty($item)) {
- throw new Exception("refill_order table is empty");
- }
- else {
- $time_stamp = intval($item['order_time']);
- }
- }
- else {
- $time_stamp = intval($item['time_stamp']) + 86400; //time_stamp那天已经统计好数据了
- }
- $date = date('Ymd',$time_stamp);
- $time_stamp = strtotime($date);
- return $time_stamp;
- }
- private function end_day()
- {
- $date = date('Ymd',time());
- $time_stamp = strtotime($date);
- return $time_stamp;
- }
- public function run()
- {
- $end_tm = $this->end_day();
- for($start_tm = $this->lastest_day(); $start_tm < $end_tm; $start_tm += stat_refill::DaySecs)
- {
- $order_count = $this->system_stat($start_tm);
- if ($order_count > 0) {
- $this->merchant_stat($start_tm);
- $this->provider_stat($start_tm);
- }
- $order_count = $this->system_stat($start_tm,'order_time');
- if ($order_count > 0) {
- $this->merchant_stat($start_tm,0,'order_time');
- $this->provider_stat($start_tm,0,'order_time');
- }
- }
- }
- private function system_stat($day_time, $order_time_type='notify_time')
- {
- $cond = [
- 'refill_order.inner_status' => 0,
- "refill_order.{$order_time_type}&refill_order.{$order_time_type}" => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]]
- ];
- $items = Model('')->table('refill_order,vr_order')
- ->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')
- ->join('inner')
- ->on('refill_order.order_id=vr_order.order_id')
- ->where($cond)
- ->group('order_state')
- ->select();
- $params = [];
- $params['time_text'] = date("Y-m-d" , $day_time);
- $params['time_stamp'] = $day_time;
- $params['type'] = 'system';
- $params['cid'] = 0;
- $params['cname'] = 'system';
- $params['order_time_type'] = $order_time_type;
- $order_count = 0;
- foreach ($items as $item)
- {
- $order_state = $item['order_state'];
- if($order_state == ORDER_STATE_SUCCESS) {
- $params['success_count'] = $item['order_count'];
- $params['success_refill_amounts'] = $item['refill_amounts'];
- $params['success_channel_amounts'] = $item['channel_amounts'];
- $params['success_mch_amounts'] = $item['mch_amounts'];
- $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
- }
- elseif($order_state == ORDER_STATE_CANCEL) {
- $params['cancel_count'] = $item['order_count'];
- }
- else {
- $params['send_count'] = $item['order_count'];
- }
- $order_count += $item['order_count'];
- }
- if($order_count <= 0) return 0;
- $params['order_count'] = $order_count;
- $order_count = intval($order_count) == 0 ? 1 : $order_count;
- $success_cout = intval($params['success_count']);
- $params['success_ratio'] = ncPriceFormat($success_cout * 100 / $order_count);
- if (defined('COMPANY_NAME') && COMPANY_NAME === 'LZKJ_COMPANY')
- {
- $amounts = $params['success_refill_amounts'];
- if ($amounts > 0 && $amounts <= 15000000) {
- $params['service_amounts'] = ncPriceFormat($amounts * 0.001);
- } elseif ($amounts > 15000000 && $amounts <= 30000000) {
- $params['service_amounts'] = 15000;
- } else {
- $params['service_amounts'] = ncPriceFormat($amounts * 0.0005);
- }
- }
- Model('')->table('refill_stats')->insert($params);
- return $order_count;
- }
- public function merchant_stat($day_time,$cur_mchid = 0,$order_time_type='notify_time')
- {
- $cond = [
- 'refill_order.inner_status' => 0,
- 'vr_order.order_state' => ORDER_STATE_SUCCESS,
- "refill_order.{$order_time_type}&refill_order.{$order_time_type}" => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]],
- ];
- $items = Model('')->table('refill_order,vr_order')
- ->field('mchid, count(*) as order_count, sum(refill_amount) as refill_amounts, sum(channel_amount) as channel_amounts, sum(mch_amount) as mch_amounts')
- ->join('inner')
- ->on('refill_order.order_id=vr_order.order_id')
- ->where($cond)
- ->group('mchid')
- ->select();
- foreach ($items as $item)
- {
- $params = [];
- $params['time_text'] = date("Y-m-d" , $day_time);
- $params['time_stamp'] = $day_time;
- $params['type'] = 'merchant';
- $params['order_time_type'] = $order_time_type;
- $mchid = intval($item['mchid']);
- if($mchid <= 0) continue;
- if($cur_mchid != 0 && $cur_mchid != $mchid) continue;
- $params['cid'] = $mchid;
- if(!array_key_exists($mchid,$this->mMerchantNames)) continue;
- $params['cname'] = $this->mMerchantNames[$mchid];
- $params['success_count'] = $item['order_count'];
- $params['success_refill_amounts'] = $item['refill_amounts'];
- $params['success_channel_amounts'] = $item['channel_amounts'];
- $params['success_mch_amounts'] = $item['mch_amounts'];
- $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
- if($cur_mchid != 0 && $cur_mchid == $mchid) {
- Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_mchid, 'type' => 'merchant', 'order_time_type' => $order_time_type])->update($params);
- }else{
- Model('')->table('refill_stats')->insert($params);
- }
- }
- }
- public function provider_stat($day_time,$cur_storeid = 0,$order_time_type='notify_time')
- {
- $cond = [
- 'refill_order.inner_status' => 0,
- 'vr_order.order_state' => ORDER_STATE_SUCCESS,
- "refill_order.{$order_time_type}&refill_order.{$order_time_type}" => ['_multi' => true, ['egt', $day_time], ['lt', $day_time + stat_refill::DaySecs]],
- ];
- $items = Model('')->table('refill_order,vr_order')
- ->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')
- ->join('inner')
- ->on('refill_order.order_id=vr_order.order_id')
- ->where($cond)
- ->group('vr_order.store_id')
- ->select();
- foreach ($items as $item)
- {
- $params = [];
- $params['time_text'] = date("Y-m-d" , $day_time);
- $params['time_stamp'] = $day_time;
- $params['type'] = 'provider';
- $params['order_time_type'] = $order_time_type;
- $store_id = intval($item['store_id']);
- if($store_id <= 0) continue;
- if($cur_storeid != 0 && $cur_storeid != $store_id) continue;
- $params['cid'] = $store_id;
- if(!array_key_exists($store_id,$this->mProviderNames)) continue;
- $params['cname'] = $this->mProviderNames[$store_id];
- $params['success_count'] = $item['order_count'];
- $params['success_refill_amounts'] = $item['refill_amounts'];
- $params['success_channel_amounts'] = $item['channel_amounts'];
- $params['success_mch_amounts'] = $item['mch_amounts'];
- $params['profit_amounts'] = $item['mch_amounts'] - $item['channel_amounts'];
- if($cur_storeid != 0 && $cur_storeid == $store_id) {
- Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'cid' => $cur_storeid, 'type' => 'provider', 'order_time_type' => $order_time_type])->update($params);
- }else{
- Model('')->table('refill_stats')->insert($params);
- }
- }
- }
- }
|