链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

167 lines
5.1 KiB

<?php
namespace App\Models\v3;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use App\Models\v3\CouponSetting as SettingModel;
use App\Models\v3\CouponReceiveType as ReceiveTypeModel;
class Coupon extends Model
{
use HasDateTimeFormatter;
use SoftDeletes;
protected $dateFormat = 'U';
protected $table = 'lanzu_coupon';
/* 查询记录数 limit */
protected $perPage = 10;
public static $activityAvailable = ['flash_sale'=>'秒杀','group_buy'=>'团购','new_product'=>'新品'];
public static $category = [1=>'订单'];
public static $type = [1=>'全平台',2=>'线上', 3=>'线下'];
public static $isNewUser = [1=>'是',2=>'否'];
public static $discountType = [1=>'金额',2=>'打折(%)'];
public static $activeType = [1=>'领取活动',2=>'转发活动',3=>'返券专用'];
public static $status = [ -1=> '已删除', 0=>'草稿', 1=>'正常',2=>'已领完',3=>'禁用'];
/* 添加转换字段 */
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',
'activity_available_text'
];
protected $casts = [
'market_ids'=>'array',
'category_ids'=>'array',
// 'tags'=>'json',
'activity_available'=>'array'
];
public function getStatusTextAttribute()
{
$value = $this->status;
return isset(self::$status[$value])?self::$status[$value]:'';
}
public function getTypeTextAttribute()
{
$value = $this->type;
return isset(self::$type[$value])?self::$type[$value]:'';
}
public function getCategoryTextAttribute()
{
$value = $this->category;
return isset(self::$category[$value])?self::$category[$value]:'';
}
public function getIsNewUserTextAttribute()
{
$value = $this->is_new_user;
return isset(self::$isNewUser[$value])?self::$isNewUser[$value]:'';
}
public function getDiscountTypeTextAttribute()
{
$value = $this->discount_type;
return isset(self::$discountType[$value])?self::$discountType[$value]:'';
}
public function getActiveTypeTextAttribute()
{
$value = $this->active_type;
return isset(self::$activeType[$value])?self::$activeType[$value]:'';
}
public function getActivityAvailableTextAttribute()
{
$value = $this->activity_available;
// return isset(self::$_ACTIVITY_AVAILABLE[$value])?self::$_ACTIVITY_AVAILABLE[$value]:'';
return '';
}
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->end_time;
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
public function getUsableStartTimeTextAttribute()
{
$value = $this->usable_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 static function getReceiveTypeText($id)
{
// $id = $this->getKey();
// $receive = SettingModel::where('receive_type.coupon_id',$id)
// ->join('lanzu_coupon_receive_type as receive_type','receive_type.receive_type','lanzu_coupon_setting.id','inner')
// ->first('name');
$receive = SettingModel::getSettingInfo($id,'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);
}
/**
* 根据id获取单条数据
*/
public static function getInfo($id,$field = '*')
{
return self::select($field)->find($id);
}
}