<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "gift_t".
 *
 * @property integer $id
 * @property integer $type_id
 * @property string $name
 * @property string $price
 * @property string $remark
 * @property integer $total
 * @property integer $total_lock
 * @property string $created_at
 * @property string $updated_at
 */
class GiftT extends \common\models\Base
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gift_t';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type_id', 'total', 'total_lock'], 'integer'],
            [['name'], 'required'],
            [['created_at', 'updated_at'], 'safe'],
            [['name'], 'string', 'max' => 100],
            [['price'], 'string', 'max' => 50],
            [['remark'], 'string', 'max' => 200],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'type_id' => 'Type ID',
            'name' => 'Name',
            'price' => 'Price',
            'remark' => 'Remark',
            'total' => 'Total',
            'total_lock' => 'Total Lock',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function getShowType()
    {
        if($this->type_id == 1) return '实名礼品';
        if($this->type_id == 2) return '礼券';
        return '';
    }

    public function getTickets()
    {
        return $this->hasMany(GiftTicketT::className(),['type_id'=>'id']);
    }
    public function getTicketsUse()
    {
        return $this->hasMany(GiftTicketTUse::className(),['type_id'=>'id']);
    }

    public function getTicketsUseByDate($date_type,$begin_date,$end_date)
    {
        $query = GiftTicketTUse::find()
            ->leftJoin('order_t','order_t.id=gift_ticket_t_use.order_id')
            ->leftJoin('gift_t','gift_t.id=gift_ticket_t_use.type_id')
            ->where('gift_t.id='.$this->id)
            ->orderBy('id DESC');

        if($date_type == 1) {
            if($begin_date != '') {
                $query = $query->andWhere('gift_ticket_t_use.finished_date>="' . $begin_date.'"');
            }
            if($end_date != '') {
                $query = $query->andWhere('gift_ticket_t_use.finished_date<="' . $end_date.'"');
            }
        }elseif ($date_type == 2){
            if($begin_date != '') {
                $query = $query->andWhere('order_t.print_date>="' . $begin_date.'"');
            }
            if($end_date != '') {
                $query = $query->andWhere('order_t.print_date<="' . $end_date.'"');
            }
        }elseif ($date_type == 3){
            if($begin_date != '') {
                $query = $query->andWhere('order_t.submit_date>="' . $begin_date.'"');
            }
            if($end_date != '') {
                $query = $query->andWhere('order_t.submit_date<="' . $end_date.'"');
            }
        }

        return $query;
    }
}