StoreExchange.php 5.6 KB

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