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

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