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.

StoreBargain.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\model\activity\bargain;
  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 StoreBargain
  11. * @package app\model\activity\bargain
  12. */
  13. class StoreBargain 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_bargain';
  26. public function getImagesAttr($value)
  27. {
  28. return json_decode($value, true) ?? [];
  29. }
  30. /**
  31. * 配送信息
  32. * @param $value
  33. * @return false|string
  34. */
  35. protected function setDeliveryTypeAttr($value)
  36. {
  37. if ($value) {
  38. return is_array($value) ? implode(',', $value) : $value;
  39. }
  40. return '';
  41. }
  42. /**
  43. * 配送信息
  44. * @param $value
  45. * @param $data
  46. * @return mixed
  47. */
  48. protected function getDeliveryTypeAttr($value)
  49. {
  50. if ($value) {
  51. return is_string($value) ? explode(',', $value) : $value;
  52. }
  53. return [];
  54. }
  55. /**
  56. * 配送信息
  57. * @param $value
  58. * @param $data
  59. * @return mixed
  60. */
  61. protected function getCustomFormAttr($value)
  62. {
  63. if ($value) {
  64. return is_string($value) ? json_decode($value, true) : $value;
  65. }
  66. return [];
  67. }
  68. /**
  69. * 商品标签
  70. * @param $value
  71. * @return array|mixed
  72. */
  73. protected function getLabelIdAttr($value)
  74. {
  75. if ($value) {
  76. return is_string($value) ? explode(',', $value) : $value;
  77. }
  78. return [];
  79. }
  80. /**
  81. * 商品保障服务
  82. * @param $value
  83. * @return array|mixed
  84. */
  85. protected function getEnsureAttr($value)
  86. {
  87. if ($value) {
  88. return is_string($value) ? explode(',', $value) : $value;
  89. }
  90. return [];
  91. }
  92. /**
  93. * 参数信息
  94. * @param $value
  95. * @return array|mixed
  96. */
  97. protected function getSpecsAttr($value)
  98. {
  99. if ($value) {
  100. return is_string($value) ? json_decode($value, true) : $value;
  101. }
  102. return [];
  103. }
  104. /**
  105. * 一对一关联
  106. * 商品关联商品商品详情
  107. * @return \think\model\relation\HasOne
  108. */
  109. public function description()
  110. {
  111. return $this->hasOne(StoreDescription::class, 'product_id', 'id')->where('type', 2)->bind(['description']);
  112. }
  113. /**
  114. * 原价
  115. * @return \think\model\relation\HasOne
  116. */
  117. public function product()
  118. {
  119. return $this->hasOne(StoreProduct::class, 'id', 'product_id')->field(['id', 'ot_price'])->bind(['ot_price']);
  120. }
  121. /**
  122. * 添加时间获取器
  123. * @param $value
  124. * @return false|string
  125. */
  126. protected function getAddTimeAttr($value)
  127. {
  128. if ($value) return date('Y-m-d H:i:s', (int)$value);
  129. return '';
  130. }
  131. /**
  132. * 砍价商品名称搜索器
  133. * @param Model $query
  134. * @param $value
  135. * @param $data
  136. */
  137. public function searchStoreNameAttr($query, $value, $data)
  138. {
  139. if ($value != '') $query->where('title|id', 'like', '%' . $value . '%');
  140. }
  141. /**
  142. * 状态搜索器
  143. * @param Model $query
  144. * @param $value
  145. * @param $data
  146. */
  147. public function searchStatusAttr($query, $value, $data)
  148. {
  149. if ($value != '') $query->where('status', $value ?? 1);
  150. }
  151. /**
  152. * 是否推荐搜索器
  153. * @param Model $query
  154. * @param $value
  155. * @param $data
  156. */
  157. public function searchIsHotAttr($query, $value, $data)
  158. {
  159. $query->where('is_hot', $value ?? 0);
  160. }
  161. /**
  162. * 是否删除搜索器
  163. * @param Model $query
  164. * @param $value
  165. * @param $data
  166. */
  167. public function searchIsDelAttr($query, $value, $data)
  168. {
  169. $query->where('is_del', $value ?? 0);
  170. }
  171. /**
  172. * 商品ID搜索器
  173. * @param Model $query
  174. * @param $value
  175. * @param $data
  176. */
  177. public function searchProductIdAttr($query, $value, $data)
  178. {
  179. if ($value) {
  180. if (is_array($value)) {
  181. $query->whereIn('product_id', $value);
  182. } else {
  183. $query->where('product_id', $value);
  184. }
  185. }
  186. }
  187. /**
  188. * 活动有效时间搜索器
  189. * @param $query
  190. * @param $value
  191. */
  192. public function searchBargainTimeAttr($query, $value)
  193. {
  194. if ($value == 1) {
  195. $time = time();
  196. $query->where('start_time', '<=', $time)->where('stop_time', '>=', $time);
  197. }
  198. }
  199. }