UserSignHongbaoRepository.php 2.6 KB

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