StoreCombination.php 8.0 KB

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