|
|
<?php
namespace App\Models;
use Dcat\Admin\Traits\HasDateTimeFormatter;use Illuminate\Database\Eloquent\SoftDeletes;use Illuminate\Database\Eloquent\Model;use App\Models\CouponSetting as SettingModel;use App\Models\CouponReceiveType as ReceiveTypeModel;
class Coupon extends Model{ use HasDateTimeFormatter; use SoftDeletes;
protected $dateFormat = 'U'; // protected $timestamp = true;
protected $table = 'ims_system_coupon_user'; /* 查询记录数 limit */ protected $perPage = 10; /* 添加转换字段 */ protected $appends = [ 'status_text', 'type_text', 'category_text', 'is_new_user_text', 'discount_type_text', 'active_type_text', 'start_time_text', 'end_time_text', 'usable_start_time_text', 'usable_end_time_text', // 'receive_type_text',
'created_at_text', 'updated_at_text' ];
public function getStatusTextAttribute() { $value = $this->status; return isset(config('coupon.status')[$value])?config('coupon.status')[$value]:''; } public function getTypeTextAttribute() { $value = $this->type; return isset(config('coupon.type')[$value])?config('coupon.type')[$value]:''; } public function getCategoryTextAttribute() { $value = $this->category; return isset(config('coupon.category')[$value])?config('coupon.category')[$value]:''; } public function getIsNewUserTextAttribute() { $value = $this->is_new_user; return isset(config('coupon.is_new_user')[$value])?config('coupon.is_new_user')[$value]:''; } public function getDiscountTypeTextAttribute() { $value = $this->discount_type; return isset(config('coupon.discount_type')[$value])?config('coupon.discount_type')[$value]:''; } public function getActiveTypeTextAttribute() { $value = $this->active_type; return isset(config('coupon.active_type')[$value])?config('coupon.active_type')[$value]:''; } public function getStartTimeTextAttribute() { $value = $this->start_time; return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; }
public function getEndTimeTextAttribute() { $value = $this->usable_start_time; return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getUsableStartTimeTextAttribute() { $value = $this->start_time; return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getUsableEndTimeTextAttribute() { $value = $this->usable_end_time; return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getCreatedAtTextAttribute() { $value = $this->created_at; return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; } public function getUpdatedAtTextAttribute() { $value = $this->updated_at; return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value; }
public function getReceiveTypeTextAttribute() { $id = $this->getKey(); $receive = SettingModel::where('receive_type.system_coupon_user_id',$id) ->join('ims_system_coupon_user_receivetype as receive_type','receive_type.receive_type','ims_system_coupon_setting.id','inner') ->first('name');
return $receive ? $receive['name'] : ''; }
//关联领取方式表
public function receiveType() { return $this->hasOne(ReceiveTypeModel::class,'coupon_id','id'); }
// 设置
protected function setStartTimeAttribute($value) { $this->attributes['start_time'] = strtotime($value); }
protected function setEndTimeAttribute($value) { $this->attributes['end_time'] = strtotime($value); }
protected function setUsableStarttimeAttribute($value) { $this->attributes['usable_start_time'] = strtotime($value); }
protected function setUsableEndTimeAttribute($value) { $this->attributes['usable_end_time'] = strtotime($value); }
protected function setAddtimeAttribute($value) { $this->attributes['addtime'] = time(); } protected function setUpdatetimeAttribute($value) { $this->attributes['updatetime'] = time(); }}
|