Goods.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model;
  13. use app\common\library\helper;
  14. use app\store\model\Spec as SpecModel;
  15. use app\common\model\Goods as GoodsModel;
  16. use app\store\model\GoodsSku as GoodsSkuModel;
  17. use app\store\model\GoodsImage as GoodsImageModel;
  18. use app\store\model\GoodsSpecRel as GoodsSpecRelModel;
  19. use app\store\model\goods\ServiceRel as GoodsServiceRelModel;
  20. use app\store\model\GoodsCategoryRel as GoodsCategoryRelModel;
  21. use app\store\service\Goods as GoodsService;
  22. use app\common\enum\goods\SpecType as GoodsSpecTypeEnum;
  23. use app\common\enum\goods\Status as GoodsStatusEnum;
  24. use cores\exception\BaseException;
  25. /**
  26. * 商品模型
  27. * Class Goods
  28. * @package app\store\model
  29. */
  30. class Goods extends GoodsModel
  31. {
  32. /**
  33. * 获取商品详情
  34. * @param int $goodsId
  35. * @return mixed
  36. * @throws BaseException
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getDetail(int $goodsId)
  42. {
  43. // 获取商品基础信息
  44. $goodsInfo = $this->getBasic($goodsId);
  45. // 分类ID集
  46. $goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']);
  47. // 商品多规格属性列表
  48. if ($goodsInfo['spec_type'] == GoodsSpecTypeEnum::MULTI) {
  49. $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
  50. }
  51. // 服务与承诺
  52. $goodsInfo['serviceIds'] = GoodsServiceRelModel::getServiceIds($goodsInfo['goods_id']);
  53. // 商品规格是否锁定(锁定状态下不允许编辑规格)
  54. $goodsInfo['isSpecLocked'] = GoodsService::checkSpecLocked($goodsId);
  55. // 返回商品详细信息
  56. return $goodsInfo;
  57. }
  58. /**
  59. * 获取商品基础信息
  60. * @param int $goodsId
  61. * @return mixed
  62. * @throws BaseException
  63. */
  64. public function getBasic(int $goodsId)
  65. {
  66. // 关联查询
  67. $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
  68. // 获取商品记录
  69. $goodsInfo = static::detail($goodsId, $with);
  70. empty($goodsInfo) && throwError('很抱歉,商品信息不存在');
  71. // 整理商品数据并返回
  72. return parent::setGoodsData($goodsInfo);
  73. }
  74. /**
  75. * 添加商品
  76. * @param array $data
  77. * @return bool
  78. * @throws \cores\exception\BaseException
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function add(array $data): bool
  84. {
  85. // 创建商品数据
  86. $data = $this->createData($data);
  87. // 事务处理
  88. $this->transaction(function () use ($data) {
  89. // 添加商品
  90. $this->save($data);
  91. // 新增商品与分类关联
  92. GoodsCategoryRelModel::increased((int)$this['goods_id'], $data['categoryIds']);
  93. // 新增商品与图片关联
  94. GoodsImageModel::increased((int)$this['goods_id'], $data['imagesIds']);
  95. // 新增商品与规格关联
  96. GoodsSpecRelModel::increased((int)$this['goods_id'], $data['newSpecList']);
  97. // 新增商品sku信息
  98. GoodsSkuModel::add((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  99. // 新增服务与承诺关联
  100. GoodsServiceRelModel::increased((int)$this['goods_id'], $data['serviceIds']);
  101. });
  102. return true;
  103. }
  104. /**
  105. * 编辑商品
  106. * @param array $data
  107. * @return bool
  108. * @throws \cores\exception\BaseException
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. public function edit(array $data): bool
  114. {
  115. // 创建商品数据
  116. $data = $this->createData($data);
  117. // 事务处理
  118. $this->transaction(function () use ($data) {
  119. // 更新商品
  120. $this->save($data);
  121. // 更新商品与分类关联
  122. GoodsCategoryRelModel::updates((int)$this['goods_id'], $data['categoryIds']);
  123. // 更新商品与图片关联
  124. GoodsImageModel::updates((int)$this['goods_id'], $data['imagesIds']);
  125. // 更新商品与规格关联
  126. GoodsSpecRelModel::updates((int)$this['goods_id'], $data['newSpecList']);
  127. // 更新商品sku信息
  128. GoodsSkuModel::edit((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  129. // 更新服务与承诺关联
  130. GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']);
  131. });
  132. return true;
  133. }
  134. /**
  135. * 修改商品状态
  136. * @param array $goodsIds 商品id集
  137. * @param bool $state 为true表示上架
  138. * @return bool|false
  139. */
  140. public function setStatus(array $goodsIds, bool $state): bool
  141. {
  142. // 批量更新记录
  143. return static::updateBase(['status' => $state ? 10 : 20], [['goods_id', 'in', $goodsIds]]);
  144. }
  145. /**
  146. * 软删除
  147. * @param array $goodsIds
  148. * @return bool
  149. */
  150. public function setDelete(array $goodsIds): bool
  151. {
  152. foreach ($goodsIds as $goodsId) {
  153. if (!GoodsService::checkIsAllowDelete($goodsId)) {
  154. $this->error = '当前商品正在参与其他活动,不允许删除';
  155. return false;
  156. }
  157. }
  158. // 批量更新记录
  159. return static::updateBase(['is_delete' => 1], [['goods_id', 'in', $goodsIds]]);
  160. }
  161. // 获取已售罄的商品
  162. public function getSoldoutGoodsTotal(): int
  163. {
  164. $filter = [
  165. ['stock_total', '=', 0],
  166. ['status', '=', GoodsStatusEnum::ON_SALE]
  167. ];
  168. return $this->getGoodsTotal($filter);
  169. }
  170. /**
  171. * 获取当前商品总数
  172. * @param array $where
  173. * @return int
  174. */
  175. public function getGoodsTotal(array $where = []): int
  176. {
  177. return $this->where($where)->where('is_delete', '=', 0)->count();
  178. }
  179. /**
  180. * 创建商品数据
  181. * @param array $data
  182. * @return array
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. * @throws \cores\exception\BaseException
  187. */
  188. private function createData(array $data): array
  189. {
  190. // 默认数据
  191. $data = array_merge($data, [
  192. 'line_price' => $data['line_price'] ?? 0,
  193. 'content' => $data['content'] ?? '',
  194. 'newSpecList' => [],
  195. 'newSkuList' => [],
  196. 'store_id' => self::$storeId,
  197. ]);
  198. // 整理商品的价格和库存总量
  199. if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
  200. $data['stock_total'] = GoodsSkuModel::getStockTotal($data['specData']['skuList']);
  201. [$data['goods_price_min'], $data['goods_price_max']] = GoodsSkuModel::getGoodsPrices($data['specData']['skuList']);
  202. [$data['line_price_min'], $data['line_price_max']] = GoodsSkuModel::getLinePrices($data['specData']['skuList']);
  203. } elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) {
  204. $data['goods_price_min'] = $data['goods_price_max'] = $data['goods_price'];
  205. $data['line_price_min'] = $data['line_price_max'] = $data['line_price'];
  206. $data['stock_total'] = $data['stock_num'];
  207. }
  208. // 规格和sku数据处理
  209. if ($data['spec_type'] == GoodsSpecTypeEnum::MULTI) {
  210. // 验证规格值是否合法
  211. SpecModel::checkSpecData($data['specData']['specList']);
  212. // 生成多规格数据 (携带id)
  213. $data['newSpecList'] = SpecModel::getNewSpecList($data['specData']['specList']);
  214. // 生成skuList (携带goods_sku_id)
  215. $data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $data['specData']['skuList']);
  216. } elseif ($data['spec_type'] == GoodsSpecTypeEnum::SINGLE) {
  217. // 生成skuItem
  218. $data['newSkuList'] = helper::pick($data, ['goods_price', 'line_price', 'stock_num', 'goods_weight']);
  219. }
  220. // 单独设置折扣的配置
  221. $data['is_enable_grade'] == 0 && $data['is_alone_grade'] = 0;
  222. $aloneGradeEquity = [];
  223. if ($data['is_alone_grade'] == 1) {
  224. foreach ($data['alone_grade_equity'] as $key => $value) {
  225. $gradeId = str_replace('grade_id:', '', $key);
  226. $aloneGradeEquity[$gradeId] = $value;
  227. }
  228. }
  229. $data['alone_grade_equity'] = $aloneGradeEquity;
  230. return $data;
  231. }
  232. }