GoodsSpecRel.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\model\GoodsSpecRel as GoodsSpecRelModel;
  14. /**
  15. * 商品规格关系模型
  16. * Class GoodsSpecRel
  17. * @package app\store\model
  18. */
  19. class GoodsSpecRel extends GoodsSpecRelModel
  20. {
  21. /**
  22. * 批量写入商品与规格值关系记录
  23. * @param int $goodsId
  24. * @param array $specList
  25. * @return array|false
  26. */
  27. public static function increased(int $goodsId, array $specList)
  28. {
  29. $dataset = [];
  30. foreach ($specList as $item) {
  31. foreach ($item['valueList'] as $specValueItem) {
  32. $dataset[] = [
  33. 'goods_id' => $goodsId,
  34. 'spec_id' => $item['spec_id'],
  35. 'spec_value_id' => $specValueItem['spec_value_id'],
  36. 'store_id' => self::$storeId
  37. ];
  38. }
  39. }
  40. return (new static)->addAll($dataset);
  41. }
  42. /**
  43. * 批量更新商品与规格值关系记录
  44. * @param int $goodsId
  45. * @param array $specList
  46. * @return array|false
  47. */
  48. public static function updates(int $goodsId, array $specList)
  49. {
  50. // 删除所有的记录
  51. static::deleteAll(['goods_id' => $goodsId]);
  52. // 批量新增记录
  53. return static::increased($goodsId, $specList);
  54. }
  55. }