Goods.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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['stock_total'] = $data['stock_num'];
  108. }
  109. // 规格和sku数据处理
  110. if ($data['spec_type'] == SpecTypeEnum::MULTI) {
  111. // 生成多规格数据(携带id)
  112. $data['newSpecList'] = SpecModel::getNewSpecList($data['specData']['specList']);
  113. // 生成skuList ( 携带goods_sku_id )
  114. $data['newSkuList'] = GoodsSkuModel::getNewSkuList($data['newSpecList'], $data['specData']['skuList']);
  115. } elseif ($data['spec_type'] == SpecTypeEnum::SINGLE) {
  116. // 生成skuItem
  117. $data['newSkuList'] = helper::pick($data, ['goods_price', 'line_price', 'stock_num', 'goods_weight']);
  118. }
  119. // 单独设置折扣的配置
  120. $data['is_enable_grade'] == 0 && $data['is_alone_grade'] = 0;
  121. $aloneGradeEquity = [];
  122. if ($data['is_alone_grade'] == 1) {
  123. foreach ($data['alone_grade_equity'] as $key => $value) {
  124. $gradeId = str_replace('grade_id:', '', $key);
  125. $aloneGradeEquity[$gradeId] = $value;
  126. }
  127. }
  128. $data['alone_grade_equity'] = $aloneGradeEquity;
  129. return $data;
  130. }
  131. /**
  132. * 编辑商品
  133. * @param $data
  134. * @return bool|mixed
  135. */
  136. public function edit(array $data)
  137. {
  138. // 创建商品数据
  139. $data = $this->createData($data);
  140. // 事务处理
  141. $this->transaction(function () use ($data) {
  142. // 更新商品
  143. $this->save($data);
  144. // 更新商品与分类关联
  145. GoodsCategoryRelModel::updates((int)$this['goods_id'], $data['categoryIds']);
  146. // 更新商品与图片关联
  147. GoodsImageModel::updates((int)$this['goods_id'], $data['imagesIds']);
  148. // 更新商品与规格关联
  149. GoodsSpecRelModel::updates((int)$this['goods_id'], $data['newSpecList']);
  150. // 更新商品sku信息
  151. GoodsSkuModel::edit((int)$this['goods_id'], $data['spec_type'], $data['newSkuList']);
  152. // 更新服务与承诺关联
  153. GoodsServiceRelModel::updates((int)$this['goods_id'], $data['serviceIds']);
  154. });
  155. return true;
  156. }
  157. /**
  158. * 修改商品状态
  159. * @param array $goodsIds 商品id集
  160. * @param bool $state 为true表示上架
  161. * @return false|int
  162. */
  163. /**
  164. * @param array $goodsIds
  165. * @param bool $state
  166. * @return bool
  167. */
  168. public function setStatus(array $goodsIds, bool $state)
  169. {
  170. // 批量更新记录
  171. return static::updateBase(['status' => $state ? 10 : 20], [['goods_id', 'in', $goodsIds]]);
  172. }
  173. /**
  174. * 软删除
  175. * @param array $goodsIds
  176. * @return bool
  177. */
  178. public function setDelete(array $goodsIds)
  179. {
  180. foreach ($goodsIds as $goodsId) {
  181. if (!GoodsService::checkIsAllowDelete($goodsId)) {
  182. $this->error = '当前商品正在参与其他活动,不允许删除';
  183. return false;
  184. }
  185. }
  186. // 批量更新记录
  187. return static::updateBase(['is_delete' => 1], [['goods_id', 'in', $goodsIds]]);
  188. }
  189. // 获取已售罄的商品
  190. public function getSoldoutGoodsTotal()
  191. {
  192. $filter = [
  193. ['stock_total', '=', 0],
  194. ['status', '=', GoodsStatusEnum::ON_SALE]
  195. ];
  196. return $this->getGoodsTotal($filter);
  197. }
  198. /**
  199. * 获取当前商品总数
  200. * @param array $where
  201. * @return int
  202. */
  203. public function getGoodsTotal($where = [])
  204. {
  205. return $this->where($where)->where('is_delete', '=', 0)->count();
  206. }
  207. }