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,'notify_time'); if ($order_count > 0) { $this->merchant_stat($start_tm,'notify_time'); $this->provider_stat($start_tm,'notify_time'); } $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'); } } } public function restat($dates) { $deleter = function ($daystamp) { Model('')->table('refill_stats')->where(['time_stamp' => $daystamp])->delete(); }; $flag = strtotime('2021-01-01'); foreach ($dates as $date) { $time = strtotime($date); $date = date('Ymd',$time); $daystamp = strtotime($date); if($daystamp < $flag) continue; $deleter($daystamp); $order_count = $this->system_stat($daystamp,'notify_time'); if ($order_count > 0) { $this->merchant_stat($daystamp,'notify_time'); $this->provider_stat($daystamp,'notify_time'); } $order_count = $this->system_stat($daystamp,'order_time'); if ($order_count > 0) { $this->merchant_stat($daystamp,0,'order_time'); $this->provider_stat($daystamp,0,'order_time'); } } } public function system_stat($day_time, $order_time_type='notify_time', $stat_type='create') { $end_time = $day_time + stat_refill::DaySecs; $cond = [ 'refill_order.inner_status' => 0, ]; if($order_time_type == 'notify_time') { $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]]; $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]]; $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]]; } else { $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]]; $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]]; } $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; $params = $this->init_count($params); $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); } } if ($stat_type == 'reload') { Model('')->table('refill_stats')->where(['time_stamp' => $day_time, 'type' => 'system', 'order_time_type' => $order_time_type])->update($params); } elseif ($stat_type == 'create') { Model('')->table('refill_stats')->insert($params); } return $order_count; } public function merchant_stat($day_time,$cur_mchid = 0,$order_time_type='notify_time') { $end_time = $day_time + stat_refill::DaySecs; $cond = [ 'refill_order.inner_status' => 0, ]; if($order_time_type == 'notify_time') { $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]]; $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]]; $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]]; } else { $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]]; $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]]; } foreach ($this->mMerchantNames as $mchid => $cname) { $mchid = intval($mchid); if($mchid <= 0) continue; if($cur_mchid != 0 && $cur_mchid != $mchid) continue; $cond['refill_order.mchid'] = $mchid; $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, order_state') ->join('inner') ->on('refill_order.order_id=vr_order.order_id') ->where($cond) ->group('order_state') ->select(); if(empty($items)) continue; $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; $params['cid'] = $mchid; $params['cname'] = $cname; $params = $this->init_count($params); 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']; } } 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); } elseif (!empty($params['success_count'])) { Model('')->table('refill_stats')->insert($params); } else { } } } public function provider_stat($day_time,$cur_storeid = 0,$order_time_type='notify_time') { $end_time = $day_time + stat_refill::DaySecs; $cond = [ 'refill_order.inner_status' => 0, ]; if($order_time_type == 'notify_time') { $cond["refill_order.notify_time&refill_order.notify_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]]; $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]]; $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time - 3 * stat_refill::DaySecs], ['lt', $end_time]]; } else { $cond["refill_order.order_time&refill_order.order_time"] = ['_multi' => true, ['egt', $day_time], ['lt', $end_time]]; $cond["vr_order.add_time&vr_order.add_time"] = ['_multi' => true, ['egt', $day_time], ['lt', time()]]; } foreach ($this->mProviderNames as $store_id => $cname) { $store_id = intval($store_id); if($store_id <= 0) continue; if($cur_storeid != 0 && $cur_storeid != $store_id) continue; $cond['vr_order.store_id'] = $store_id; $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, order_state') ->join('inner') ->on('refill_order.order_id=vr_order.order_id') ->where($cond) ->group('order_state') ->select(); if(empty($items)) continue; $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; $params['cid'] = $store_id; $params['cname'] = $cname; $params = $this->init_count($params); 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']; } } 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); } elseif (!empty($params['success_count'])) { Model('')->table('refill_stats')->insert($params); } else { } } } private function init_count($params) { $params['success_count'] = 0; $params['success_refill_amounts'] = 0; $params['success_channel_amounts'] = 0; $params['success_mch_amounts'] = 0; $params['profit_amounts'] = 0; $params['cancel_count'] = 0; $params['send_count'] = 0; return $params; } }