Goods.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. namespace app\common\model\sharing;
  3. use app\common\model\BaseModel;
  4. use app\common\library\helper;
  5. /**
  6. * 拼团商品模型
  7. * Class Goods
  8. * @package app\common\model\sharing
  9. */
  10. class Goods extends BaseModel
  11. {
  12. protected $name = 'sharing_goods';
  13. protected $append = ['goods_sales'];
  14. /**
  15. * 计算显示销量 (初始销量 + 实际销量)
  16. * @param $value
  17. * @param $data
  18. * @return mixed
  19. */
  20. public function getGoodsSalesAttr($value, $data)
  21. {
  22. return $data['sales_initial'] + $data['sales_actual'];
  23. }
  24. /**
  25. * 获取器:单独设置折扣的配置
  26. * @param $json
  27. * @return mixed
  28. */
  29. public function getAloneGradeEquityAttr($json)
  30. {
  31. return json_decode($json, true);
  32. }
  33. /**
  34. * 修改器:单独设置折扣的配置
  35. * @param $data
  36. * @return mixed
  37. */
  38. public function setAloneGradeEquityAttr($data)
  39. {
  40. return json_encode($data);
  41. }
  42. /**
  43. * 关联商品分类表
  44. * @return \think\model\relation\BelongsTo
  45. */
  46. public function category()
  47. {
  48. return $this->belongsTo('Category');
  49. }
  50. /**
  51. * 关联商品规格表
  52. * @return \think\model\relation\HasMany
  53. */
  54. public function sku()
  55. {
  56. return $this->hasMany('GoodsSku', 'goods_id')->order(['goods_sku_id' => 'asc']);
  57. }
  58. /**
  59. * 关联商品规格关系表
  60. * @return \think\model\relation\BelongsToMany
  61. */
  62. public function specRel()
  63. {
  64. $module = self::getCalledModule() ?: 'common';
  65. return $this->belongsToMany(
  66. "app\\{$module}\\model\\SpecValue",
  67. 'SharingGoodsSpecRel',
  68. 'spec_value_id',
  69. 'goods_id'
  70. )->order(['id' => 'asc']);
  71. }
  72. /**
  73. * 关联商品图片表
  74. * @return \think\model\relation\HasMany
  75. */
  76. public function image()
  77. {
  78. $module = self::getCalledModule() ?: 'common';
  79. return $this->hasMany("app\\{$module}\\model\\sharing\\GoodsImage", 'goods_id')
  80. ->order(['id' => 'asc']);
  81. }
  82. /**
  83. * 关联运费模板表
  84. * @return \think\model\relation\BelongsTo
  85. */
  86. public function delivery()
  87. {
  88. $module = self::getCalledModule() ?: 'common';
  89. return $this->BelongsTo("app\\{$module}\\model\\Delivery");
  90. }
  91. /**
  92. * 关联订单评价表
  93. * @return \think\model\relation\HasMany
  94. */
  95. public function commentData()
  96. {
  97. return $this->hasMany('Comment', 'goods_id');
  98. }
  99. /**
  100. * 计费方式
  101. * @param $value
  102. * @return mixed
  103. */
  104. public function getGoodsStatusAttr($value)
  105. {
  106. $status = [10 => '上架', 20 => '下架'];
  107. return ['text' => $status[$value], 'value' => $value];
  108. }
  109. /**
  110. * 获取商品列表
  111. * @param $param
  112. * @return mixed
  113. * @throws \think\exception\DbException
  114. */
  115. public function getList($param)
  116. {
  117. // 商品列表获取条件
  118. $params = array_merge([
  119. 'status' => 10, // 商品状态
  120. 'category_id' => 0, // 分类id
  121. 'search' => '', // 搜索关键词
  122. 'sortType' => 'all', // 排序类型
  123. 'sortPrice' => false, // 价格排序 高低
  124. 'listRows' => 15, // 每页数量
  125. ], $param);
  126. // 筛选条件
  127. $filter = [];
  128. $params['category_id'] > 0 && $filter['category_id'] = ['IN', Category::getSubCategoryId($params['category_id'])];
  129. $params['status'] > 0 && $filter['goods_status'] = $params['status'];
  130. !empty($params['search']) && $filter['goods_name'] = ['like', '%' . trim($params['search']) . '%'];
  131. // 排序规则
  132. $sort = [];
  133. if ($params['sortType'] === 'all') {
  134. $sort = ['goods_sort', 'goods_id' => 'desc'];
  135. } elseif ($params['sortType'] === 'sales') {
  136. $sort = ['goods_sales' => 'desc'];
  137. } elseif ($params['sortType'] === 'price') {
  138. $sort = $params['sortPrice'] ? ['goods_max_price' => 'desc'] : ['goods_min_price'];
  139. }
  140. // 商品表名称
  141. $tableName = $this->getTable();
  142. // 多规格商品 最高价与最低价
  143. $GoodsSku = new GoodsSku;
  144. $minPriceSql = $GoodsSku->field(['MIN(sharing_price)'])
  145. ->where('goods_id', 'EXP', "= `$tableName`.`goods_id`")->buildSql();
  146. $maxPriceSql = $GoodsSku->field(['MAX(sharing_price)'])
  147. ->where('goods_id', 'EXP', "= `$tableName`.`goods_id`")->buildSql();
  148. // 执行查询
  149. $list = $this
  150. ->field(['*', '(sales_initial + sales_actual) as goods_sales',
  151. "$minPriceSql AS goods_min_price",
  152. "$maxPriceSql AS goods_max_price"
  153. ])
  154. ->with(['category', 'image.file', 'sku'])
  155. ->where('is_delete', '=', 0)
  156. ->where($filter)
  157. ->order($sort)
  158. ->paginate($params['listRows'], false, [
  159. 'query' => \request()->request()
  160. ]);
  161. // 整理列表数据并返回
  162. return $this->setGoodsListData($list, true);
  163. }
  164. /**
  165. * 设置商品展示的数据
  166. * @param $data
  167. * @param bool $isMultiple
  168. * @param callable $callback
  169. * @return mixed
  170. */
  171. protected function setGoodsListData(&$data, $isMultiple = true, callable $callback = null)
  172. {
  173. if (!$isMultiple) $dataSource = [&$data]; else $dataSource = &$data;
  174. // 整理商品列表数据
  175. foreach ($dataSource as &$goods) {
  176. // 商品默认规格
  177. $goodsSku = $goods['sku'][0];
  178. // 商品默认数据
  179. $goods['goods_image'] = $goods['image'][0]['file_path'];
  180. $goods['goods_sku'] = $goodsSku;
  181. // 回调函数
  182. is_callable($callback) && call_user_func($callback, $goods);
  183. }
  184. return $data;
  185. }
  186. /**
  187. * 根据商品id集获取商品列表
  188. * @param array $goodsIds
  189. * @param null $status
  190. * @return false|\PDOStatement|string|\think\Collection
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. * @throws \think\exception\DbException
  194. */
  195. public function getListByIds($goodsIds, $status = null)
  196. {
  197. // 筛选条件
  198. $filter = ['goods_id' => ['in', $goodsIds]];
  199. $status > 0 && $filter['goods_status'] = $status;
  200. if (!empty($goodsIds)) {
  201. $this->orderRaw('field(goods_id, ' . implode(',', $goodsIds) . ')');
  202. }
  203. // 获取商品列表数据
  204. $data = $this->with(['category', 'image.file', 'sku', 'spec_rel.spec', 'delivery.rule'])
  205. ->where($filter)
  206. ->select();
  207. if ($data->isEmpty()) return $data;
  208. // 格式化数据
  209. foreach ($data as &$item) {
  210. $item['goods_image'] = $item['image'][0]['file_path'];
  211. }
  212. return $data;
  213. }
  214. /**
  215. * 商品多规格信息
  216. * @param \think\Collection $spec_rel
  217. * @param \think\Collection $skuData
  218. * @return array
  219. */
  220. public function getManySpecData($spec_rel, $skuData)
  221. {
  222. // spec_attr
  223. $specAttrData = [];
  224. foreach ($spec_rel->toArray() as $item) {
  225. if (!isset($specAttrData[$item['spec_id']])) {
  226. $specAttrData[$item['spec_id']] = [
  227. 'group_id' => $item['spec']['spec_id'],
  228. 'group_name' => $item['spec']['spec_name'],
  229. 'spec_items' => [],
  230. ];
  231. }
  232. $specAttrData[$item['spec_id']]['spec_items'][] = [
  233. 'item_id' => $item['spec_value_id'],
  234. 'spec_value' => $item['spec_value'],
  235. ];
  236. }
  237. // spec_list
  238. $specListData = [];
  239. foreach ($skuData->toArray() as $item) {
  240. $image = (isset($item['image']) && !empty($item['image'])) ? $item['image'] : ['file_id' => 0, 'file_path' => ''];
  241. $specListData[] = [
  242. 'goods_sku_id' => $item['goods_sku_id'],
  243. 'spec_sku_id' => $item['spec_sku_id'],
  244. 'rows' => [],
  245. 'form' => [
  246. 'image_id' => $image['file_id'],
  247. 'image_path' => $image['file_path'],
  248. 'goods_no' => $item['goods_no'],
  249. 'goods_price' => $item['goods_price'],
  250. 'sharing_price' => $item['sharing_price'],
  251. 'goods_weight' => $item['goods_weight'],
  252. 'line_price' => $item['line_price'],
  253. 'stock_num' => $item['stock_num'],
  254. ],
  255. ];
  256. }
  257. return ['spec_attr' => array_values($specAttrData), 'spec_list' => $specListData];
  258. }
  259. /**
  260. * 多规格表格数据
  261. * @param $goods
  262. * @return array
  263. */
  264. public function getManySpecTable(&$goods)
  265. {
  266. $specData = $this->getManySpecData($goods['spec_rel'], $goods['sku']);
  267. $totalRow = count($specData['spec_list']);
  268. foreach ($specData['spec_list'] as $i => &$sku) {
  269. $rowData = [];
  270. $rowCount = 1;
  271. foreach ($specData['spec_attr'] as $attr) {
  272. $skuValues = $attr['spec_items'];
  273. $rowCount *= count($skuValues);
  274. $anInterBankNum = ($totalRow / $rowCount);
  275. $point = (($i / $anInterBankNum) % count($skuValues));
  276. if (0 === ($i % $anInterBankNum)) {
  277. $rowData[] = [
  278. 'rowspan' => $anInterBankNum,
  279. 'item_id' => $skuValues[$point]['item_id'],
  280. 'spec_value' => $skuValues[$point]['spec_value']
  281. ];
  282. }
  283. }
  284. $sku['rows'] = $rowData;
  285. }
  286. return $specData;
  287. }
  288. /**
  289. * 获取商品详情
  290. * @param $goodsId
  291. * @return static
  292. */
  293. public static function detail($goodsId)
  294. {
  295. /* @var $model self */
  296. $model = (new static)->with([
  297. 'category',
  298. 'image.file',
  299. 'sku.image',
  300. 'spec_rel.spec',
  301. ])->where('goods_id', '=', $goodsId)
  302. ->find();
  303. // 整理列表数据并返回
  304. return $model->setGoodsListData($model, false);
  305. }
  306. /**
  307. * 指定的商品规格信息
  308. * @param static $goods 商品详情
  309. * @param int $specSkuId
  310. * @return array|bool
  311. */
  312. public static function getGoodsSku($goods, $specSkuId)
  313. {
  314. // 获取指定的sku
  315. $goodsSku = [];
  316. foreach ($goods['sku'] as $item) {
  317. if ($item['spec_sku_id'] == $specSkuId) {
  318. $goodsSku = $item;
  319. break;
  320. }
  321. }
  322. if (empty($goodsSku)) {
  323. return false;
  324. }
  325. // 多规格文字内容
  326. $goodsSku['goods_attr'] = '';
  327. if ($goods['spec_type'] == 20) {
  328. $specRelData = helper::arrayColumn2Key($goods['spec_rel'], 'spec_value_id');
  329. $attrs = explode('_', $goodsSku['spec_sku_id']);
  330. foreach ($attrs as $specValueId) {
  331. $goodsSku['goods_attr'] .= $specRelData[$specValueId]['spec']['spec_name'] . ':'
  332. . $specRelData[$specValueId]['spec_value'] . '; ';
  333. }
  334. }
  335. return $goodsSku;
  336. }
  337. /**
  338. * 更新商品库存销量
  339. * @param $goodsList
  340. * @throws \Exception
  341. */
  342. public function updateStockSales($goodsList)
  343. {
  344. // 整理批量更新商品销量
  345. $goodsSave = [];
  346. // 批量更新商品规格:sku销量、库存
  347. $goodsSpecSave = [];
  348. foreach ($goodsList as $goods) {
  349. $goodsSave[] = [
  350. 'goods_id' => $goods['goods_id'],
  351. 'sales_actual' => ['inc', $goods['total_num']]
  352. ];
  353. // 付款减库存
  354. if ($goods['deduct_stock_type'] == 20) {
  355. $goodsSpecSave[] = [
  356. 'data' => ['stock_num' => ['dec', $goods['total_num']]],
  357. 'where' => [
  358. 'goods_id' => $goods['goods_id'],
  359. 'spec_sku_id' => $goods['spec_sku_id'],
  360. ],
  361. ];
  362. }
  363. }
  364. // 更新商品总销量
  365. $this->allowField(true)->isUpdate()->saveAll($goodsSave);
  366. // 更新商品规格库存
  367. !empty($goodsSpecSave) && (new GoodsSku)->updateAll($goodsSpecSave);
  368. }
  369. }