UserPvLogRepository.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserPvLogDao;
  4. use app\common\enum\user\UserPvLogEnum;
  5. use app\common\repositories\BaseRepository;
  6. use app\common\repositories\store\order\StoreOrderRepository;
  7. use app\entity\data\user\UserPvLogEntity;
  8. use think\exception\ValidateException;
  9. class UserPvLogRepository extends BaseRepository
  10. {
  11. public function __construct(UserPvLogDao $dao)
  12. {
  13. $this->dao = $dao;
  14. }
  15. public function create($data)
  16. {
  17. return $this->dao->create($data);
  18. }
  19. /**
  20. * 写入 PV记录
  21. * @param UserPvLogEntity $userPvLogEntity
  22. * @return UserPvLogEntity
  23. */
  24. public function createByEntity(UserPvLogEntity $userPvLogEntity): UserPvLogEntity
  25. {
  26. $result = $this->dao->create($userPvLogEntity->toUnderlineArray())->toArray();
  27. /** @var UserPvLogEntity $entity */
  28. $entity = UserPvLogEntity::newInstance($result);
  29. return $entity;
  30. }
  31. /**
  32. * 添加Pv 记录
  33. * @param int $uid
  34. * @param float $pv
  35. * @param string $mark
  36. * @param string $orderType
  37. * @param string $orderId
  38. * @return UserPvLogEntity
  39. */
  40. public function addUserPvLog(int $uid, float $pv, string $mark = '', string $orderType = '', string $orderId = ''): UserPvLogEntity
  41. {
  42. if (!empty($orderType)) {
  43. if (!empty($orderId)) {
  44. if ($orderType == UserPvLogEnum::ORDER_TYPE['Platform']['code']) {
  45. /** @var StoreOrderRepository $storeOrderRepository */
  46. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  47. $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId((int)$orderId);
  48. if (empty($storeOrderEntity->getOrderId())) {
  49. throw new ValidateException('未查询到订单信息');
  50. }
  51. }
  52. }
  53. }
  54. /** @var UserPvLogEntity $userPvLogEntity */
  55. $userPvLogEntity = UserPvLogEntity::newInstance();
  56. $userPvLogEntity->setUid($uid)
  57. ->setPv($pv)
  58. ->setMark($mark)
  59. ->setOrderType($orderType)
  60. ->setOrderId($orderId)
  61. ->setAddtime(time())
  62. ->setStatus(UserPvLogEnum::STATUS['PendingConfirmation']['code']);
  63. return $this->createByEntity($userPvLogEntity);
  64. }
  65. }