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.

StoreSeckill.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\model\activity\seckill;
  3. use app\model\product\product\StoreDescription;
  4. use app\model\product\product\StoreProduct;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\traits\ModelTrait;
  7. use think\Model;
  8. /**
  9. * 秒杀商品Model
  10. * Class StoreSeckill
  11. * @package app\model\activity\seckill
  12. */
  13. class StoreSeckill extends BaseModel
  14. {
  15. use ModelTrait;
  16. /**
  17. * 数据表主键
  18. * @var string
  19. */
  20. protected $pk = 'id';
  21. /**
  22. * 模型名称
  23. * @var string
  24. */
  25. protected $name = 'store_seckill';
  26. /**
  27. * 配送信息
  28. * @param $value
  29. * @return false|string
  30. */
  31. protected function setDeliveryTypeAttr($value)
  32. {
  33. if ($value) {
  34. return is_array($value) ? implode(',', $value) : $value;
  35. }
  36. return '';
  37. }
  38. /**
  39. * 配送信息
  40. * @param $value
  41. * @param $data
  42. * @return mixed
  43. */
  44. protected function getDeliveryTypeAttr($value)
  45. {
  46. if ($value) {
  47. return is_string($value) ? explode(',', $value) : $value;
  48. }
  49. return [];
  50. }
  51. /**
  52. * 配送信息
  53. * @param $value
  54. * @param $data
  55. * @return mixed
  56. */
  57. protected function getCustomFormAttr($value)
  58. {
  59. if ($value) {
  60. return is_string($value) ? json_decode($value, true) : $value;
  61. }
  62. return [];
  63. }
  64. /**
  65. * 商品标签
  66. * @param $value
  67. * @return array|mixed
  68. */
  69. protected function getLabelIdAttr($value)
  70. {
  71. if ($value) {
  72. return is_string($value) ? explode(',', $value) : $value;
  73. }
  74. return [];
  75. }
  76. /**
  77. * 商品保障服务
  78. * @param $value
  79. * @return array|mixed
  80. */
  81. protected function getEnsureAttr($value)
  82. {
  83. if ($value) {
  84. return is_string($value) ? explode(',', $value) : $value;
  85. }
  86. return [];
  87. }
  88. /**
  89. * 参数信息
  90. * @param $value
  91. * @return array|mixed
  92. */
  93. protected function getSpecsAttr($value)
  94. {
  95. if ($value) {
  96. return is_string($value) ? json_decode($value, true) : $value;
  97. }
  98. return [];
  99. }
  100. /**
  101. * 一对一关联
  102. * 商品关联商品商品详情
  103. * @return \think\model\relation\HasOne
  104. */
  105. public function description()
  106. {
  107. return $this->hasOne(StoreDescription::class, 'product_id', 'id')->where('type', 1)->bind(['description']);
  108. }
  109. /**
  110. * 一对一关联
  111. * 商品关联商品商品详情
  112. * @return \think\model\relation\HasOne
  113. */
  114. public function product()
  115. {
  116. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->where('is_show', 1)->where('is_del', 0)->field(['SUM(sales+ficti) as total', 'id'])->bind([
  117. 'total' => 'total'
  118. ]);
  119. }
  120. /**
  121. * 添加时间获取器
  122. * @param $value
  123. * @return false|string
  124. */
  125. protected function getAddTimeAttr($value)
  126. {
  127. if ($value) return date('Y-m-d H:i:s', (int)$value);
  128. return '';
  129. }
  130. /**
  131. * 图片获取器
  132. * @param $value
  133. * @return array|mixed
  134. */
  135. protected function getImagesAttr($value)
  136. {
  137. return json_decode($value, true) ?: [];
  138. }
  139. /**
  140. * 秒杀商品名称搜索器
  141. * @param Model $query
  142. * @param $value
  143. * @param $data
  144. */
  145. public function searchStoreNameAttr($query, $value, $data)
  146. {
  147. if ($value) $query->where('title|id', 'like', '%' . $value . '%');
  148. }
  149. /**
  150. * 是否推荐搜索器
  151. * @param Model $query
  152. * @param $value
  153. * @param $data
  154. */
  155. public function searchIsHotAttr($query, $value, $data)
  156. {
  157. $query->where('is_hot', $value ?? 1);
  158. }
  159. /**
  160. * 状态搜索器
  161. * @param Model $query
  162. * @param $value
  163. * @param $data
  164. */
  165. public function searchIsShowAttr($query, $value, $data)
  166. {
  167. $query->where('is_show', $value ?? 1);
  168. }
  169. /**
  170. * 是否删除搜索器
  171. * @param Model $query
  172. * @param $value
  173. * @param $data
  174. */
  175. public function searchIsDelAttr($query, $value, $data)
  176. {
  177. $query->where('is_del', $value ?? 0);
  178. }
  179. /**
  180. * 状态搜索器
  181. * @param Model $query
  182. * @param $value
  183. * @param $data
  184. */
  185. public function searchStatusAttr($query, $value, $data)
  186. {
  187. if ($value != '') $query->where('status', $value);
  188. }
  189. /**
  190. * 商品ID搜索器
  191. * @param Model $query
  192. * @param $value
  193. * @param $data
  194. */
  195. public function searchProductIdAttr($query, $value, $data)
  196. {
  197. if ($value) {
  198. if (is_array($value)) {
  199. $query->whereIn('product_id', $value);
  200. } else {
  201. $query->where('product_id', $value);
  202. }
  203. }
  204. }
  205. /**
  206. * 活动有效时间搜索器
  207. * @param $query
  208. * @param $value
  209. */
  210. public function searchSeckillTimeAttr($query, $value)
  211. {
  212. if ($value == 1) {
  213. $time = time();
  214. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time - 86400);
  215. }
  216. }
  217. }