Goods.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 app\common\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. */
  64. public function add(array $data)
  65. {
  66. // 创建商品数据
  67. $data = $this->createData($data);
  68. // 事务处理
  69. $this->transaction(function () use ($data) {
  70. // 添加商品
  71. $this->save($data);
  72. // 新增商品与分类关联
  73. GoodsCategoryRelModel::increased((int)$this['goods_id'], $data['categoryIds']);
  74. // 新增商品与图片关联
  75. GoodsImageModel::increased((int)$this['goods_id'], $data['imagesIds']);
  76. // 新增商品与规格关联
  77. GoodsSpecRelModel::increased((int)$this['goods_id'], $data['newSpecList']);
  78. // 新增商品sku信息
  79. GoodsSkuModel::add((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  80. // 新增服务与承诺关联
  81. GoodsServiceRelModel::increased((int)$this['goods_id'], $data['serviceIds']);
  82. });
  83. return true;
  84. }
  85. /**
  86. * 创建商品数据
  87. * @param array $data
  88. * @return array
  89. */
  90. private function createData(array $data)
  91. {
  92. // 默认数据
  93. $data = array_merge($data, [
  94. 'content' => $data['content'] ?? '',
  95. 'newSpecList' => [],
  96. 'newSkuList' => [],
  97. 'store_id' => self::$storeId,
  98. ]);
  99. // 库存总量 stock_total
  100. // 商品价格 最低最高
  101. if ($data['spec_type'] == SpecTypeEnum::MULTI) {
  102. $data['stock_total'] = GoodsSkuModel::getStockTotal($data['specData']['skuList']);
  103. list($data['goods_price_min'], $data['goods_price_max']) = GoodsSkuModel::getGoodsPrices($data['specData']['skuList']);
  104. list($data['line_price_min'], $data['line_price_max']) = GoodsSkuModel::getLinePrices($data['specData']['skuList']);
  105. } elseif ($data['spec_type'] == SpecTypeEnum::SINGLE) {
  106. $data['goods_price_min'] = $data['goods_price_max'] = $data['goods_price'];
  107. $data['line_price_min'] = $data['line_price_max'] = $data['line_price'];
  108. $data['stock_total'] = $data['stock_num'];
  109. }
  110. // 规格和sku数据处理
  111. if ($data['spec_type'] == SpecTypeEnum::MULTI) {
  112. // 生成多规格数据(携带id)
  113. $data['newSpecList'] = SpecModel::getNewSpecList($data['specData']['specList']);
  114. // 生成skuList ( 携带goods_sku_id )
  115. $data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $data['specData']['skuList']);
  116. } elseif ($data['spec_type'] == SpecTypeEnum::SINGLE) {
  117. // 生成skuItem
  118. $data['newSkuList'] = helper::pick($data, ['goods_price', 'line_price', 'stock_num', 'goods_weight']);
  119. }
  120. // 单独设置折扣的配置
  121. $data['is_enable_grade'] == 0 && $data['is_alone_grade'] = 0;
  122. $aloneGradeEquity = [];
  123. if ($data['is_alone_grade'] == 1) {
  124. foreach ($data['alone_grade_equity'] as $key => $value) {
  125. $gradeId = str_replace('grade_id:', '', $key);
  126. $aloneGradeEquity[$gradeId] = $value;
  127. }
  128. }
  129. $data['alone_grade_equity'] = $aloneGradeEquity;
  130. return $data;
  131. }
  132. /**
  133. * 编辑商品
  134. * @param $data
  135. * @return bool|mixed
  136. */
  137. public function edit(array $data)
  138. {
  139. // 创建商品数据
  140. $data = $this->createData($data);
  141. // 事务处理
  142. $this->transaction(function () use ($data) {
  143. // 更新商品
  144. $this->save($data);
  145. // 更新商品与分类关联
  146. GoodsCategoryRelModel::updates((int)$this['goods_id'], $data['categoryIds']);
  147. // 更新商品与图片关联
  148. GoodsImageModel::updates((int)$this['goods_id'], $data['imagesIds']);
  149. // 更新商品与规格关联
  150. GoodsSpecRelModel::updates((int)$this['goods_id'], $data['newSpecList']);
  151. // 更新商品sku信息
  152. GoodsSkuModel::edit((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  153. // 更新服务与承诺关联
  154. GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']);
  155. });
  156. return true;
  157. }
  158. /**
  159. * 修改商品状态
  160. * @param array $goodsIds 商品id集
  161. * @param bool $state 为true表示上架
  162. * @return false|int
  163. */
  164. /**
  165. * @param array $goodsIds
  166. * @param bool $state
  167. * @return bool
  168. */
  169. public function setStatus(array $goodsIds, bool $state)
  170. {
  171. // 批量更新记录
  172. return static::updateBase(['status' => $state ? 10 : 20], [['goods_id', 'in', $goodsIds]]);
  173. }
  174. /**
  175. * 软删除
  176. * @param array $goodsIds
  177. * @return bool
  178. */
  179. public function setDelete(array $goodsIds)
  180. {
  181. foreach ($goodsIds as $goodsId) {
  182. if (!GoodsService::checkIsAllowDelete($goodsId)) {
  183. $this->error = '当前商品正在参与其他活动,不允许删除';
  184. return false;
  185. }
  186. }
  187. // 批量更新记录
  188. return static::updateBase(['is_delete' => 1], [['goods_id', 'in', $goodsIds]]);
  189. }
  190. // 获取已售罄的商品
  191. public function getSoldoutGoodsTotal()
  192. {
  193. $filter = [
  194. ['stock_total', '=', 0],
  195. ['status', '=', GoodsStatusEnum::ON_SALE]
  196. ];
  197. return $this->getGoodsTotal($filter);
  198. }
  199. /**
  200. * 获取当前商品总数
  201. * @param array $where
  202. * @return int
  203. */
  204. public function getGoodsTotal($where = [])
  205. {
  206. return $this->where($where)->where('is_delete', '=', 0)->count();
  207. }
  208. }