StoreActivity.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\store;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\product\Spu;
  14. use app\common\model\system\form\Form;
  15. use app\common\model\system\Relevance;
  16. use app\common\repositories\store\StoreActivityRepository;
  17. use app\common\repositories\system\RelevanceRepository;
  18. class StoreActivity extends BaseModel
  19. {
  20. public static function tablePk(): string
  21. {
  22. return 'activity_id';
  23. }
  24. public static function tableName(): string
  25. {
  26. return 'store_activity';
  27. }
  28. public function getFullAttr($val)
  29. {
  30. return $val ? (float)$val : $val;
  31. }
  32. public function getImagesAttr($val)
  33. {
  34. if (empty($val)) {
  35. return [];
  36. }
  37. return explode(',',$val);
  38. }
  39. public function spu()
  40. {
  41. return $this->hasMany(Spu::class, 'activity_id', 'activity_id');
  42. }
  43. public function socpeData()
  44. {
  45. return $this->hasMany(Relevance::class,'left_id','activity_id')->whereIn('type',[RelevanceRepository::SCOPE_TYPE_STORE,RelevanceRepository::SCOPE_TYPE_CATEGORY,RelevanceRepository::SCOPE_TYPE_PRODUCT,RelevanceRepository::SCOPE_TYPE_PRODUCT_LABEL]);
  46. }
  47. /**
  48. * 系统表单关联
  49. * @return \think\model\relation\HasOne
  50. * @author Qinii
  51. * @day 2023/11/16
  52. */
  53. public function systemForm()
  54. {
  55. return $this->hasOne(Form::class, 'form_id','link_id');
  56. }
  57. /**
  58. * 绑定系统表单一下显示字段
  59. * @return \think\model\relation\HasOne
  60. */
  61. public function systemFormKeys()
  62. {
  63. return $this->hasOne(Form::class, 'form_id','link_id')->bind(['form_name'=>'name']);
  64. }
  65. public function relevance()
  66. {
  67. return $this->hasMany(Relevance::class,'left_id','activity_id');
  68. }
  69. /**
  70. * 活动的进行状态
  71. * @author Qinii
  72. * @day 2023/11/22
  73. */
  74. public function getTimeStatusAttr()
  75. {
  76. $start_time = $this->start_time ? strtotime($this->start_time) : '';
  77. $end_time = $this->end_time ? strtotime($this->end_time) : '';
  78. $time = time();
  79. if ($this->status == 1) {
  80. if ($start_time && $end_time) {
  81. if ($start_time > $time) return 0; //未开始
  82. if ($end_time < $time) return -1; //已结束
  83. }
  84. return 1;
  85. } else {
  86. return $this->status;
  87. }
  88. }
  89. public function searchIsShowAttr($query, $value)
  90. {
  91. if ($value !== '') {
  92. $query->where('is_show', $value);
  93. }
  94. }
  95. public function searchStatusAttr($query, $value)
  96. {
  97. $date_time = date('Y-m-d H:i:s');
  98. if ($value !== '') {
  99. switch ($value) {
  100. case 0: // 查询未开始
  101. $query->whereTime('start_time', '>', $date_time);
  102. break;
  103. case 1:
  104. $query->whereTime('start_time', '<', $date_time)->where('end_time', '>', $date_time);
  105. break;
  106. case -1:
  107. $query->whereTime('end_time', '<', $date_time);
  108. break;
  109. }
  110. }
  111. }
  112. public function searchActivityTypeAttr($query, $value)
  113. {
  114. if ($value !== '') {
  115. $query->where('activity_type', $value);
  116. }
  117. }
  118. public function searchIsStatusAttr($query, $value)
  119. {
  120. $query->whereIn('status',[0,1]);
  121. }
  122. public function searchActivityIdAttr($query, $value)
  123. {
  124. if ($value !== '') {
  125. $query->where('activity_id', $value);
  126. }
  127. }
  128. public function searchDateAttr($query, $value)
  129. {
  130. if ($value !== '') {
  131. getModelTime($query, $value, 'create_time');
  132. }
  133. }
  134. public function searchKeywordAttr($query, $value)
  135. {
  136. if ($value !== '') {
  137. $query->whereLike('activity_id|activity_name', '%' . $value . '%');
  138. }
  139. }
  140. public function searchIsDelAttr($query, $value)
  141. {
  142. if ($value !== '') {
  143. $query->where('is_del', $value);
  144. }
  145. }
  146. public function searchGtEndTimeAttr($query, $value)
  147. {
  148. if ($value !== '') {
  149. $query->whereTime('end_time','>', $value);
  150. }
  151. }
  152. public function searchFormIdAttr($query, $value)
  153. {
  154. if ($value !== '') {
  155. $query->where('link_id', $value);
  156. }
  157. }
  158. }