StoreIntegral.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/18
  6. */
  7. namespace app\models\store;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\services\GroupDataService;
  10. use app\admin\model\store\StoreProductAttrValue;
  11. use app\models\store\StoreProduct;
  12. /**
  13. * TODO 秒杀产品Model
  14. * Class StoreSeckill
  15. * @package app\models\store
  16. */
  17. class StoreIntegral extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'store_integral';
  29. protected function getImagesAttr($value)
  30. {
  31. return json_decode($value, true) ?: [];
  32. }
  33. public function getDescriptionAttr($value)
  34. {
  35. return htmlspecialchars_decode($value);
  36. }
  37. public static function getSeckillCount()
  38. {
  39. return self::where('is_del', 0)->where('status', 1)->count();
  40. }
  41. /**
  42. * 获取秒杀列表
  43. * @param $time
  44. * @param int $page
  45. * @param int $limit
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public static function seckillList($page = 0, $limit = 20)
  52. {
  53. // var_dump($page);
  54. // var_dump($limit);
  55. if ($page) $list = self::alias('n')->join('store_product c', 'c.id=n.product_id')->where('c.is_show', 1)->where('c.is_del', 0)->where('n.is_del', 0)->where('n.status', 1)->field('n.*')->order('n.sort desc')->page($page, $limit)->select();
  56. else $list = self::alias('n')->join('store_product c', 'c.id=n.product_id')->where('c.is_show', 1)->where('c.is_del', 0)->where('n.is_del', 0)->where('n.status', 1)->field('n.*')->order('sort desc')->select();
  57. if ($list) return $list->hidden(['cost', 'add_time', 'is_del'])->toArray();
  58. return [];
  59. }
  60. /**
  61. * 获取所有秒杀产品
  62. * @param string $field
  63. * @return array
  64. */
  65. public static function getListAll($offset = 0, $limit = 10, $field = 'id,product_id,image,title,price,integral,ot_price,start_time,stop_time,stock,sales')
  66. {
  67. $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  68. ->order('sort DESC,add_time DESC');
  69. $model = $model->limit($offset, $limit);
  70. $list = $model->select();
  71. if ($list) return $list->toArray();
  72. else return [];
  73. }
  74. /**
  75. * 获取热门推荐的秒杀产品
  76. * @param int $limit
  77. * @param string $field
  78. * @return array
  79. */
  80. public static function getHotList($limit = 0, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,integral')
  81. {
  82. $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  83. ->order('sort DESC,add_time DESC');
  84. if ($limit) $model->limit($limit);
  85. $list = $model->select();
  86. if ($list) return $list->toArray();
  87. else return [];
  88. }
  89. /**
  90. * 获取一条秒杀产品
  91. * @param $id
  92. * @param string $field
  93. * @return array|false|\PDOStatement|string|\think\Model
  94. */
  95. public static function getValidProduct($id, $field = '*')
  96. {
  97. $info = self::alias('n')->join('store_product c', 'c.id=n.product_id')->where('n.id', $id)->where('c.is_show', 1)->where('c.is_del', 0)->where('n.is_del', 0)->where('n.status', 1)->field('n.*,SUM(c.sales+c.ficti) as total')->find();
  98. if ($info['id']) {
  99. return $info;
  100. } else {
  101. return [];
  102. }
  103. }
  104. /**
  105. * 获取秒杀是否有开启
  106. * @return bool
  107. */
  108. public static function getSeckillContStatus()
  109. {
  110. $count = self::where('is_del', 0)->where('status', 1)->count();
  111. return $count ? true : false;
  112. }
  113. public static function initFailSeckill()
  114. {
  115. self::where('is_hot', 1)->where('is_del', 0)->where('status', '<>', 1)->update(['status' => '-1']);
  116. }
  117. public static function idBySimilaritySeckill($id, $limit = 4, $field = '*')
  118. {
  119. $list = [];
  120. $productId = self::where('id', $id)->value('product_id');
  121. if ($productId) {
  122. $list = array_merge($list, self::where('product_id', $productId)->where('id', '<>', $id)
  123. ->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)
  124. ->field($field)
  125. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  126. }
  127. $limit = $limit - count($list);
  128. if ($limit) {
  129. $list = array_merge($list, self::getHotList($limit, $field));
  130. }
  131. return $list;
  132. }
  133. /** 获取秒杀产品库存
  134. * @param $id
  135. * @return mixed
  136. */
  137. public static function getProductStock($id)
  138. {
  139. return self::where('id', $id)->value('stock');
  140. }
  141. /**
  142. * 获取字段值
  143. * @param $id
  144. * @param string $field
  145. * @return mixed
  146. */
  147. public static function getProductField($id, $field = 'title')
  148. {
  149. return self::where('id', $id)->value($field);
  150. }
  151. /**
  152. * 修改秒杀库存
  153. * @param int $num
  154. * @param int $seckillId
  155. * @return bool
  156. */
  157. public static function decIntegralStock($num = 0, $seckillId = 0, $unique = '')
  158. {
  159. $product_id = self::where('id', $seckillId)->value('product_id');
  160. if ($unique) {
  161. $res = false !== StoreProductAttrValue::decProductAttrStock($seckillId, $unique, $num, 4);
  162. $res = $res && self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  163. $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 4)->value('suk');
  164. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  165. } else {
  166. $res = false !== self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  167. }
  168. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  169. return $res;
  170. }
  171. /**
  172. * 增加库存较少销量
  173. * @param int $num
  174. * @param int $seckillId
  175. * @return bool
  176. */
  177. public static function incSeckillStock($num = 0, $seckillId = 0)
  178. {
  179. $seckill = self::where('id', $seckillId)->field(['stock', 'sales'])->find();
  180. if (!$seckill) return true;
  181. if ($seckill->sales > 0) $seckill->sales = bcsub($seckill->sales, $num, 0);
  182. if ($seckill->sales < 0) $seckill->sales = 0;
  183. $seckill->stock = bcadd($seckill->stock, $num, 0);
  184. return $seckill->save();
  185. }
  186. }