dao = $dao; } /** * @param int $id * @return UserPvLogEntity */ public function getUserPvLogEntityById(int $id): UserPvLogEntity { $entityList = $this->getUserPvLogEntityListByIdList([$id]); $entity = array_shift($entityList); if ($entity instanceof UserPvLogEntity) { return $entity; } else { /** @var UserPvLogEntity */ return UserPvLogEntity::newInstance(); } } /** * @param array $idList * @return UserPvLogEntity[] */ public function getUserPvLogEntityListByIdList(array $idList): array { return $this->dao->getUserPvLogEntityListByIdList($idList); } /** * @param string $orderType * @param array $orderIdList * @return UserPvLogEntity[] */ public function getUserPvLogEntityListByOrderIdListAndPending(string $orderType, array $orderIdList): array { return $this->dao->getUserPvLogEntityListByOrderIdListAndPending($orderType, $orderIdList); } public function create($data) { return $this->dao->create($data); } /** * 写入 PV记录 * @param UserPvLogEntity $userPvLogEntity * @return UserPvLogEntity */ public function createByEntity(UserPvLogEntity $userPvLogEntity): UserPvLogEntity { $result = $this->dao->create($userPvLogEntity->toUnderlineArray())->toArray(); /** @var UserPvLogEntity $entity */ $entity = UserPvLogEntity::newInstance($result); return $entity; } /** * 添加Pv 记录 * @param int $uid * @param float $pv * @param string $mark * @param string $orderType * @param string $orderId * @return UserPvLogEntity */ public function addUserPvLog(int $uid, float $pv, string $mark = '', string $orderType = '', string $orderId = ''): UserPvLogEntity { if (!empty($orderType)) { if (!empty($orderId)) { if ($orderType == UserPvLogEnum::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 UserPvLogEntity $userPvLogEntity */ $userPvLogEntity = UserPvLogEntity::newInstance(); $userPvLogEntity->setUid($uid) ->setPv($pv) ->setMark($mark) ->setOrderType($orderType) ->setOrderId($orderId) ->setAddtime(time()) ->setStatus(UserPvLogEnum::STATUS['PendingConfirmation']['code']); return $this->createByEntity($userPvLogEntity); } /** * 将 贡献值 直推贡献值 记录设置为生效状态 并对用户的 贡献值 直推贡献值 字段进行增减 * @param array $idList * @return array */ public function executeByIdList(array $idList): array { $userPvLogEntityList = $this->getUserPvLogEntityListByIdList($idList); $userIdList = []; // 忽略 已经生效的 记录 $userPvLogEntityList = array_filter($userPvLogEntityList, function ($entity) use (&$userIdList) { // 积分数据不为空 用户不为空 状态属于待结算 并且结算字段为空 if ((!empty($entity->getId()) && !empty($entity->getUid()) && ($entity->getStatus() == UserPvLogEnum::STATUS['PendingConfirmation']['code']) && empty($entity->getSettlementTime()))) { $userIdList[] = $entity->getUid(); return true; } return false; }); if (!empty($userPvLogEntityList)) { // 获取用户 映射信息 /** @var UserRepository $userRepository */ $userRepository = app()->make(UserRepository::class); /** @var UserEntity[] $userEntityMapByUid */ $userEntityMapByUid = []; if (!empty($userIdList)) { $userEntityMapByUid = $userRepository->getUserEntityMapByUidList($userIdList); } /** @var UserEntity[] $updateUserEntityList */ $updateUserEntityList = []; /** @var UserPvLogEntity[] $updateUserPvLogEntityList */ $updateUserPvLogEntityList = []; foreach ($userPvLogEntityList as $userPvLogEntity) { $userEntity = $userEntityMapByUid[$userPvLogEntity->getUid()]; if (empty($userEntity) || empty($userEntity->getUid())) { throw new ValidateException("未查询到用户信息(PvLogId:{$userPvLogEntity->getId()})"); } // 组建 更新 用户信息 if (isset($updateUserEntityList[$userEntity->getUid()])) { $updateUserEntity = $updateUserEntityList[$userEntity->getUid()]; } else { /** @var UserEntity $updateUserEntity */ $updateUserEntity = UserEntity::newInstance(); $updateUserEntity ->setUid($userEntity->getUid()) ->setPv($userEntity->getPv()); } // 组建 账单 记录 if (isset($updateUserPvLogEntityList[$userPvLogEntity->getId()])) { $updateUserPvLogEntity = $updateUserPvLogEntityList[$userPvLogEntity->getId()]; } else { /** @var UserPvLogEntity $updateUserPvLogEntity */ $updateUserPvLogEntity = UserPvLogEntity::newInstance(); // 更新 账单记录 状态 $updateUserPvLogEntity ->setId($userPvLogEntity->getId()) ->setStatus(UserPvLogEnum::STATUS['Valid']['code']) ->setSettlementTime(date('Y-m-d H:i:s')); } // 写入Pv $updateUserEntity->setPv(bcadd($updateUserEntity->getPv(), $userPvLogEntity->getPv(), 2)); $updateUserEntityList[$userPvLogEntity->getUid()] = $updateUserEntity; $updateUserPvLogEntityList[$userPvLogEntity->getId()] = $updateUserPvLogEntity; } if (!empty($updateUserEntityList)) { $userRepository->batchUpdateUserDataByDataList( array_map(function (UserEntity $updateUserEntity) { return $updateUserEntity->toUnderlineArray(); }, $updateUserEntityList) ); } if (!empty($updateUserPvLogEntityList)) { $this->batchUpdateUserPvLogDataByDataList( array_map(function (UserPvLogEntity $updateUserPvLogEntity) { return $updateUserPvLogEntity->toUnderlineArray(); }, $updateUserPvLogEntityList) ); } } return $idList; } /** * @param $dataList * @return array */ public function batchUpdateUserPvLogDataByDataList($dataList): array { return $this->dao->saveAll($dataList); } }