Spec.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 cores\exception\BaseException;
  15. use app\common\library\helper;
  16. use app\store\model\SpecValue as SpecValueModel;
  17. /**
  18. * 规格/属性(组)模型
  19. * Class Spec
  20. * @package app\common\model
  21. */
  22. class Spec extends BaseModel
  23. {
  24. // 定义表名
  25. protected $name = 'spec';
  26. // 定义主键
  27. protected $pk = 'spec_id';
  28. protected $updateTime = false;
  29. /**
  30. * 验证规格值是否合法
  31. * @param array $specList
  32. * @throws BaseException
  33. */
  34. public static function checkSpecData(array $specList)
  35. {
  36. $specNames = helper::getArrayColumn($specList, 'spec_name');
  37. if (count($specList) != count(array_unique($specNames))) {
  38. throwError('很抱歉,不能存在重复的规格组');
  39. }
  40. foreach ($specList as $item) {
  41. $values = helper::getArrayColumn($item['valueList'], 'spec_value');
  42. if (count($item['valueList']) != count(array_unique($values))) {
  43. throwError('很抱歉,不能存在重复的规格值');
  44. }
  45. }
  46. }
  47. /**
  48. * 根据规格值计算能生成的SKU总量
  49. * @param array $specList
  50. * @return int
  51. */
  52. public static function calcSkuListTotal(array $specList): int
  53. {
  54. $total = 1;
  55. foreach ($specList as $item) {
  56. $total *= \count($item['valueList']);
  57. }
  58. return $total;
  59. }
  60. /**
  61. * 规格组写入数据库并生成id
  62. * 此时的$specList是用户端传来的
  63. * @param array $specList
  64. * @param int|null $storeId
  65. * @return array
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public static function getNewSpecList(array $specList, int $storeId = null): array
  71. {
  72. // 规格组名称合集
  73. $names = helper::getArrayColumn($specList, 'spec_name');
  74. // 获取到已存在的规格组
  75. $alreadyData = static::getListByNames($names, $storeId);
  76. // 遍历整理新的规格集
  77. foreach ($specList as $key => &$item) {
  78. $alreadyItem = helper::getArrayItemByColumn($alreadyData, 'spec_name', $item['spec_name']);
  79. if (!empty($alreadyItem)) {
  80. // 规格名已存在的记录spec_id
  81. $item['spec_id'] = $alreadyItem['spec_id'];
  82. } else {
  83. // 规格名不存在的新增记录
  84. $result = static::add($item, $storeId);
  85. $item['spec_id'] = $result['spec_id'];
  86. }
  87. // 规格值写入数据库并生成id
  88. $item['valueList'] = SpecValueModel::getNewValueList((int)$item['spec_id'], $item['valueList'], $storeId);
  89. }
  90. return $specList;
  91. }
  92. /**
  93. * 根据规格组名称集获取列表
  94. * @param array $names
  95. * @param int|null $storeId
  96. * @return \think\Collection
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. private static function getListByNames(array $names, int $storeId = null): \think\Collection
  102. {
  103. return (new static)->where('spec_name', 'in', $names)
  104. ->where('store_id', '=', $storeId)
  105. ->select();
  106. }
  107. /**
  108. * 新增规格组记录
  109. * @param array $item
  110. * @param int|null $storeId
  111. * @return Spec|\think\Model
  112. */
  113. private static function add(array $item, int $storeId = null)
  114. {
  115. return self::create([
  116. 'spec_name' => $item['spec_name'],
  117. 'store_id' => $storeId ?: self::$storeId
  118. ]);
  119. }
  120. }