StoreExchange.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. use crmeb\traits\ModelTrait;
  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. use ModelTrait;
  31. protected function getImagesAttr($value)
  32. {
  33. return json_decode($value, true) ?: [];
  34. }
  35. public function getDescriptionAttr($value)
  36. {
  37. return htmlspecialchars_decode($value);
  38. }
  39. public static function getSeckillCount()
  40. {
  41. return self::where('is_del', 0)->where('status', 1)->count();
  42. }
  43. /**
  44. * 获取秒杀列表
  45. * @param $time
  46. * @param int $page
  47. * @param int $limit
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public static function seckillList($page = 0, $limit = 20)
  54. {
  55. // var_dump($page);
  56. // var_dump($limit);
  57. 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();
  58. 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();
  59. if ($list) return $list->hidden(['cost', 'add_time', 'is_del'])->toArray();
  60. return [];
  61. }
  62. /**
  63. * 获取所有秒杀产品
  64. * @param string $field
  65. * @return array
  66. */
  67. public static function getListAll($offset = 0, $limit = 10, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,sales')
  68. {
  69. $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  70. ->order('sort DESC,add_time DESC');
  71. $model = $model->limit($offset, $limit);
  72. $list = $model->select();
  73. if ($list) return $list->toArray();
  74. else return [];
  75. }
  76. /**
  77. * 获取热门推荐的秒杀产品
  78. * @param int $limit
  79. * @param string $field
  80. * @return array
  81. */
  82. public static function getHotList($limit = 0, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,integral')
  83. {
  84. $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
  85. ->order('sort DESC,add_time DESC');
  86. if ($limit) $model->limit($limit);
  87. $list = $model->select();
  88. if ($list) return $list->toArray();
  89. else return [];
  90. }
  91. /**
  92. * 获取一条秒杀产品
  93. * @param $id
  94. * @param string $field
  95. * @return array|false|\PDOStatement|string|\think\Model
  96. */
  97. public static function getValidProduct($id, $field = '*')
  98. {
  99. $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();
  100. if ($info['id']) {
  101. return $info;
  102. } else {
  103. return [];
  104. }
  105. }
  106. /**
  107. * 获取秒杀是否有开启
  108. * @return bool
  109. */
  110. public static function getSeckillContStatus()
  111. {
  112. $count = self::where('is_del', 0)->where('status', 1)->count();
  113. return $count ? true : false;
  114. }
  115. public static function initFailSeckill()
  116. {
  117. self::where('is_hot', 1)->where('is_del', 0)->where('status', '<>', 1)->update(['status' => '-1']);
  118. }
  119. public static function idBySimilaritySeckill($id, $limit = 4, $field = '*')
  120. {
  121. $list = [];
  122. $productId = self::where('id', $id)->value('product_id');
  123. if ($productId) {
  124. $list = array_merge($list, self::where('product_id', $productId)->where('id', '<>', $id)
  125. ->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)
  126. ->field($field)
  127. ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
  128. }
  129. $limit = $limit - count($list);
  130. if ($limit) {
  131. $list = array_merge($list, self::getHotList($limit, $field));
  132. }
  133. return $list;
  134. }
  135. /** 获取秒杀产品库存
  136. * @param $id
  137. * @return mixed
  138. */
  139. public static function getProductStock($id)
  140. {
  141. return self::where('id', $id)->value('stock');
  142. }
  143. /**
  144. * 获取字段值
  145. * @param $id
  146. * @param string $field
  147. * @return mixed
  148. */
  149. public static function getProductField($id, $field = 'title')
  150. {
  151. return self::where('id', $id)->value($field);
  152. }
  153. /**
  154. * 修改秒杀库存
  155. * @param int $num
  156. * @param int $seckillId
  157. * @return bool
  158. */
  159. public static function decIntegralStock($num = 0, $seckillId = 0, $unique = '')
  160. {
  161. $product_id = self::where('id', $seckillId)->value('product_id');
  162. if ($unique) {
  163. $res = false !== StoreProductAttrValue::decProductAttrStock($seckillId, $unique, $num, 5);
  164. $res = $res && self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  165. $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 5)->value('suk');
  166. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  167. } else {
  168. $res = false !== self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
  169. }
  170. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  171. return $res;
  172. }
  173. /**
  174. * 增加库存较少销量
  175. * @param int $num
  176. * @param int $seckillId
  177. * @return bool
  178. */
  179. public static function incSeckillStock($num = 0, $seckillId = 0)
  180. {
  181. $seckill = self::where('id', $seckillId)->field(['stock', 'sales'])->find();
  182. if (!$seckill) return true;
  183. if ($seckill->sales > 0) $seckill->sales = bcsub($seckill->sales, $num, 0);
  184. if ($seckill->sales < 0) $seckill->sales = 0;
  185. $seckill->stock = bcadd($seckill->stock, $num, 0);
  186. return $seckill->save();
  187. }
  188. public static function createOrder($order)
  189. {
  190. $res = true;
  191. $carts = StoreOrderCartInfo::where('oid', $order['id'])->where('cart_id', 'in', $order['cart_id'])
  192. ->select();
  193. foreach ($carts as $cart) {
  194. $info = $cart['cart_info'];
  195. for ($i = 0; $i < $cart['cart_info']['cart_num']; $i++) {
  196. $res = $res && StoreExchangeOrder::create([
  197. 'order_id' => $order['order_id'],
  198. 'uid' => $order['uid'],
  199. 'pay_price' => isset($info['productInfo']['attrInfo']) ? $info['productInfo']['attrInfo']['price'] : $info['productInfo']['price'],
  200. 'add_time' => time(),
  201. 'status' => 0,
  202. 'verify_code' => self::createCode(),
  203. 'store_id' => $order['store_id'],
  204. 'deposit' => isset($info['productInfo']['attrInfo']) ? $info['productInfo']['attrInfo']['deposit'] : $info['productInfo']['deposit'],
  205. 'exchange_id' => $order['exchange_id'],
  206. 'cart_id' => $cart['cart_id'],
  207. 'oid' => $order['id'],
  208. ]);
  209. }
  210. }
  211. StoreOrder::where('id', $order['id'])->update(['status' => 2, 'deposit' => 0]);
  212. return $res;
  213. }
  214. public static function createCode()
  215. {
  216. do {
  217. $code = 'ticket:' . substr(md5(time() . rand(1000, 9999)), 8);
  218. } while (StoreExchangeOrder::be(['verify_code' => $code]));
  219. return $code;
  220. }
  221. }