123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\services\user;
- use app\dao\user\UserBrokerageDao;
- use app\services\BaseServices;
- use app\services\order\StoreOrderCartInfoServices;
- use app\services\order\StoreOrderServices;
- use think\exception\ValidateException;
- use crmeb\services\CacheService;
- /**
- * 用户佣金
- * Class UserBrokerageServices
- * @package app\services\user
- * @mixin UserBrokerageDao
- */
- class UserBrokerageServices extends BaseServices
- {
- /**
- * 用户记录模板
- * @var array[]
- */
- protected $incomeData = [
- 'get_self_brokerage' => [
- 'title' => '获得自购订单佣金',
- 'type' => 'self_brokerage',
- 'mark' => '您成功消费{%pay_price%}元,奖励自购佣金{%number%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'get_brokerage' => [
- 'title' => '获得下级推广订单佣金',
- 'type' => 'one_brokerage',
- 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'get_two_brokerage' => [
- 'title' => '获得推广订单佣金',
- 'type' => 'two_brokerage',
- 'mark' => '二级推广人{%nickname%}成功消费{%pay_price%}元,奖励推广佣金{%number%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'get_user_brokerage' => [
- 'title' => '获得推广用户佣金',
- 'type' => 'brokerage_user',
- 'mark' => '成功推广用户:{%nickname%},奖励推广佣金{%number%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'extract' => [
- 'title' => '佣金提现',
- 'type' => 'extract',
- 'mark' => '{%mark%},佣金提现{%number%}元',
- 'status' => 1,
- 'pm' => 0
- ],
- 'extract_fail' => [
- 'title' => '提现失败',
- 'type' => 'extract_fail',
- 'mark' => '提现失败,退回佣金{%num%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'brokerage_to_nowMoney' => [
- 'title' => '佣金提现到余额',
- 'type' => 'extract_money',
- 'mark' => '佣金提现到余额{%num%}元',
- 'status' => 1,
- 'pm' => 0
- ],
- 'brokerage_to_energy' => [
- 'title' => '佣金转换到能量',
- 'type' => 'extract_energy',
- 'mark' => '转换到能量{%num%}',
- 'status' => 1,
- 'pm' => 0
- ],
- 'brokerage_refund' => [
- 'title' => '退款退佣金',
- 'type' => 'refund',
- 'mark' => '订单退款扣除佣金{%num%}元',
- 'status' => 1,
- 'pm' => 0
- ],
- 'extract_integral' => [
- 'title' => '积分出局',
- 'type' => 'extract_integral',
- 'mark' => '用户{%type%}出局,出局时积分价格{%price%},共出局{%integral_num%}积分,获得佣金{%number%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'extract_integral_new' => [
- 'title' => '贡献分佣金',
- 'type' => 'activity_brokerage',
- 'mark' => '{%mark%},获得佣金{%number%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'extract_integral_static' => [
- 'title' => '参考分提取佣金',
- 'type' => 'static_brokerage',
- 'mark' => '{%mark%},获得佣金{%number%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'get_area_brokerage' => [
- 'title' => '获得运营中心订单佣金',
- 'type' => 'area_brokerage',
- 'mark' => '{%nickname%}成功消费{%pay_price%}元,奖励{%area%}运营中心分红{%number%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'get_area_award' => [
- 'title' => '获得运营中心订单佣金',
- 'type' => 'area_brokerage',
- 'mark' => '结算周期内地区订单达到{%pay_price%}元,奖励{%area%}运营中心分红{%number%}',
- 'status' => 1,
- 'pm' => 1
- ],
- 'trade_in' => [
- 'title' => '佣金转入',
- 'type' => 'trade_in',
- 'mark' => '{%mark%}{%number%}元',
- 'status' => 1,
- 'pm' => 1
- ],
- 'trade_out' => [
- 'title' => '佣金转出',
- 'type' => 'trade_out',
- 'mark' => '{%mark%}{%number%}元',
- 'status' => 1,
- 'pm' => 0
- ],
- ];
- /**
- * UserBrokerageServices constructor.
- * @param UserBrokerageDao $dao
- */
- public function __construct(UserBrokerageDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 计算佣金
- * @param array $where
- * @param int $time
- * @return mixed
- */
- public function getUsersBokerageSum(array $where, $time = 0)
- {
- $where_data = [
- 'status' => 1,
- 'pm' => $where['pm'] ?? '',
- 'uid' => $where['uid'] ?? '',
- 'time' => $where['time'] ?? 0,
- 'type' => $where['type'] ?? '',
- 'not_type' => $where['not_type'] ?? ''
- ];
- if ($time) $where_data['time'] = $time;
- return $this->dao->getBrokerageSumColumn($where_data);
- }
- /**
- * 某个用户佣金总和
- * @param int $uid
- * @param array|string[] $type
- * @param string $time
- * @return float
- */
- public function getUserBillBrokerageSum(int $uid, array $type = ['self_brokerage', 'one_brokerage', 'two_brokerage', 'brokerage_user'], $time = '')
- {
- $where = ['uid' => $uid];
- if ($type) $where['type'] = $type;
- if ($time) $where['time'] = $time;
- return $this->dao->getBrokerageSum($where);
- }
- /**
- * 写入用户记录
- * @param string $type 写入类型
- * @param int $uid
- * @param int|string|array $number
- * @param int|string $balance
- * @param int $link_id
- * @return bool|mixed
- */
- public function income(string $type, int $uid, $number, $balance, $link_id)
- {
- $data = $this->incomeData[$type] ?? null;
- if (!$data) {
- return true;
- }
- $data['uid'] = $uid;
- $data['balance'] = $balance ?? 0;
- $data['link_id'] = $link_id;
- if (is_array($number)) {
- $key = array_keys($number);
- $key = array_map(function ($item) {
- return '{%' . $item . '%}';
- }, $key);
- $value = array_values($number);
- $data['number'] = $number['number'] ?? 0;
- $data['frozen_time'] = $number['frozen_time'] ?? 0;
- $data['mark'] = str_replace($key, $value, $data['mark']);
- } else {
- $data['number'] = $number;
- $data['mark'] = str_replace(['{%num%}'], $number, $data['mark']);
- }
- $data['add_time'] = time();
- if ((float)$data['number']) {
- return $this->dao->save($data);
- }
- return true;
- }
- /**
- * 资金类型
- */
- public function bill_type()
- {
- return CacheService::get('user_brokerage_type_list', function () {
- return ['list' => $this->dao->getBrokerageType([])];
- }, 600);
- }
- /**
- * 获取资金列表
- * @param array $where
- * @param string $field
- * @param int $limit
- * @return array
- */
- public function getBrokerageList(array $where, string $field = '*', int $limit = 0)
- {
- $where_data = [];
- if (isset($where['uid']) && $where['uid'] != '') {
- $where_data['uid'] = $where['uid'];
- }
- if ($where['start_time'] != '' && $where['end_time'] != '') {
- $where_data['time'] = str_replace('-', '/', $where['start_time']) . ' - ' . str_replace('-', '/', $where['end_time']);
- }
- if (isset($where['type']) && $where['type'] != '') {
- $where_data['type'] = $where['type'];
- }
- if (isset($where['nickname']) && $where['nickname'] != '') {
- $where_data['like'] = $where['nickname'];
- }
- if ($limit) {
- [$page] = $this->getPageValue();
- } else {
- [$page, $limit] = $this->getPageValue();
- }
- $data = $this->dao->getBrokerageList($where_data, $field, $page, $limit);
- foreach ($data as &$item) {
- $item['nickname'] = $item['user']['nickname'] ?? '';
- $item['_add_time'] = $item['add_time'] ? date('Y-m-d H:i:s', $item['add_time']) : '';
- unset($item['user']);
- }
- $count = $this->dao->count($where_data);
- return compact('data', 'count');
- }
- /**
- * 获取佣金列表
- * @param array $where
- * @param int $limit
- * @return array
- */
- public function getCommissionList(array $where, int $limit = 0)
- {
- $where_data = [];
- $where_data['time'] = $where['time'];
- if (isset($where['nickname']) && $where['nickname']) {
- $where_data[] = ['u.account|u.nickname|u.uid|u.phone', 'LIKE', "%$where[nickname]%"];
- }
- if (isset($where['price_max']) && isset($where['price_min'])) {
- if ($where['price_max'] != '' && $where['price_min'] != '') {
- $where_data[] = ['u.brokerage_price', 'between', [$where['price_min'], $where['price_max']]];
- } elseif ($where['price_min'] != '' && $where['price_max'] == '') {
- $where_data[] = ['u.brokerage_price', '>=', $where['price_min']];
- } elseif ($where['price_min'] == '' && $where['price_max'] != '') {
- $where_data[] = ['u.brokerage_price', '<=', $where['price_max']];
- }
- }
- $order_string = '';
- $order_arr = ['asc', 'desc'];
- if (isset($where['sum_number']) && in_array($where['sum_number'], $order_arr)) {
- $order_string .= ',income ' . $where['sum_number'];
- }
- if (isset($where['brokerage_price']) && in_array($where['brokerage_price'], $order_arr)) {
- $order_string .= ',u.brokerage_price ' . $where['brokerage_price'];
- }
- if ($order_string) {
- $order_string = trim($order_string, ',');
- }
- /** @var UserUserBrokerageServices $userUserBrokerage */
- $userUserBrokerage = app()->make(UserUserBrokerageServices::class);
- [$count, $list] = $userUserBrokerage->getBrokerageList($where_data, 'b.type,b.pm,sum(IF(b.pm = 1, b.number, 0)) as income,sum(IF(b.pm = 0, b.number, 0)) as pay,u.nickname,u.phone,u.uid,u.now_money,u.brokerage_price,u.delete_time,b.add_time as time', $order_string, $limit);
- $uids = array_unique(array_column($list, 'uid'));
- /** @var UserExtractServices $userExtract */
- $userExtract = app()->make(UserExtractServices::class);
- $extractSumList = $userExtract->getUsersSumList($uids);
- foreach ($list as &$item) {
- // $item['sum_number'] = $item['income'] > $item['pay'] ? bcsub($item['income'], $item['pay'], 2) : 0;
- $item['nickname'] = $item['nickname'] . "|" . ($item['phone'] ? $item['phone'] . "|" : '') . $item['uid'];
- $item['extract_price'] = $extractSumList[$item['uid']] ?? 0;
- $item['sum_number'] = bcadd((string)$item['extract_price'], (string)$item['brokerage_price'], 2);
- $item['time'] = $item['time'] ? date('Y-m-d H:i:s', $item['time']) : '';
- }
- return compact('count', 'list');
- }
- /**
- * 用户佣金详情
- * @param int $uid
- * @return array
- */
- public function user_info(int $uid)
- {
- /** @var UserServices $user */
- $user = app()->make(UserServices::class);
- $user_info = $user->getUserWithTrashedInfo($uid, 'nickname,spread_uid,now_money,brokerage_price,add_time');
- if (!$user_info) {
- throw new ValidateException('您查看的用户信息不存在!');
- }
- $user_info = $user_info->toArray();
- $income = $this->getUserBillBrokerageSum($uid);
- $expend = $this->getUserBillBrokerageSum($uid, ['refund']);
- $number = (float)bcsub((string)$income, (string)$expend, 2);
- $user_info['number'] = max($number, 0);
- $user_info['add_time'] = date('Y-m-d H:i:s', $user_info['add_time']);
- $user_info['spread_name'] = $user_info['spread_uid'] ? $user->getUserInfo((int)$user_info['spread_uid'], 'nickname', true)['nickname'] ?? '' : '';
- return compact('user_info');
- }
- /**
- * 退佣金
- * @param $order
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function orderRefundBrokerageBack($order)
- {
- $id = (int)$order['id'];
- $where = [
- 'uid' => [$order['spread_uid'], $order['spread_two_uid']],
- 'type' => ['self_brokerage', 'one_brokerage', 'two_brokerage'],
- 'link_id' => $id,
- 'pm' => 1
- ];
- $brokerageList = $this->dao->getUserBrokerageList($where);
- //子订单
- if (!$brokerageList && $order['pid']) {
- $where['link_id'] = $order['pid'];
- $p_brokerageList = $this->dao->getUserBrokerageList($where);
- //主订单已分佣 子订单按订单拆分后计算结果回退
- if ($p_brokerageList) {
- $brokerageList = [
- ['uid' => $order['spread_uid'], 'number' => $order['one_brokerage']],
- ['uid' => $order['spread_two_uid'], 'number' => $order['two_brokerage']],
- ];
- }
- }
- $res = true;
- if ($brokerageList) {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- $brokerages = $userServices->getColumn([['uid', 'in', array_column($brokerageList, 'uid')]], 'brokerage_price', 'uid');
- $brokerageData = [];
- foreach ($brokerageList as $item) {
- if (!$item['uid'] || $item['uid'] <= 0) continue;
- $usermoney = $brokerages[$item['uid']] ?? 0;
- if ($item['number'] > $usermoney) {
- $item['number'] = $usermoney;
- }
- if ($item['number'] <= 0) continue;
- $res = $res && $userServices->bcDec($item['uid'], 'brokerage_price', (string)$item['number'], 'uid');
- $brokerageData[] = [
- 'title' => '退款退佣金',
- 'uid' => $item['uid'],
- 'pm' => 0,
- 'add_time' => time(),
- 'type' => 'refund',
- 'number' => $item['number'],
- 'link_id' => $id,
- 'balance' => bcsub((string)$usermoney, (string)$item['number'], 2),
- 'mark' => '订单退款扣除佣金' . floatval($item['number']) . '元'
- ];
- }
- if ($brokerageData) {
- $res = $res && $this->dao->saveAll($brokerageData);
- }
- //修改佣金冻结时间
- $this->dao->update($where, ['frozen_time' => 0]);
- }
- return $res;
- }
- /**
- * 佣金排行
- * @param string $time
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function brokerageRankList(string $time = 'week')
- {
- $where = [];
- if ($time) {
- $where['time'] = $time;
- }
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->brokerageRankList($where, $page, $limit);
- foreach ($list as $key => &$item) {
- if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
- unset($list[$key]);
- continue;
- }
- $item['nickname'] = $item['user']['nickname'] ?? '';
- $item['avatar'] = $item['user']['avatar'] ?? '';
- if ($item['brokerage_price'] == '0.00' || $item['brokerage_price'] == 0 || !$item['brokerage_price']) {
- unset($list[$key]);
- }
- unset($item['user']);
- }
- return array_merge($list);
- }
- /**
- * 获取用户排名
- * @param int $uid
- * @param string $time
- */
- public function getUserBrokerageRank(int $uid, string $time = 'week')
- {
- $where = [];
- if ($time) {
- $where['time'] = $time;
- }
- $list = $this->dao->brokerageRankList($where);
- foreach ($list as $key => &$item) {
- if (!isset($item['user']) || !$item['user'] || $item['brokerage_price'] <= 0) {
- unset($list[$key]);
- }
- }
- $position_tmp_one = array_column($list, 'uid');
- $position_tmp_two = array_column($list, 'brokerage_price', 'uid');
- if (!in_array($uid, $position_tmp_one)) {
- $position = 0;
- } else {
- if ($position_tmp_two[$uid] == 0.00) {
- $position = 0;
- } else {
- $position = array_search($uid, $position_tmp_one) + 1;
- }
- }
- return $position;
- }
- /**
- * 推广数据 昨天的佣金 累计提现金额 当前佣金
- * @param int $uid
- * @return mixed
- */
- public function commission(int $uid)
- {
- /** @var UserServices $userServices */
- $userServices = app()->make(UserServices::class);
- if (!$userServices->userExist($uid)) {
- throw new ValidateException('数据不存在');
- }
- /** @var UserExtractServices $userExtract */
- $userExtract = app()->make(UserExtractServices::class);
- $data = [];
- $data['uid'] = $uid;
- $data['pm'] = 1;
- $data['commissionSum'] = $this->getUsersBokerageSum($data);
- $data['pm'] = 0;
- $data['commissionRefund'] = $this->getUsersBokerageSum($data);
- $data['commissionCount'] = $data['commissionSum'] > $data['commissionRefund'] ? bcsub((string)$data['commissionSum'], (string)$data['commissionRefund'], 2) : 0.00;
- $data['lastDayCount'] = $this->getUsersBokerageSum($data, 'yesterday');//昨天的佣金
- $data['extractCount'] = $userExtract->getUserExtract($uid);//累计提现金额
- return $data;
- }
- /**
- * 前端佣金排行页面数据
- * @param int $uid
- * @param $type
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function brokerage_rank(int $uid, $type)
- {
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- if (!$userService->userExist($uid)) {
- throw new ValidateException('数据不存在');
- }
- return [
- 'rank' => $this->brokerageRankList($type),
- 'position' => $this->getUserBrokerageRank($uid, $type)
- ];
- }
- /**
- * 推广 佣金/提现 总和
- * @param int $uid
- * @param $type 3 佣金 4 提现
- * @return mixed
- */
- public function spread_count(int $uid, $type)
- {
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- if (!$userService->userExist($uid)) {
- throw new ValidateException('数据不存在');
- }
- $count = 0;
- if ($type == 3) {
- $where = [
- 'uid' => $uid,
- 'status' => 1,
- 'pm' => 1
- ];
- $count1 = $this->dao->getBrokerageSumColumn($where);
- $where['pm'] = 0;
- $count2 = $this->dao->getBrokerageSumColumn($where);
- $count = $count1 - $count2;
- } else if ($type == 4) {
- /** @var UserExtractServices $userExtract */
- $userExtract = app()->make(UserExtractServices::class);
- $count = $userExtract->getUserExtract($uid);//累计提现
- }
- return $count ? $count : 0;
- }
- /**
- * 推广订单
- * @param Request $request
- * @return mixed
- */
- public function spread_order(int $uid, array $data)
- {
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- if (!$userService->userExist($uid)) {
- throw new ValidateException('数据不存在');
- }
- $result = ['list' => [], 'time' => [], 'count' => 0];
- /** @var StoreOrderServices $storeOrderServices */
- $storeOrderServices = app()->make(StoreOrderServices::class);
- [$page, $limit] = $this->getPageValue();
- $time_data = [];
- $where = ['type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0, 'spread_or_uid' => $uid];
- if ($data['start'] || $data['stop']) {
- $where['time'] = [$data['start'], $data['stop']];
- }
- $where['real_name'] = $data['keyword'];
- $list = $storeOrderServices->getList($where, ['id,order_id,uid,add_time,spread_uid,status,spread_two_uid,one_brokerage,two_brokerage,pay_price,cart_id'], $page, $limit, ['brokerage' => function ($query) use ($uid) {
- $query->where('uid', $uid);
- }]);
- $result['count'] = $storeOrderServices->count($where);
- if ($list) {
- /** @var StoreOrderCartInfoServices $cartInfoServices */
- $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $uids = array_unique(array_column($list, 'uid'));
- $userInfos = $userService->getColumn([['uid', 'in', $uids]], 'uid,avatar,nickname', 'uid');
- foreach ($list as &$item) {
- $item['store_name'] = $cartInfoServices->getCarIdByProductTitle((int)$item['id']);
- $item['avatar'] = $userInfos[$item['uid']]['avatar'] ?? '';
- $item['nickname'] = $userInfos[$item['uid']]['nickname'] ?? '';
- $item['number'] = $item['spread_uid'] == $uid ? $item['one_brokerage'] : $item['two_brokerage'];
- $item['time_key'] = $item['add_time'] ? date('Y-m', $item['add_time']) : '';
- $item['type'] = in_array($item['status'], [2, 3]) ? 'brokerage' : 'number';
- $brokerage = $item['brokerage'];
- $time = $brokerage['add_time'] ?? $item['add_time'];
- $item['time'] = $time ? date('Y-m-d H:i', $time) : '';
- $item['is_frozen'] = $brokerage && $brokerage['frozen_time'] > time() ? 1 : 0;
- unset($item['brokerage']);
- }
- $times = array_unique(array_column($list, 'time_key'));
- $time_data = [];
- $i = 0;
- foreach ($times as $time) {
- $time_data[$i]['time'] = $time;
- $time_data[$i]['count'] = $storeOrderServices->getMonthCount($where, $time);
- $time_data[$i]['sumPrice'] = $storeOrderServices->getMonthMoneyCount($where, $time, 'pay_price');
- $i++;
- }
- }
- $result['list'] = $list;
- $result['time'] = $time_data;
- $priceWhere = ['pid' => 0, 'type' => 0, 'paid' => 1, 'refund_status' => [0, 3], 'is_del' => 0, 'is_system_del' => 0];
- if ($data['start'] || $data['stop']) {
- $priceWhere['time'] = [$data['start'], $data['stop']];
- }
- //条件获取一级佣金总和
- $sum_price_one = $storeOrderServices->sum($priceWhere + ['spread_uid' => $uid], 'one_brokerage', true);
- $sum_price_two = $storeOrderServices->sum($priceWhere + ['spread_two_uid' => $uid], 'two_brokerage', true);
- $result['sum_brokerage'] = bcadd((string)$sum_price_one, (string)$sum_price_two, 2);
- return $result;
- }
- /**
- * 用户佣金记录v2
- * @param int $uid
- * @return array
- */
- public function userBrokerageList(int $uid, $data = [])
- {
- $where = [];
- $where['uid'] = $uid;
- if ($data['start'] || $data['stop']) {
- $where['time'] = [$data['start'], $data['stop']];
- }
- $where['like'] = $data['keyword'];
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getList($where, '*', $page, $limit);
- $times = [];
- if ($list) {
- foreach ($list as &$item) {
- $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
- }
- $times = array_merge(array_unique(array_column($list, 'time_key')));
- }
- $income = $this->dao->sum($where + ['pm' => 1, 'not_type' => ['extract_fail']], 'number', true);
- $expend = $this->dao->sum($where + ['pm' => 0], 'number', true);
- return ['list' => $list, 'time' => $times, 'income' => $income, 'expend' => $expend];
- }
- /**
- * 用户提现记录v2
- * @param int $uid
- * @param array $data
- * @return array
- */
- public function userExtractList(int $uid, array $data)
- {
- $where = [];
- $where['uid'] = $uid;
- $where['type'] = ['extract', 'extract_money', 'extract_fail'];
- if ((isset($data['start']) && $data['start']) || (isset($data['stop']) && $data['stop'])) {
- $where['time'] = [$data['start'], $data['stop']];
- }
- [$page, $limit] = $this->getPageValue();
- $list = $this->dao->getList($where, '*', $page, $limit);
- $times = [];
- if ($list) {
- foreach ($list as &$item) {
- $item['time_key'] = $item['add_time'] ? date('Y-m', (int)$item['add_time']) : '';
- $item['add_time'] = $item['add_time'] ? date('Y-m-d H:i', (int)$item['add_time']) : '';
- }
- $times = array_merge(array_unique(array_column($list, 'time_key')));
- }
- return ['list' => $list, 'time' => $times];
- }
- public function trade(int $uid, int $to_uid, $num)
- {
- $switch = sys_config('brokerage_trade_switch', 1);
- if (!$switch) throw new ValidateException('暂不支持');
- /** @var UserServices $userService */
- $userService = app()->make(UserServices::class);
- $user = $userService->getUserInfo($uid);
- $to_user = $userService->getUserInfo($to_uid);
- if (!$user || !$to_user) {
- throw new ValidateException('数据不存在');
- }
- if ($to_uid == $uid) throw new ValidateException('不能自己转给自己');
- if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
- $broken_commission = $this->getUserFrozenPrice($uid);
- if ($broken_commission < 0)
- $broken_commission = 0;
- $brokerage_price = $user['brokerage_price'];
- //可提现佣金
- $commissionCount = (float)bcsub((string)$brokerage_price, (string)$broken_commission, 2);
- if ($num > $commissionCount) {
- throw new ValidateException('可用佣金不足');
- }
- $extractPrice = $user['brokerage_price'];
- if ($extractPrice < 0) {
- throw new ValidateException('转账佣金不足' . $num);
- }
- if ($num > $extractPrice) {
- throw new ValidateException('转账佣金不足' . $num);
- }
- if ($num <= 0) {
- throw new ValidateException('转账佣金大于0');
- }
- return $this->transaction(function () use ($num, $user, $to_user, $userService) {
- //修改用户佣金
- $balance = bcsub((string)$user['brokerage_price'], (string)$num, 2) ?? 0;
- $balance2 = bcadd((string)$to_user['brokerage_price'], (string)$num, 2) ?? 0;
- if (!$userService->update($user['uid'], ['brokerage_price' => $balance], 'uid')) {
- throw new ValidateException('修改用户信息失败');
- }
- if (!$userService->update($to_user['uid'], ['brokerage_price' => $balance2], 'uid')) {
- throw new ValidateException('修改用户信息失败');
- }
- //保存佣金记录
- $this->income('trade_out', $user['uid'], ['mark' => '转账给' . $to_user['nickname'] . '(' . $to_user['uid'] . ')', 'number' => $num], $balance, 0);
- $this->income('trade_in', $to_user['uid'], ['mark' => '转账自' . $user['nickname'] . '(' . $user['uid'] . ')', 'number' => $num], $balance2, 0);
- return true;
- });
- }
- }
|