Goods.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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 SpecTypeEnum;
  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. $with = ['images' => ['file'], 'skuList' => ['image']];
  45. // 获取商品记录
  46. $goodsInfo = static::detail($goodsId, $with);
  47. empty($goodsInfo) && throwError('很抱歉,商品信息不存');
  48. // 整理商品数据并返回
  49. $goodsInfo = parent::setGoodsData($goodsInfo);
  50. // 分类ID集
  51. $goodsInfo['categoryIds'] = GoodsCategoryRelModel::getCategoryIds($goodsInfo['goods_id']);
  52. // 商品规格列表
  53. $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
  54. // 服务与承诺
  55. $goodsInfo['serviceIds'] = GoodsServiceRelModel::getServiceIds($goodsInfo['goods_id']);
  56. // 商品基本信息
  57. return $goodsInfo;
  58. }
  59. /**
  60. * 添加商品
  61. * @param array $data
  62. * @return bool
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function add(array $data)
  68. {
  69. // 创建商品数据
  70. $data = $this->createData($data);
  71. // 事务处理
  72. $this->transaction(function () use ($data) {
  73. // 添加商品
  74. $this->save($data);
  75. // 新增商品与分类关联
  76. GoodsCategoryRelModel::increased((int)$this['goods_id'], $data['categoryIds']);
  77. // 新增商品与图片关联
  78. GoodsImageModel::increased((int)$this['goods_id'], $data['imagesIds']);
  79. // 新增商品与规格关联
  80. GoodsSpecRelModel::increased((int)$this['goods_id'], $data['newSpecList']);
  81. // 新增商品sku信息
  82. GoodsSkuModel::add((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  83. // 新增服务与承诺关联
  84. GoodsServiceRelModel::increased((int)$this['goods_id'], $data['serviceIds']);
  85. });
  86. return true;
  87. }
  88. /**
  89. * 编辑商品
  90. * @param array $data
  91. * @return bool
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public function edit(array $data): bool
  97. {
  98. // 创建商品数据
  99. $data = $this->createData($data);
  100. // 事务处理
  101. $this->transaction(function () use ($data) {
  102. // 更新商品
  103. $this->save($data);
  104. // 更新商品与分类关联
  105. GoodsCategoryRelModel::updates((int)$this['goods_id'], $data['categoryIds']);
  106. // 更新商品与图片关联
  107. GoodsImageModel::updates((int)$this['goods_id'], $data['imagesIds']);
  108. // 更新商品与规格关联
  109. GoodsSpecRelModel::updates((int)$this['goods_id'], $data['newSpecList']);
  110. // 更新商品sku信息
  111. GoodsSkuModel::edit((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  112. // 更新服务与承诺关联
  113. GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']);
  114. });
  115. return true;
  116. }
  117. /**
  118. * 修改商品状态
  119. * @param array $goodsIds 商品id集
  120. * @param bool $state 为true表示上架
  121. * @return bool|false
  122. */
  123. public function setStatus(array $goodsIds, bool $state): bool
  124. {
  125. // 批量更新记录
  126. return static::updateBase(['status' => $state ? 10 : 20], [['goods_id', 'in', $goodsIds]]);
  127. }
  128. /**
  129. * 软删除
  130. * @param array $goodsIds
  131. * @return bool
  132. */
  133. public function setDelete(array $goodsIds): bool
  134. {
  135. foreach ($goodsIds as $goodsId) {
  136. if (!GoodsService::checkIsAllowDelete($goodsId)) {
  137. $this->error = '当前商品正在参与其他活动,不允许删除';
  138. return false;
  139. }
  140. }
  141. // 批量更新记录
  142. return static::updateBase(['is_delete' => 1], [['goods_id', 'in', $goodsIds]]);
  143. }
  144. // 获取已售罄的商品
  145. public function getSoldoutGoodsTotal(): int
  146. {
  147. $filter = [
  148. ['stock_total', '=', 0],
  149. ['status', '=', GoodsStatusEnum::ON_SALE]
  150. ];
  151. return $this->getGoodsTotal($filter);
  152. }
  153. /**
  154. * 获取当前商品总数
  155. * @param array $where
  156. * @return int
  157. */
  158. public function getGoodsTotal(array $where = []): int
  159. {
  160. return $this->where($where)->where('is_delete', '=', 0)->count();
  161. }
  162. /**
  163. * 创建商品数据
  164. * @param array $data
  165. * @return array
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. * @throws BaseException
  170. */
  171. private function createData(array $data): array
  172. {
  173. // 默认数据
  174. $data = array_merge($data, [
  175. 'line_price' => $data['line_price'] ?? 0,
  176. 'content' => $data['content'] ?? '',
  177. 'newSpecList' => [],
  178. 'newSkuList' => [],
  179. 'store_id' => self::$storeId,
  180. ]);
  181. // 库存总量 stock_total
  182. // 商品价格 最低最高
  183. if ($data['spec_type'] == SpecTypeEnum::MULTI) {
  184. $data['stock_total'] = GoodsSkuModel::getStockTotal($data['specData']['skuList']);
  185. list($data['goods_price_min'], $data['goods_price_max']) = GoodsSkuModel::getGoodsPrices($data['specData']['skuList']);
  186. list($data['line_price_min'], $data['line_price_max']) = GoodsSkuModel::getLinePrices($data['specData']['skuList']);
  187. } elseif ($data['spec_type'] == SpecTypeEnum::SINGLE) {
  188. $data['goods_price_min'] = $data['goods_price_max'] = $data['goods_price'];
  189. $data['line_price_min'] = $data['line_price_max'] = $data['line_price'];
  190. $data['stock_total'] = $data['stock_num'];
  191. }
  192. // 规格和sku数据处理
  193. if ($data['spec_type'] == SpecTypeEnum::MULTI) {
  194. // 验证规格值是否合法
  195. SpecModel::checkSpecData($data['specData']['specList']);
  196. // 生成多规格数据 (携带id)
  197. $data['newSpecList'] = SpecModel::getNewSpecList($data['specData']['specList']);
  198. // 生成skuList (携带goods_sku_id)
  199. $data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $data['specData']['skuList']);
  200. } elseif ($data['spec_type'] == SpecTypeEnum::SINGLE) {
  201. // 生成skuItem
  202. $data['newSkuList'] = helper::pick($data, ['goods_price', 'line_price', 'stock_num', 'goods_weight']);
  203. }
  204. // 单独设置折扣的配置
  205. $data['is_enable_grade'] == 0 && $data['is_alone_grade'] = 0;
  206. $aloneGradeEquity = [];
  207. if ($data['is_alone_grade'] == 1) {
  208. foreach ($data['alone_grade_equity'] as $key => $value) {
  209. $gradeId = str_replace('grade_id:', '', $key);
  210. $aloneGradeEquity[$gradeId] = $value;
  211. }
  212. }
  213. $data['alone_grade_equity'] = $aloneGradeEquity;
  214. return $data;
  215. }
  216. }