StoreCombination.php 6.7 KB

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