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