123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- /**
- *
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/12/18
- */
- namespace app\models\store;
- use crmeb\basic\BaseModel;
- use crmeb\services\GroupDataService;
- use app\admin\model\store\StoreProductAttrValue;
- use app\models\store\StoreProduct;
- use crmeb\traits\ModelTrait;
- /**
- * TODO 秒杀产品Model
- * Class StoreSeckill
- * @package app\models\store
- */
- class StoreExchange extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'store_exchange';
- use ModelTrait;
- protected function getImagesAttr($value)
- {
- return json_decode($value, true) ?: [];
- }
- public function getDescriptionAttr($value)
- {
- return htmlspecialchars_decode($value);
- }
- public static function getSeckillCount()
- {
- return self::where('is_del', 0)->where('status', 1)->count();
- }
- /**
- * 获取秒杀列表
- * @param $time
- * @param int $page
- * @param int $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function seckillList($page = 0, $limit = 20)
- {
- // var_dump($page);
- // var_dump($limit);
- 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();
- 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();
- if ($list) return $list->hidden(['cost', 'add_time', 'is_del'])->toArray();
- return [];
- }
- /**
- * 获取所有秒杀产品
- * @param string $field
- * @return array
- */
- public static function getListAll($offset = 0, $limit = 10, $field = 'id,product_id,image,title,price,ot_price,stock,sales')
- {
- $model = self::where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
- ->order('sort DESC,add_time DESC');
- $model = $model->limit($offset, $limit);
- $list = $model->select();
- // var_dump(self::getLastSql());
- if ($list) return $list->toArray();
- else return [];
- }
- /**
- * 获取热门推荐的秒杀产品
- * @param int $limit
- * @param string $field
- * @return array
- */
- public static function getHotList($limit = 0, $field = 'id,product_id,image,title,price,ot_price,start_time,stop_time,stock,integral')
- {
- $model = self::where('is_hot', 1)->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)->field($field)
- ->order('sort DESC,add_time DESC');
- if ($limit) $model->limit($limit);
- $list = $model->select();
- if ($list) return $list->toArray();
- else return [];
- }
- /**
- * 获取一条秒杀产品
- * @param $id
- * @param string $field
- * @return array|false|\PDOStatement|string|\think\Model
- */
- public static function getValidProduct($id, $field = '*')
- {
- $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();
- if ($info['id']) {
- return $info;
- } else {
- return [];
- }
- }
- /**
- * 获取秒杀是否有开启
- * @return bool
- */
- public static function getSeckillContStatus()
- {
- $count = self::where('is_del', 0)->where('status', 1)->count();
- return $count ? true : false;
- }
- public static function initFailSeckill()
- {
- self::where('is_hot', 1)->where('is_del', 0)->where('status', '<>', 1)->update(['status' => '-1']);
- }
- public static function idBySimilaritySeckill($id, $limit = 4, $field = '*')
- {
- $list = [];
- $productId = self::where('id', $id)->value('product_id');
- if ($productId) {
- $list = array_merge($list, self::where('product_id', $productId)->where('id', '<>', $id)
- ->where('is_del', 0)->where('status', 1)->where('stock', '>', 0)
- ->field($field)
- ->order('sort DESC,add_time DESC')->limit($limit)->select()->toArray());
- }
- $limit = $limit - count($list);
- if ($limit) {
- $list = array_merge($list, self::getHotList($limit, $field));
- }
- return $list;
- }
- /** 获取秒杀产品库存
- * @param $id
- * @return mixed
- */
- public static function getProductStock($id)
- {
- return self::where('id', $id)->value('stock');
- }
- /**
- * 获取字段值
- * @param $id
- * @param string $field
- * @return mixed
- */
- public static function getProductField($id, $field = 'title')
- {
- return self::where('id', $id)->value($field);
- }
- /**
- * 修改秒杀库存
- * @param int $num
- * @param int $seckillId
- * @return bool
- */
- public static function decIntegralStock($num = 0, $seckillId = 0, $unique = '')
- {
- $product_id = self::where('id', $seckillId)->value('product_id');
- if ($unique) {
- $res = false !== StoreProductAttrValue::decProductAttrStock($seckillId, $unique, $num, 5);
- $res = $res && self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
- $sku = StoreProductAttrValue::where('product_id', $seckillId)->where('unique', $unique)->where('type', 5)->value('suk');
- $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
- } else {
- $res = false !== self::where('id', $seckillId)->dec('stock', $num)->inc('sales', $num)->update();
- }
- $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
- return $res;
- }
- /**
- * 增加库存较少销量
- * @param int $num
- * @param int $seckillId
- * @return bool
- */
- public static function incSeckillStock($num = 0, $seckillId = 0)
- {
- $seckill = self::where('id', $seckillId)->field(['stock', 'sales'])->find();
- if (!$seckill) return true;
- if ($seckill->sales > 0) $seckill->sales = bcsub($seckill->sales, $num, 0);
- if ($seckill->sales < 0) $seckill->sales = 0;
- $seckill->stock = bcadd($seckill->stock, $num, 0);
- return $seckill->save();
- }
- public static function createOrder($order)
- {
- $res = true;
- $carts = StoreOrderCartInfo::where('oid', $order['id'])->where('cart_id', 'in', $order['cart_id'])
- ->select();
- foreach ($carts as $cart) {
- $info = $cart['cart_info'];
- for ($i = 0; $i < $cart['cart_info']['cart_num']; $i++) {
- $res = $res && StoreExchangeOrder::create([
- 'order_id' => $order['order_id'],
- 'uid' => $order['uid'],
- 'pay_price' => isset($info['productInfo']['attrInfo']) ? $info['productInfo']['attrInfo']['price'] : $info['productInfo']['price'],
- 'add_time' => time(),
- 'status' => 0,
- 'verify_code' => self::createCode(),
- 'store_id' => $order['store_id'],
- 'deposit' => isset($info['productInfo']['attrInfo']) ? $info['productInfo']['attrInfo']['deposit'] : $info['productInfo']['deposit'],
- 'exchange_id' => $order['exchange_id'],
- 'cart_id' => $cart['cart_id'],
- 'oid' => $order['id'],
- ]);
- }
- }
- StoreOrder::where('id', $order['id'])->update(['status' => 2, 'deposit' => 0]);
- return $res;
- }
- public static function createCode()
- {
- do {
- $code = 'ticket:' . substr(md5(time() . rand(1000, 9999)), 8);
- } while (StoreExchangeOrder::be(['verify_code' => $code]));
- return $code;
- }
- }
|