<?php

//use Admin\FinancialSummarySetController;
namespace Admin\Controller;

/**
 * 新功能之后还未来的急跑的脚本初始化数据
 * @author chenzhi
 */
class OldCountController extends \Think\Controller
{
    /**
     * 新的游戏比例后的数据迁移
     * 重复执行将会叠加数据,请勿重复执行
     */
    public function PromoteToCompanyRatioInit()
    {
        //获取旧数据
        $oldres = M("promote_game_ratio","tab_")->field("promote_id,relation_game_id,ratio,turnover_ratio,begin_time,end_time")->group("promote_id,relation_game_id")->select();
        $adddata = [];
        $CompanyGameRatio = M("company_game_ratio","tab_");
        foreach ($oldres as $k => &$v) {
            $comoany_id = $this->promoteIdToCompanyId($v['promote_id']);
            unset($v['promote_id']);
            $v['company_id'] = $comoany_id ;
            $adddata[$comoany_id] = $v;
        }
        foreach($adddata as $k => &$v){
            $map = [
                "company_id"=>$v['company_id'],
                "relation_game_id"=>$v['relation_game_id']
            ];
            $cres = $CompanyGameRatio->where($map)->find();
            if(empty($cres)){
                $CompanyGameRatio->add($v);
            }
        }
        echo "PromoteToCompanyRatioInit success";
    }
    protected function promoteIdToCompanyId($promote_id)
    {
        return M("promote","tab_")->field("company_id")->where("id='{$promote_id}'")->find()['company_id'];
    }
    
    /**
     * 财务分类汇总统计旧数据聚合
     */
    public function FinancialSummaryInit()
    {
        //清空数据库
        $sql = "truncate table tab_financial_summary";
        $res = M()->execute($sql);
        echo "tab_financial_summary 清空成功".PHP_EOL;
        //获取19年9月到上个月的所有月份
        $monthList = $this->getMonthsList();
        $Financial = new FinancialSummarySetController();
        foreach ($monthList as $k => $v) {
            $Financial->setMonthFinancialSummary($v);
        }
    }
    /*
    *超级签订单新增识别微信或者支付宝2019-12-11
    */
    public function gameSupersignPaywayInit()
    {
        $map = array(
            "pay_status"=>1,
            "pay_way"=>0
        );
        $GameSupersign = M("GameSupersign","tab_");
        $dbres = $GameSupersign->field("id,trade_id")->where($map)->select();
        $countdb = count($dbres);
        if($countdb < 1){
            die("No initialization required");
        }
        foreach ($dbres as $key => $value) {
            $savedata = array(
                "id"=>$value['id']
            );
            $tmp = (int)substr($value['trade_id'],0,4);
            if($tmp > 3000){
                $savedata['pay_way'] = 2; //微信
            }else{
                $savedata['pay_way'] = 1;//支付宝
            }
           $tres = $GameSupersign->save($savedata);
           if($tres === false){
            die("DB ERROR");
           }
        }
        die("init ok");
    }
    /**
     * mend表新增paymount字段用于统计补链前归属金额 
     */
    public function mendPayamountInit()
    {
        //获取符合条件的
        $map = array("pay_amount"=>0);
        $Mend = M("mend","tab_");
        $Spend = M("Spend","tab_");
        $Mend->where($map)->select();
        $dbres = $Mend->field("id,user_account,promote_account,order_time,create_time")->where($map)->select();
        $countdb = count($dbres);
        if($countdb < 1) die("No initialization required");
        echo "总需要统计数:{$countdb}".PHP_EOL;
        foreach ($dbres as $key => $value) {
            $savedata = array("id"=>$value['id']);
            $saveflag = false;
            //设置订单时间
            $order_time = $value['order_time'];
            if($value['order_time'] == 0){
                $saveflag = true;
                $savedata['order_time'] = $value['create_time'];
                $order_time = $savedata['create_time'];
            }
            //获取金额
            $smap = array(
                "user_account"=>$value['user_account'],
                "promote_account"=>$value['promote_account'],
                "pay_time"=>array("LT",$order_time),
                "pay_status"=>1
            );
            $pay_amount = $Spend->field("IFNULL(sum(pay_amount),0) pay_amount")->where($smap)->find()["pay_amount"];
            if($pay_amount > 0){
                $saveflag = true;
                $savedata['pay_amount'] = $pay_amount;
            }
            if($saveflag){
                $tres = $Mend->save($savedata);
                if($tres === false){
                    die("DB ERROR");
                }
            }
            if( $key % 20 == 0){
                $j = (floor)($key/$countdb*100);
                echo "当前进度:{$j}%".PHP_EOL;
            }
            
        }
        die("init ok");
    }
    /**
     * 修改公会内外团关系跟随公司 旧数据跟随 20200318
     */
    public function promoteCompanyBelongInit()
    {
        //获取所有公司
       $comoany =  M("PromoteCompany","tab_")->field("id,company_belong")->select();
       $comoany[] = ["id"=>0,"company_belong"=>0]; //海南万盟
       $count = count($comoany);
       $promote = M("Promote","tab_");
       for ($i=0; $i < $count; $i++) { 
           $savedata = array(
             "company_belong"=>$comoany[$i]['company_belong']
           );
           $tmpid = $comoany[$i]['id'];
           $promote->where("company_id ='{$tmpid}'")->save($savedata);
       }
       die("promoteCompanyBelongInit success");
    }
    //获取公司月结所需要的所有月份截止至上个月
    public function getMonthsList()
    {
        $start_month = "2019-09";

        $temp = strtotime(date('Y-m-01 00:00:00',strtotime('-1 month')));
        $end_month = date('Y',$temp)."-".date('m',$temp);

        $time_arr = [];
        $begin = new \DateTime($start_month);
        $end = new \DateTime($end_month);
        $end = $end->modify('+1 month');
        $interval = new \DateInterval('P1M');
        $daterange = new \DatePeriod($begin, $interval ,$end);
        foreach($daterange as $date){
            $time_arr[] = $date->format("Y-m");
        }
        return $time_arr;
    }
    
    
    
}