Goods.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\store\model\sharp;
  3. use app\common\model\sharp\Goods as GoodsModel;
  4. use app\store\model\sharp\GoodsSku as GoodsSkuModel;
  5. use app\store\model\sharp\ActiveGoods as ActiveGoodsModel;
  6. use app\store\service\Goods as GoodsService;
  7. /**
  8. * 整点秒杀-商品模型
  9. * Class Goods
  10. * @package app\store\model\sharp
  11. */
  12. class Goods extends GoodsModel
  13. {
  14. /**
  15. * 获取列表数据
  16. * @param string $search
  17. * @return mixed|\think\Paginator
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\ModelNotFoundException
  20. * @throws \think\exception\DbException
  21. */
  22. public function getList($search = '')
  23. {
  24. $this->setBaseQuery($this->alias, [
  25. ['goods', 'goods_id'],
  26. ]);
  27. // 检索查询条件
  28. if (!empty($search)) {
  29. $this->where('goods.goods_name', 'like', "%{$search}%");
  30. }
  31. // 获取活动列表
  32. $list = $this->where("{$this->alias}.is_delete", '=', 0)
  33. ->order(["{$this->alias}.sort" => 'asc', "{$this->alias}.create_time" => 'desc'])
  34. ->paginate(15, false, [
  35. 'query' => \request()->request()
  36. ]);
  37. // 设置商品数据
  38. return $this->setGoodsListData($list, true);
  39. }
  40. /**
  41. * 根据商品id集获取商品列表
  42. * @param array $goodsIds
  43. * @param array $param
  44. * @return false|\PDOStatement|string|\think\Collection
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\ModelNotFoundException
  47. * @throws \think\exception\DbException
  48. */
  49. public function getListByIds($goodsIds, $param = [])
  50. {
  51. // 获取商品列表数据
  52. $list = parent::getListByIds($goodsIds, $param);
  53. // 整理列表数据并返回
  54. return $this->setGoodsListData($list, true);
  55. }
  56. /**
  57. * 添加商品
  58. * @param $goods
  59. * @param $data
  60. * @return bool
  61. * @throws \Exception
  62. */
  63. public function add($goods, $data)
  64. {
  65. // 添加商品
  66. $this->allowField(true)->save(array_merge($data, [
  67. 'goods_id' => $goods['goods_id'],
  68. 'seckill_stock' => $this->getSeckillStock($goods, $data),
  69. 'wxapp_id' => self::$wxapp_id,
  70. ]));
  71. // 商品规格
  72. $this->addGoodsSpec($goods, $data);
  73. return true;
  74. }
  75. /**
  76. * 编辑商品
  77. * @param $goods
  78. * @param $data
  79. * @return bool
  80. * @throws \Exception
  81. */
  82. public function edit($goods, $data)
  83. {
  84. // 更新商品
  85. $this->allowField(true)->save(array_merge($data, [
  86. 'seckill_stock' => $this->getSeckillStock($goods, $data),
  87. ]));
  88. // 商品规格
  89. $this->addGoodsSpec($goods, $data, true);
  90. return true;
  91. }
  92. /**
  93. * 获取总库存数量
  94. * @param $goods
  95. * @param $data
  96. * @return int
  97. */
  98. private function getSeckillStock($goods, $data)
  99. {
  100. if ($goods['spec_type'] == '10') {
  101. return $data['sku']['seckill_stock'];
  102. }
  103. $seckillStock = 0;
  104. foreach ($data['spec_many']['spec_list'] as $item) {
  105. $seckillStock += $item['form']['seckill_stock'];
  106. }
  107. return $seckillStock;
  108. }
  109. /**
  110. * 验证商品ID能否被添加
  111. * @param $goodsId
  112. * @return bool
  113. */
  114. public function validateGoodsId($goodsId)
  115. {
  116. if ($goodsId <= 0) {
  117. $this->error = '很抱歉,您还没有选择商品';
  118. return false;
  119. }
  120. // 验证是否存在秒杀商品
  121. if ($this->isExistGoodsId($goodsId)) {
  122. $this->error = '很抱歉,该商品已存在,无需重复添加';
  123. return false;
  124. }
  125. return true;
  126. }
  127. /**
  128. * 添加商品规格
  129. * @param $goods
  130. * @param $data
  131. * @param bool $isUpdate
  132. * @return array|false|\think\Model
  133. * @throws \Exception
  134. */
  135. private function addGoodsSpec($goods, $data, $isUpdate = false)
  136. {
  137. // 更新模式: 先删除所有规格
  138. $model = new GoodsSkuModel;
  139. $isUpdate && $model->removeAll($this['sharp_goods_id']);
  140. // 添加sku (单规格)
  141. if ($goods['spec_type'] == '10') {
  142. return $this->sku()->save(array_merge($data['sku'], [
  143. 'wxapp_id' => self::$wxapp_id,
  144. ]));
  145. }
  146. // 添加sku (多规格)
  147. return $model->addSkuList($this['sharp_goods_id'], $data['spec_many']['spec_list']);
  148. }
  149. /**
  150. * 商品ID是否存在
  151. * @param $goodsId
  152. * @return bool
  153. */
  154. public static function isExistGoodsId($goodsId)
  155. {
  156. return !!(new static)->where('goods_id', '=', $goodsId)
  157. ->where('is_delete', '=', 0)
  158. ->value('sharp_goods_id');
  159. }
  160. /**
  161. * 软删除
  162. * @return false|int
  163. */
  164. public function setDelete()
  165. {
  166. // 同步删除活动会场与商品关联记录
  167. $model = new ActiveGoodsModel;
  168. $model->onDeleteSharpGoods($this['sharp_goods_id']);
  169. return $this->allowField(true)->save(['is_delete' => 1]);
  170. }
  171. }