| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace app\common\repositories\shared;
- use app\common\dao\shared\SharedProsperityBalanceLogDao;
- use app\common\dao\shared\SharedProsperityPointLogDao;
- use app\common\dao\shared\SharedProsperityRecommendConfigDao;
- use app\common\dao\shared\SharedProsperityUserDao;
- use app\common\enum\CommonEnum;
- use app\common\enum\shared\SharedProsperityRewardRecordEnum;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\user\UserBillRepository;
- use think\facade\Db;
- use think\facade\Log;
- class SharedProsperityTaskRepository extends BaseRepository
- {
- const PARTNER_RADIO = 1;
- const REWARD_TYPE_PV = SharedProsperityRewardRecordEnum::TYPE['PV']['code'];
- /**
- * @param $data
- * @return void
- * @throws \Exception
- * @author 史晨
- * @date 2026/2/10 10:10
- * 合伙人分红(加权分配)
- */
- public function partner($data)
- {
- // 获取时间范围内订单
- $merId = CommonEnum::DESIGN_MERCHANT_ID['SharedProsperity']['code'];
- $start = $data['startTime'];
- $end = $data['endTime'];
- // 获取pv
- /** @var SharedProsperityRewardRecordRepository $rewardRecordRepository */
- $rewardRecordRepository = app()->make(SharedProsperityRewardRecordRepository::class);
- $orderList = $rewardRecordRepository->getListByTime($start, $end, self::REWARD_TYPE_PV);
- if (empty($orderList)) {
- throw new \Exception('没有订单');
- }
- // 计算可分配利润
- $totalConPri = array_sum(array_column($orderList, 'number'));
- $conPri = floor($totalConPri * self::PARTNER_RADIO * 100) / 100;
- // 获取合伙人分配规则
- /** @var SharedProsperityRecommendConfigDao $configDao */
- $configDao = app()->make(SharedProsperityRecommendConfigDao::class);
- $config = $configDao->getConfigList(false);
- $config = array_column($config, 'profit_ratio', 'level');
- if (empty($config)) {
- throw new \Exception('没有合伙人分配规则');
- }
- // 获取合伙人
- /** @var SharedProsperityUserDao $userDao */
- $userDao = app()->make(SharedProsperityUserDao::class);
- $users = $userDao->getUserGroupByPartnerLevel();
- /** @var SharedProsperityBalanceLogDao $balanceDao */
- $balanceDao = app()->make(SharedProsperityBalanceLogDao::class);
- Db::startTrans();
- try {
- foreach ($users as $key => $userIds) {
- if ($key == 0) {
- continue;
- }
- $radio = $config[$key];
- // 该等级总分红
- $levelTotal = floor(($conPri * ($radio / 100)) * 100) / 100;
- // 获取该等级所有用户的基本信息
- $userInfos = $userDao->getSharedProsperityUserEntityListByUserIdList($userIds);
- if (empty($userInfos)) {
- continue;
- }
- // 计算每个用户的团队业绩(实时统计)
- $teamPvMap = [];
- $totalTeamPv = 0;
- foreach ($userInfos as $userInfo) {
- $teamPv = $userDao->getTeamPvSum($userInfo->getUserId());
- $teamPvMap[$userInfo->getUserId()] = $teamPv;
- $totalTeamPv += $teamPv;
- }
- // 如果总团队业绩为0,则平均分配
- if ($totalTeamPv == 0) {
- $userNum = count($userInfos);
- $eachReward = floor(($levelTotal / $userNum) * 100) / 100;
- foreach ($userInfos as $userInfo) {
- $userReward = $eachReward;
- $this->distributeReward($userInfo, $userReward, $radio, $levelTotal, $balanceDao, $userDao);
- }
- } else {
- // 加权分配
- foreach ($userInfos as $userInfo) {
- $weight = $teamPvMap[$userInfo->getUserId()] / $totalTeamPv;
- $userReward = floor($levelTotal * $weight * 100) / 100;
- $this->distributeReward($userInfo, $userReward, $radio, $levelTotal, $balanceDao, $userDao);
- }
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- /**
- * 分发奖励(记录日志和更新余额)
- * @param $userInfo
- * @param $userReward
- * @param $radio
- * @param $levelTotal
- * @param $balanceDao
- * @param $userDao
- * @return void
- */
- private function distributeReward($userInfo, $userReward, $radio, $levelTotal, $balanceDao, $userDao)
- {
- $userId = $userInfo->getUserId();
- $balanceDao->create([
- 'user_id' => $userId,
- 'level' => $userInfo->getPartnerLevel(),
- 'radio' => $radio,
- 'total_profit' => $levelTotal,
- 'old' => $userInfo->getBalance(),
- 'number' => $userReward,
- 'after' => $userInfo->getBalance() + $userReward
- ]);
- $bill = [
- 'uid' => $userId,
- 'link_id' => 0,
- 'pm' => 1,
- 'title' => '共富体系合伙人分佣(加权)',
- 'category' => 'now_money',
- 'type' => 'commission',
- 'number' => $userReward,
- 'balance' => 0,
- 'mark' => '共富体系合伙人分佣明细入账(加权)',
- 'create_time' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'commission_type' => CommonEnum::COMMISSION_TYPE['SHARED_PROSPERITY_PARTNER_COMMISSION']['code'],
- 'order_sn' => 0,
- 'tripartite' => 0,
- 'type_shop' => 0,
- 'mer_id' => 0,
- 'source' => 0,
- 'order_type' => 1,
- 'is_red_brokerage' => 0,
- 'gzc' => 3,
- 'take_time' => time(),
- 'district_id' => '',
- 'street_id' => '',
- 'month' => '',
- ];
- Db::name('user_bill')->insert($bill);
- $userDao->updateByUserId($userId, ['balance' => ['inc', $userReward]]);
- }
- /**
- * @param $data
- * @return true
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 史晨
- * @date 2026/2/10 15:22
- * 感恩奖
- */
- public function thank($data)
- {
- // 获取时间范围内订单
- $start = $data['startTime'];
- $end = $data['endTime'];
- // 获取收入
- /** @var UserBillRepository $userBillRepository */
- $userBillRepository = app()->make(UserBillRepository::class);
- $billList = $userBillRepository->getListForSharedProsperityThank($start, $end);
- if (empty($billList)) {
- throw new \Exception('没有佣金记录,无需处理');
- }
- // /** @var SharedProsperityRewardRecordRepository $rewardRecordRepository */
- // $rewardRecordRepository = app()->make(SharedProsperityRewardRecordRepository::class);
- // $rewardList = $rewardRecordRepository->getListByTime($start, $end, self::REWARD_TYPE_PV);
- // if (empty($rewardList)) {
- // throw new \Exception('没有订单');
- // }
- // user_id分区求和 [id]=>[number]
- $groupedResult = array_reduce($billList, function ($carry, $item) {
- $userId = $item['uid'];
- $number = floatval($item['number']);
- if (!isset($carry[$userId])) {
- $carry[$userId] = 0;
- }
- $carry[$userId] += $number;
- return $carry;
- }, []);
- if (empty($groupedResult)) {
- throw new \Exception('分组求和错误');
- }
- /** @var SharedProsperityUserRepository $userRepository */
- $userRepository = app()->make(SharedProsperityUserRepository::class);
- /** @var SharedProsperityPointLogDao $balanceDao */
- $logDao = app()->make(SharedProsperityPointLogDao::class);
- DB::startTrans();
- try {
- foreach ($groupedResult as $key => $value) {
- // 获取感恩奖比例
- $radio = CommonEnum::ORDER_EARNING_ALLOCATION['SharedProsperity']['Thank']['code'];
- // 计算积分
- $points = floor($value * $radio * 100) / 100;
- // 获取上级
- $userData = $userRepository->getSharedProsperityUserEntityByUserId($key);
- if (empty($userData->getUserId())) {
- Log::info('共富感恩奖发放:' . $key . '不是共富会员,不发放感恩奖');
- continue;
- }
- $parentId = $userData->getParentId();
- if ($parentId == 0) {
- Log::info('共富感恩奖发放:' . $key . '的上级是0,不发放感恩奖');
- continue;
- }
- // 给上级发积分,记录日志
- if ($parentId > 0) {
- if ($points <= 0) {
- Log::info('共富感恩奖发放:' . $key . '的收入是' . $value . ',奖励占比是' . $radio . ',积分是' . $value * $radio . '所以' . $parentId . '发放感恩奖');
- continue;
- }
- $parentData = $userRepository->getSharedProsperityUserEntityByUserId($parentId);
- // 记录日志
- $logDao->create([
- 'user_id' => $parentId,
- 'total' => $value,
- 'radio' => $radio,
- 'old' => $parentData->getPoints(),
- 'number' => $points,
- 'after' => $parentData->getPoints() + $points
- ]);
- // 发放积分
- $userRepository->updateByUserId($parentId, ['points' => ['inc', $points]]);
- }
- }
- Db::commit();
- return true;
- } catch (\Exception $e) {
- DB::rollback();
- throw new \Exception($e->getMessage());
- }
- }
- }
|