StoreCombination.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\wap\model\store;
  8. use traits\ModelTrait;
  9. use basic\ModelBasic;
  10. /**
  11. * 拼团model
  12. * Class StoreCombination
  13. * @package app\admin\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. * @param $id
  83. * @return mixed
  84. */
  85. public static function getCombinationOne($id){
  86. $model = new self();
  87. $model = $model->alias('c');
  88. $model = $model->join('StoreProduct s','s.id=c.product_id');
  89. $model = $model->field('c.*,s.price as product_price,s.cate_id');
  90. $model = $model->where('c.is_show',1);
  91. $model = $model->where('c.is_del',0);
  92. $model = $model->where('c.id',$id);
  93. $model = $model->where('c.start_time','LT',time());
  94. $model = $model->where('c.stop_time','GT',time()-86400);
  95. $list = $model->find();
  96. if($list) return $list->toArray();
  97. else return [];
  98. }
  99. /**
  100. * 获取产品状态
  101. * @param $id
  102. * @return mixed
  103. */
  104. public static function isValidCombination($id){
  105. $model = new self();
  106. $model = $model->where('id',$id);
  107. $model = $model->where('is_del',0);
  108. $model = $model->where('is_show',1);
  109. return $model->count();
  110. }
  111. /**
  112. * 判断库存是否足够
  113. * @param $id
  114. * @param $cart_num
  115. * @return int|mixed
  116. */
  117. public static function getCombinationStock($id,$cart_num){
  118. $stock = self::where('id',$id)->value('stock');
  119. return $stock > $cart_num ? $stock : 0;
  120. }
  121. /**
  122. * 获取推荐的拼团产品
  123. * @return mixed
  124. */
  125. public static function getCombinationHost($limit = 0){
  126. $model = new self();
  127. $model = $model->alias('c');
  128. $model = $model->join('StoreProduct s','s.id=c.product_id');
  129. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  130. $model = $model->where('c.is_del',0);
  131. $model = $model->where('c.is_host',1);
  132. $model = $model->where('c.is_show',1);
  133. $model = $model->where('c.start_time','LT',time());
  134. $model = $model->where('c.stop_time','GT',time());
  135. if($limit) $model = $model->limit($limit);
  136. $list = $model->select();
  137. if($list) return $list->toArray();
  138. else return [];
  139. }
  140. /**
  141. * 修改销量和库存
  142. * @param $num
  143. * @param $CombinationId
  144. * @return bool
  145. */
  146. public static function decCombinationStock($num,$CombinationId)
  147. {
  148. $res = false !== self::where('id',$CombinationId)->dec('stock',$num)->inc('sales',$num)->update();
  149. return $res;
  150. }
  151. /**
  152. * 增加浏览量
  153. * @param int $id
  154. * @return bool
  155. */
  156. public static function editIncBrowse($id = 0){
  157. if(!$id) return false;
  158. $browse = self::where('id',$id)->value('browse');
  159. $browse = bcadd($browse,1,0);
  160. self::edit(['browse'=>$browse],$id);
  161. }
  162. }