StoreProduct.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app\wap\model\store\StoreCart;
  14. use basic\ModelBasic;
  15. use traits\ModelTrait;
  16. use think\Db;
  17. /**商品表
  18. * Class StoreProduct
  19. * @package app\wap\model\store
  20. */
  21. class StoreProduct extends ModelBasic
  22. {
  23. use ModelTrait;
  24. protected function getSliderImageAttr($value)
  25. {
  26. return json_decode($value, true) ?: [];
  27. }
  28. public static function getValidProduct($productId, $field = 'id,mer_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')
  29. {
  30. return self::validWhere()->where('id', $productId)->field($field)->find();
  31. }
  32. public static function validWhere()
  33. {
  34. return self::where(['is_del' => 0, 'is_show' => 1, 'status' => 1]);
  35. }
  36. /**
  37. * 新品产品
  38. * @param string $field
  39. * @param int $limit
  40. * @return false|\PDOStatement|string|\think\Collection
  41. */
  42. public static function getNewProduct($field = '*', $limit = 0)
  43. {
  44. $model = self::validWhere()->where('is_new', 1)->where('stock', '>', 0)->field($field)
  45. ->order('sort DESC, id DESC');
  46. if ($limit) $model->limit($limit);
  47. return $model->select();
  48. }
  49. /**
  50. * 热卖产品
  51. * @param string $field
  52. * @param int $limit
  53. * @return false|\PDOStatement|string|\think\Collection
  54. */
  55. public static function getHotProduct($field = '*', $limit = 0)
  56. {
  57. $model = self::validWhere()->where('is_hot', 1)
  58. ->where('stock', '>', 0)->field($field)
  59. ->order('sort DESC, id DESC');
  60. if ($limit) $model->limit($limit);
  61. return $model->select();
  62. }
  63. /**
  64. * 精品产品
  65. * @param string $field
  66. * @param int $limit
  67. * @return false|\PDOStatement|string|\think\Collection
  68. */
  69. public static function getBestProduct($field = '*', $limit = 0)
  70. {
  71. $model = self::validWhere()->where('is_best', 1)
  72. ->where('stock', '>', 0)->field($field)
  73. ->order('sort DESC, id DESC');
  74. if ($limit) $model->limit($limit);
  75. return $model->select();
  76. }
  77. /**
  78. * 优惠产品
  79. * @param string $field
  80. * @param int $limit
  81. * @return false|\PDOStatement|string|\think\Collection
  82. */
  83. public static function getBenefitProduct($field = '*', $limit = 0)
  84. {
  85. $model = self::validWhere()->where('is_benefit', 1)->where('stock', '>', 0)
  86. ->field($field)->order('sort DESC, id DESC');
  87. if ($limit) $model->limit($limit);
  88. return $model->select();
  89. }
  90. public static function cateIdBySimilarityProduct($cateId, $field = '*', $limit = 0)
  91. {
  92. $pid = StoreCategory::cateIdByPid($cateId) ?: $cateId;
  93. $cateList = StoreCategory::pidByCategory($pid, 'id') ?: [];
  94. $cid = [$pid];
  95. foreach ($cateList as $cate) {
  96. $cid[] = $cate['id'];
  97. }
  98. $model = self::where('cate_id', 'IN', $cid)->where('is_show', 1)->where('is_del', 0)
  99. ->field($field)->order('sort DESC,id DESC');
  100. if ($limit) $model->limit($limit);
  101. return $model->select();
  102. }
  103. public static function isValidProduct($productId)
  104. {
  105. return self::be(['id' => $productId, 'is_del' => 0, 'is_show' => 1, 'status' => 1]) > 0;
  106. }
  107. /**获取商品库存
  108. * @param $productId
  109. * @param string $uniqueId
  110. * @return int
  111. */
  112. public static function getProductStock($productId, $uniqueId = '')
  113. {
  114. return self::where('id', $productId)->value('stock');
  115. }
  116. public static function decProductStock($num, $productId, $unique = '')
  117. {
  118. $res = false !== self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
  119. return $res;
  120. }
  121. /**
  122. * 获取单独分销设置
  123. */
  124. public static function getIndividualDistributionSettings($oid)
  125. {
  126. $product_id = Db::name('store_order_cart_info')->where('oid', $oid)->value('product_id');
  127. if ($product_id) {
  128. $data = self::validWhere()->where('id', $product_id)->field('is_alone,brokerage_ratio,brokerage_two')->find();
  129. if ($data) return $data;
  130. else return [];
  131. } else {
  132. return [];
  133. }
  134. }
  135. /**讲师名下商品
  136. * @param $mer_id
  137. * @param $page
  138. * @param $limit
  139. */
  140. public static function getLecturerStoreList($mer_id, $page, $limit)
  141. {
  142. if ($mer_id) {
  143. $model = self::validWhere();
  144. $model = $model->where(['mer_id' => $mer_id])->order('sort desc,id desc');
  145. $list = $model->page($page, $limit)->select();
  146. $list = count($list) ? $list->toArray() : [];
  147. } else {
  148. $list = [];
  149. }
  150. return $list;
  151. }
  152. }