Spec.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\common\model\Spec as SpecModel;
  15. use app\store\model\SpecValue as SpecValueModel;
  16. use cores\exception\BaseException;
  17. /**
  18. * 规格组模型
  19. * Class Spec
  20. * @package app\store\model
  21. */
  22. class Spec extends SpecModel
  23. {
  24. /**
  25. * 验证规格值是否合法
  26. * @param array $specList
  27. * @throws BaseException
  28. */
  29. public static function checkSpecData(array $specList)
  30. {
  31. foreach ($specList as $item) {
  32. $values = helper::getArrayColumn($item['valueList'], 'spec_value');
  33. if (count($item['valueList']) != count(array_unique($values))) {
  34. throwError('很抱歉,不能存在重复的规格值 请检查您的输入');
  35. }
  36. }
  37. }
  38. /**
  39. * 规格组写入数据库并生成id
  40. * 此时的$specList是用户端传来的
  41. * @param array $specList
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public static function getNewSpecList(array $specList): array
  48. {
  49. // 规格组名称合集
  50. $names = helper::getArrayColumn($specList, 'spec_name');
  51. // 获取到已存在的规格组
  52. $alreadyData = static::getListByNames($names);
  53. // 遍历整理新的规格集
  54. foreach ($specList as $key => &$item) {
  55. $alreadyItem = helper::getArrayItemByColumn($alreadyData, 'spec_name', $item['spec_name']);
  56. if (!empty($alreadyItem)) {
  57. // 规格名已存在的记录spec_id
  58. $item['spec_id'] = $alreadyItem['spec_id'];
  59. } else {
  60. // 规格名不存在的新增记录
  61. $result = static::add($item);
  62. $item['spec_id'] = $result['spec_id'];
  63. }
  64. // 规格值写入数据库并生成id
  65. $item['valueList'] = SpecValueModel::getNewValueList((int)$item['spec_id'], $item['valueList']);
  66. }
  67. return $specList;
  68. }
  69. /**
  70. * 新增规格组记录
  71. * @param array $item
  72. * @return static|\think\Model
  73. */
  74. private static function add(array $item)
  75. {
  76. // 拿到所有的规格组名称集
  77. // 获取到已存在的
  78. return self::create([
  79. 'spec_name' => $item['spec_name'],
  80. 'store_id' => self::$storeId
  81. ]);
  82. }
  83. /**
  84. * 根据规格组名称集获取列表
  85. * @param array $names
  86. * @return \think\Collection
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. private static function getListByNames(array $names): \think\Collection
  92. {
  93. return (new static)->where('spec_name', 'in', $names)->select();
  94. }
  95. }