| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace app\common\repositories\third;
- use app\common\dao\third\ThirdPartyGoodCacheDao;
- use app\common\enum\third\ThirdPartyGoodCacheEnum;
- use app\common\repositories\BaseRepository;
- use app\entity\data\third\ThirdPartyGoodCacheEntity;
- use think\exception\ValidateException;
- class ThirdPartyGoodCacheRepository extends BaseRepository
- {
- /**
- * @var ThirdPartyGoodCacheDao
- */
- protected $dao;
- public function __construct(ThirdPartyGoodCacheDao $dao)
- {
- $this->dao = $dao;
- }
- public function create($data)
- {
- return $this->dao->create($data);
- }
- /**
- * 写入贡献值记录
- * @param ThirdPartyGoodCacheEntity $storeGroupOrderEntity
- * @return ThirdPartyGoodCacheEntity
- */
- public function createByEntity(ThirdPartyGoodCacheEntity $storeGroupOrderEntity): ThirdPartyGoodCacheEntity
- {
- $result = $this->create($storeGroupOrderEntity->toUnderlineArray())->toArray();
- /** @var ThirdPartyGoodCacheEntity $entity */
- $entity = ThirdPartyGoodCacheEntity::newInstance($result);
- return $entity;
- }
- /**
- * @param $ids
- * @param $data
- * @return int
- * @throws \think\db\exception\DbException
- */
- public function updates($ids, $data): int
- {
- return $this->dao->updates($ids, $data);
- }
- /**
- * @param ThirdPartyGoodCacheEntity $thirdPartyProductEntity
- * @return ThirdPartyGoodCacheEntity|false
- * @throws \think\db\exception\DbException
- */
- public function updatesByEntity(ThirdPartyGoodCacheEntity $thirdPartyProductEntity)
- {
- $result = $this->updates([$thirdPartyProductEntity->getProductId()], $thirdPartyProductEntity->toUnderlineArray());
- if (empty($result)) {
- return false;
- }
- return $thirdPartyProductEntity;
- }
- /**
- * @param string $productId
- * @return ThirdPartyGoodCacheEntity
- */
- public function getThirdPartyGoodCacheEntityByProductId(string $productId): ThirdPartyGoodCacheEntity
- {
- $entityList = $this->getThirdPartyGoodCacheEntityListByProductIdList([$productId]);
- $entity = array_shift($entityList);
- if ($entity instanceof ThirdPartyGoodCacheEntity) {
- return $entity;
- } else {
- /** @var ThirdPartyGoodCacheEntity */
- return ThirdPartyGoodCacheEntity::newInstance();
- }
- }
- /**
- * @param array $productIdList
- * @return ThirdPartyGoodCacheEntity[]
- */
- public function getThirdPartyGoodCacheEntityListByProductIdList(array $productIdList): array
- {
- return $this->dao->getThirdPartyGoodCacheEntityListByProductIdList($productIdList);
- }
- /**
- * @param $param
- * @return ThirdPartyGoodCacheEntity
- * @throws \think\db\exception\DbException
- */
- public function saveGoodCacheByFit($param): ThirdPartyGoodCacheEntity
- {
- if (empty($param['skuId']) || empty($param['type'])) {
- throw new ValidateException('缺少必要参数');
- }
- $skuId = $param['skuId'];
- $platform = ThirdPartyGoodCacheEnum::platformCodeMapByThirdPartyGoodEnumTypeCode($param['type']);
- /** @var ThirdPartyGoodCacheRepository $thirdPartyProductRepository */
- $thirdPartyProductRepository = app()->make(ThirdPartyGoodCacheRepository::class);
- $thirdPartyProductEntity = $thirdPartyProductRepository->getThirdPartyGoodCacheEntityByProductId($skuId);
- if (empty($thirdPartyProductEntity->getProductId())) {
- /** @var ThirdPartyGoodCacheEntity $thirdPartyProductEntity */
- $thirdPartyProductEntity = ThirdPartyGoodCacheEntity::newInstance();
- $thirdPartyProductEntity
- ->setProductId($skuId)
- ->setPlatform($platform)
- ->setExtraContent($param);
- $thirdPartyProductEntity = $thirdPartyProductRepository->createByEntity($thirdPartyProductEntity);
- if (empty($thirdPartyProductEntity->getProductId())) {
- throw new ValidateException('保存失败');
- }
- } else {
- $thirdPartyProductEntity
- ->setProductId($skuId)
- ->setPlatform($platform)
- ->setExtraContent($param);
- $thirdPartyProductRepository->updatesByEntity($thirdPartyProductEntity);
- }
- return $thirdPartyProductEntity;
- }
- }
|