GoodsSpecRel.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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\common\model;
  13. use cores\BaseModel;
  14. use think\model\relation\BelongsTo;
  15. /**
  16. * 商品规格关系模型
  17. * Class GoodsSpecRel
  18. * @package app\common\model
  19. */
  20. class GoodsSpecRel extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'goods_spec_rel';
  24. // 定义主键
  25. protected $pk = 'id';
  26. protected $updateTime = false;
  27. /**
  28. * 关联规格组
  29. * @return BelongsTo
  30. */
  31. public function spec(): BelongsTo
  32. {
  33. return $this->belongsTo('Spec');
  34. }
  35. /**
  36. * 关联规格值
  37. * @return BelongsTo
  38. */
  39. public function specValue(): BelongsTo
  40. {
  41. return $this->belongsTo('SpecValue');
  42. }
  43. /**
  44. * 指定商品的规格列表
  45. * @param int $goodsId 商品ID
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public static function getSpecList(int $goodsId): array
  52. {
  53. // 获取指定商品的规格值关系记录
  54. $data = static::getList($goodsId);
  55. // 规格组
  56. $groupData = [];
  57. foreach ($data as $groupKey => $item) {
  58. $groupData[$item['spec_id']] = [
  59. 'spec_id' => $item['spec']['spec_id'],
  60. 'spec_name' => $item['spec']['spec_name']
  61. ];
  62. }
  63. // 去除索引
  64. $specList = array_values($groupData);
  65. // 规格值
  66. foreach ($specList as $groupKey => &$group) {
  67. $group['key'] = $groupKey;
  68. foreach ($data as $valueKey => $item) {
  69. ($item['spec_id'] == $group['spec_id']) && $group['valueList'][] = [
  70. 'key' => isset($group['valueList']) ? count($group['valueList']) : 0,
  71. 'groupKey' => $groupKey,
  72. 'spec_value_id' => $item['specValue']['spec_value_id'],
  73. 'spec_value' => $item['specValue']['spec_value']
  74. ];
  75. }
  76. }
  77. return $specList;
  78. }
  79. /**
  80. * 获取指定商品的规格值关系记录
  81. * @param int $goodsId 商品ID
  82. * @return iterable|\think\model\Collection|\think\Paginator
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. private static function getList(int $goodsId)
  88. {
  89. return (new static)->with(['spec', 'specValue'])
  90. ->where('goods_id', '=', $goodsId)
  91. ->select();
  92. }
  93. /**
  94. * 批量写入商品与规格值关系记录
  95. * @param int $goodsId 商品ID
  96. * @param array $specList 规格数据
  97. * @param int|null $storeId 商城ID
  98. * @return array|false
  99. */
  100. public static function increased(int $goodsId, array $specList, int $storeId = null)
  101. {
  102. $dataset = [];
  103. foreach ($specList as $item) {
  104. foreach ($item['valueList'] as $specValueItem) {
  105. $dataset[] = [
  106. 'goods_id' => $goodsId,
  107. 'spec_id' => $item['spec_id'],
  108. 'spec_value_id' => $specValueItem['spec_value_id'],
  109. 'store_id' => $storeId ?: self::$storeId
  110. ];
  111. }
  112. }
  113. return (new static)->addAll($dataset);
  114. }
  115. }