<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "order_contacts".
 *
 * @property integer $id
 * @property integer $order_id
 * @property string $contacts_name
 * @property string $contacts_phone
 * @property string $contacts_ID
 * @property integer $contacts_type
 * @property string $update_time
 * @property string $create_time
 * @property integer $delete_flag
 * @property integer $bus_run_id
 */
class OrderContacts extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_contacts';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id', 'contacts_name'], 'required'],
            [['order_id', 'contacts_type', 'delete_flag', 'bus_run_id'], 'integer'],
            [['update_time', 'create_time'], 'safe'],
            [['contacts_name'], 'string', 'max' => 120],
            [['contacts_phone', 'contacts_ID'], 'string', 'max' => 20],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => 'Order ID',
            'contacts_name' => 'Contacts Name',
            'contacts_phone' => 'Contacts Phone',
            'contacts_ID' => 'Contacts  ID',
            'contacts_type' => 'Contacts Type',
            'update_time' => 'Update Time',
            'create_time' => 'Create Time',
            'delete_flag' => 'Delete Flag',
            'bus_run_id' => 'Bus Run ID',
        ];
    }

    /**
     * Function Description:插入联系人表
     * Function Name: insertContacts
     * @param $param_arr
     * @param $orderId
     *
     * @return bool
     *
     * @author 娄梦宁
     */
    public function insertContacts($param_arr, $orderId)
    {
        $values = [
            'order_id' => $orderId,
            'contacts_name' => $param_arr['contacts_name'],
            'contacts_phone' => $param_arr['contacts_phone'],
            'contacts_ID' => empty($param_arr['contacts_ID']) ? '' : $param_arr['contacts_ID'],
            'bus_run_id' => empty($param_arr['run_id']) ? 0 : $param_arr['run_id'],
            'create_time' => date('Y-m-d H:i:s'),
        ];
        $this->attributes = $values;
        $res = $this->insert();
        if (!$res) {
            return false;
        }
        return true;
    }

    /**
     * Des:获取订单拓展信息
     * Name: getRowByOrderId
     * @param $orderId
     * @return array
     * @author 倪宗锋
     */
    public function getRowByOrderId($orderId)
    {
        $where = ['and'];
        $where[] = ['=', 'order_id', $orderId];
        $where[] = ['=', 'contacts_type', 1];
        $return = $this->find()->select(['contacts_name', 'contacts_phone', 'contacts_ID', 'contacts_type', 'bus_run_id', 'order_id'])
            ->from(static::tableName())
            ->where($where)
            ->asArray(true)
            ->one();
        return $return;
    }

    /**
     * Des:获取保险数量
     * Name: getInsurance
     * @param $orderId
     * @return int
     * @author 倪宗锋
     */
    public function getInsuranceCnt($orderId)
    {
        $where = ['and'];
        $where[] = ['=', 'order_id', $orderId];
        $where[] = ['=', 'contacts_type', 2];
        $return = $this->find()->select(['contacts_name', 'contacts_phone', 'contacts_ID', 'contacts_type', 'bus_run_id', 'order_id'])
            ->from(static::tableName())
            ->where($where)
            ->asArray(true)
            ->count();
        return $return;
    }
}
