StoreCombination.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\ebapi\model\store;
  8. use traits\ModelTrait;
  9. use basic\ModelBasic;
  10. /**
  11. * 拼团model
  12. * Class StoreCombination
  13. * @package app\ebapi\model\store
  14. */
  15. class StoreCombination extends ModelBasic
  16. {
  17. use ModelTrait;
  18. /**
  19. * @param $where
  20. * @return array
  21. */
  22. public static function get_list($length=10){
  23. if($post=input('post.')){
  24. $where=$post['where'];
  25. $model = new self();
  26. $model = $model->alias('c');
  27. $model = $model->join('StoreProduct s','s.id=c.product_id');
  28. $model = $model->where('c.is_show',1)->where('c.is_del',0)->where('c.start_time','LT',time())->where('c.stop_time','GT',time());
  29. if(!empty($where['search'])){
  30. $model = $model->where('c.title','like',"%{$where['search']}%");
  31. $model = $model->whereOr('s.keyword','like',"{$where['search']}%");
  32. }
  33. $model = $model->field('c.*,s.price as product_price');
  34. if($where['key']){
  35. if($where['sales']==1){
  36. $model = $model->order('c.sales desc');
  37. }else if($where['sales']==2){
  38. $model = $model->order('c.sales asc');
  39. }
  40. if($where['price']==1){
  41. $model = $model->order('c.price desc');
  42. }else if($where['price']==2){
  43. $model = $model->order('c.price asc');
  44. }
  45. if($where['people']==1){
  46. $model = $model->order('c.people asc');
  47. }
  48. if($where['default']==1){
  49. $model = $model->order('c.sort desc,c.id desc');
  50. }
  51. }else{
  52. $model = $model->order('c.sort desc,c.id desc');
  53. }
  54. $page=is_string($where['page'])?(int)$where['page']+1:$where['page']+1;
  55. $list = $model->page($page,$length)->select()->toArray();
  56. return ['list'=>$list,'page'=>$page];
  57. }
  58. }
  59. /**
  60. * 获取所有拼团数据
  61. * @param int $limit
  62. * @param int $length
  63. * @return mixed
  64. */
  65. public static function getAll($limit = 0,$length = 0){
  66. $model = new self();
  67. $model = $model->alias('c');
  68. $model = $model->join('StoreProduct s','s.id=c.product_id');
  69. $model = $model->field('c.*,s.price as product_price');
  70. $model = $model->order('c.sort desc,c.id desc');
  71. $model = $model->where('c.is_show',1);
  72. $model = $model->where('c.is_del',0);
  73. $model = $model->where('c.start_time','LT',time());
  74. $model = $model->where('c.stop_time','GT',time());
  75. if($limit && $length) $model = $model->limit($limit,$length);
  76. $list = $model->select();
  77. if($list) return $list->toArray();
  78. else return [];
  79. }
  80. /*
  81. * 获取是否有拼团产品
  82. * */
  83. public static function getPinkIsOpen()
  84. {
  85. return self::alias('c')->join('StoreProduct s','s.id=c.product_id')->where('c.is_show',1)->where('c.is_del',0)
  86. ->where('c.start_time','LT',time())->where('c.stop_time','GT',time())->count();
  87. }
  88. /**
  89. * 获取一条拼团数据
  90. * @param $id
  91. * @return mixed
  92. */
  93. public static function getCombinationOne($id){
  94. $model = new self();
  95. $model = $model->alias('c');
  96. $model = $model->join('StoreProduct s','s.id=c.product_id');
  97. $model = $model->field('c.*,s.price as product_price');
  98. $model = $model->where('c.is_show',1);
  99. $model = $model->where('c.is_del',0);
  100. $model = $model->where('c.id',$id);
  101. // $model = $model->where('c.start_time','LT',time());
  102. // $model = $model->where('c.stop_time','GT',time()-86400);
  103. $list = $model->find();
  104. if($list) return $list->toArray();
  105. else return [];
  106. }
  107. /**
  108. * 获取推荐的拼团产品
  109. * @return mixed
  110. */
  111. public static function getCombinationHost($limit = 0){
  112. $model = new self();
  113. $model = $model->alias('c');
  114. $model = $model->join('StoreProduct s','s.id=c.product_id');
  115. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  116. $model = $model->where('c.is_del',0);
  117. $model = $model->where('c.is_host',1);
  118. $model = $model->where('c.start_time','LT',time());
  119. $model = $model->where('c.stop_time','GT',time());
  120. if($limit) $model = $model->limit($limit);
  121. $list = $model->select();
  122. if($list) return $list->toArray();
  123. else return [];
  124. }
  125. /**
  126. * 修改销量和库存
  127. * @param $num
  128. * @param $CombinationId
  129. * @return bool
  130. */
  131. public static function decCombinationStock($num,$CombinationId)
  132. {
  133. $res = false !== self::where('id',$CombinationId)->dec('stock',$num)->inc('sales',$num)->update();
  134. return $res;
  135. }
  136. /**
  137. * 增加库存,减少销量
  138. * @param $num
  139. * @param $CombinationId
  140. * @return bool
  141. */
  142. public static function incCombinationStock($num,$CombinationId)
  143. {
  144. $combination=self::where('id',$CombinationId)->field(['stock','sales'])->find();
  145. if(!$combination) return true;
  146. if($combination->sales > 0) $combination->sales=bcsub($combination->sales,$num,0);
  147. if($combination->sales < 0) $combination->sales=0;
  148. $combination->stock=bcadd($combination->stock,$num,0);
  149. return $combination->save();
  150. }
  151. /**
  152. * 判断库存是否足够
  153. * @param $id
  154. * @param $cart_num
  155. * @return int|mixed
  156. */
  157. public static function getCombinationStock($id,$cart_num){
  158. $stock = self::where('id',$id)->value('stock');
  159. return $stock > $cart_num ? $stock : 0;
  160. }
  161. /**
  162. * 获取产品状态
  163. * @param $id
  164. * @return mixed
  165. */
  166. public static function isValidCombination($id){
  167. $model = new self();
  168. $model = $model->where('id',$id);
  169. $model = $model->where('is_del',0);
  170. $model = $model->where('is_show',1);
  171. return $model->count();
  172. }
  173. }