<?php

namespace app\controller;

use support\Request;
use support\Db;
class TaskController
{
    public function test(Request $request)
    {
        set_time_limit(3600 * 100);
        ini_set('memory_limit', '2048M');
        
        $rows = Db::table('smt_task')
        ->join('smt_rules', 'smttask_sr_id', '=', 'sr_id')
        ->select(
                    'smttask_id','smttask_err_count','smttask_isnow','smttask_rundate',
                    'sr_id','sr_title','sr_rule1','sr_cols','sr_factorys','sr_sql'
        )
        ->where('smttask_err_count','<',3)
        //->whereNotIn('smttask_state',['done','doing'])
        ->orderBy('smttask_isnow','desc')
        ->orderBy('smttask_rundate','asc')
        ->get();
        foreach($rows as $row){
            if(date('Y-m-d')>=$row->smttask_rundate){
                $back = $this->doit($row);
                return response(put('@'.$back));
            }
        }
        return response(put([$rows]));
    }
        
    public function doit($row)
    {
        $this->setState($row,'doing');
        $sqlArr = json_decode($row->sr_sql,true);
        if($sqlArr){
            $clientCount = count($sqlArr);
            $this->setRunQty($row,0,$clientCount);
            $i=1;
            $fields = [
                'NULL smt_report_id',
                $row->smttask_id .' smt_report_ver',
                't.smt_report_cid',
                't.smt_report_pid',
                $row->sr_id .' smt_report_sr_id',
                '"" smt_report_user',
                't.smt_report_rule',
                '0 smt_report_copy_id',
                '"'.date('Y-m-d H:i:s').'" smt_report_time',
                '0 smt_report_area'
            ];
            foreach($sqlArr as $cid=>$arr){
                foreach($arr as $k=>$strSql){
                    $arr[$k] = "SELECT * FROM (".$strSql.") t".$k;
                }
                $unionSql = implode(' UNION ', $arr);
                $selectSql = "SELECT ". implode(',', $fields) ." FROM (".$unionSql.") t";
                $insertSQL = "INSERT INTO smt_report ".$selectSql;
                return $insertSQL;
                try{
                    Db::select($insertSQL);
                } catch(Exception $e) {
                    $this->setErrorCount($row);
                }
                $this->setRunQty($row,$i,$clientCount);
                $i++;
            }
            $this->setState($row,'done');
        }else{
            $this->setErrorCount($row);
        }
    }
        
    public function setState($row,$state)
    {
        Db::table('smt_task')
        ->where('smttask_id','=',$row->smttask_id)
        ->update(['smttask_state'=>$state]);
    }
        
    public function setRunQty($row,$qty,$count)
    {
        Db::table('smt_task')
        ->where('smttask_id','=',$row->smttask_id)
        ->update(['smttask_runqty'=>$qty.' / '.$count]);
    }
        
    public function setErrorCount($row)
    {
        Db::table('smt_task')
        ->where('smttask_id','=',$row->smttask_id)
        ->update(['smttask_err_count'=>$row->smttask_err_count++]);
    }
}