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

146 lines
4.3 KiB

  1. <?php
  2. namespace App\Models;
  3. use Dcat\Admin\Traits\HasDateTimeFormatter;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Database\Eloquent\Model;
  6. use App\Models\CouponSetting as SettingModel;
  7. use App\Models\CouponReceiveType as ReceiveTypeModel;
  8. class Coupon extends Model
  9. {
  10. use HasDateTimeFormatter;
  11. use SoftDeletes;
  12. protected $dateFormat = 'U';
  13. // protected $timestamp = true;
  14. protected $table = 'ims_system_coupon_user';
  15. /* 查询记录数 limit */
  16. protected $perPage = 10;
  17. /* 添加转换字段 */
  18. protected $appends = [
  19. 'status_text',
  20. 'type_text',
  21. 'category_text',
  22. 'is_new_user_text',
  23. 'discount_type_text',
  24. 'active_type_text',
  25. 'start_time_text',
  26. 'end_time_text',
  27. 'usable_start_time_text',
  28. 'usable_end_time_text',
  29. // 'receive_type_text',
  30. 'created_at_text',
  31. 'updated_at_text'
  32. ];
  33. public function getStatusTextAttribute()
  34. {
  35. $value = $this->status;
  36. return isset(config('coupon.status')[$value])?config('coupon.status')[$value]:'';
  37. }
  38. public function getTypeTextAttribute()
  39. {
  40. $value = $this->type;
  41. return isset(config('coupon.type')[$value])?config('coupon.type')[$value]:'';
  42. }
  43. public function getCategoryTextAttribute()
  44. {
  45. $value = $this->category;
  46. return isset(config('coupon.category')[$value])?config('coupon.category')[$value]:'';
  47. }
  48. public function getIsNewUserTextAttribute()
  49. {
  50. $value = $this->is_new_user;
  51. return isset(config('coupon.is_new_user')[$value])?config('coupon.is_new_user')[$value]:'';
  52. }
  53. public function getDiscountTypeTextAttribute()
  54. {
  55. $value = $this->discount_type;
  56. return isset(config('coupon.discount_type')[$value])?config('coupon.discount_type')[$value]:'';
  57. }
  58. public function getActiveTypeTextAttribute()
  59. {
  60. $value = $this->active_type;
  61. return isset(config('coupon.active_type')[$value])?config('coupon.active_type')[$value]:'';
  62. }
  63. public function getStartTimeTextAttribute()
  64. {
  65. $value = $this->start_time;
  66. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  67. }
  68. public function getEndTimeTextAttribute()
  69. {
  70. $value = $this->usable_start_time;
  71. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  72. }
  73. public function getUsableStartTimeTextAttribute()
  74. {
  75. $value = $this->start_time;
  76. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  77. }
  78. public function getUsableEndTimeTextAttribute()
  79. {
  80. $value = $this->usable_end_time;
  81. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  82. }
  83. public function getCreatedAtTextAttribute()
  84. {
  85. $value = $this->created_at;
  86. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  87. }
  88. public function getUpdatedAtTextAttribute()
  89. {
  90. $value = $this->updated_at;
  91. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  92. }
  93. public function getReceiveTypeTextAttribute()
  94. {
  95. $id = $this->getKey();
  96. $receive = SettingModel::where('receive_type.system_coupon_user_id',$id)
  97. ->join('ims_system_coupon_user_receivetype as receive_type','receive_type.receive_type','ims_system_coupon_setting.id','inner')
  98. ->first('name');
  99. return $receive ? $receive['name'] : '';
  100. }
  101. //关联领取方式表
  102. public function receiveType()
  103. {
  104. return $this->hasOne(ReceiveTypeModel::class,'coupon_id','id');
  105. }
  106. // 设置
  107. protected function setStartTimeAttribute($value)
  108. {
  109. $this->attributes['start_time'] = strtotime($value);
  110. }
  111. protected function setEndTimeAttribute($value)
  112. {
  113. $this->attributes['end_time'] = strtotime($value);
  114. }
  115. protected function setUsableStarttimeAttribute($value)
  116. {
  117. $this->attributes['usable_start_time'] = strtotime($value);
  118. }
  119. protected function setUsableEndTimeAttribute($value)
  120. {
  121. $this->attributes['usable_end_time'] = strtotime($value);
  122. }
  123. protected function setAddtimeAttribute($value)
  124. {
  125. $this->attributes['addtime'] = time();
  126. }
  127. protected function setUpdatetimeAttribute($value)
  128. {
  129. $this->attributes['updatetime'] = time();
  130. }
  131. }