| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\common\repositories\user;
- use app\common\dao\user\UserSignVrDao;
- use app\common\enum\user\UserSignVrEnum;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\entity\data\user\UserSignVrEntity;
- use think\db\exception\DbException;
- use think\exception\ValidateException;
- class UserSignVrRepository extends BaseRepository
- {
- public function __construct(UserSignVrDao $dao)
- {
- $this->dao = $dao;
- }
- public function create($data)
- {
- return $this->dao->create($data);
- }
- /**
- * @param int $id
- * @return UserSignVrEntity
- */
- public function getStoreSignVrEntityById(int $id): UserSignVrEntity
- {
- $entityList = $this->getStoreSignVrEntityListByIdList([$id]);
- $entity = array_shift($entityList);
- if ($entity instanceof UserSignVrEntity) {
- return $entity;
- } else {
- /** @var UserSignVrEntity */
- return UserSignVrEntity::newInstance();
- }
- }
- /**
- * @param array $idList
- * @return UserSignVrEntity[]
- */
- public function getStoreSignVrEntityListByIdList(array $idList): array
- {
- return $this->dao->getStoreSignVrEntityListByIdList($idList);
- }
- /**
- * 写入Vr记录
- * @param UserSignVrEntity $userSignVrEntity
- * @return UserSignVrEntity
- */
- public function createByEntity(UserSignVrEntity $userSignVrEntity): UserSignVrEntity
- {
- $result = $this->dao->create($userSignVrEntity->toUnderlineArray())->toArray();
- /** @var UserSignVrEntity $entity */
- $entity = UserSignVrEntity::newInstance($result);
- return $entity;
- }
- /**
- * 添加Vr记录
- * @param int $uid
- * @param int $type
- * @param float $number
- * @param string $mark
- * @param string $orderType
- * @param string $orderId
- * @return UserSignVrEntity
- */
- public function addUserSingVr(int $uid, int $type, float $number, string $mark = '', string $orderType = '', string $orderId = ''): UserSignVrEntity
- {
- if (!empty($orderType)) {
- if (!empty($orderId)) {
- if ($orderType == UserSignVrEnum::ORDER_TYPE['Platform']['code']) {
- /** @var StoreOrderRepository $storeOrderRepository */
- $storeOrderRepository = app()->make(StoreOrderRepository::class);
- $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId((int)$orderId);
- if (empty($storeOrderEntity->getOrderId())) {
- throw new ValidateException('未查询到订单信息');
- }
- }
- }
- }
- /** @var UserSignVrEntity $userSignVrEntity */
- $userSignVrEntity = UserSignVrEntity::newInstance();
- $userSignVrEntity->setUid($uid)
- ->setType($type)
- ->setNumber($number)
- ->setMark($mark)
- ->setOrderType($orderType)
- ->setOrderId($orderId)
- ->setSurplus(0.00)
- ->setAddtime(time());
- return $this->createByEntity($userSignVrEntity);
- }
- /**
- * 将 Vr 记录设置为生效状态 并对用户的 Vr 字段进行增减
- * @param int $id
- * @return UserSignVrEntity
- */
- public function execute(int $id): UserSignVrEntity
- {
- $userSignVrEntity = $this->getStoreSignVrEntityById($id);
- if (empty($userSignVrEntity->getId()) || empty($userSignVrEntity->getUid())) {
- throw new ValidateException('无效的Vr记录');
- }
- if (!empty($userSignVrEntity->getSettlementTime())) {
- throw new ValidateException('该Vr记录已更新,请勿重复操作');
- }
- /** @var UserRepository $userRepository */
- $userRepository = app()->make(UserRepository::class);
- $userEntity = $userRepository->getUserEntityByUid($userSignVrEntity->getUid());
- if (empty($userEntity->getUid())) {
- throw new ValidateException('未查询到用户信息');
- }
- if ($userSignVrEntity->getType() == UserSignVrEnum::TYPE['Consume']['code']) {
- $surplus = bcsub($userEntity->getVr(), $userSignVrEntity->getNumber(), 2);
- if ($surplus < 0.00) {
- throw new ValidateException('用户VR不足');
- }
- } else {
- $surplus = bcadd($userEntity->getVr(), $userSignVrEntity->getNumber(), 2);
- }
- try {
- $userSignVrEntity
- ->setSurplus($surplus)
- ->setSettlementTime(date('Y-m-d H:i:s'));
- $userRepository->update($userEntity->getUid(), ['vr' => $surplus]);
- $this->dao->update($userSignVrEntity->getId(), [
- 'surplus' => $userSignVrEntity->getSurplus(),
- 'settlement_time' => $userSignVrEntity->getSettlementTime()
- ]);
- } catch (DbException $e) {
- }
- return $userSignVrEntity;
- }
- }
|