链街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

  1. <?php
  2. namespace App\Models\v3;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Database\Eloquent\Model;
  6. use App\Models\v3\CouponSetting as SettingModel;
  7. use App\Models\v3\CouponReceiveType as ReceiveTypeModel;
  8. class Coupon extends Model
  9. {
  10. use HasDateTimeFormatter;
  11. use SoftDeletes;
  12. protected $dateFormat = 'U';
  13. protected $table = 'lanzu_coupon';
  14. /* 查询记录数 limit */
  15. protected $perPage = 10;
  16. public static $activityAvailable = ['flash_sale'=>'秒杀','group_buy'=>'团购','new_product'=>'新品'];
  17. public static $category = [1=>'订单'];
  18. public static $type = [1=>'全平台',2=>'线上', 3=>'线下'];
  19. public static $isNewUser = [1=>'是',2=>'否'];
  20. public static $discountType = [1=>'金额',2=>'打折(%)'];
  21. public static $activeType = [1=>'领取活动',2=>'转发活动',3=>'返券专用'];
  22. public static $status = [ -1=> '已删除', 0=>'草稿', 1=>'正常',2=>'已领完',3=>'禁用'];
  23. /* 添加转换字段 */
  24. protected $appends = [
  25. 'status_text',
  26. 'type_text',
  27. 'category_text',
  28. 'is_new_user_text',
  29. 'discount_type_text',
  30. 'active_type_text',
  31. 'start_time_text',
  32. 'end_time_text',
  33. 'usable_start_time_text',
  34. 'usable_end_time_text',
  35. // 'receive_type_text',
  36. 'created_at_text',
  37. 'updated_at_text',
  38. 'activity_available_text'
  39. ];
  40. protected $casts = [
  41. 'market_ids'=>'array',
  42. 'category_ids'=>'array',
  43. // 'tags'=>'json',
  44. // 'activity_available'=>'array'
  45. ];
  46. public function getStatusTextAttribute()
  47. {
  48. $value = $this->status;
  49. return isset(self::$status[$value])?self::$status[$value]:'';
  50. }
  51. public function getTypeTextAttribute()
  52. {
  53. $value = $this->type;
  54. return isset(self::$type[$value])?self::$type[$value]:'';
  55. }
  56. public function getCategoryTextAttribute()
  57. {
  58. $value = $this->category;
  59. return isset(self::$category[$value])?self::$category[$value]:'';
  60. }
  61. public function getIsNewUserTextAttribute()
  62. {
  63. $value = $this->is_new_user;
  64. return isset(self::$isNewUser[$value])?self::$isNewUser[$value]:'';
  65. }
  66. public function getDiscountTypeTextAttribute()
  67. {
  68. $value = $this->discount_type;
  69. return isset(self::$discountType[$value])?self::$discountType[$value]:'';
  70. }
  71. public function getActiveTypeTextAttribute()
  72. {
  73. $value = $this->active_type;
  74. return isset(self::$activeType[$value])?self::$activeType[$value]:'';
  75. }
  76. public function getActivityAvailableTextAttribute()
  77. {
  78. $value = $this->activity_available;
  79. // return isset(self::$_ACTIVITY_AVAILABLE[$value])?self::$_ACTIVITY_AVAILABLE[$value]:'';
  80. return '';
  81. }
  82. public function getStartTimeTextAttribute()
  83. {
  84. $value = $this->start_time;
  85. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  86. }
  87. public function getEndTimeTextAttribute()
  88. {
  89. $value = $this->end_time;
  90. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  91. }
  92. public function getUsableStartTimeTextAttribute()
  93. {
  94. $value = $this->usable_start_time;
  95. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  96. }
  97. public function getUsableEndTimeTextAttribute()
  98. {
  99. $value = $this->usable_end_time;
  100. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  101. }
  102. public function getCreatedAtTextAttribute()
  103. {
  104. $value = $this->created_at;
  105. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  106. }
  107. public function getUpdatedAtTextAttribute()
  108. {
  109. $value = $this->updated_at;
  110. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  111. }
  112. public static function getReceiveTypeText($id)
  113. {
  114. // $id = $this->getKey();
  115. // $receive = SettingModel::where('receive_type.coupon_id',$id)
  116. // ->join('lanzu_coupon_receive_type as receive_type','receive_type.receive_type','lanzu_coupon_setting.id','inner')
  117. // ->first('name');
  118. $receive = SettingModel::getSettingInfo($id,'name');
  119. return $receive ? $receive['name'] : '';
  120. }
  121. //关联领取方式表
  122. public function receiveType()
  123. {
  124. return $this->hasOne(ReceiveTypeModel::class,'coupon_id','id');
  125. }
  126. // 设置
  127. protected function setStartTimeAttribute($value)
  128. {
  129. $this->attributes['start_time'] = strtotime($value);
  130. }
  131. protected function setEndTimeAttribute($value)
  132. {
  133. $this->attributes['end_time'] = strtotime($value);
  134. }
  135. protected function setUsableStarttimeAttribute($value)
  136. {
  137. $this->attributes['usable_start_time'] = strtotime($value);
  138. }
  139. protected function setUsableEndTimeAttribute($value)
  140. {
  141. $this->attributes['usable_end_time'] = strtotime($value);
  142. }
  143. /**
  144. * 根据id获取单条数据
  145. */
  146. public static function getInfo($id,$field = '*')
  147. {
  148. return self::select($field)->find($id);
  149. }
  150. }