<?php
/**
 * Created by PhpStorm.
 * User: liuyingjie
 * Date: 2017/10/10
 * Time: 17:18
 */

namespace frontend\controllers;


use common\models\BrandT;
use common\models\CarLogT;
use common\models\DirectionT;
use common\models\DisplacementT;
use common\models\FixCarCategory;
use common\models\FixCarImg;
use common\models\FixCarScale;
use common\models\FixCarSn;
use common\models\RangeT;
use common\models\SeriesT;
use common\models\StoreRoom;
use common\models\UserT;
use common\models\YearT;
use yii\helpers\ArrayHelper;

class FixCarBaseController extends BaseController
{
    //图片
    protected function getImagesByType($id,$type){
        $images = null;
        if (!empty(($images = FixCarImg::find()->where('fix_id=' . $id . ' AND type <='.$type)->asArray()->all()))){
            return ArrayHelper::index($images, null, 'type');
        }
        return [];
    }

    //品牌
    protected function getBrands(){
        return BrandT::find()->all();
    }

    //车型或系列
    protected function getSeries($brand_id=0){
        $query = SeriesT::find();
        if($brand_id > 0 ) $query = $query->where('brand_id=' . intval($brand_id));
        return $query->all();
    }

    //排量
    protected function getDisplacement($series_id=0){
        $query = DisplacementT::find();
        if($series_id > 0) $query = $query->where('series_id=' . intval($series_id));
        return $query->all();
    }

    //年款
    protected function getYears($year_id=0){
        $query = YearT::find();
        if($year_id > 0) $query = $query->where('id='.intval($year_id));
        return $query->all();
    }

    //日志
    protected function getCarLog($car_id=0){
        if($logs = CarLogT::find()
            ->where('car_id=' . $car_id . ' and type=2')
            ->orderBy('id DESC')
            ->all()) return $logs;
        return false;
    }

    //司机
    protected function getDrivers($driver_id=0){
        $query = UserT::find()->where('group_id=41')->orderBy('username ASC');
        if($driver_id > 0) $query = $query->andWhere('id='.intval($driver_id));
       return $query->all();
    }

    //方位
    protected function getDirection(){
        if($direction =  DirectionT::find()->all()) return $direction;
        return false;
//        throw new NotFoundHttpException('方位不存在!');
    }

    //范围
    //        $range_items = RangeT::find()->all();

    protected function getRange(){
        if($range = RangeT::find()->all()) return $range;
        return false;
//        throw new NotFoundHttpException('范围为空!');
    }

    protected function getParents($condition='pid=0'){
        return FixCarCategory::find()->where($condition)->all();
    }
    protected function getCreateOrderSn(){
        $sn = 'CHPJ-'.date('ymds').'-'.mt_rand(1000,9999);
        $res = ($r = FixCarSn::findOne('sn=\''.$sn.'\'')) ? $r : new FixCarSn();
        if(!$res->isNewRecord){
            $this->getCreateOrderSn();
        }else{
            $res->sn = $sn;
            $res->save();
        }
        return $sn;  //如果商品货号重复则重新生成
    }
}