<?php
/**
 * 充值分析-钻石消耗统计数据
 */
class DiamondConsumeModel extends CI_Model
{

    /**
     * 钻石消耗统计数据
     * @param string $start
     * @param string $end
     * @return array
     */
    public function diamondConsumeData($start = '', $end = '') {
        if(empty($start)){
            $start = date('Ymd', strtotime('-7 days 0:0:1'));
        }
        if(empty($end)){
            $end = date('Ymd', strtotime('-1 days 23:59:59'));
        }
        $titles = ['diamond'];
        $tb = otable::AssetsSta();
        $sql = "SELECT * FROM {$tb} WHERE date >= {$start} AND date <= {$end}";
        $dataTemp = oo::commonOprDb('statistics')->getAll($sql,1);

        $retData = $lowRids = $addRids = [];
        $types = oo::commonOprModel('statistics')->getTypes();
        foreach($dataTemp as $k => $v){
            $v['data'] = json_decode($v['data'],true);
            $data = [];
            foreach($v['data'] as $key => $row){
                if(!in_array($row['type'], $titles) || !isset($types[$row['rid']])){
                    continue;
                }
                if(!isset($addRids[$row['type']])){
                    $addRids[$row['type']] = [];
                }
                if(!isset($lowRids[$row['type']])){
                    $lowRids[$row['type']] = [];
                }
                //排除掉的rid是因为宝藏活动的214是abs的，无法计算出发放、消耗，分那么多rid因为宝藏活动又有一个进度统计，干脆就做一块去了
                if($row['crease'] == "+" && !in_array($row['rid'], $addRids[$row['type']]) && !in_array($row['rid'],[214,1011,1012,1013,1014,1015,1016,1017,1018,1019,1020,1021,1022,1023,1024,1025,1026,1027,1028])){
                    $addRids[$row['type']][] = $row['rid'];
                }
                if($row['crease'] == "-" && !in_array($row['rid'], $lowRids[$row['type']]) && !in_array($row['rid'],[214])){
                    $lowRids[$row['type']][] = $row['rid'];
                }
                if(empty($data[$row['type']])){
                    $data[$row['type']] = [];
                }

                foreach($addRids[$row['type']] as $title_key){
                    if(!isset($data[$row['type']]['add'][$title_key])){
                        $data[$row['type']]['add'][$title_key] = 0;
                    }
                    if($row['rid'] == $title_key && $row['crease'] == '+'){
                        $data[$row['type']]['add'][$title_key] = $row['num'];
                    }
                }
                
                foreach($lowRids[$row['type']] as $title_key){
                    if(!isset($data[$row['type']]['low'][$title_key])){
                        $data[$row['type']]['low'][$title_key] = 0;
                    }
                    if($row['rid'] == $title_key && $row['crease'] == '-'){
                        $data[$row['type']]['low'][$title_key] = $row['num'];
                    }
                }
            }
            $retData['data'][$k] = $data;
            $retData['data'][$k]['date'] = $v['date'];
            $todayLogin = oo::commonOprModel('statistics')->getAllPlayer($v['date'],2);
            $retData['data'][$k]['todayLogin'] = count($todayLogin);
        }
        $name = current($titles);
        //拉直数据
        foreach($retData['data'] as $k => $val){
            foreach($titles as $v){
                if(!isset($val[$v])){
                    $val[$v] = [];
                    $retData[$k] = $val;
                }
            }
            foreach($val as $kk => $row){
                if(!is_array($addRids[$kk])){
                    continue;
                }
                foreach($addRids[$kk] as $key){
                    if(isset($retData['data'][$k][$kk]['add'][$key])){
                        continue;
                    }
                    $retData['data'][$k][$kk]['add'][$key] = 0; 
                }    
                foreach($lowRids[$kk] as $key){
                    if(isset($retData['data'][$k][$kk]['low'][$key])){
                        continue;
                    }
                    $retData['data'][$k][$kk]['low'][$key] = 0; 
                }
            }
            $retData['data'][$k]['add'] = $retData['data'][$k][$name]['add'];
            $retData['data'][$k]['low'] = $retData['data'][$k][$name]['low'];
            unset($retData['data'][$k][$name]);
        }
        foreach($addRids[$name] as $v){
            $title['add'][$v] = isset($types[$v]) ? $types[$v] : '';
            $title['add'][$v] == '推金币-投金币' && $title['add'][$v] = '推金币-投入金币';//因为发放跟消耗的rid一样，策划要不一样名称
        }
        foreach($lowRids[$name] as $v){
            $title['low'][$v] = isset($types[$v]) ? $types[$v] : '';
            $title['low'][$v] == '推金币-投金币' && $title['low'][$v] = '推金币-掉落金币';//因为发放跟消耗的rid一样，策划要不一样名称
        }
        $retData['title'] = $title;
        return $retData;
    }
}
