StoreCombination.php 8.5 KB

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