ThirdPartyGoodCacheRepository.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace app\common\repositories\third;
  3. use app\common\dao\third\ThirdPartyGoodCacheDao;
  4. use app\common\enum\third\ThirdPartyGoodCacheEnum;
  5. use app\common\repositories\BaseRepository;
  6. use app\entity\data\third\ThirdPartyGoodCacheEntity;
  7. use think\exception\ValidateException;
  8. class ThirdPartyGoodCacheRepository extends BaseRepository
  9. {
  10. /**
  11. * @var ThirdPartyGoodCacheDao
  12. */
  13. protected $dao;
  14. public function __construct(ThirdPartyGoodCacheDao $dao)
  15. {
  16. $this->dao = $dao;
  17. }
  18. public function create($data)
  19. {
  20. return $this->dao->create($data);
  21. }
  22. /**
  23. * 写入贡献值记录
  24. * @param ThirdPartyGoodCacheEntity $storeGroupOrderEntity
  25. * @return ThirdPartyGoodCacheEntity
  26. */
  27. public function createByEntity(ThirdPartyGoodCacheEntity $storeGroupOrderEntity): ThirdPartyGoodCacheEntity
  28. {
  29. $result = $this->create($storeGroupOrderEntity->toUnderlineArray())->toArray();
  30. /** @var ThirdPartyGoodCacheEntity $entity */
  31. $entity = ThirdPartyGoodCacheEntity::newInstance($result);
  32. return $entity;
  33. }
  34. /**
  35. * @param $ids
  36. * @param $data
  37. * @return int
  38. * @throws \think\db\exception\DbException
  39. */
  40. public function updates($ids, $data): int
  41. {
  42. return $this->dao->updates($ids, $data);
  43. }
  44. /**
  45. * @param ThirdPartyGoodCacheEntity $thirdPartyProductEntity
  46. * @return ThirdPartyGoodCacheEntity|false
  47. * @throws \think\db\exception\DbException
  48. */
  49. public function updatesByEntity(ThirdPartyGoodCacheEntity $thirdPartyProductEntity)
  50. {
  51. $result = $this->updates([$thirdPartyProductEntity->getProductId()], $thirdPartyProductEntity->toUnderlineArray());
  52. if (empty($result)) {
  53. return false;
  54. }
  55. return $thirdPartyProductEntity;
  56. }
  57. /**
  58. * @param string $productId
  59. * @return ThirdPartyGoodCacheEntity
  60. */
  61. public function getThirdPartyGoodCacheEntityByProductId(string $productId): ThirdPartyGoodCacheEntity
  62. {
  63. $entityList = $this->getThirdPartyGoodCacheEntityListByProductIdList([$productId]);
  64. $entity = array_shift($entityList);
  65. if ($entity instanceof ThirdPartyGoodCacheEntity) {
  66. return $entity;
  67. } else {
  68. /** @var ThirdPartyGoodCacheEntity */
  69. return ThirdPartyGoodCacheEntity::newInstance();
  70. }
  71. }
  72. /**
  73. * @param array $productIdList
  74. * @return ThirdPartyGoodCacheEntity[]
  75. */
  76. public function getThirdPartyGoodCacheEntityListByProductIdList(array $productIdList): array
  77. {
  78. return $this->dao->getThirdPartyGoodCacheEntityListByProductIdList($productIdList);
  79. }
  80. /**
  81. * @param $param
  82. * @return ThirdPartyGoodCacheEntity
  83. * @throws \think\db\exception\DbException
  84. */
  85. public function saveGoodCacheByFit($param): ThirdPartyGoodCacheEntity
  86. {
  87. if (empty($param['skuId']) || empty($param['type'])) {
  88. throw new ValidateException('缺少必要参数');
  89. }
  90. $skuId = $param['skuId'];
  91. $platform = ThirdPartyGoodCacheEnum::platformCodeMapByThirdPartyGoodEnumTypeCode($param['type']);
  92. /** @var ThirdPartyGoodCacheRepository $thirdPartyProductRepository */
  93. $thirdPartyProductRepository = app()->make(ThirdPartyGoodCacheRepository::class);
  94. $thirdPartyProductEntity = $thirdPartyProductRepository->getThirdPartyGoodCacheEntityByProductId($skuId);
  95. if (empty($thirdPartyProductEntity->getProductId())) {
  96. /** @var ThirdPartyGoodCacheEntity $thirdPartyProductEntity */
  97. $thirdPartyProductEntity = ThirdPartyGoodCacheEntity::newInstance();
  98. $thirdPartyProductEntity
  99. ->setProductId($skuId)
  100. ->setPlatform($platform)
  101. ->setExtraContent($param);
  102. $thirdPartyProductEntity = $thirdPartyProductRepository->createByEntity($thirdPartyProductEntity);
  103. if (empty($thirdPartyProductEntity->getProductId())) {
  104. throw new ValidateException('保存失败');
  105. }
  106. } else {
  107. $thirdPartyProductEntity
  108. ->setProductId($skuId)
  109. ->setPlatform($platform)
  110. ->setExtraContent($param);
  111. $thirdPartyProductRepository->updatesByEntity($thirdPartyProductEntity);
  112. }
  113. return $thirdPartyProductEntity;
  114. }
  115. }