StoreActivity.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\model\activity;
  12. use app\model\activity\seckill\StoreSeckill;
  13. use crmeb\basic\BaseModel;
  14. use crmeb\traits\ModelTrait;
  15. use think\Model;
  16. /**
  17. * 活动model
  18. * Class StoreActivity
  19. * @package app\model\activity
  20. */
  21. class StoreActivity extends BaseModel
  22. {
  23. use ModelTrait;
  24. /**
  25. * 数据表主键
  26. * @var string
  27. */
  28. protected $pk = 'id';
  29. /**
  30. * 模型名称
  31. * @var string
  32. */
  33. protected $name = 'store_activity';
  34. /**
  35. * 时间段ID
  36. * @param $value
  37. * @return string
  38. */
  39. protected function setTimeIdAttr($value)
  40. {
  41. if ($value) {
  42. return is_array($value) ? implode(',', $value) : $value;
  43. }
  44. return '';
  45. }
  46. /**
  47. * 时间段ID
  48. * @param $value
  49. * @return array|false|string[]
  50. */
  51. protected function getTimeIdAttr($value)
  52. {
  53. if ($value) {
  54. return is_string($value) ? array_map('intval', array_filter(explode(',', $value))) : $value;
  55. }
  56. return [];
  57. }
  58. /**
  59. * 适用门店信息
  60. * @param $value
  61. * @return string
  62. */
  63. protected function setApplicableStoreIdAttr($value)
  64. {
  65. if ($value) {
  66. return is_array($value) ? implode(',', $value) : $value;
  67. }
  68. return '';
  69. }
  70. /**
  71. * 适用门店信息
  72. * @param $value
  73. * @return array|false|string[]
  74. */
  75. protected function getApplicableStoreIdAttr($value)
  76. {
  77. if ($value) {
  78. return is_string($value) ? array_map('intval', array_filter(explode(',', $value))) : $value;
  79. }
  80. return [];
  81. }
  82. /**
  83. * 添加时间获取器
  84. * @param $value
  85. * @return false|string
  86. */
  87. protected function getAddTimeAttr($value)
  88. {
  89. if ($value) return date('Y-m-d H:i:s', (int)$value);
  90. return '';
  91. }
  92. /**
  93. * @return \think\model\relation\HasMany
  94. */
  95. public function seckill()
  96. {
  97. return $this->hasMany(StoreSeckill::class, 'activity_id', 'id')->where('is_show', 1)->where('is_del', 0);
  98. }
  99. /**
  100. * 秒杀商品名称搜索器
  101. * @param Model $query
  102. * @param $value
  103. * @param $data
  104. */
  105. public function searchNameAttr($query, $value, $data)
  106. {
  107. if ($value) $query->where('title|id', 'like', '%' . $value . '%');
  108. }
  109. /**
  110. * 状态搜索器
  111. * @param Model $query
  112. * @param $value
  113. * @param $data
  114. */
  115. public function searchStatusAttr($query, $value, $data)
  116. {
  117. if ($value != '') $query->where('status', $value);
  118. }
  119. /**
  120. * 适用门店类型搜索器
  121. * @param Model $query
  122. * @param $value
  123. */
  124. public function searchApplicableTypeAttr($query, $value)
  125. {
  126. if (is_array($value)) {
  127. if ($value) $query->whereIn('applicable_type', $value);
  128. } else {
  129. if ($value !== '') $query->where('applicable_type', $value);
  130. }
  131. }
  132. /**
  133. * 秒杀时间段
  134. * @param $query
  135. * @param $value
  136. */
  137. public function searchTimeIdAttr($query, $value)
  138. {
  139. if ($value) $query->where('find_in_set(' . $value . ',`time_id`)');
  140. }
  141. /**
  142. * 是否删除
  143. * @param $query
  144. * @param $value
  145. * @return void
  146. */
  147. public function searchIsDelAttr($query, $value)
  148. {
  149. if ($value !== '') $query->where('is_del', $value);
  150. }
  151. }