<?php

namespace App\Modules\Subscribe\Models;

use Illuminate\Database\Eloquent\Model;

class YearOrder extends Model
{
    protected $table = 'year_orders';
    protected $fillable = ['distribution_channel_id','uid','u','fee','begin_time','end_time','send_order_id','created_at','updated_at'];

    /**
     * 获取订购记录
     * @param $uid
     * @return mixed
     */
    public static function getRecord($uid){
        $now = date('Y-m-d H:i:s');
        $where = [
            ['uid','=',$uid],
            ['begin_time','<=',$now],
            ['end_time','>=',$now],
        ];
        return self::where($where)->select('id','uid','u','begin_time','end_time')->first();
    }

    static function save_year_order($data){
        $insert_data = array();
        $insert_data['uid'] = $data['uid'];
        $insert_data['begin_time'] = date('Y-m-d H:i:s');
        $insert_data['end_time'] = date('Y-m-d H:i:s',strtotime('+365 day'));
        $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
        $insert_data['fee'] = $data['fee'];
        $insert_data['send_order_id'] = $data['send_order_id'];

        return  self::firstOrCreate($insert_data);
    }

    static function updateEndtime($id,$endTime){
        $updateParam = array();
        $updateParam['end_time'] = $endTime;
        return self::where('id',$id)->update($updateParam);
    }
}
