getMerchantList(['mchid' => ['gt',0]]); foreach ($items as $item) { $mchid = intval($item['mchid']); $this->mMerchantNames[$mchid] = $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]]) ->limit(100) ->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); } } } private function system_stat($day_time) { $cond = [ 'refill_order.inner_status' => 0, 'refill_order.order_time&refill_order.order_time' => ['_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'; $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; } private function merchant_stat($day_time) { $cond = [ 'refill_order.inner_status' => 0, 'vr_order.order_state' => ORDER_STATE_SUCCESS, 'refill_order.order_time&refill_order.order_time' => ['_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'; $mchid = intval($item['mchid']); if($mchid <= 0) 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']; Model('')->table('refill_stats')->insert($params); } } private function provider_stat($day_time) { $cond = [ 'refill_order.inner_status' => 0, 'vr_order.order_state' => ORDER_STATE_SUCCESS, 'vr_order.add_time&vr_order.add_time' => ['_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'; $store_id = intval($item['store_id']); if($store_id <= 0) 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']; Model('')->table('refill_stats')->insert($params); } } }