StoreCart.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\ProductAttrValue;
  16. use app\common\model\store\product\ProductGroup;
  17. use app\common\model\store\product\ProductPresell;
  18. use app\common\model\store\product\ProductPresellSku;
  19. use app\common\model\system\merchant\Merchant;
  20. use app\common\repositories\store\order\StoreOrderProductRepository;
  21. use app\common\repositories\store\order\StoreOrderRepository;
  22. use app\common\repositories\store\product\ProductAssistSkuRepository;
  23. use app\common\repositories\store\product\ProductAttrValueRepository;
  24. use app\common\repositories\store\product\ProductGroupSkuRepository;
  25. use app\common\repositories\store\product\ProductPresellSkuRepository;
  26. use app\common\repositories\store\StoreSeckillActiveRepository;
  27. use function Symfony\Component\String\b;
  28. class StoreCart extends BaseModel
  29. {
  30. public static function tablePk(): ?string
  31. {
  32. return 'cart_id';
  33. }
  34. public static function tableName(): string
  35. {
  36. return 'store_cart';
  37. }
  38. public function searchCartIdAttr($query,$value)
  39. {
  40. $query->where('cart_id',$value);
  41. }
  42. public function product()
  43. {
  44. return $this->hasOne(Product::class, 'product_id', 'product_id');
  45. }
  46. public function productAttr()
  47. {
  48. return $this->hasOne(ProductAttrValue::class, 'unique', 'product_attr_unique');
  49. }
  50. public function merchant()
  51. {
  52. return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
  53. }
  54. public function productPresell()
  55. {
  56. return $this->hasOne(ProductPresell::class,'product_presell_id','source_id');
  57. }
  58. public function productAssistSet()
  59. {
  60. return $this->hasOne(ProductAssistSet::class,'product_assist_set_id','source_id');
  61. }
  62. public function getProductPresellAttrAttr()
  63. {
  64. return app()->make(ProductPresellSkuRepository::class)->getSearch(['product_presell_id' => $this->source_id, 'unique' => $this->product_attr_unique])->find();
  65. }
  66. public function getProductAssistAttrAttr()
  67. {
  68. $make = app()->make(ProductAssistSkuRepository::class);
  69. $where = [
  70. "product_assist_id" => $this->productAssistSet->product_assist_id,
  71. "unique" => $this->product_attr_unique
  72. ];
  73. return $make->getSearch($where)->find();
  74. }
  75. /**
  76. * TODO 活动商品 SKU
  77. * @return array|\think\Model|null
  78. * @author Qinii
  79. * @day 1/13/21
  80. */
  81. public function getActiveSkuAttr()
  82. {
  83. switch ($this->product_type)
  84. {
  85. case 2:
  86. $make = app()->make(ProductPresellSkuRepository::class);
  87. $where['product_presell_id'] = $this->source_id;
  88. break;
  89. case 3:
  90. $make = app()->make(ProductAssistSkuRepository::class);
  91. $where['product_assist_id'] = $this->productAssistSet->product_assist_id;
  92. break;
  93. case 4:
  94. $make = app()->make(ProductGroupSkuRepository::class);
  95. $where['product_group_id'] = $this->product->productGroup->product_group_id;
  96. break;
  97. default:
  98. $make = app()->make(ProductAttrValueRepository::class);
  99. $where['product_id'] = $this->product_id;
  100. break;
  101. }
  102. $where['unique'] = $this->product_attr_unique;
  103. return $make->getSearch($where)->find();
  104. }
  105. /**
  106. * TODO 检测商品是否有效
  107. * @return bool
  108. * @author Qinii
  109. * @day 2020-10-29
  110. */
  111. public function getCheckCartProductAttr()
  112. {
  113. if($this->is_fail == 1) return false;
  114. if(is_null($this->product) || is_null($this->productAttr) || $this->product->status !== 1 || $this->product->mer_status !== 1 || $this->product->is_del !== 0)
  115. {$this->is_fail = 1;$this->save();return false;}
  116. switch ($this->product_type)
  117. {
  118. case 0: //普通商品
  119. if ($this->product->product_type !== 0 || $this->product->is_show !== 1 || $this->productAttr->stock < $this->cart_num || $this->product->is_used !== 1) {
  120. return false;
  121. }
  122. break;
  123. case 1: //秒杀商品
  124. if ($this->product->product_type !== 1 || $this->product->is_show !== 1) return false;
  125. //结束时间
  126. if ($this->product->end_time < time()) return false;
  127. //限量
  128. $order_make = app()->make(StoreOrderRepository::class);
  129. $count = $order_make->seckillOrderCounut($this->product_id);
  130. if ($this->productAttr->stock <= $count) return false;
  131. //原商品sku库存
  132. $value_make = app()->make(ProductAttrValueRepository::class);
  133. $sku = $value_make->getWhere(['sku' => $this->productAttr->sku, 'product_id' => $this->product->old_product_id]);
  134. if (!$sku || $sku['stock'] <= 0) return false;
  135. break;
  136. case 2: //预售商品
  137. if($this->source !== 2 || $this->product->product_type !== 2) return false;
  138. if($this->productPresell->status !== 1 ||
  139. $this->productPresell->is_show !== 1 ||
  140. $this->productPresell->is_del !== 0 ||
  141. $this->productPresell->presell_status !== 1)
  142. {$this->is_fail = 1;$this->save();return false;}
  143. $sku = $this->ActiveSku;
  144. if(!$sku || !$sku->sku()) {$this->is_fail = 1; $this->save(); return false; }
  145. //库存不足
  146. if($sku->stock < $this->cart_num || $sku->sku->stock < $this->cart_num) return false;
  147. break;
  148. case 3: //助力商品
  149. if($this->source !== 3 || $this->product->product_type !== 3 || ($this->productAssistSet->assist_count !== $this->productAssistSet->yet_assist_count)) return false;
  150. if(
  151. $this->productAssistSet->stop_time < time() ||
  152. $this->productAssistSet->sataus === -1 ||
  153. !$this->productAssistSet->assist->is_show ||
  154. $this->productAssistSet->assist->is_del !== 0 ||
  155. $this->productAssistSet->assist->status !== 1)
  156. {$this->is_fail = 1;$this->save();return false;}
  157. $sku = $this->ActiveSku;
  158. if(!$sku || !$sku->sku()) { $this->is_fail = 1; $this->save(); return false; }
  159. //库存不足
  160. if($sku->stock < $this->cart_num || $sku->sku->stock < $this->cart_num) return false;
  161. break;
  162. case 4:
  163. if($this->source !== 4 || $this->product->product_type !== 4 ) return false;
  164. $sku = $this->ActiveSku;
  165. if(!$sku || !$sku->sku()) { $this->is_fail = 1; $this->save(); return false; }
  166. //库存不足
  167. if($sku->stock < $this->cart_num || $sku->sku->stock < $this->cart_num) return false;
  168. break;
  169. }
  170. return true;
  171. }
  172. /**
  173. * TODO
  174. * @return bool
  175. * @author Qinii
  176. * @day 2020-10-29
  177. */
  178. public function getUserPayCountAttr()
  179. {
  180. $make = app()->make(StoreOrderRepository::class);
  181. switch ($this->product_type)
  182. {
  183. case 1: //秒杀
  184. if(!$make->getDayPayCount($this->uid,$this->product_id) || !$make->getPayCount($this->uid,$this->product_id))
  185. return false;
  186. break;
  187. case 2: //预售
  188. $count = $this->productPresell->pay_count;
  189. if($count == 0) return true;
  190. $tattend = [
  191. 'activity_id' => $this->source_id,
  192. 'product_type' => 2,
  193. ];
  194. $pay_count = $make->getTattendCount($tattend,$this->uid)->sum('total_num');
  195. if($pay_count < $count) return false;
  196. if (($count - $pay_count) < $this->cart_num) return false;
  197. break;
  198. case 3: //助力
  199. $tattend = [
  200. 'activity_id' => $this->source_id,
  201. 'product_type' => 3,
  202. ];
  203. $pay_count = $make->getTattendCount($tattend,$this->uid)->count();
  204. if($pay_count) return false;
  205. $count = $this->productAssistSet->assist->pay_count;
  206. if($count !== 0){
  207. $_tattend = [
  208. 'exsits_id' => $this->productAssistSet->assist->product_assist_id,
  209. 'product_type' => 3,
  210. ];
  211. $_count = $make->getTattendCount($_tattend,$this->uid)->count();
  212. if($_count >= $count) return false;
  213. }
  214. break;
  215. case 4:
  216. $tattend = [
  217. 'exsits_id' => $this->product_id,
  218. 'product_type' => 4,
  219. ];
  220. $pay_count = $make->getTattendCount($tattend,$this->uid)->count();
  221. if($pay_count) return false;
  222. $count = $this->product->productGroup->pay_count;
  223. if($count !== 0){
  224. $_tattend = [
  225. 'exsits_id' => $this->product_id,
  226. 'product_type' => 34,
  227. ];
  228. $_count = $make->getTattendCount($_tattend,$this->uid)->count();
  229. if($_count >= $count) return false;
  230. }
  231. break;
  232. }
  233. return true;
  234. }
  235. }