SpecValue.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 app\common\library\helper;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\model\relation\BelongsTo;
  19. /**
  20. * 规格/属性(值)模型
  21. * Class SpecValue
  22. * @package app\common\model
  23. */
  24. class SpecValue extends BaseModel
  25. {
  26. // 定义表名
  27. protected $name = 'spec_value';
  28. // 定义主键
  29. protected $pk = 'spec_value_id';
  30. protected $updateTime = false;
  31. /**
  32. * 关联规格组表
  33. * @return BelongsTo
  34. */
  35. public function spec(): BelongsTo
  36. {
  37. return $this->belongsTo('Spec');
  38. }
  39. /**
  40. * 规格值写入数据库并生成id
  41. * @param int $specId
  42. * @param array $valueList
  43. * @param int|null $storeId
  44. * @return array
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. */
  49. public static function getNewValueList(int $specId, array $valueList, int $storeId = null): array
  50. {
  51. // 规格组名称合集
  52. $values = helper::getArrayColumn($valueList, 'spec_value');
  53. // 获取到已存在的规格值
  54. $alreadyData = static::getListByValues($specId, $values, $storeId);
  55. // 遍历整理新的规格集
  56. foreach ($valueList as $key => &$item) {
  57. $alreadyItem = helper::getArrayItemByColumn($alreadyData, 'spec_value', $item['spec_value']);
  58. if (!empty($alreadyItem)) {
  59. // 规格值已存在的记录spec_value_id
  60. $item['spec_value_id'] = $alreadyItem['spec_value_id'];
  61. } else {
  62. // 规格值不存在的新增记录
  63. $result = self::add($specId, $item, $storeId);
  64. $item['spec_value_id'] = $result['spec_value_id'];
  65. }
  66. }
  67. return $valueList;
  68. }
  69. /**
  70. * 根据规格组名称集获取列表
  71. * @param int $specId
  72. * @param array $values
  73. * @param int|null $storeId
  74. * @return \think\Collection
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. private static function getListByValues(int $specId, array $values, int $storeId = null): \think\Collection
  80. {
  81. return (new static)->where('spec_id', '=', $specId)
  82. ->where('spec_value', 'in', $values)
  83. ->where('store_id', '=', $storeId)
  84. ->select();
  85. }
  86. /**
  87. * 新增规格值记录
  88. * @param int $specId
  89. * @param array $item
  90. * @param int|null $storeId
  91. * @return SpecValue|\think\Model
  92. */
  93. private static function add(int $specId, array $item, int $storeId = null)
  94. {
  95. return self::create([
  96. 'spec_value' => $item['spec_value'],
  97. 'spec_id' => $specId,
  98. 'store_id' => $storeId ?: self::$storeId
  99. ]);
  100. }
  101. }