StoreCombination.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\routine\model\store;
  8. use traits\ModelTrait;
  9. use basic\ModelBasic;
  10. /**
  11. * 拼团model
  12. * Class StoreCombination
  13. * @package app\routine\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');
  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. * @return mixed
  102. */
  103. public static function getCombinationHost($limit = 0){
  104. $model = new self();
  105. $model = $model->alias('c');
  106. $model = $model->join('StoreProduct s','s.id=c.product_id');
  107. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  108. $model = $model->where('c.is_del',0);
  109. $model = $model->where('c.is_host',1);
  110. $model = $model->where('c.start_time','LT',time());
  111. $model = $model->where('c.stop_time','GT',time());
  112. if($limit) $model = $model->limit($limit);
  113. $list = $model->select();
  114. if($list) return $list->toArray();
  115. else return [];
  116. }
  117. /**
  118. * 修改销量和库存
  119. * @param $num
  120. * @param $CombinationId
  121. * @return bool
  122. */
  123. public static function decCombinationStock($num,$CombinationId)
  124. {
  125. $res = false !== self::where('id',$CombinationId)->dec('stock',$num)->inc('sales',$num)->update();
  126. return $res;
  127. }
  128. /**
  129. * 判断库存是否足够
  130. * @param $id
  131. * @param $cart_num
  132. * @return int|mixed
  133. */
  134. public static function getCombinationStock($id,$cart_num){
  135. $stock = self::where('id',$id)->value('stock');
  136. return $stock > $cart_num ? $stock : 0;
  137. }
  138. /**
  139. * 获取产品状态
  140. * @param $id
  141. * @return mixed
  142. */
  143. public static function isValidCombination($id){
  144. $model = new self();
  145. $model = $model->where('id',$id);
  146. $model = $model->where('is_del',0);
  147. $model = $model->where('is_show',1);
  148. return $model->count();
  149. }
  150. /**
  151. * TODO 判断是否可以出售
  152. * @param $id
  153. * @param int $cartNum
  154. * @return int|string
  155. * @throws \think\Exception
  156. */
  157. public static function isValidCartCombination($id,$cartNum = 1){
  158. return self::where('id',$id)->where('is_del',0)->where('is_show',1)->where('stock','>',$cartNum)->count();
  159. }
  160. }