Product.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace app\common\model\store\product;
  3. use app\common\dao\store\StoreSeckillActiveDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\store\coupon\StoreCouponProduct;
  6. use app\common\model\store\Guarantee;
  7. use app\common\model\store\GuaranteeTemplate;
  8. use app\common\model\store\GuaranteeValue;
  9. use app\common\model\store\shipping\ShippingTemplate;
  10. use app\common\model\store\StoreBrand;
  11. use app\common\model\store\StoreCategory;
  12. use app\common\model\store\StoreSeckillActive;
  13. use app\common\model\system\merchant\Merchant;
  14. use app\common\repositories\store\StoreCategoryRepository;
  15. use ln\services\VicWordService;
  16. use think\db\BaseQuery;
  17. use think\facade\Db;
  18. use think\model\concern\SoftDelete;
  19. class Product extends BaseModel
  20. {
  21. use SoftDelete;
  22. protected $deleteTime = 'is_del';
  23. protected $defaultSoftDelete = 0;
  24. /**
  25. * @Author:Qinii
  26. * @Date: 2020/5/8
  27. * @return string
  28. */
  29. public static function tablePk(): string
  30. {
  31. return 'product_id';
  32. }
  33. /**
  34. * @Author:Qinii
  35. * @Date: 2020/5/8
  36. * @return string
  37. */
  38. public static function tableName(): string
  39. {
  40. return 'store_product';
  41. }
  42. /*
  43. * -----------------------------------------------------------------------------------------------------------------
  44. * 属性
  45. * -----------------------------------------------------------------------------------------------------------------
  46. */
  47. public function getSliderImageAttr($value)
  48. {
  49. return $value ? explode(',',$value) : [];
  50. }
  51. public function getGiveCouponIdsAttr($value)
  52. {
  53. return $value ? explode(',',$value) : [];
  54. }
  55. public function getMaxExtensionAttr($value)
  56. {
  57. if($this->extension_type){
  58. return ($this->attrValue()->order('extension_two DESC')->value('extension_one'));
  59. } else {
  60. return bcmul(($this->attrValue()->order('price DESC')->value('price')) , systemConfig('extension_one_rate'),2);
  61. }
  62. }
  63. public function getMinExtensionAttr($value)
  64. {
  65. if($this->extension_type){
  66. return ($this->attrValue()->order('extension_two ASC')->value('extension_two'));
  67. } else {
  68. return bcmul(($this->attrValue()->order('price ASC')->value('price')) , systemConfig('extension_one_rate'),2);
  69. }
  70. }
  71. public function check()
  72. {
  73. if(!$this || !$this->is_show || !$this->is_used || !$this->status || $this->is_del || !$this->mer_status) return false;
  74. return true;
  75. }
  76. /**
  77. * TODO 秒杀商品结束时间
  78. * @return false|int
  79. * @author Qinii
  80. * @day 2020-08-15
  81. */
  82. public function getEndTimeAttr()
  83. {
  84. if($this->product_type !== 1) return true;
  85. $day = date('Y-m-d',time());
  86. $_day = strtotime($day);
  87. $end_day = strtotime($this->seckillActive['end_day']);
  88. if($end_day >= $_day)
  89. return strtotime($day.$this->seckillActive['end_time'].':00:00');
  90. if($end_day < strtotime($day))
  91. return strtotime(date('Y-m-d',$end_day).$this->seckillActive['end_time'].':00:00');
  92. }
  93. /**
  94. * TODO 秒杀商品状态
  95. * @return array|int
  96. * @author Qinii
  97. * @day 2020-08-19
  98. */
  99. public function getSeckillStatusAttr()
  100. {
  101. if($this->product_type !== 1) return true;
  102. $day = strtotime(date('Y-m-d',time()));
  103. $_h = date('H',time());
  104. $start_day = strtotime($this->seckillActive['start_day']);
  105. $end_day = strtotime($this->seckillActive['end_day']);
  106. if(!$this->seckillActive) return '';
  107. if($this->seckillActive['status'] !== -1){
  108. //还未开始
  109. if($start_day > time() || $this->is_show !== 1)return 0;
  110. //已结束
  111. if($end_day < $day) return -1;
  112. //开始 - 结束
  113. if($start_day <= $day && $day <= $end_day){
  114. //未开始
  115. if($this->seckillActive['start_time'] > $_h) return 0;
  116. //已结束
  117. if($this->seckillActive['end_time'] <= $_h) return -1;
  118. //进行中
  119. if($this->seckillActive['start_time'] <= $_h && $this->seckillActive['end_time'] > $_h) return 1;
  120. }
  121. }
  122. //已结束
  123. return -1;
  124. }
  125. public function getImageAttr($value)
  126. {
  127. if (is_int(strpos($value, 'http'))){
  128. return $value;
  129. }else{
  130. return systemConfig('site_url') .$value;
  131. }
  132. }
  133. public function getTopReplyAttr()
  134. {
  135. $res = ProductReply::where('product_id',$this->product_id)->where('is_del',0)->with(['orderProduct'])->field('reply_id,uid,nickname,merchant_reply_content,avatar,order_product_id,product_id,product_score,service_score,postage_score,comment,pics,rate,create_time')
  136. ->order('sort DESC,create_time DESC')->limit(1)->find();
  137. if(!$res) return null;
  138. if ($res['orderProduct'])
  139. $res['sku'] = $res['orderProduct']['cart_info']['productAttr']['sku'];
  140. unset($res['orderProduct']);
  141. return $res;
  142. }
  143. public function getUsStatusAttr()
  144. {
  145. return ($this->status == 1) ? ($this->is_used == 1 ? ( $this->is_show ? 1 : 0 ) : -1) : -1;
  146. }
  147. public function getGuaranteeTemplateAttr()
  148. {
  149. $gua = GuaranteeTemplate::where('guarantee_template_id',$this->guarantee_template_id)->where('status',1)->where('is_del',0)->find();
  150. if(!$gua) return [];
  151. $guarantee_id = GuaranteeValue::where('guarantee_template_id',$this->guarantee_template_id)->column('guarantee_id');
  152. return Guarantee::where('guarantee_id','in',$guarantee_id)->where('status',1)->where('is_del',0)->select();
  153. }
  154. public function getMaxIntegralAttr()
  155. {
  156. if(systemConfig('integral_status') && merchantConfig($this->mer_id,'mer_integral_status')){
  157. $price = ($this->attrValue()->order('price DESC')->value('price'));
  158. $rate = ($this->integral_rate < 0) ? merchantConfig($this->mer_id,'mer_integral_rate') : $this->integral_rate;
  159. $rate = $rate / 100;
  160. return bcmul($price ,$rate,2);
  161. }
  162. return '0';
  163. }
  164. /*
  165. * -----------------------------------------------------------------------------------------------------------------
  166. * 关联模型
  167. * -----------------------------------------------------------------------------------------------------------------
  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 attr()
  174. {
  175. return $this->hasMany(ProductAttr::class,'product_id','product_id');
  176. }
  177. public function attrValue()
  178. {
  179. return $this->hasMany(ProductAttrValue::class,'product_id','product_id');
  180. }
  181. public function oldAttrValue()
  182. {
  183. return $this->hasMany(ProductAttrValue::class,'product_id','old_product_id');
  184. }
  185. public function content()
  186. {
  187. return $this->hasOne(ProductContent::class,'product_id','product_id');
  188. }
  189. protected function temp()
  190. {
  191. return $this->hasOne(ShippingTemplate::class,'shipping_template_id','temp_id');
  192. }
  193. public function storeCategory()
  194. {
  195. return $this->hasOne(StoreCategory::class,'store_category_id','cate_id')->field('store_category_id,cate_name');
  196. }
  197. public function merchant()
  198. {
  199. return $this->hasOne(Merchant::class,'mer_id','mer_id')->field('is_trader,type_id,mer_id,mer_name,mer_avatar,product_score,service_score,postage_score,service_phone,care_count');
  200. }
  201. public function reply()
  202. {
  203. return $this->hasMany(ProductReply::class,'product_id','product_id')->order('create_time DESC');
  204. }
  205. public function brand()
  206. {
  207. return $this->hasOne(StoreBrand::class,'brand_id','brand_id')->field('brand_id,brand_name');
  208. }
  209. public function seckillActive()
  210. {
  211. return $this->hasOne(StoreSeckillActive::class,'product_id','product_id');
  212. }
  213. public function issetCoupon()
  214. {
  215. return $this->hasOne(StoreCouponProduct::class, 'product_id', 'product_id')->alias('A')
  216. ->rightJoin('StoreCoupon B', 'A.coupon_id = B.coupon_id')->where(function (BaseQuery $query) {
  217. $query->where('B.is_limited', 0)->whereOr(function (BaseQuery $query) {
  218. $query->where('B.is_limited', 1)->where('B.remain_count', '>', 0);
  219. });
  220. })->where(function (BaseQuery $query) {
  221. $query->where('B.is_timeout', 0)->whereOr(function (BaseQuery $query) {
  222. $time = date('Y-m-d H:i:s');
  223. $query->where('B.is_timeout', 1)->where('B.start_time', '<', $time)->where('B.end_time', '>', $time);
  224. });
  225. })->field('A.product_id,B.*')->where('status', 1)->where('type', 1)->where('send_type', 0)->where('is_del', 0)
  226. ->order('sort DESC,coupon_id DESC')->hidden(['is_del', 'status']);
  227. }
  228. public function assist()
  229. {
  230. return $this->hasOne(ProductAssist::class,'product_id','product_id');
  231. }
  232. public function productGroup()
  233. {
  234. return $this->hasOne(ProductGroup::class,'product_id','product_id');
  235. }
  236. public function guarantee()
  237. {
  238. return $this->hasOne(GuaranteeTemplate::class,'guarantee_template_id','guarantee_template_id')->where('status',1)->where('is_del',0);
  239. }
  240. /*
  241. * -----------------------------------------------------------------------------------------------------------------
  242. * 搜索器
  243. * -----------------------------------------------------------------------------------------------------------------
  244. */
  245. public function searchMerCateIdAttr($query, $value)
  246. {
  247. $cate_ids = (StoreCategory::where('path','like','%/'.$value.'/%'))->column('store_category_id');
  248. $cate_ids[] = intval($value);
  249. $product_id = ProductCate::whereIn('mer_cate_id',$cate_ids)->column('product_id');
  250. $query->whereIn('Product.product_id',$product_id);
  251. }
  252. public function searchKeywordAttr($query, $value)
  253. {
  254. if (!$value) return;
  255. if (is_numeric($value)) {
  256. $query->whereLike("Product.store_name|Product.keyword|bar_code|Product.product_id", "%{$value}%");
  257. } else {
  258. $word = app()->make(VicWordService::class)->getWord($value);
  259. $query->where(function ($query) use ($word, $value) {
  260. foreach ($word as $item) {
  261. $query->whereOr('Product.store_name|Product.keyword', 'LIKE', "%$item%");
  262. }
  263. $query->order(Db::raw('REPLACE(Product.store_name,\'' . $value . '\',\'\')'));
  264. });
  265. }
  266. }
  267. public function searchStatusAttr($query, $value)
  268. {
  269. if($value === -1){
  270. $query->where('Product.status', 'in',[-1,-2]);
  271. }else {
  272. $query->where('Product.status',$value);
  273. }
  274. }
  275. public function searchCateIdAttr($query, $value)
  276. {
  277. $query->where('cate_id',$value);
  278. }
  279. public function searchCateIdsAttr($query, $value)
  280. {
  281. $query->whereIn('cate_id',$value);
  282. }
  283. public function searchIsShowAttr($query, $value)
  284. {
  285. $query->where('is_show',$value);
  286. }
  287. public function searchPidAttr($query, $value)
  288. {
  289. $cateId = app()->make(StoreCategoryRepository::class)->allChildren(intval($value));
  290. $query->whereIn('cate_id', $cateId);
  291. }
  292. public function searchStockAttr($query, $value)
  293. {
  294. $value ? $query->where('stock','<=', $value) : $query->where('stock', $value);
  295. }
  296. public function searchIsNewAttr($query, $value)
  297. {
  298. $query->where('is_new',$value);
  299. }
  300. public function searchPriceAttr($query, $value)
  301. {
  302. if(empty($value[0]) && !empty($value[1]))
  303. $query->where('price','<',$value[1]);
  304. if(!empty($value[0]) && empty($value[1]))
  305. $query->where('price','>',$value[0]);
  306. if(!empty($value[0]) && !empty($value[1]))
  307. $query->whereBetween('price',[$value[0],$value[1]]);
  308. }
  309. public function searchBrandIdAttr($query, $value)
  310. {
  311. $query->whereIn('brand_id',$value);
  312. }
  313. public function searchIsGiftBagAttr($query, $value)
  314. {
  315. $query->where('is_gift_bag',$value);
  316. }
  317. public function searchIsGoodAttr($query, $value)
  318. {
  319. $query->where('is_good',$value);
  320. }
  321. public function searchIsUsedAttr($query, $value)
  322. {
  323. $query->where('is_used',$value);
  324. }
  325. public function searchProductTypeAttr($query, $value)
  326. {
  327. $query->where('Product.product_type',$value);
  328. }
  329. public function searchSeckillStatusAttr($query, $value)
  330. {
  331. $product_id = (new StoreSeckillActiveDao())->getStatus($value)->column('product_id');
  332. $query->whereIn('Product.product_id',$product_id);
  333. }
  334. public function searchStoreNameAttr($query, $value)
  335. {
  336. $query->where('Product.store_name','like','%'.$value.'%');
  337. }
  338. public function searchMerStatusAttr($query, $value)
  339. {
  340. $query->where('mer_status',$value);
  341. }
  342. public function searchProductIdAttr($query, $value)
  343. {
  344. $query->where('Product.product_id',$value);
  345. }
  346. public function searchPriceOnAttr($query, $value)
  347. {
  348. $query->where('price','>=',$value);
  349. }
  350. public function searchPriceOffAttr($query, $value)
  351. {
  352. $query->where('price','<=',$value);
  353. }
  354. public function searchGuaranteeTemplateIdAttr($query, $value)
  355. {
  356. $query->whereIn('guarantee_template_id',$value);
  357. }
  358. }