Spec.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  17. * 规格组模型
  18. * Class Spec
  19. * @package app\store\model
  20. */
  21. class Spec extends SpecModel
  22. {
  23. /**
  24. * 规格组写入数据库并生成id
  25. * 此时的$specList是用户端传来的
  26. * @param array $specList
  27. * @return array
  28. */
  29. public static function getNewSpecList(array $specList)
  30. {
  31. // 规格组名称合集
  32. $names = helper::getArrayColumn($specList, 'spec_name');
  33. // 获取到已存在的规格组
  34. $alreadyData = static::getListByNames($names);
  35. // 遍历整理新的规格集
  36. foreach ($specList as $key => &$item) {
  37. $alreadyItem = helper::getArrayItemByColumn($alreadyData, 'spec_name', $item['spec_name']);
  38. if (!empty($alreadyItem)) {
  39. // 规格名已存在的记录spec_id
  40. $item['spec_id'] = $alreadyItem['spec_id'];
  41. } else {
  42. // 规格名不存在的新增记录
  43. $result = static::add($item);
  44. $item['spec_id'] = (int)$result['spec_id'];
  45. }
  46. // 规格值写入数据库并生成id
  47. $item['valueList'] = SpecValueModel::getNewValueList((int)$item['spec_id'], $item['valueList']);
  48. }
  49. return $specList;
  50. }
  51. /**
  52. * 新增规格组记录
  53. * @param $item
  54. * @return Spec|\think\Model
  55. */
  56. private static function add(array $item)
  57. {
  58. // 拿到所有的规格组名称集
  59. // 获取到已存在的
  60. return self::create([
  61. 'spec_name' => $item['spec_name'],
  62. 'store_id' => self::$storeId
  63. ]);
  64. }
  65. /**
  66. * 根据规格组名称集获取列表
  67. * @param array $names
  68. * @return \think\Collection
  69. */
  70. private static function getListByNames(array $names)
  71. {
  72. return (new static)->where('spec_name', 'in', $names)->select();
  73. }
  74. /**
  75. * 获取指定管理员的所有规格id
  76. * @param int $specId
  77. * @return array
  78. */
  79. public static function getSpecIdsByUserId(int $specId)
  80. {
  81. return (new static)->where('store_user_id', '=', $specId)->column('spec_id');
  82. }
  83. }