Exemplo n.º 1
0
 public function getPayDay($StartDate, $EndDate, $PassageId, $oWherePartnerPermission)
 {
     //查询列
     $select_fields = array('PayUser' => 'count(distinct(AcceptUserId))', 'PayCount' => 'count(*)', 'TotalCoin' => 'sum(Coin)', 'TotalAmount' => 'sum(Amount)', 'Date' => "from_unixtime(PayTime,'%Y-%m-%d')", 'AppId', 'PartnerId');
     //分类统计列
     $group_fields = array('Date', 'AppId', 'PartnerId');
     //初始化查询条件
     $whereStartDate = $StartDate ? " PayTime >= '" . strtotime($StartDate) . "' " : "";
     $whereEndDate = $EndDate ? " PayTime <= '" . (strtotime($EndDate) + 86400 - 1) . "' " : "";
     $wherePassage = $PassageId ? " PassageId = " . $PassageId . " " : "";
     $whereCondition = array($whereStartDate, $whereEndDate, $oWherePartnerPermission, $wherePassage);
     //生成查询列
     $fields = Base_common::getSqlFields($select_fields);
     //生成分类汇总列
     $groups = Base_common::getGroupBy($group_fields);
     //生成条件列
     $where = Base_common::getSqlWhere($whereCondition);
     $date = $StartDate;
     //初始化结果数组
     $StatArr['TotalData'] = array('TotalCoin' => 0, 'TotalAmount' => 0, 'PayUser' => 0, 'PayCount' => 0, 'ConvertedAmount' => 0);
     do {
         $StatArr['PayDate'][$date] = array('Total' => array('TotalCoin' => 0, 'TotalAmount' => 0, 'PayUser' => 0, 'PayCount' => 0, 'ConvertedAmount' => 0));
         $date = date("Y-m-d", strtotime($date) + 86400);
     } while (strtotime($date) <= strtotime($EndDate));
     $DateStart = date("Ym", strtotime($StartDate));
     $DateEnd = date("Ym", strtotime($EndDate));
     $DateList = array();
     $Date = $StartDate;
     do {
         $D = date("Ym", strtotime($Date));
         $DateList[] = $D;
         $Date = date("Y-m-d", strtotime("{$Date} +1 month"));
     } while ($D != $DateEnd);
     $oPartnerApp = new Config_Partner_App();
     $oArea = new Config_Area();
     foreach ($DateList as $key => $value) {
         $table_name = Base_Widget::getDbTable($this->table_date) . "_" . $value;
         $sql = "SELECT  {$fields} FROM {$table_name} as log where 1 " . $where . $groups;
         $PayDateArr = $this->db->getAll($sql);
         if (is_array($PayDateArr)) {
             foreach ($PayDateArr as $key => $Stat) {
                 if (!isset($PartnerAppList[$Stat['AppId']][$Stat['PartnerId']])) {
                     $PartnerAppList[$Stat['AppId']][$Stat['PartnerId']] = $oPartnerApp->getRow(array($Stat['PartnerId'], $Stat['AppId']));
                 }
                 if (!isset($AreaList[$PartnerAppList[$Stat['AppId']][$Stat['PartnerId']]['AreaId']])) {
                     $AreaList[$PartnerAppList[$Stat['AppId']][$Stat['PartnerId']]['AreaId']] = $oArea->getRow($PartnerAppList[$Stat['AppId']][$Stat['PartnerId']]['AreaId']);
                 }
                 $currency_rate = $AreaList[$PartnerAppList[$Stat['AppId']][$Stat['PartnerId']]['AreaId']]['currency_rate'];
                 //累加数据
                 if (isset($StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']])) {
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['PayCount'] += $Stat['PayCount'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['PayUser'] += $Stat['PayUser'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['TotalCoin'] += $Stat['TotalCoin'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['TotalAmount'] += $Stat['TotalAmount'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['ConvertedAmount'] += $Stat['TotalAmount'] * $currency_rate;
                 } else {
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']] = array('TotalCoin' => 0, 'TotalAmount' => 0, 'PayCount' => 0, 'PayUser' => 0, 'ConvertedAmount' => 0);
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['PayCount'] += $Stat['PayCount'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['PayUser'] += $Stat['PayUser'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['TotalCoin'] += $Stat['TotalCoin'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['TotalAmount'] += $Stat['TotalAmount'];
                     $StatArr['PayDate'][$Stat['Date']]['Detail'][$Stat['AppId']][$Stat['PartnerId']]['ConvertedAmount'] += $Stat['TotalAmount'] * $currency_rate;
                 }
                 $StatArr['PayDate'][$Stat['Date']]['Total']['PayCount'] += $Stat['PayCount'];
                 $StatArr['PayDate'][$Stat['Date']]['Total']['PayUser'] += $Stat['PayUser'];
                 $StatArr['PayDate'][$Stat['Date']]['Total']['ConvertedAmount'] += $Stat['TotalAmount'] * $currency_rate;
                 $StatArr['TotalData']['PayCount'] += $Stat['PayCount'];
                 $StatArr['TotalData']['ConvertedAmount'] += $Stat['TotalAmount'] * $currency_rate;
             }
             $StatArr['TotalData']['AmountPerPay'] = $StatArr['TotalData']['PayCount'] ? $StatArr['TotalData']['ConvertedAmount'] / $StatArr['TotalData']['PayCount'] : 0;
         }
     }
     return $StatArr;
 }