"游戏现金充值收入", "balance_coin_spend"=>"平台币消耗", "balance_coin_income"=>"平台币充值收入", "game_supersign_income"=>"超级签购买收入", "test_user_income"=>"测试收入", "reward_count"=>"奖励", "fine_count"=>"违规处罚", "inside_count"=>"内团", "outer_count"=>"外团", "outer_branch_count"=>"外团-分发", "other_group_count"=>"无归属团", "unsettlement_count"=>"不结算流水", ); public function _initialize() { parent::_initialize(); // echo "
";
    }
    public function index($p=1)
    {
        if(!array_key_exists("year",$_REQUEST) || !array_key_exists("type",$_REQUEST)){
            $this->redirect(ACTION_NAME, array('year' => date('Y',time()),"type"=>3));
        }
        if(!IS_ROOT){
            $this->OpAuthList= getModuleControllerAuth();
        }
        $this->assign('menubtn',$this->menuAuth());        
        $this->getYearList();
        $this->getYearData();
        $this->display();

    }
    public function updateStatement()
    {
        if(!isset($_REQUEST['time'])) $this->error("参数错误");

        $time = $_REQUEST['time'];
        $params = "php ".SUBSITE_INDEX." FinancialSummarySet/setMonthFinancialSummary/recount/1/count_date/{$time}";
        $r =  D("CmdTasks")->addTask("FinancialSummary",$params);
        if($r){
            $this->ajaxReturn(["success"=>"ok"]);
        }else{
            $this->ajaxReturn(["error"=>"error"]);
        }
    }
    //获取指定年份数据
    public function getYearData()
    {
        $Channel =  D("PaymentMerchant")->Channel;
        $Ways =  D("PaymentMerchant")->Ways;

        $senddata = array(
            "income"=>[
                "count"=>['0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00']
            ],
            "promote"=>[
                "count"=>['0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00']
            ],
            "channel"=>[
                "count"=>['0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00']
            ],
            "payway"=>[
                "count"=>['0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00']
            ]
        );

        $merchant_data = M("payment_merchant","tab_")
            ->field("identifier,name")
            ->where(['type'=>1])
            ->select();

        //将商户插入到默认数组中
        foreach ($merchant_data as $key => $value) {
            $identifier = (string)$value['identifier'];
            $this->keyname[$identifier] = $value['name'];
        }
        //其他信息插入默认
        foreach ($Channel as $key => $value) {
            $this->keyname["c".$key] = $value;
        }
        foreach ($Ways as $key => $value) {
            $this->keyname["w".$key] = $value;
        }

        $data = [];
        //初始化数据
        foreach ($this->keyname as $k => $v) {
            $data[$k] = array("name"=>$v,"money"=>['0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00','0.00']);
        }
        $map = array(
            "count_year"=>$_REQUEST['year'],
            "type"=>array("IN","1,{$_REQUEST['type']},4,5,6")
        );
        $dbres = SM("FinancialSummary","tab_")->field("*")->where($map)->select();
        // dd( $dbres);

        foreach ($dbres as $k => $v) {
            if($v['type'] == 1){
                if(!in_array($v['key_name'],$senddata['income']['list'])){
                    $senddata['income']['list'][] = $v['key_name'];
                }
            }
            if($v['type'] == 4){
                if(!in_array($v['key_name'],$senddata['channel']['list'])){
                    $senddata['channel']['list'][] = $v['key_name'];
                }
            }
            if($v['type'] == 5){
                if(!in_array($v['key_name'],$senddata['payway']['list'])){
                    $senddata['payway']['list'][] = $v['key_name'];
                }
            }
            if($v['type'] == 6){
                if(!in_array($v['key_name'],$senddata['merchant']['list'])){
                    $senddata['merchant']['list'][] = $v['key_name'];
                }
            }
            if($v['type'] == $_REQUEST['type']){
                if(!in_array($v['key_name'],$senddata['promote']['list'])){
                    $senddata['promote']['list'][] = $v['key_name'];
                }
            }
            $data[$v['key_name']]["money"][((int)$v['count_month']-1)] = $v['sum_money'];
            # code...
        }
        foreach ($senddata as $k => $v) {
            foreach ($v as $ke => $va) {
                if($ke !='count'){
                    foreach ($va as $key => $val) {
                        for ($i=0; $i < 12; $i++) { 
                            $data[$val]["money"][12] += $data[$val]["money"][$i];
                            $senddata[$k]['count'][$i] += $data[$val]["money"][$i];
                            $senddata[$k]['count'][12] +=$data[$val]["money"][$i];
                        }
                        $senddata[$k][$ke][$key] = $data[$val];
                    }
                }
                
            }
            ksort($senddata[$k]['count']);
        }
        // dd($senddata);
        $this->assign('data', $senddata);
    }
    //获取历史年份
    public function getYearList()
    {
        $star = 2019;
        $end = date("Y",time());
        $list = [];
        for ($i=$star; $i <= $end; $i++) { 
            $temp = array(
                "value"=>$i,
                "name"=>"{$i}年"
            );
            $list[] = $temp ;
        }
        $this->assign('YearList', $list);
    }
    public function menuAuth()
    {

        $isCan =  D("CmdTasks")->isCanAddTask("FinancialSummary");
        if(!$isCan){
            return [];
        }

        $mentBtn = [
            "updateStatement"=>"重算金额"
        ];
        $resarr = [];
        foreach ($mentBtn as $k => $v) {
            if(IS_ROOT){
                $resarr[] = $v;
            }else{
                if(in_array($k,$this->OpAuthList)){
                    $resarr[] = $v;
                }
            }
        }
        return $resarr;
    }

    
    
}