StoreProduct.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\wap\model\store;
  12. use app\admin\model\store\StoreProductAttrValue;
  13. use basic\ModelBasic;
  14. use traits\ModelTrait;
  15. class StoreProduct extends ModelBasic
  16. {
  17. use ModelTrait;
  18. protected function getSliderImageAttr($value)
  19. {
  20. return json_decode($value,true)?:[];
  21. }
  22. public static function getValidProduct($productId,$field = 'id,store_name,image,slider_image,store_info,keyword,ot_price,description,is_postage,give_gold_num,free_shipping,postage,price,vip_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as sales')
  23. {
  24. return self::where('is_del',0)->where('is_show',1)->where('id',$productId)->field($field)->find();
  25. }
  26. public static function validWhere()
  27. {
  28. return self::where('is_del',0)->where('is_show',1)->where('mer_id',0);
  29. }
  30. /**
  31. * 新品产品
  32. * @param string $field
  33. * @param int $limit
  34. * @return false|\PDOStatement|string|\think\Collection
  35. */
  36. public static function getNewProduct($field = '*',$limit = 0)
  37. {
  38. $model = self::where('is_new',1)->where('is_del',0)->where('mer_id',0)
  39. ->where('stock','>',0)->where('is_show',1)->field($field)
  40. ->order('sort DESC, id DESC');
  41. if($limit) $model->limit($limit);
  42. return $model->select();
  43. }
  44. /**
  45. * 热卖产品
  46. * @param string $field
  47. * @param int $limit
  48. * @return false|\PDOStatement|string|\think\Collection
  49. */
  50. public static function getHotProduct($field = '*',$limit = 0)
  51. {
  52. $model = self::where('is_hot',1)->where('is_del',0)->where('mer_id',0)
  53. ->where('stock','>',0)->where('is_show',1)->field($field)
  54. ->order('sort DESC, id DESC');
  55. if($limit) $model->limit($limit);
  56. return $model->select();
  57. }
  58. /**
  59. * 精品产品
  60. * @param string $field
  61. * @param int $limit
  62. * @return false|\PDOStatement|string|\think\Collection
  63. */
  64. public static function getBestProduct($field = '*',$limit = 0)
  65. {
  66. $model = self::where('is_best',1)->where('is_del',0)->where('mer_id',0)
  67. ->where('stock','>',0)->where('is_show',1)->field($field)
  68. ->order('sort DESC, id DESC');
  69. if($limit) $model->limit($limit);
  70. return $model->select();
  71. }
  72. /**
  73. * 优惠产品
  74. * @param string $field
  75. * @param int $limit
  76. * @return false|\PDOStatement|string|\think\Collection
  77. */
  78. public static function getBenefitProduct($field = '*',$limit = 0)
  79. {
  80. $model = self::where('is_benefit',1)
  81. ->where('is_del',0)->where('mer_id',0)->where('stock','>',0)
  82. ->where('is_show',1)->field($field)
  83. ->order('sort DESC, id DESC');
  84. if($limit) $model->limit($limit);
  85. return $model->select();
  86. }
  87. public static function cateIdBySimilarityProduct($cateId,$field='*',$limit = 0)
  88. {
  89. $pid = StoreCategory::cateIdByPid($cateId)?:$cateId;
  90. $cateList = StoreCategory::pidByCategory($pid,'id') ?:[];
  91. $cid = [$pid];
  92. foreach ($cateList as $cate){
  93. $cid[] = $cate['id'];
  94. }
  95. $model = self::where('cate_id','IN',$cid)->where('is_show',1)->where('is_del',0)
  96. ->field($field)->order('sort DESC,id DESC');
  97. if($limit) $model->limit($limit);
  98. return $model->select();
  99. }
  100. public static function isValidProduct($productId)
  101. {
  102. return self::be(['id'=>$productId,'is_del'=>0,'is_show'=>1]) > 0;
  103. }
  104. /**获取商品库存
  105. * @param $productId
  106. * @param string $uniqueId
  107. * @return int
  108. */
  109. public static function getProductStock($productId,$uniqueId = '')
  110. {
  111. if(!$uniqueId) $uniqueId = '';
  112. return (string)$uniqueId == '' ?
  113. self::where('id',$productId)->value('stock')?:0
  114. : StoreProductAttr::uniqueByStock($uniqueId);
  115. }
  116. public static function decProductStock($num,$productId,$unique = '')
  117. {
  118. if($unique){
  119. $res = false !== StoreProductAttrValue::decProductAttrStock($productId,$unique,$num);
  120. $res = $res && self::where('id',$productId)->setInc('sales',$num);
  121. }else{
  122. $res = false !== self::where('id',$productId)->dec('stock',$num)->inc('sales',$num)->update();
  123. }
  124. return $res;
  125. }
  126. }