<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "car_t".
 *
 * @property int $id
 * @property int $user_id
 * @property int $car_type_id
 * @property int $car_use_id
 * @property string $car_no
 * @property string $factory_model
 * @property string $car_model
 * @property string $register_date
 * @property string $insurer1_date
 * @property string $insurer2_date
 * @property string $id_man
 * @property string $id_man_number 被保险人证件号码
 * @property string $engine_no
 * @property string $car_frame_no
 * @property int $seats
 * @property string $car_man
 * @property string $car_man_phone
 * @property string $remark
 * @property int $location
 * @property string $print_date
 * @property string $car_man_number
 * @property string $company
 * @property string $op_user1
 * @property string $op_user2
 * @property string $from_src 来源
 * @property string $created_at
 * @property string $updated_at
 * @property int $biaozhu
 * @property int $is_xubao 是否续保
 * @property string $id_man_phone
 * @property string $purchase_price
 * @property string $car_man_type 车主证件类型
 * @property int $op1_id 新保ID
 * @property int $op2_id 续保ID
 * @property string $purchase_value
 */
class CarT extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'car_t';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['user_id', 'car_type_id', 'car_use_id', 'seats', 'location', 'biaozhu', 'is_xubao', 'op1_id', 'op2_id'], 'integer'],
            [['register_date', 'insurer1_date', 'insurer2_date', 'print_date', 'created_at', 'updated_at'], 'safe'],
            [['purchase_price', 'purchase_value'], 'number'],
            [['car_no', 'factory_model', 'car_model', 'engine_no', 'car_frame_no', 'car_man_phone', 'id_man_phone'], 'string', 'max' => 50],
            [['id_man', 'id_man_number', 'car_man', 'car_man_number', 'company', 'op_user1', 'op_user2', 'from_src', 'car_man_type'], 'string', 'max' => 100],
            [['remark'], 'string', 'max' => 200],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'car_type_id' => 'Car Type ID',
            'car_use_id' => 'Car Use ID',
            'car_no' => 'Car No',
            'factory_model' => 'Factory Model',
            'car_model' => 'Car Model',
            'register_date' => 'Register Date',
            'insurer1_date' => 'Insurer1 Date',
            'insurer2_date' => 'Insurer2 Date',
            'id_man' => 'Id Man',
            'id_man_number' => 'Id Man Number',
            'engine_no' => 'Engine No',
            'car_frame_no' => 'Car Frame No',
            'seats' => 'Seats',
            'car_man' => 'Car Man',
            'car_man_phone' => 'Car Man Phone',
            'remark' => 'Remark',
            'location' => 'Location',
            'print_date' => 'Print Date',
            'car_man_number' => 'Car Man Number',
            'company' => 'Company',
            'op_user1' => 'Op User1',
            'op_user2' => 'Op User2',
            'from_src' => 'From Src',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'biaozhu' => 'Biaozhu',
            'is_xubao' => 'Is Xubao',
            'id_man_phone' => 'Id Man Phone',
            'purchase_price' => 'Purchase Price',
            'car_man_type' => 'Car Man Type',
            'op1_id' => 'Op1 ID',
            'op2_id' => 'Op2 ID',
            'purchase_value' => 'Purchase Value',
        ];
    }

    public function getLinkmen() {
        return $this->hasMany(LinkmanT::className(),['car_id'=>'id']);
    }

    public function getUser()
    {
        return $this->hasOne(UserT::className(),['id'=>'user_id']);
    }

    public function getCarType()
    {
        return $this->hasOne(CarTypeT::className(),['id'=>'car_type_id']);
    }

    public function getCarUse()
    {
        return $this->hasOne(CarUseT::className(),['id'=>'car_use_id']);
    }

    public function getAppointment()
    {
        return $this->hasOne(AppointmentT::className(),['car_id'=>'id']);
    }

    public function getAppointments()
    {
        return $this->hasMany(AppointmentHistoryT::className(),['car_id'=>'id']);
    }

    public function getOrders()
    {
        return $this->hasMany(OrderT::className(),['car_id'=>'id']);
    }
    public function getLastOpUser1()
    {
        $count = OrderT::find()->where('car_id='.$this->id)->count();
        if($count >1){
            $order = OrderT::find()->where('car_id='.$this->id)->orderBy('id ASC')->one();
            return $order->user->getShowName();
        }
        return '';
    }


    public function getBrand()
    {
        return $this->hasOne(BrandT::className(),['id'=>'brand_id']);
    }

    public function getInvalid()
    {
        return $this->hasOne(InvalidT::className(),['id'=>'invalid_id']);
    }

    public function getSeries()
    {
        return $this->hasOne(SeriesT::className(),['id'=>'series_id']);
    }

    public function getDisplacement()
    {
        return $this->hasOne(DisplacementT::className(),['id'=>'displacement_id']);

    }

    public function getLocation()
    {
        $name = '';
        switch($this->location) {
            case 1:
                $car=CarT::findOne(['id'=>$this->id]);
                $name = 'A库';
                break;
            case 2:
                $carb=CarBT::findOne(['id'=>$this->id]);
                if($carb->back==1){
                    $name = 'B库(新保处理无效库)';
                }else{
                    $name = 'B库';
                }
                break;
            case 3:
                $name = 'C库';
                break;
            case 4:
                $name = 'D库';
                $card=CarDT::findOne(['id'=>$this->id]);
                if($card->back==1){
                    $name = 'D库(续保处理无效库)';
                }else{
                    $name = 'D库';
                }
                break;
            case 5:
                $name = '无效库';
                break;
            case 6:
                $name = 'E库';
                break;
            case 7:
                $name = 'F库';
                break;
            case 8:
                $name = 'F1新保四大公司无效库';
                break;
            case 9:
                $name = 'F2新保其他小公司无效库';
                break;
            case 10:
                $name = 'F3新保正常无效库';
                break;
            case 11:
                $name = 'F4续保四大公司无效库';
                break;
            case 12:
                $name = 'F5续保其他小公司无效库';
                break;
            case 13:
                $name = 'F6续保正常无效库';
                break;
            case 14:
                $name = '清洗岗数据库';
                break;
            case 15:
                $name = '数据准确库';
                break;
            case 16:
                $name = '数据不准确库';
                break;
            default:
                $name = '';
        }
        return $name;
    }
    public function getLib()
    {
        if($this->location == 1)
            return null;
        if($this->location == 2)
            return $this->hasOne(CarBT::className(),['id'=>'id']);
        if($this->location == 3)
            return $this->hasOne(CarCT::className(),['id'=>'id']);
        if($this->location == 4)
            return $this->hasOne(CarDT::className(),['id'=>'id']);
        if($this->location == 5)
            return $this->hasOne(CarInvalidT::className(),['id'=>'id']);
        if($this->location == 7)
            return $this->hasOne(CarFT::className(),['id'=>'id']);
        return null;
    }

    public function getOp1()
    {
        return $this->hasOne(UserT::className(),['id'=>'op1_id']);
    }

    public function getOp2()
    {
        return $this->hasOne(UserT::className(),['id'=>'op2_id']);
    }
}
