Goods.php 5.4 KB

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