UserSignVrRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\common\repositories\user;
  3. use app\common\dao\user\UserSignVrDao;
  4. use app\common\enum\user\UserSignVrEnum;
  5. use app\common\repositories\BaseRepository;
  6. use app\common\repositories\store\order\StoreOrderRepository;
  7. use app\entity\data\user\UserSignVrEntity;
  8. use think\db\exception\DbException;
  9. use think\exception\ValidateException;
  10. class UserSignVrRepository extends BaseRepository
  11. {
  12. public function __construct(UserSignVrDao $dao)
  13. {
  14. $this->dao = $dao;
  15. }
  16. public function create($data)
  17. {
  18. return $this->dao->create($data);
  19. }
  20. /**
  21. * @param int $id
  22. * @return UserSignVrEntity
  23. */
  24. public function getStoreSignVrEntityById(int $id): UserSignVrEntity
  25. {
  26. $entityList = $this->getStoreSignVrEntityListByIdList([$id]);
  27. $entity = array_shift($entityList);
  28. if ($entity instanceof UserSignVrEntity) {
  29. return $entity;
  30. } else {
  31. /** @var UserSignVrEntity */
  32. return UserSignVrEntity::newInstance();
  33. }
  34. }
  35. /**
  36. * @param array $idList
  37. * @return UserSignVrEntity[]
  38. */
  39. public function getStoreSignVrEntityListByIdList(array $idList): array
  40. {
  41. return $this->dao->getStoreSignVrEntityListByIdList($idList);
  42. }
  43. /**
  44. * 写入Vr记录
  45. * @param UserSignVrEntity $userSignVrEntity
  46. * @return UserSignVrEntity
  47. */
  48. public function createByEntity(UserSignVrEntity $userSignVrEntity): UserSignVrEntity
  49. {
  50. $result = $this->dao->create($userSignVrEntity->toUnderlineArray())->toArray();
  51. /** @var UserSignVrEntity $entity */
  52. $entity = UserSignVrEntity::newInstance($result);
  53. return $entity;
  54. }
  55. /**
  56. * 添加Vr记录
  57. * @param int $uid
  58. * @param int $type
  59. * @param float $number
  60. * @param string $mark
  61. * @param string $orderType
  62. * @param string $orderId
  63. * @return UserSignVrEntity
  64. */
  65. public function addUserSingVr(int $uid, int $type, float $number, string $mark = '', string $orderType = '', string $orderId = ''): UserSignVrEntity
  66. {
  67. if (!empty($orderType)) {
  68. if (!empty($orderId)) {
  69. if ($orderType == UserSignVrEnum::ORDER_TYPE['Platform']['code']) {
  70. /** @var StoreOrderRepository $storeOrderRepository */
  71. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  72. $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId((int)$orderId);
  73. if (empty($storeOrderEntity->getOrderId())) {
  74. throw new ValidateException('未查询到订单信息');
  75. }
  76. }
  77. }
  78. }
  79. /** @var UserSignVrEntity $userSignVrEntity */
  80. $userSignVrEntity = UserSignVrEntity::newInstance();
  81. $userSignVrEntity->setUid($uid)
  82. ->setType($type)
  83. ->setNumber($number)
  84. ->setMark($mark)
  85. ->setOrderType($orderType)
  86. ->setOrderId($orderId)
  87. ->setSurplus(0.00)
  88. ->setAddtime(time());
  89. return $this->createByEntity($userSignVrEntity);
  90. }
  91. /**
  92. * 将 Vr 记录设置为生效状态 并对用户的 Vr 字段进行增减
  93. * @param int $id
  94. * @return UserSignVrEntity
  95. */
  96. public function execute(int $id): UserSignVrEntity
  97. {
  98. $userSignVrEntity = $this->getStoreSignVrEntityById($id);
  99. if (empty($userSignVrEntity->getId()) || empty($userSignVrEntity->getUid())) {
  100. throw new ValidateException('无效的Vr记录');
  101. }
  102. if (!empty($userSignVrEntity->getSettlementTime())) {
  103. throw new ValidateException('该Vr记录已更新,请勿重复操作');
  104. }
  105. /** @var UserRepository $userRepository */
  106. $userRepository = app()->make(UserRepository::class);
  107. $userEntity = $userRepository->getUserEntityByUid($userSignVrEntity->getUid());
  108. if (empty($userEntity->getUid())) {
  109. throw new ValidateException('未查询到用户信息');
  110. }
  111. if ($userSignVrEntity->getType() == UserSignVrEnum::TYPE['Consume']['code']) {
  112. $surplus = bcsub($userEntity->getVr(), $userSignVrEntity->getNumber(), 2);
  113. if ($surplus < 0.00) {
  114. throw new ValidateException('用户VR不足');
  115. }
  116. } else {
  117. $surplus = bcadd($userEntity->getVr(), $userSignVrEntity->getNumber(), 2);
  118. }
  119. try {
  120. $userSignVrEntity
  121. ->setSurplus($surplus)
  122. ->setSettlementTime(date('Y-m-d H:i:s'));
  123. $userRepository->update($userEntity->getUid(), ['vr' => $surplus]);
  124. $this->dao->update($userSignVrEntity->getId(), [
  125. 'surplus' => $userSignVrEntity->getSurplus(),
  126. 'settlement_time' => $userSignVrEntity->getSettlementTime()
  127. ]);
  128. } catch (DbException $e) {
  129. }
  130. return $userSignVrEntity;
  131. }
  132. }