StoreCombination.php 8.6 KB

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