| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace app\common\repositories\user;
- use app\common\dao\user\UserPvLogDao;
- use app\common\enum\user\UserPvLogEnum;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\entity\data\user\UserEntity;
- use app\entity\data\user\UserPvLogEntity;
- use think\db\exception\DbException;
- use think\exception\ValidateException;
- class UserPvLogRepository extends BaseRepository
- {
- public function __construct(UserPvLogDao $dao)
- {
- $this->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);
- }
- }
|