<?php
/**
 * Created by PhpStorm.
 * User: chaoxinzeng
 * Date: 2017/2/21
 * Time: 下午10:51
 */
namespace frontend\controllers;

use common\libs\MyLib;
use common\models\ConfigT;
use common\models\UserT;
use yii\web\Controller;
use common\models\CarLogT;
use common\models\NonLogT;
use Yii;

class BaseController extends \yii\web\Controller
{
    public $my = null;
    public $web = null;

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = UserT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
        $this->web = ConfigT::findOne(['id'=>1]);
    }

    public function getChildrenUserIDs()
    {
        $user_ids = array();
        $user_items = $this->my->getChildren();
        if($user_items) {
            foreach($user_items as $item) {
                $user_ids[] = $item->id;
            }
        }
        $user_ids[] = $this->my->id;
        return $user_ids;
    }

    public function addLog($car_id,$info,$type)
    {
        $log_info = new CarLogT();
        $log_info->car_id = $car_id;
        $log_info->op_time = time();
        $log_info->op_man = $this->my->getShowName();
        $log_info->group_name = $this->my->group?$this->my->group->getPath():'';
        $log_info->type = $type;
        $log_info->remark = $info;
        $log_info->info = '';
        if(!$log_info->save()) {
            throw new \Exception(print_r($log_info->getErrors(), true));
        }
    }
    public function addNonLog($non_id,$info,$type)
    {
        $log_info = new NonLogT();
        $log_info->non_id = $non_id;
        $log_info->op_time = time();
        $log_info->op_man = $this->my->getShowName();
        $log_info->group_name = $this->my->group?$this->my->group->getPath():'';
        $log_info->type = $type;
        $log_info->remark = $info;
        $log_info->info = '';
        if(!$log_info->save()) {
            var_dump($log_info->errors);
        }
    }
    public function addLog1($car_id,$info,$type)
    {
        $log_info = new CarLogT();
        $log_info->car_id = $car_id;
        $log_info->op_time = strtotime("-1 year");
        $log_info->op_man = $this->my->getShowName();
        $log_info->group_name = $this->my->group?$this->my->group->getPath():'';
        $log_info->type = $type;
        $log_info->remark = $info;
        $log_info->info = '';
        if(!$log_info->save()) {
            var_dump($log_info->errors);
        }
    }
    public function removeLog($car_id)
    {
        CarLogT::deleteAll('car_id='.$car_id);
    }
}