ServiceRel.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\store\model\goods;
  13. use app\common\model\goods\ServiceRel as ServiceRelModel;
  14. /**
  15. * 商品服务与承诺模型
  16. * Class ServiceRel
  17. */
  18. class ServiceRel extends ServiceRelModel
  19. {
  20. /**
  21. * 根据服务ID获取记录总数量
  22. * @param int $serviceId
  23. * @return int
  24. */
  25. public static function getCountByServiceId(int $serviceId): int
  26. {
  27. return (new static)->where('service_id', '=', $serviceId)->count();
  28. }
  29. /**
  30. * 更新关系记录
  31. * @param int $goodsId
  32. * @param array $serviceIds 新的服务集
  33. * @return array|false
  34. */
  35. public static function updates(int $goodsId, array $serviceIds)
  36. {
  37. // 已分配的服务集
  38. $assignServiceIds = self::getServiceIds($goodsId);
  39. // 找出删除的服务
  40. $deleteServiceIds = array_diff($assignServiceIds, $serviceIds);
  41. if (!empty($deleteServiceIds)) {
  42. static::deleteAll([
  43. ['goods_id', '=', $goodsId],
  44. ['service_id', 'in', $deleteServiceIds]
  45. ]);
  46. }
  47. // 找出添加的服务
  48. $newServiceIds = array_diff($serviceIds, $assignServiceIds);
  49. $dataset = [];
  50. foreach ($newServiceIds as $serviceId) {
  51. $dataset[] = [
  52. 'goods_id' => $goodsId,
  53. 'service_id' => $serviceId,
  54. 'store_id' => self::$storeId,
  55. ];
  56. }
  57. return (new static)->addAll($dataset);
  58. }
  59. }