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

139 lines
4.2 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 $table = 'ims_system_coupon_user';
  14. /* 查询记录数 limit */
  15. protected $perPage = 10;
  16. /* 添加转换字段 */
  17. protected $appends = [
  18. 'status_text',
  19. 'type_text',
  20. 'category_text',
  21. 'is_new_user_text',
  22. 'discount_type_text',
  23. 'active_type_text',
  24. 'start_time_text',
  25. 'end_time_text',
  26. 'usable_start_time_text',
  27. 'usable_end_time_text',
  28. 'receive_type_text',
  29. 'created_at_text',
  30. 'updated_at_text'
  31. ];
  32. public function getStatusTextAttribute()
  33. {
  34. $value = $this->status;
  35. return isset(config('coupon.status')[$value])?config('coupon.status')[$value]:'';
  36. }
  37. public function getTypeTextAttribute()
  38. {
  39. $value = $this->type;
  40. return isset(config('coupon.type')[$value])?config('coupon.type')[$value]:'';
  41. }
  42. public function getCategoryTextAttribute()
  43. {
  44. $value = $this->category;
  45. return isset(config('coupon.category')[$value])?config('coupon.category')[$value]:'';
  46. }
  47. public function getIsNewUserTextAttribute()
  48. {
  49. $value = $this->is_new_user;
  50. return isset(config('coupon.is_new_user')[$value])?config('coupon.is_new_user')[$value]:'';
  51. }
  52. public function getDiscountTypeTextAttribute()
  53. {
  54. $value = $this->discount_type;
  55. return isset(config('coupon.discount_type')[$value])?config('coupon.discount_type')[$value]:'';
  56. }
  57. public function getActiveTypeTextAttribute()
  58. {
  59. $value = $this->active_type;
  60. return isset(config('coupon.active_type')[$value])?config('coupon.active_type')[$value]:'';
  61. }
  62. public function getStartTimeTextAttribute()
  63. {
  64. $value = $this->start_time;
  65. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  66. }
  67. public function getEndTimeTextAttribute()
  68. {
  69. $value = $this->usable_start_time;
  70. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  71. }
  72. public function getUsableStartTimeTextAttribute()
  73. {
  74. $value = $this->start_time;
  75. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  76. }
  77. public function getUsableEndTimeTextAttribute()
  78. {
  79. $value = $this->usable_end_time;
  80. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  81. }
  82. public function getCreatedAtTextAttribute()
  83. {
  84. $value = $this->created_at;
  85. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  86. }
  87. public function getUpdatedAtTextAttribute()
  88. {
  89. $value = $this->updated_at;
  90. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  91. }
  92. public function getReceiveTypeTextAttribute()
  93. {
  94. $id = $this->getKey();
  95. $receive = SettingModel::where('receive_type.system_coupon_user_id',$id)
  96. ->join('ims_system_coupon_user_receivetype as receive_type','receive_type.receive_type','ims_system_coupon_setting.id','inner')
  97. ->first('name');
  98. return $receive ? $receive['name'] : '';
  99. }
  100. // 设置
  101. protected function setStartTimeAttribute($value)
  102. {
  103. $this->attributes['start_time'] = strtotime($value);
  104. }
  105. protected function setEndTimeAttribute($value)
  106. {
  107. $this->attributes['end_time'] = strtotime($value);
  108. }
  109. protected function setUsableStarttimeAttribute($value)
  110. {
  111. $this->attributes['usable_start_time'] = strtotime($value);
  112. }
  113. protected function setUsableEndTimeAttribute($value)
  114. {
  115. $this->attributes['usable_end_time'] = strtotime($value);
  116. }
  117. protected function setAddtimeAttribute($value)
  118. {
  119. $this->attributes['addtime'] = time();
  120. }
  121. protected function setUpdatetimeAttribute($value)
  122. {
  123. $this->attributes['updatetime'] = time();
  124. }
  125. }