UserPvLogRepository.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\UserEntity;
  8. use app\entity\data\user\UserPvLogEntity;
  9. use think\db\exception\DbException;
  10. use think\exception\ValidateException;
  11. class UserPvLogRepository extends BaseRepository
  12. {
  13. public function __construct(UserPvLogDao $dao)
  14. {
  15. $this->dao = $dao;
  16. }
  17. /**
  18. * @param int $id
  19. * @return UserPvLogEntity
  20. */
  21. public function getUserPvLogEntityById(int $id): UserPvLogEntity
  22. {
  23. $entityList = $this->getUserPvLogEntityListByIdList([$id]);
  24. $entity = array_shift($entityList);
  25. if ($entity instanceof UserPvLogEntity) {
  26. return $entity;
  27. } else {
  28. /** @var UserPvLogEntity */
  29. return UserPvLogEntity::newInstance();
  30. }
  31. }
  32. /**
  33. * @param array $idList
  34. * @return UserPvLogEntity[]
  35. */
  36. public function getUserPvLogEntityListByIdList(array $idList): array
  37. {
  38. return $this->dao->getUserPvLogEntityListByIdList($idList);
  39. }
  40. /**
  41. * @param string $orderType
  42. * @param array $orderIdList
  43. * @return UserPvLogEntity[]
  44. */
  45. public function getUserPvLogEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array
  46. {
  47. return $this->dao->getUserPvLogEntityListByOrderIdListAndPending($orderType, $orderIdList);
  48. }
  49. public function create($data)
  50. {
  51. return $this->dao->create($data);
  52. }
  53. /**
  54. * 写入 PV记录
  55. * @param UserPvLogEntity $userPvLogEntity
  56. * @return UserPvLogEntity
  57. */
  58. public function createByEntity(UserPvLogEntity $userPvLogEntity): UserPvLogEntity
  59. {
  60. $result = $this->dao->create($userPvLogEntity->toUnderlineArray())->toArray();
  61. /** @var UserPvLogEntity $entity */
  62. $entity = UserPvLogEntity::newInstance($result);
  63. return $entity;
  64. }
  65. /**
  66. * 添加Pv 记录
  67. * @param int $uid
  68. * @param float $pv
  69. * @param string $mark
  70. * @param string $orderType
  71. * @param string $orderId
  72. * @return UserPvLogEntity
  73. */
  74. public function addUserPvLog(int $uid, float $pv, string $mark = '', string $orderType = '', string $orderId = ''): UserPvLogEntity
  75. {
  76. if (!empty($orderType)) {
  77. if (!empty($orderId)) {
  78. if ($orderType == UserPvLogEnum::ORDER_TYPE['Platform']['code']) {
  79. /** @var StoreOrderRepository $storeOrderRepository */
  80. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  81. $storeOrderEntity = $storeOrderRepository->getStoreOrderEntityByOrderId((int)$orderId);
  82. if (empty($storeOrderEntity->getOrderId())) {
  83. throw new ValidateException('未查询到订单信息');
  84. }
  85. }
  86. }
  87. }
  88. /** @var UserPvLogEntity $userPvLogEntity */
  89. $userPvLogEntity = UserPvLogEntity::newInstance();
  90. $userPvLogEntity->setUid($uid)
  91. ->setPv($pv)
  92. ->setMark($mark)
  93. ->setOrderType($orderType)
  94. ->setOrderId($orderId)
  95. ->setAddtime(time())
  96. ->setStatus(UserPvLogEnum::STATUS['PendingConfirmation']['code']);
  97. return $this->createByEntity($userPvLogEntity);
  98. }
  99. /**
  100. * 将 贡献值 直推贡献值 记录设置为生效状态 并对用户的 贡献值 直推贡献值 字段进行增减
  101. * @param array $idList
  102. * @return array
  103. */
  104. public function executeByIdList(array $idList): array
  105. {
  106. $userPvLogEntityList = $this->getUserPvLogEntityListByIdList($idList);
  107. $userIdList = [];
  108. // 忽略 已经生效的 记录
  109. $userPvLogEntityList = array_filter($userPvLogEntityList, function ($entity) use (&$userIdList) {
  110. // 积分数据不为空 用户不为空 状态属于待结算 并且结算字段为空
  111. if ((!empty($entity->getId()) && !empty($entity->getUid()) && ($entity->getStatus() == UserPvLogEnum::STATUS['PendingConfirmation']['code']) && empty($entity->getSettlementTime()))) {
  112. $userIdList[] = $entity->getUid();
  113. return true;
  114. }
  115. return false;
  116. });
  117. if (!empty($userPvLogEntityList)) {
  118. // 获取用户 映射信息
  119. /** @var UserRepository $userRepository */
  120. $userRepository = app()->make(UserRepository::class);
  121. /** @var UserEntity[] $userEntityMapByUid */
  122. $userEntityMapByUid = [];
  123. if (!empty($userIdList)) {
  124. $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList);
  125. }
  126. /** @var UserEntity[] $updateUserEntityList */
  127. $updateUserEntityList = [];
  128. /** @var UserPvLogEntity[] $updateUserPvLogEntityList */
  129. $updateUserPvLogEntityList = [];
  130. foreach ($userPvLogEntityList as $userPvLogEntity) {
  131. $userEntity = $userEntityMapByUid[$userPvLogEntity->getUid()];
  132. if (empty($userEntity) || empty($userEntity->getUid())) {
  133. throw new ValidateException("未查询到用户信息(PvLogId:{$userPvLogEntity->getId()})");
  134. }
  135. // 组建 更新 用户信息
  136. if (isset($updateUserEntityList[$userEntity->getUid()])) {
  137. $updateUserEntity = $updateUserEntityList[$userEntity->getUid()];
  138. } else {
  139. /** @var UserEntity $updateUserEntity */
  140. $updateUserEntity = UserEntity::newInstance();
  141. $updateUserEntity
  142. ->setUid($userEntity->getUid())
  143. ->setPv($userEntity->getPv());
  144. }
  145. // 组建 账单 记录
  146. if (isset($updateUserPvLogEntityList[$userPvLogEntity->getId()])) {
  147. $updateUserPvLogEntity = $updateUserPvLogEntityList[$userPvLogEntity->getId()];
  148. } else {
  149. /** @var UserPvLogEntity $updateUserPvLogEntity */
  150. $updateUserPvLogEntity = UserPvLogEntity::newInstance();
  151. // 更新 账单记录 状态
  152. $updateUserPvLogEntity
  153. ->setId($userPvLogEntity->getId())
  154. ->setStatus(UserPvLogEnum::STATUS['Valid']['code'])
  155. ->setSettlementTime(date('Y-m-d H:i:s'));
  156. }
  157. // 写入Pv
  158. $updateUserEntity->setPv(bcadd($updateUserEntity->getPv(), $userPvLogEntity->getPv(), 2));
  159. $updateUserEntityList[$userPvLogEntity->getUid()] = $updateUserEntity;
  160. $updateUserPvLogEntityList[$userPvLogEntity->getId()] = $updateUserPvLogEntity;
  161. }
  162. if (!empty($updateUserEntityList)) {
  163. $userRepository->batchUpdateUserDataByDataList(
  164. array_map(function (UserEntity $updateUserEntity) {
  165. return $updateUserEntity->toUnderlineArray();
  166. }, $updateUserEntityList)
  167. );
  168. }
  169. if (!empty($updateUserPvLogEntityList)) {
  170. $this->batchUpdateUserPvLogDataByDataList(
  171. array_map(function (UserPvLogEntity $updateUserPvLogEntity) {
  172. return $updateUserPvLogEntity->toUnderlineArray();
  173. }, $updateUserPvLogEntityList)
  174. );
  175. }
  176. }
  177. return $idList;
  178. }
  179. /**
  180. * @param $dataList
  181. * @return array
  182. */
  183. public function batchUpdateUserPvLogDataByDataList($dataList): array
  184. {
  185. return $this->dao->saveAll($dataList);
  186. }
  187. }