StoreExchange.php 8.3 KB

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