StoreCart.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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\order;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\product\Product;
  14. use app\common\model\store\product\ProductAssistSet;
  15. use app\common\model\store\product\ProductAttr;
  16. use app\common\model\store\product\ProductAttrValue;
  17. use app\common\model\store\product\ProductPresell;
  18. use app\common\model\store\product\Spu;
  19. use app\common\model\store\product\StoreDiscounts;
  20. use app\common\model\system\merchant\Merchant;
  21. use app\common\model\store\product\ProductReservation;
  22. use app\common\model\store\product\ProductAttrValueReservation;
  23. use app\common\repositories\store\order\StoreOrderRepository;
  24. use app\common\repositories\store\product\ProductAssistSkuRepository;
  25. use app\common\repositories\store\product\ProductAttrValueRepository;
  26. use app\common\repositories\store\product\ProductGroupSkuRepository;
  27. use app\common\repositories\store\product\ProductPresellSkuRepository;
  28. use app\common\repositories\store\product\ProductSkuRepository;
  29. class StoreCart extends BaseModel
  30. {
  31. public static function tablePk(): ?string
  32. {
  33. return 'cart_id';
  34. }
  35. public static function tableName(): string
  36. {
  37. return 'store_cart';
  38. }
  39. public function searchCartIdAttr($query,$value)
  40. {
  41. $query->where('cart_id',$value);
  42. }
  43. public function product()
  44. {
  45. return $this->hasOne(Product::class, 'product_id', 'product_id');
  46. }
  47. public function productAttr()
  48. {
  49. return $this->hasOne(ProductAttrValue::class, 'unique', 'product_attr_unique');
  50. }
  51. public function attr()
  52. {
  53. return $this->hasMany(ProductAttr::class,'product_id','product_id');
  54. }
  55. public function attrValue()
  56. {
  57. return $this->hasMany(ProductAttrValue::class, 'product_id', 'product_id');
  58. }
  59. public function merchant()
  60. {
  61. return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
  62. }
  63. public function productPresell()
  64. {
  65. return $this->hasOne(ProductPresell::class,'product_presell_id','source_id');
  66. }
  67. public function reservationAttr()
  68. {
  69. return $this->hasOne(ProductAttrValueReservation::class,'attr_reservation_id','reservation_id');
  70. }
  71. public function reservation()
  72. {
  73. return $this->hasOne(ProductReservation::class, 'product_id', 'product_id');
  74. }
  75. public function productDiscount()
  76. {
  77. return $this->hasOne(StoreDiscounts::class, 'discount_id', 'source_id');
  78. }
  79. public function getProductDiscountAttrAttr()
  80. {
  81. return app()->make(ProductSkuRepository::class)->getSearch(['active_id' => $this->source_id, 'unique' => $this->product_attr_unique,'active_type'=> 10])->find();
  82. }
  83. public function productAssistSet()
  84. {
  85. return $this->hasOne(ProductAssistSet::class,'product_assist_set_id','source_id');
  86. }
  87. public function getProductPresellAttrAttr()
  88. {
  89. return app()->make(ProductPresellSkuRepository::class)->getSearch(['product_presell_id' => $this->source_id, 'unique' => $this->product_attr_unique])->find();
  90. }
  91. public function getProductAssistAttrAttr()
  92. {
  93. $make = app()->make(ProductAssistSkuRepository::class);
  94. $where = [
  95. "product_assist_id" => $this->productAssistSet->product_assist_id,
  96. "unique" => $this->product_attr_unique
  97. ];
  98. return $make->getSearch($where)->find();
  99. }
  100. /**
  101. * 活动商品 SKU
  102. * @return array|\think\Model|null
  103. * @author Qinii
  104. * @day 1/13/21
  105. */
  106. public function getActiveSkuAttr()
  107. {
  108. switch ($this->product_type)
  109. {
  110. case 2:
  111. $make = app()->make(ProductPresellSkuRepository::class);
  112. $where['product_presell_id'] = $this->source_id;
  113. break;
  114. case 3:
  115. $make = app()->make(ProductAssistSkuRepository::class);
  116. $where['product_assist_id'] = $this->productAssistSet->product_assist_id;
  117. break;
  118. case 4:
  119. $make = app()->make(ProductGroupSkuRepository::class);
  120. $where['product_group_id'] = $this->product->productGroup->product_group_id;
  121. break;
  122. default:
  123. $make = app()->make(ProductAttrValueRepository::class);
  124. $where['product_id'] = $this->product_id;
  125. break;
  126. }
  127. $where['unique'] = $this->product_attr_unique;
  128. return $make->getSearch($where)->find();
  129. }
  130. public function getSpuAttr()
  131. {
  132. if ($this->product_type) {
  133. $where = [
  134. 'activity_id' => $this->source_id,
  135. 'product_type' => $this->product_type,
  136. ];
  137. } else {
  138. $where = [
  139. 'product_id' => $this->product_id,
  140. 'product_type' => $this->product_type,
  141. ];
  142. }
  143. return Spu::where($where)->field('spu_id,store_name')->find();
  144. }
  145. /**
  146. * 检测商品是否有效
  147. * @return bool
  148. * @author Qinii
  149. * @day 2020-10-29
  150. */
  151. public function getCheckCartProductAttr()
  152. {
  153. if($this->is_fail == 1) return false;
  154. if(is_null($this->product) || is_null($this->productAttr) || $this->product->status !== 1 || $this->product->mer_status !== 1 || $this->product->is_del !== 0)
  155. {$this->is_fail = 1;$this->save();return false;}
  156. switch ($this->product_type)
  157. {
  158. case 0: //普通商品
  159. if ($this->product->product_type !== 0 || $this->product->is_show !== 1 || $this->productAttr->stock < $this->cart_num || $this->product->is_used !== 1) {
  160. return false;
  161. }
  162. break;
  163. case 1: //秒杀商品
  164. if ($this->product->product_type !== 1 || $this->product->is_show !== 1) return false;
  165. //结束时间
  166. if ($this->product->seckill_status !== 1) return false;
  167. //限量
  168. $order_make = app()->make(StoreOrderRepository::class);
  169. $count = $order_make->seckillOrderCounut($this->product_id);
  170. if ($this->productAttr->stock <= $count) return false;
  171. //原商品sku库存
  172. $value_make = app()->make(ProductAttrValueRepository::class);
  173. $sku = $value_make->getWhere(['sku' => $this->productAttr->sku, 'product_id' => $this->product->old_product_id]);
  174. if (!$sku || $sku['stock'] <= 0) return false;
  175. break;
  176. case 2: //预售商品
  177. if($this->source !== 2 || $this->product->product_type !== 2) return false;
  178. if($this->productPresell->status !== 1 ||
  179. $this->productPresell->is_show !== 1 ||
  180. $this->productPresell->is_del !== 0 ||
  181. $this->productPresell->presell_status !== 1)
  182. {$this->is_fail = 1;$this->save();return false;}
  183. $sku = $this->ActiveSku;
  184. if(!$sku || !$sku->sku()) {$this->is_fail = 1; $this->save(); return false; }
  185. //库存不足
  186. if($sku->stock < $this->cart_num || $sku->sku->stock < $this->cart_num) return false;
  187. break;
  188. case 3: //助力商品
  189. if($this->source !== 3 || $this->product->product_type !== 3 || ($this->productAssistSet->assist_count !== $this->productAssistSet->yet_assist_count)) return false;
  190. if(
  191. $this->productAssistSet->stop_time < time() ||
  192. $this->productAssistSet->sataus === -1 ||
  193. !$this->productAssistSet->assist->is_show ||
  194. $this->productAssistSet->assist->is_del !== 0 ||
  195. $this->productAssistSet->assist->status !== 1)
  196. {$this->is_fail = 1;$this->save();return false;}
  197. $sku = $this->ActiveSku;
  198. if(!$sku || !$sku->sku()) { $this->is_fail = 1; $this->save(); return false; }
  199. //库存不足
  200. if($sku->stock < $this->cart_num || $sku->sku->stock < $this->cart_num) return false;
  201. break;
  202. case 4:
  203. if($this->source !== 4 || $this->product->product_type !== 4 ) return false;
  204. $sku = $this->ActiveSku;
  205. if(!$sku || !$sku->sku()) { $this->is_fail = 1; $this->save(); return false; }
  206. //库存不足
  207. if($sku->stock < $this->cart_num || $sku->sku->stock < $this->cart_num) return false;
  208. break;
  209. }
  210. return true;
  211. }
  212. /**
  213. *
  214. * @return bool
  215. * @author Qinii
  216. * @day 2020-10-29
  217. */
  218. public function getUserPayCountAttr()
  219. {
  220. $make = app()->make(StoreOrderRepository::class);
  221. switch ($this->product_type)
  222. {
  223. case 1: //秒杀
  224. if(!$make->getDayPayCount($this->uid,$this->product_id,$this->cart_num) || !$make->getPayCount($this->uid,$this->product_id,$this->cart_num))
  225. return false;
  226. break;
  227. case 2: //预售
  228. $count = $this->productPresell->pay_count;
  229. if($count == 0) return true;
  230. $tattend = [
  231. 'activity_id' => $this->source_id,
  232. 'product_type' => 2,
  233. ];
  234. $pay_count = $make->getTattendCount($tattend,$this->uid)->sum('total_num');
  235. if($pay_count < $count) return false;
  236. if (($count - $pay_count) < $this->cart_num) return false;
  237. break;
  238. case 3: //助力
  239. $tattend = [
  240. 'activity_id' => $this->source_id,
  241. 'product_type' => 3,
  242. ];
  243. $pay_count = $make->getTattendCount($tattend,$this->uid)->count();
  244. if($pay_count) return false;
  245. $count = $this->productAssistSet->assist->pay_count;
  246. if($count !== 0){
  247. $_tattend = [
  248. 'exsits_id' => $this->productAssistSet->assist->product_assist_id,
  249. 'product_type' => 3,
  250. ];
  251. $_count = $make->getTattendCount($_tattend,$this->uid)->count();
  252. if($_count >= $count) return false;
  253. }
  254. break;
  255. case 4:
  256. $tattend = [
  257. 'exsits_id' => $this->product_id,
  258. 'product_type' => 4,
  259. ];
  260. $pay_count = $make->getTattendCount($tattend,$this->uid)->count();
  261. if($pay_count) return false;
  262. $count = $this->product->productGroup->pay_count;
  263. if($count !== 0){
  264. $_tattend = [
  265. 'exsits_id' => $this->product_id,
  266. 'product_type' => 34,
  267. ];
  268. $_count = $make->getTattendCount($_tattend,$this->uid)->count();
  269. if($_count >= $count) return false;
  270. }
  271. break;
  272. }
  273. return true;
  274. }
  275. public function searchProductIdAttr($query,$value)
  276. {
  277. $query->where('product_id',$value);
  278. }
  279. public function searchUidAttr($query,$value)
  280. {
  281. $query->where('uid',$value);
  282. }
  283. public function searchIsNewAttr($query,$value)
  284. {
  285. $query->where('is_new',$value);
  286. }
  287. public function searchIsPayAttr($query,$value)
  288. {
  289. $query->where('is_pay',$value);
  290. }
  291. public function searchIsDelAttr($query,$value)
  292. {
  293. $query->where('is_del',$value);
  294. }
  295. public function searchIsFailAttr($query,$value)
  296. {
  297. $query->where('is_fail',$value);
  298. }
  299. }