db()->where($where)->find(); $update['percent'] = $percent; $update['order_total'] = $order_total; $update['order_success'] = $order_success; $update['order_fail'] = $order_fail; $update['order_channel_cash'] = $order_channel_cash; $update['account_total'] = $account_total; $update['account_success'] = $account_success; $update['account_fail'] = $account_fail; $update['account_product_success'] = $account_product_success; $update['account_profit'] = $account_profit; $update['yue'] = $yue; $update['channel_yue'] = $channel_yue; $update['yesterday_channel_yue'] = $yesterday_channel_yue; //print_r($update); if (!$info) { $update['cid'] = $cid; $update['pid'] = $pid; $update['day'] = $day; $this->db()->insert($update); } else { unset($where['cid']); unset($where['pid']); unset($where['day']); $where['id'] = $info['id']; $this->db()->where($where)->update($update); } return true; } # 统计数据 public function stat($day) { $product = ProductService::instance()->getAll(); Log::write('gateway', 'channel_stat', json_encode($product)); if ($product) { $order_data = $account_data = array(); // $orderHistoryService = OrderHistoryService::instance(); $orderHistoryService = OrderLastweekHistoryService::instance(); $merchantLogService = MerchantLogService::instance(); $accountService = AccountService::instance(); foreach ($product as $k => $v) { $order_data_temp = $orderHistoryService->getTotalData($day, $v['cid'], $v['id']); if ($order_data_temp) { if (isset($order_data[$v['cid']]) && $order_data[$v['cid']]) { foreach ($order_data_temp as $k1 => $v1) { $order_data[$v['cid']][$k1] += $order_data_temp[$k1]; } } else { $order_data[$v['cid']] = $order_data_temp; } $order_data[$v['cid']]['percent'] = $v['percent']; } $account_data_temp = $merchantLogService->getTotalData($day, $v['id']); if ($account_data_temp) { if (isset($account_data[$v['cid']]) && $account_data[$v['cid']]) { foreach ($account_data[$v['cid']] as $k1 => $v1) { $account_data[$v['cid']][$k1] += $account_data_temp[$k1]; } } else { $account_data[$v['cid']] = $account_data_temp; } } } if ($order_data) { foreach ($order_data as $k => $v) { if (isset($account_data[$k]) && $account_data[$k]) { $c = $account_data[$k]; } else { $c = false; } # 获取当天的折扣 $today = $this->db()->where(array('cid' => $k, 'day' => $day))->find(); if ($today && $today['percent']) { $v['percent'] = $today['percent']; } # 计算渠道产品的利润 $v['order_channel_cash'] = $v['success'] * $v['percent']; $old_day = date('Y-m-d', strtotime($day . '-1 day')); $old_day = $this->db()->where(array('cid' => $k, 'day' => $old_day))->find(); if (!$old_day) { $account_info = $accountService->db()->where(array('cid' => $k))->order('id asc')->find(); #todo 未查到记录是否需要写入?目前的逻辑没有写入,导致渠道订单统计永远是空的 if (!$account_info) { continue; } # 得到初始余额 $yesterday_channel_yue = $account_info['num']; # 得到当天的总充值余额 $yue = 0; # 得到最新余额 $channel_yue = $yesterday_channel_yue; } else { # 得到昨天的余额 $yesterday_channel_yue = $old_day['channel_yue']; # 得到当天的总充值余额 $yue = $accountService->db()->where(array('cid' => $k, 'day' => $day))->sum('num'); # 得到最新余额 $channel_yue = $yesterday_channel_yue + $yue - $c['product_success'];//$v['order_channel_cash'] } if (!$channel_yue) { $channel_yue = 0; } if ($channel_yue <= 0) { //continue; } $this->handle($day, $k, 0, $v['percent'], $channel_yue, $v, $c, $yue, $yesterday_channel_yue); } } } } # 处理数据 public function handle($day, $cid, $pid, $percent, $channel_yue, $order_data, $account_data, $yue, $yesterday_channel_yue) { if (!$account_data) { $account_data['total'] = 0; $account_data['success'] = 0; $account_data['fail'] = 0; $account_data['product_success'] = 0; $account_data['profit'] = 0; } $this->up($day, $cid, $pid, $percent, $order_data['total'], $order_data['success'], $order_data['fail'], $order_data['order_channel_cash'], $account_data['total'], $account_data['success'], $account_data['fail'], $account_data['product_success'], $account_data['profit'], $yue, $channel_yue, $yesterday_channel_yue); } }