123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace app\store\model\sharp;
- use app\common\model\sharp\Goods as GoodsModel;
- use app\store\model\sharp\GoodsSku as GoodsSkuModel;
- use app\store\model\sharp\ActiveGoods as ActiveGoodsModel;
- use app\store\service\Goods as GoodsService;
- /**
- * 整点秒杀-商品模型
- * Class Goods
- * @package app\store\model\sharp
- */
- class Goods extends GoodsModel
- {
- /**
- * 获取列表数据
- * @param string $search
- * @return mixed|\think\Paginator
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getList($search = '')
- {
- $this->setBaseQuery($this->alias, [
- ['goods', 'goods_id'],
- ]);
- // 检索查询条件
- if (!empty($search)) {
- $this->where('goods.goods_name', 'like', "%{$search}%");
- }
- // 获取活动列表
- $list = $this->where("{$this->alias}.is_delete", '=', 0)
- ->order(["{$this->alias}.sort" => 'asc', "{$this->alias}.create_time" => 'desc'])
- ->paginate(15, false, [
- 'query' => \request()->request()
- ]);
- // 设置商品数据
- return $this->setGoodsListData($list, true);
- }
- /**
- * 根据商品id集获取商品列表
- * @param array $goodsIds
- * @param array $param
- * @return false|\PDOStatement|string|\think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function getListByIds($goodsIds, $param = [])
- {
- // 获取商品列表数据
- $list = parent::getListByIds($goodsIds, $param);
- // 整理列表数据并返回
- return $this->setGoodsListData($list, true);
- }
- /**
- * 添加商品
- * @param $goods
- * @param $data
- * @return bool
- * @throws \Exception
- */
- public function add($goods, $data)
- {
- // 添加商品
- $this->allowField(true)->save(array_merge($data, [
- 'goods_id' => $goods['goods_id'],
- 'seckill_stock' => $this->getSeckillStock($goods, $data),
- 'wxapp_id' => self::$wxapp_id,
- ]));
- // 商品规格
- $this->addGoodsSpec($goods, $data);
- return true;
- }
- /**
- * 编辑商品
- * @param $goods
- * @param $data
- * @return bool
- * @throws \Exception
- */
- public function edit($goods, $data)
- {
- // 更新商品
- $this->allowField(true)->save(array_merge($data, [
- 'seckill_stock' => $this->getSeckillStock($goods, $data),
- ]));
- // 商品规格
- $this->addGoodsSpec($goods, $data, true);
- return true;
- }
- /**
- * 获取总库存数量
- * @param $goods
- * @param $data
- * @return int
- */
- private function getSeckillStock($goods, $data)
- {
- if ($goods['spec_type'] == '10') {
- return $data['sku']['seckill_stock'];
- }
- $seckillStock = 0;
- foreach ($data['spec_many']['spec_list'] as $item) {
- $seckillStock += $item['form']['seckill_stock'];
- }
- return $seckillStock;
- }
- /**
- * 验证商品ID能否被添加
- * @param $goodsId
- * @return bool
- */
- public function validateGoodsId($goodsId)
- {
- if ($goodsId <= 0) {
- $this->error = '很抱歉,您还没有选择商品';
- return false;
- }
- // 验证是否存在秒杀商品
- if ($this->isExistGoodsId($goodsId)) {
- $this->error = '很抱歉,该商品已存在,无需重复添加';
- return false;
- }
- return true;
- }
- /**
- * 添加商品规格
- * @param $goods
- * @param $data
- * @param bool $isUpdate
- * @return array|false|\think\Model
- * @throws \Exception
- */
- private function addGoodsSpec($goods, $data, $isUpdate = false)
- {
- // 更新模式: 先删除所有规格
- $model = new GoodsSkuModel;
- $isUpdate && $model->removeAll($this['sharp_goods_id']);
- // 添加sku (单规格)
- if ($goods['spec_type'] == '10') {
- return $this->sku()->save(array_merge($data['sku'], [
- 'wxapp_id' => self::$wxapp_id,
- ]));
- }
- // 添加sku (多规格)
- return $model->addSkuList($this['sharp_goods_id'], $data['spec_many']['spec_list']);
- }
- /**
- * 商品ID是否存在
- * @param $goodsId
- * @return bool
- */
- public static function isExistGoodsId($goodsId)
- {
- return !!(new static)->where('goods_id', '=', $goodsId)
- ->where('is_delete', '=', 0)
- ->value('sharp_goods_id');
- }
- /**
- * 软删除
- * @return false|int
- */
- public function setDelete()
- {
- // 同步删除活动会场与商品关联记录
- $model = new ActiveGoodsModel;
- $model->onDeleteSharpGoods($this['sharp_goods_id']);
- return $this->allowField(true)->save(['is_delete' => 1]);
- }
- }
|