Spu.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\product;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\coupon\StoreCouponProduct;
  14. use app\common\model\store\StoreSeckillActive;
  15. use app\common\model\system\merchant\Merchant;
  16. use think\db\BaseQuery;
  17. class Spu extends BaseModel
  18. {
  19. /**
  20. *
  21. * @return string
  22. * @author Qinii
  23. * @day 12/18/20
  24. */
  25. public static function tablePk(): string
  26. {
  27. return 'spu_id';
  28. }
  29. /**
  30. *
  31. * @return string
  32. * @author Qinii
  33. * @day 12/18/20
  34. */
  35. public static function tableName(): string
  36. {
  37. return 'store_spu';
  38. }
  39. /*
  40. * -----------------------------------------------------------------------------------------------------------------
  41. * 属性
  42. * -----------------------------------------------------------------------------------------------------------------
  43. */
  44. public function getMinExtensionAttr($value)
  45. {
  46. return isset($this->product) ? $this->product->min_extension : 0;
  47. }
  48. public function getMaxExtensionAttr()
  49. {
  50. return isset($this->product) ? $this->product->max_extension : 0;
  51. }
  52. public function getStopTimeAttr($value,$data)
  53. {
  54. if($this->product_type == 1){
  55. if (is_null($this->seckillActive)) {
  56. return date('Y-m-d H:i:s',strtotime("-1 day"));
  57. }
  58. return $this->seckillActive['stop_time'];
  59. // $day = date('Y-m-d',time());
  60. // $_day = strtotime($day);
  61. // $end_day = strtotime($this->seckillActive['end_day']);
  62. // if($end_day >= $_day)
  63. // return strtotime($day.$this->seckillActive['end_time'].':00:00');
  64. // if($end_day < strtotime($day))
  65. // return strtotime(date('Y-m-d',$end_day).$this->seckillActive['end_time'].':00:00');
  66. }
  67. }
  68. public function setMerLabelsAttr($value)
  69. {
  70. if (!empty($value)) {
  71. if (!is_array($value))
  72. return ','. $value .',';
  73. return ','. implode(',', $value) .',';
  74. }
  75. return $value;
  76. }
  77. public function getMerLabelsAttr($value)
  78. {
  79. if (!$value) return [];
  80. return explode(',',rtrim(ltrim($value,','),','));
  81. }
  82. public function setSysLabelsAttr($value)
  83. {
  84. if (!empty($value)) {
  85. if (!is_array($value))
  86. return ','. $value .',';
  87. return ','. implode(',', $value) .',';
  88. }
  89. return $value;
  90. }
  91. public function getSysLabelsAttr($value)
  92. {
  93. if (!$value) return [];
  94. return explode(',',rtrim(ltrim($value,','),','));
  95. }
  96. public function getImageAttr($value)
  97. {
  98. if (is_int(strpos($value, 'http'))) {
  99. return $value;
  100. } else {
  101. return rtrim(systemConfig('site_url'), '/') . $value;
  102. }
  103. }
  104. public function getOtPriceAttr($value)
  105. {
  106. return (int)$value;
  107. }
  108. /**
  109. * 是否展示会员价
  110. * @return array
  111. * @author Qinii
  112. * @day 2023/1/4
  113. */
  114. public function getShowSvipInfoAttr($value, $data)
  115. {
  116. return ($this->product_type == 0) ? $this->product->show_svip_info ?? false : false;
  117. }
  118. /**
  119. * 获取会员价
  120. * @return int|string
  121. * @author Qinii
  122. * @day 2023/1/4
  123. */
  124. public function getSvipPriceAttr()
  125. {
  126. return ($this->product_type == 0) ? $this->product->svip_price ?? 0 : 0;
  127. }
  128. public function getIsSvipPriceAttr()
  129. {
  130. return ($this->product_type == 0) ? $this->product->is_svip_price ??0 : 0;
  131. }
  132. public function getMerLabelsDataAttr()
  133. {
  134. return ProductLabel::whereIn('product_label_id',$this->mer_labels)->column('label_name');
  135. }
  136. public function getSysLabelsDataAttr()
  137. {
  138. return ProductLabel::whereIn('product_label_id',$this->sys_labels)->column('label_name');
  139. }
  140. /*
  141. * -----------------------------------------------------------------------------------------------------------------
  142. * 关联表
  143. * -----------------------------------------------------------------------------------------------------------------
  144. */
  145. public function product()
  146. {
  147. return $this->hasOne(Product::class,'product_id','product_id');
  148. }
  149. public function merchant()
  150. {
  151. return $this->hasOne(Merchant::class,'mer_id','mer_id');
  152. }
  153. public function issetCoupon()
  154. {
  155. return $this->hasOne(StoreCouponProduct::class, 'product_id', 'product_id')->alias('A')
  156. ->rightJoin('StoreCoupon B', 'A.coupon_id = B.coupon_id')
  157. ->where(function (BaseQuery $query) {
  158. $query->where('B.is_limited', 0)->whereOr(function (BaseQuery $query) {
  159. $query->where('B.is_limited', 1)->where('B.remain_count', '>', 0);
  160. });
  161. })->where(function (BaseQuery $query) {
  162. $query->where('B.is_timeout', 0)->whereOr(function (BaseQuery $query) {
  163. $time = date('Y-m-d H:i:s');
  164. $query->where('B.is_timeout', 1)->where('B.start_time', '<', $time)->where('B.end_time', '>', $time);
  165. });
  166. })->field('A.product_id,B.*')->where('status', 1)->where('type', 1)->where('send_type', 0)->where('is_del', 0)
  167. ->order('sort DESC,coupon_id DESC')->hidden(['is_del', 'status']);
  168. }
  169. public function merCateId()
  170. {
  171. return $this->hasMany(ProductCate::class,'product_id','product_id')->field('product_id,mer_cate_id');
  172. }
  173. public function seckillActive()
  174. {
  175. return $this->hasOne(StoreSeckillActive::class,'seckill_active_id','activity_id');
  176. }
  177. /*
  178. * -----------------------------------------------------------------------------------------------------------------
  179. * 搜索器
  180. * -----------------------------------------------------------------------------------------------------------------
  181. */
  182. public function searchMerIdAttr($query,$value)
  183. {
  184. $query->where('mer_id',$value);
  185. }
  186. public function searchProductIdAttr($query,$value)
  187. {
  188. $query->where('product_id',$value);
  189. }
  190. public function searchProductTypeAttr($query,$value)
  191. {
  192. $query->where('product_type',$value);
  193. }
  194. public function searchActivityIdAttr($query,$value)
  195. {
  196. $query->where('activity_id',$value);
  197. }
  198. public function searchKeyworkAttr($query,$value)
  199. {
  200. $query->whereLike('store_name|keyword',$value);
  201. }
  202. public function searchPriceOnAttr($query, $value)
  203. {
  204. $query->where('price','>=',$value);
  205. }
  206. public function searchPriceOffAttr($query, $value)
  207. {
  208. $query->where('price','<=',$value);
  209. }
  210. public function searchSpuIdsAttr($query, $value)
  211. {
  212. $query->whereIn('spu_id',$value);
  213. }
  214. }