12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- <?php
- namespace app\api\controller\user;
- use app\http\validates\user\AddressValidate;
- use app\models\system\SystemCity;
- use app\models\user\UserPay;
- use app\models\user\UserSingleAward;
- use app\models\user\UserVisit;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- use app\Request;
- use app\models\user\UserLevel;
- use app\models\user\UserSign;
- use app\models\store\StoreBargain;
- use app\models\store\StoreCombination;
- use app\models\store\StoreCouponUser;
- use app\models\store\StoreOrder;
- use app\models\store\StoreProductRelation;
- use app\models\store\StoreSeckill;
- use app\models\user\User;
- use app\models\user\UserAddress;
- use app\models\user\UserBill;
- use app\models\user\UserExtract;
- use app\models\user\UserNotice;
- use crmeb\services\GroupDataService;
- use crmeb\services\UtilService;
- use think\facade\Db;
- use think\facade\Validate;
- /**
- * 用户类
- * Class UserController
- * @package app\api\controller\store
- */
- class UserController
- {
- /**
- * 获取用户信息
- * @param Request $request
- * @return mixed
- */
- public function userInfo(Request $request)
- {
- $info = $request->user()->toArray();
- $broken_time = intval(sys_config('extract_time'));
- $search_time = time() - 86400 * $broken_time;
- //返佣 +
- $brokerage_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
- ->where('add_time', '>', $search_time)
- ->where('pm', 1)
- ->sum('number');
- //退款退的佣金 -
- $refund_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
- ->where('add_time', '>', $search_time)
- ->where('pm', 0)
- ->sum('number');
- $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
- if ($info['broken_commission'] < 0)
- $info['broken_commission'] = 0;
- $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
- if ($info['commissionCount'] < 0)
- $info['commissionCount'] = 0;
- $info['grant'] = UserSingleAward::where('uid', $info['uid'])->sum('grant');
- $info['profit'] = intval(UserSingleAward::where('uid', $info['uid'])->sum('money') - $info['grant']);
- UserLevel::setLevelComplete($request->uid());
- return app('json')->success($info);
- }
- /**
- * 用户资金统计
- * @param Request $request
- * @return mixed
- * @throws \think\Exception
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function balance(Request $request)
- {
- $uid = $request->uid();
- $user['now_money'] = User::getUserInfo($uid, 'now_money')['now_money'];//当前总资金
- $user['recharge'] = UserBill::getRecharge($uid);//累计充值
- $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($uid);//累计消费
- return app('json')->successful($user);
- }
- /**
- * 个人中心
- * @param Request $request
- * @return mixed
- */
- public function user(Request $request)
- {
- $user = $request->user();
- $user = $user->toArray();
- $user['couponCount'] = StoreCouponUser::getUserValidCouponCount($user['uid']);
- $user['like'] = StoreProductRelation::getUserIdCollect($user['uid']);
- $user['orderStatusNum'] = StoreOrder::getOrderData($user['uid']);
- $user['notice'] = UserNotice::getNotice($user['uid']);
- // $user['brokerage'] = UserBill::getBrokerage($user['uid']);//获取总佣金
- $user['recharge'] = UserBill::getRecharge($user['uid']);//累计充值
- $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($user['uid']);//累计消费
- $user['extractTotalPrice'] = UserExtract::userExtractTotalPrice($user['uid']);//累计提现
- $user['extractPrice'] = $user['brokerage_price'];//可提现
- $user['statu'] = (int)sys_config('store_brokerage_statu');
- $broken_time = intval(sys_config('extract_time'));
- $search_time = time() - 86400 * $broken_time;
- if (!$user['is_promoter'] && $user['statu'] == 2) {
- $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']])->sum('pay_price');
- $status = is_brokerage_statu($price);
- if ($status) {
- User::where('uid', $user['uid'])->update(['is_promoter' => 1]);
- $user['is_promoter'] = 1;
- } else {
- $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
- $user['promoter_price'] = bcsub($storeBrokeragePrice, $price, 2);
- }
- }
- //可提现佣金
- //返佣 +
- $brokerage_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
- ->where('add_time', '>', $search_time)
- ->where('pm', 1)
- ->sum('number');
- //退款退的佣金 -
- $refund_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
- ->where('add_time', '>', $search_time)
- ->where('pm', 0)
- ->sum('number');
- $user['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
- if ($user['broken_commission'] < 0)
- $user['broken_commission'] = 0;
- $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
- if ($user['commissionCount'] < 0)
- $user['commissionCount'] = 0;
- if (!sys_config('vip_open'))
- $user['vip'] = false;
- else {
- $vipId = UserLevel::getUserLevel($user['uid']);
- $user['vip'] = $vipId !== false ? true : false;
- if ($user['vip']) {
- $user['vip_id'] = $vipId;
- $user['vip_icon'] = UserLevel::getUserLevelInfo($vipId, 'icon');
- $user['vip_name'] = UserLevel::getUserLevelInfo($vipId, 'name');
- }
- }
- $user['yesterDay'] = UserBill::yesterdayCommissionSum($user['uid']);
- $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
- $user['adminid'] = (boolean)\app\models\store\StoreService::orderServiceStatus($user['uid']);
- if ($user['phone'] && $user['user_type'] != 'h5') {
- $user['switchUserInfo'][] = $request->user();
- if ($h5UserInfo = User::where('account', $user['phone'])->where('user_type', 'h5')->find()) {
- $user['switchUserInfo'][] = $h5UserInfo;
- }
- } else if ($user['phone'] && $user['user_type'] == 'h5') {
- if ($wechatUserInfo = User::where('phone', $user['phone'])->where('user_type', '<>', 'h5')->find()) {
- $user['switchUserInfo'][] = $wechatUserInfo;
- }
- $user['switchUserInfo'][] = $request->user();
- } else if (!$user['phone']) {
- $user['switchUserInfo'][] = $request->user();
- }
- return app('json')->successful($user);
- }
- /**
- * 地址 获取单个
- * @param Request $request
- * @param $id
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function address(Request $request, $id)
- {
- $addressInfo = [];
- if ($id && is_numeric($id) && UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()])) {
- $addressInfo = UserAddress::find($id)->toArray();
- }
- return app('json')->successful($addressInfo);
- }
- /**
- * 地址列表
- * @param Request $request
- * @param $page
- * @param $limit
- * @return mixed
- */
- public function address_list(Request $request)
- {
- list($page, $limit) = UtilService::getMore([['page', 0], ['limit', 20]], $request, true);
- $list = UserAddress::getUserValidAddressList($request->uid(), $page, $limit, 'id,real_name,phone,province,city,district,detail,is_default');
- return app('json')->successful($list);
- }
- /**
- * 设置默认地址
- *
- * @param Request $request
- * @return mixed
- */
- public function address_default_set(Request $request)
- {
- list($id) = UtilService::getMore([['id', 0]], $request, true);
- if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
- if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
- return app('json')->fail('地址不存在!');
- $res = UserAddress::setDefaultAddress($id, $request->uid());
- if (!$res)
- return app('json')->fail('地址不存在!');
- else
- return app('json')->successful();
- }
- /**
- * 获取默认地址
- * @param Request $request
- * @return mixed
- */
- public function address_default(Request $request)
- {
- $defaultAddress = UserAddress::getUserDefaultAddress($request->uid(), 'id,real_name,phone,province,city,district,detail,is_default');
- if ($defaultAddress) {
- $defaultAddress = $defaultAddress->toArray();
- return app('json')->successful('ok', $defaultAddress);
- }
- return app('json')->successful('empty', []);
- }
- /**
- * 修改 添加地址
- * @param Request $request
- * @return mixed
- */
- public function address_edit(Request $request)
- {
- $addressInfo = UtilService::postMore([
- ['address', []],
- ['is_default', false],
- ['real_name', ''],
- ['post_code', ''],
- ['phone', ''],
- ['detail', ''],
- ['id', 0],
- ['type', 0]
- ], $request);
- if (!isset($addressInfo['address']['province'])) return app('json')->fail('收货地址格式错误!');
- if (!isset($addressInfo['address']['city'])) return app('json')->fail('收货地址格式错误!');
- if (!isset($addressInfo['address']['district'])) return app('json')->fail('收货地址格式错误!');
- if (!isset($addressInfo['address']['city_id']) && $addressInfo['type'] == 0) {
- return app('json')->fail('收货地址格式错误!请重新选择!');
- } else if ($addressInfo['type'] == 1) {
- $city = $addressInfo['address']['city'];
- $cityId = SystemCity::where('name', $city)->where('parent_id', '<>', 0)->value('city_id');
- if ($cityId) {
- $addressInfo['address']['city_id'] = $cityId;
- } else {
- if (!($cityId = SystemCity::where('parent_id', '<>', 0)->where('name', 'like', "%$city%")->value('city_id'))) {
- return app('json')->fail('收货地址格式错误!修改后请重新导入!');
- }
- $addressInfo['address']['city_id'] = $cityId;
- }
- }
- $addressInfo['province'] = $addressInfo['address']['province'];
- $addressInfo['city'] = $addressInfo['address']['city'];
- $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
- $addressInfo['district'] = $addressInfo['address']['district'];
- $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
- $addressInfo['uid'] = $request->uid();
- unset($addressInfo['address'], $addressInfo['type']);
- try {
- validate(AddressValidate::class)->check($addressInfo);
- } catch (ValidateException $e) {
- return app('json')->fail($e->getError());
- }
- if ($addressInfo['id'] && UserAddress::be(['id' => $addressInfo['id'], 'uid' => $request->uid(), 'is_del' => 0])) {
- $id = $addressInfo['id'];
- unset($addressInfo['id']);
- if (UserAddress::edit($addressInfo, $id, 'id')) {
- if ($addressInfo['is_default'])
- UserAddress::setDefaultAddress($id, $request->uid());
- return app('json')->successful();
- } else
- return app('json')->fail('编辑收货地址失败!');
- } else {
- $addressInfo['add_time'] = time();
- if ($address = UserAddress::create($addressInfo)) {
- if ($addressInfo['is_default']) {
- UserAddress::setDefaultAddress($address->id, $request->uid());
- }
- return app('json')->successful(['id' => $address->id]);
- } else {
- return app('json')->fail('添加收货地址失败!');
- }
- }
- }
- /**
- * 删除地址
- *
- * @param Request $request
- * @return mixed
- */
- public function address_del(Request $request)
- {
- list($id) = UtilService::postMore([['id', 0]], $request, true);
- if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
- if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
- return app('json')->fail('地址不存在!');
- if (UserAddress::edit(['is_del' => '1'], $id, 'id'))
- return app('json')->successful();
- else
- return app('json')->fail('删除地址失败!');
- }
- /**
- * 获取收藏产品
- *
- * @param Request $request
- * @return mixed
- */
- public function collect_user(Request $request)
- {
- list($page, $limit) = UtilService::getMore([
- ['page', 0],
- ['limit', 0]
- ], $request, true);
- if (!(int)$limit) return app('json')->successful([]);
- $productRelationList = StoreProductRelation::getUserCollectProduct($request->uid(), (int)$page, (int)$limit);
- return app('json')->successful($productRelationList);
- }
- /**
- * 添加收藏
- * @param Request $request
- * @param $id
- * @param $category
- * @return mixed
- */
- public function collect_add(Request $request)
- {
- list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
- if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
- $res = StoreProductRelation::productRelation($id, $request->uid(), 'collect', $category);
- if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
- else return app('json')->successful();
- }
- /**
- * 取消收藏
- *
- * @param Request $request
- * @return mixed
- */
- public function collect_del(Request $request)
- {
- list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
- if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
- $res = StoreProductRelation::unProductRelation($id, $request->uid(), 'collect', $category);
- if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
- else return app('json')->successful();
- }
- /**
- * 批量收藏
- * @param Request $request
- * @return mixed
- */
- public function collect_all(Request $request)
- {
- $collectInfo = UtilService::postMore([
- ['id', []],
- ['category', 'product'],
- ], $request);
- if (!count($collectInfo['id'])) return app('json')->fail('参数错误');
- $productIdS = $collectInfo['id'];
- $res = StoreProductRelation::productRelationAll($productIdS, $request->uid(), 'collect', $collectInfo['category']);
- if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
- else return app('json')->successful('收藏成功');
- }
- /**
- * 添加点赞
- *
- * @param Request $request
- * @return mixed
- */
- // public function like_add(Request $request)
- // {
- // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
- // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
- // $res = StoreProductRelation::productRelation($id,$request->uid(),'like',$category);
- // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
- // else return app('json')->successful();
- // }
- /**
- * 取消点赞
- *
- * @param Request $request
- * @return mixed
- */
- // public function like_del(Request $request)
- // {
- // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
- // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
- // $res = StoreProductRelation::unProductRelation($id, $request->uid(),'like',$category);
- // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
- // else return app('json')->successful();
- // }
- /**
- * 签到 配置
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function sign_config()
- {
- $signConfig = sys_data('sign_day_num') ?? [];
- return app('json')->successful($signConfig);
- }
- /**
- * 签到 列表
- * @param Request $request
- * @param $page
- * @param $limit
- * @return mixed
- */
- public function sign_list(Request $request)
- {
- list($page, $limit) = UtilService::getMore([
- ['page', 0],
- ['limit', 0]
- ], $request, true);
- if (!$limit) return app('json')->successful([]);
- $signList = UserSign::getSignList($request->uid(), (int)$page, (int)$limit);
- if ($signList) $signList = $signList->toArray();
- return app('json')->successful($signList);
- }
- /**
- * 签到
- * @param Request $request
- * @return mixed
- */
- public function sign_integral(Request $request)
- {
- $signed = UserSign::getIsSign($request->uid());
- if ($signed) return app('json')->fail('已签到');
- if (false !== ($integral = UserSign::sign($request->uid())))
- return app('json')->successful('签到获得' . floatval($integral) . '积分', ['integral' => $integral]);
- return app('json')->fail(UserSign::getErrorInfo('签到失败'));
- }
- /**
- * 签到用户信息
- * @param Request $request
- * @return mixed
- */
- public function sign_user(Request $request)
- {
- list($sign, $integral, $all) = UtilService::postMore([
- ['sign', 0],
- ['integral', 0],
- ['all', 0],
- ], $request, true);
- $user = $request->user();
- //是否统计签到
- if ($sign || $all) {
- $user['sum_sgin_day'] = UserSign::getSignSumDay($user['uid']);
- $user['is_day_sgin'] = UserSign::getIsSign($user['uid']);
- $user['is_YesterDay_sgin'] = UserSign::getIsSign($user['uid'], 'yesterday');
- if (!$user['is_day_sgin'] && !$user['is_YesterDay_sgin']) {
- $user['sign_num'] = 0;
- }
- }
- //是否统计积分使用情况
- if ($integral || $all) {
- $user['sum_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain');
- $user['deduction_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'deduction', '', true) ?? 0;
- $user['today_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain', 'today');
- }
- unset($user['pwd']);
- if (!$user['is_promoter']) {
- $user['is_promoter'] = (int)sys_config('store_brokerage_statu') == 2 ? true : false;
- }
- return app('json')->successful($user->hidden(['account', 'real_name', 'birthday', 'card_id', 'mark', 'partner_id', 'group_id', 'add_time', 'add_ip', 'phone', 'last_time', 'last_ip', 'spread_uid', 'spread_time', 'user_type', 'status', 'level', 'clean_time', 'addres'])->toArray());
- }
- /**
- * 签到列表(年月)
- *
- * @param Request $request
- * @return mixed
- */
- public function sign_month(Request $request)
- {
- list($page, $limit) = UtilService::getMore([
- ['page', 0],
- ['limit', 0]
- ], $request, true);
- if (!$limit) return app('json')->successful([]);
- $userSignList = UserSign::getSignMonthList($request->uid(), (int)$page, (int)$limit);
- return app('json')->successful($userSignList);
- }
- /**
- * 获取活动状态
- * @return mixed
- */
- public function activity()
- {
- $data['is_bargin'] = StoreBargain::validBargain() ? true : false;
- $data['is_pink'] = StoreCombination::getPinkIsOpen() ? true : false;
- $data['is_seckill'] = StoreSeckill::getSeckillCount() ? true : false;
- return app('json')->successful($data);
- }
- /**
- * 用户修改信息
- * @param Request $request
- * @return mixed
- */
- public function edit(Request $request)
- {
- list($avatar, $nickname) = UtilService::postMore([
- ['avatar', ''],
- ['nickname', ''],
- ], $request, true);
- if (User::editUser($avatar, $nickname, $request->uid())) return app('json')->successful('修改成功');
- return app('json')->fail('修改失败');
- }
- /**
- * 推广人排行
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function rank(Request $request)
- {
- $data = UtilService::getMore([
- ['page', ''],
- ['limit', ''],
- ['type', '']
- ], $request);
- $users = User::getRankList($data);
- return app('json')->success($users);
- }
- /**
- * 佣金排行
- * @param Request $request
- * @return mixed
- */
- public function brokerage_rank(Request $request)
- {
- $data = UtilService::getMore([
- ['page', ''],
- ['limit'],
- ['type']
- ], $request);
- return app('json')->success([
- 'rank' => User::brokerageRank($data),
- 'position' => User::currentUserRank($data['type'], $request->user()['brokerage_price'])
- ]);
- }
- /**
- * 添加访问记录
- * @param Request $request
- * @return mixed
- */
- public function set_visit(Request $request)
- {
- $data = UtilService::postMore([
- ['url', ''],
- ['stay_time', 0]
- ], $request);
- if ($data['url'] == '') return app('json')->fail('未获取页面路径');
- $data['uid'] = $request->uid();
- $data['ip'] = $request->ip();
- $data['add_time'] = time();
- $res = UserVisit::insert($data);
- if ($res) {
- return app('json')->success('添加访问记录成功');
- } else {
- return app('json')->fail('添加访问记录失败');
- }
- }
- /**
- * 静默绑定推广人
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function spread(Request $request)
- {
- $puid = $request->post('puid/d', 0);
- return app('json')->success(User::setSpread($puid, $request->uid()));
- }
- /**
- * 加权奖励
- * @param Request $request
- * @return void
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function weighting(Request $request)
- {
- // $userbll = UserBill::where('type', 'brokerage')->where('add_time', '=', 1675395623)->select();
- // foreach ($userbll as $item)
- // {
- // $user = User::where('uid', $item['uid'])->dec('brokerage_price', $item['number'])->update();
- // UserSingleAward::where('uid', $item['uid'])->dec('grant', $item['number'])->update();
- // }
- // halt(111);
- $start = strtotime('today');
- $end = strtotime('tomorrow');
- $money = StoreOrder::where('paid', 1)->whereBetweenTime('add_time', $start, $end)->where('is_explosion_order', 1)->sum('pay_price'); // 全网业绩
- $userUid = User::where('level', 1)->column('uid');//当前是会员的用户
- $userOrder = StoreOrder::where('paid', 1)->where('is_explosion_order', 1)->sum('total_num');
- $partnerCount = User::where('level', '>=',2)->count();//合伙人总数
- $shareholderCount = User::where('level', '>=',3)->count();// 股东总数
- $directorCount = User::where('level', '>=',4)->count();// 董事总数
- $partner = sys_config('partner')/100;//合伙人比列
- $shareholder = sys_config('shareholder')/100;//股东比列
- $director = sys_config('director')/100;//董事比列
- $user = User::where('level', '>=', 1)->select()->toArray();
- $userMoney = 0;
- if ($userOrder > 0){
- $userMoney = $money * $partner/$userOrder;//会员单个奖励
- }
- $partnerMoney = 0;
- if ($partnerCount > 0){
- $partnerMoney = $money * $partner/$partnerCount;//合伙人单个奖励
- }
- $shareholderMoney = 0;
- if ($shareholderCount > 0){
- $shareholderMoney = $money * $shareholder/$shareholderCount;//股东单个奖励
- }
- $directorMoney = 0;
- if ($directorCount > 0){
- $directorMoney = $money * $director/$directorCount;//董事单个奖励
- }
- if (!empty($user)) {
- foreach ($user as $vo) {
- $jl = 0;
- $str = '';
- $orderCount = StoreOrder::where('paid', 1)->where('uid', $vo['uid'])->where('is_explosion_order', 1)->sum('total_num');
- $jl += $userMoney * $orderCount;// 合伙人加权奖励
- $userjl = $userMoney * $orderCount;//会员分红
- $sp_jl = $userMoney * $orderCount;//上级奖励
- // if ($vo['level'] == 1) {
- // //会员
- // $orderCount = StoreOrder::where('paid', 1)->where('uid', $vo['uid'])->where('is_explosion_order', 1)->sum('total_num');
- // $jl += $userMoney * $orderCount;// 合伙人加权奖励
- // $userjl = $userMoney * $orderCount;//会员分红
- // $sp_jl = $userMoney * $orderCount;//上级奖励
- // } elseif ($vo['level'] == 2) {
- // //合伙人
- // $orderCount = StoreOrder::where('paid', 1)->where('uid', $vo['uid'])->where('is_explosion_order', 1)->sum('total_num');
- // $jl += $userMoney * $orderCount;// 会员分红
- // $jl += $partnerMoney;// 合伙人加权奖励
- // $userjl = $userMoney * $orderCount;//会员分红
- // $partnerjl = $partnerMoney;//合伙人分红
- // } elseif ($vo['level'] == 3) {
- if ($vo['level'] == 3){
- //股东
- $jl += $shareholderMoney;// 股东加权奖励
- $shareholderjl = $shareholderMoney;//股东分红
- } elseif ($vo['level'] == 4) {
- //董事
- $jl += $shareholderMoney;// 股东加权奖励
- $jl += $directorMoney;// 董事加权奖励
- $shareholderjl = $shareholderMoney;//股东分红
- $directorjl = $directorMoney;//股东分红
- }
- $model = new UserSingleAward();
- $award = $model->where('uid', $vo['uid'])->where('status', 0)->select()->toArray();
- if ($award) {
- $bdjl = 0; // 爆单剩余奖励
- $moneyjl = 0;//余额奖励
- foreach ($award as &$item) {
- $bs = $item['money'] /7000;
- $brokerage = $bs * 4000;//该订单应改获得的佣金
- $surplus = $item['money'] - $item['grant'];
- if($jl > 0){
- if ($item['grant'] < $brokerage){
- if ($item['grant'] + $jl > $brokerage){
- $bdjl += $brokerage - $item['grant'];//能够发放的佣金
- $moneyjl += $jl - $bdjl;// 能够发放的余额
- $item['grant'] += $jl;
- $jl = 0;
- }else{
- $bdjl = $jl;//能够发放的佣金
- $item['grant'] += $jl;
- $jl = 0;
- }
- }else{
- if ($surplus < $jl){
- $moneyjl += $surplus;
- $jl -= $surplus;
- $item['status'] = 1;
- $item['grant'] = $item['money'];
- }else{
- $moneyjl += $jl;
- $item['grant'] += $jl;
- $jl = 0;
- }
- }
- }
- }
- // $surplus = $item['money'] - $item['grant'];
- // if ($jl > 0){
- // if ($surplus < $jl) {
- // $bdjl += $surplus;
- // $jl -= $surplus;
- // $item['status'] = 1;
- // $item['grant'] = $item['money'];
- // } else {
- // $bdjl += $jl;
- // $item['grant'] += $jl;
- // $jl = 0;
- // }
- // }
- $model->saveAll($award);
- $nowUser = User::where('uid', $vo['uid'])->find();
- if ($bdjl > 0){
- UserBill::income('佣金', $vo['uid'], 'now_money', 'brokerage', $userjl, '', $nowUser['brokerage_price'] + $bdjl, '会员分红');
- if ($vo['level'] == 3){
- UserBill::income('佣金', $vo['uid'], 'now_money', 'brokerage', $shareholderjl, '', $nowUser['brokerage_price'] + $bdjl, '股东分红');
- }elseif ($vo['level'] == 4){
- UserBill::income('佣金', $vo['uid'], 'now_money', 'brokerage', $shareholderjl, '', $nowUser['brokerage_price'] + $bdjl, '股东分红');
- UserBill::income('佣金', $vo['uid'], 'now_money', 'brokerage', $directorjl, '', $nowUser['brokerage_price'] + $bdjl, '董事分红');
- }
- User::where('uid', $vo['uid'])->inc('brokerage_price', $bdjl)->update();
- }
- if ($moneyjl > 0){
- UserBill::income('余额', $vo['uid'], 'now_money', 'fhjl', $moneyjl, '', $nowUser['now_money']+$moneyjl, '分红余额奖励');
- User::where('uid', $vo['uid'])->inc('now_money', $moneyjl)->update();
- }
- // if ($vo['level'] == 1){
- // 如果是会员
- if ($vo['spread_uid']){
- $spread = User::where('uid', $vo['spread_uid'])->where('level', '>=', 1)->find();
- if ($spread){
- $model = new UserSingleAward();
- $award = $model->where('uid', $spread['uid'])->where('status', 0)->select()->toArray();
- if ($award){
- $bdjl = 0; // 爆单剩余奖励
- $moneyjl = 0;//余额奖励
- foreach ($award as &$item){
- $bs = $item['money'] /7000;
- $brokerage = $bs * 4000;//该订单应改获得的佣金
- $surplus = $item['money'] - $item['grant'];
- if($sp_jl > 0){
- if ($item['grant'] < $brokerage){
- if ($item['grant'] + $sp_jl > $brokerage){
- $bdjl += $brokerage - $item['grant'];//能够发放的佣金
- $moneyjl += $sp_jl - $bdjl;// 能够发放的余额
- $item['grant'] += $sp_jl;
- $sp_jl = 0;
- }else{
- $bdjl = $sp_jl;//能够发放的佣金
- $item['grant'] += $sp_jl;
- $sp_jl = 0;
- }
- }else{
- if ($surplus < $sp_jl){
- $moneyjl += $surplus;
- $sp_jl -= $surplus;
- $item['status'] = 1;
- $item['grant'] = $item['money'];
- }else{
- $moneyjl += $sp_jl;
- $item['grant'] += $sp_jl;
- $sp_jl = 0;
- }
- }
- }
- // if ($sp_jl > 0){
- // if ($surplus < $sp_jl){
- // $bdjl += $surplus;
- // $sp_jl -= $surplus;
- // $item['status'] = 1;
- // $item['grant'] = $item['money'];
- // }else{
- // $bdjl += $sp_jl;
- // $item['grant'] += $sp_jl;
- // $sp_jl = 0;
- // }
- // }
- }
- $model->saveAll($award);
- if ($bdjl > 0){
- UserBill::income('佣金', $spread['uid'], 'now_money', 'brokerage', $bdjl, '', $spread['brokerage_price']+$bdjl, '合伙人分红');
- User::where('uid', $spread['uid'])->inc('brokerage_price', $bdjl)->update();
- }
- if ($moneyjl > 0){
- UserBill::income('余额', $spread['uid'], 'now_money', 'fhjl', $moneyjl, '', $spread['now_money']+$moneyjl, '合伙人余额奖励');
- User::where('uid', $spread['uid'])->inc('now_money', $moneyjl)->update();
- }
- }
- }
- // }
- }
- }
- }
- }
- echo '发放成功';
- }
- public function transfer_account(Request $request)
- {
- $param = UtilService::postMore([
- ['uid', ''],
- ['price', ''],
- ['type', '']
- ]);
- Db::startTrans();
- if (empty($param['uid']))return app('json')->fail('账号不能为空');
- $user = User::where('uid', $request->uid())->lock(true)->find();
- $tr_user = User::where('uid', $param['uid'])->find();
- if (!$tr_user) return app('json')->fail('转账用户不存在');
- if ($user['uid'] == $param['uid']) return app('json')->fail('不能转给自己');
- if ($user['brokerage_price'] < $param['price']) return app('json')->fail('佣金余额不足');
- try {
- UserBill::expend('佣金', $request->uid(), 'now_money', 'extract', $param['price'], 0, $user['brokerage_price']-$param['price'], '佣金转账用户'.$tr_user['uid']);
- UserBill::income('佣金', $tr_user['uid'], 'now_money', 'brokerage', $param['price'], 0 , $tr_user['brokerage_price']+$param['price'], '接收用户'.$user['uid'].'转账佣金');
- User::where('uid', $request->uid())->dec('brokerage_price', $param['price'])->update();
- User::where('uid', $param['uid'])->inc('brokerage_price', $param['price'])->update();
- Db::commit();
- return app('json')->success('转账成功');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('转账失败');
- }
- }
- public function transfer_nowMoney(Request $request)
- {
- $param = UtilService::postMore([
- ['uid', ''],
- ['price', ''],
- ]);
- Db::startTrans();
- if (empty($param['uid']))return app('json')->fail('账号不能为空');
- $user = User::where('uid', $request->uid())->lock(true)->find();
- $tr_user = User::where('uid', $param['uid'])->find();
- if (!$tr_user) return app('json')->fail('转账用户不存在');
- if ($user['uid'] == $param['uid']) return app('json')->fail('不能转给自己');
- if ($user['now_money'] < $param['price']) return app('json')->fail('余额不足');
- try {
- UserBill::expend('转账余额', $request->uid(), 'now_money', 'zz_now_money', $param['price'], 0, $user['now_money']-$param['price'], '余额转账用户'.$tr_user['uid']);
- UserBill::income('转账余额', $tr_user['uid'], 'now_money', 'js_now_money', $param['price'], 0 , $tr_user['now_money']+$param['price'], '接收用户'.$user['uid'].'转账余额');
- User::where('uid', $request->uid())->dec('now_money', $param['price'])->update();
- User::where('uid', $param['uid'])->inc('now_money', $param['price'])->update();
- Db::commit();
- return app('json')->success('转账成功');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('转账失败');
- }
- }
- public function transformation(Request $request)
- {
- $param = UtilService::postMore([
- ['price', ''],
- ]);
- Db::startTrans();
- $user = User::where('uid', $request->uid())->lock(true)->find();
- if ($user['brokerage_price'] < $param['price']) return app('json')->fail('佣金不足');
- try {
- UserBill::expend('佣金', $request->uid(), 'now_money', 'extract', $param['price'], 0, $user['brokerage_price']-$param['price'], '佣金转换');
- UserBill::income('余额', $request->uid(), 'now_money', 'zh', $param['price'], 0 , $user['now_money']+$param['price'], '转换余额');
- User::where('uid', $request->uid())->dec('brokerage_price', $param['price'])->update();
- User::where('uid', $request->uid())->inc('now_money', $param['price'])->update();
- Db::commit();
- return app('json')->success('转换成功');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('转换失败');
- }
- }
- /**
- * 添加收款方式
- * @param Request $request
- * @return void
- */
- public function pay(Request $request)
- {
- $data = UtilService::postMore([
- ['payment'],
- ['image'],
- ['bank'],
- ['name'],
- ['type'],
- ['phone'],
- ['bank_name']
- ], $request);
- if (!$data['type']) return app('json')->fail('数据传入错误');
- $data['uid'] =$request->uid();
- $model = new UserPay();
- $pay = $model->where([['uid', '=', $request->uid()], ['type', '=', $data['type']]])->find();
- $res = Validate::rule([
- 'phone' => 'mobile'
- ]);
- $res->message([
- 'phone.mobile' => '请填写正确手机格式'
- ]);
- if (!$res->check($data)){
- return app('json')->fail($res->getError());
- }
- if (!empty($pay)){
- if ($data['type'] == 1 ){
- // 微信收款方式
- if (!$data['payment']) return app('json')->fail('微信账号不能为空');
- if (!$data['image']) return app('json')->fail('二维码不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- $pay['payment'] = $data['payment'];
- $pay['image'] = $data['image'];
- $pay['name'] = $data['name'];
- $pay['phone'] = $data['phone'];
- }elseif ($data['type'] == 2){
- // 支付宝收款方式
- if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- $pay['payment'] = $data['payment'];
- $pay['name'] = $data['name'];
- // $pay['phone'] = $data['phone'];
- }elseif ($data['type'] == 3){
- // 银行卡收款方式
- if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['bank']) return app('json')->fail('开户行不能为空');
- // if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- $pay['payment'] = $data['payment'];
- $pay['bank'] = $data['bank'];
- // $pay['bank_name'] = $data['bank_name'];
- $pay['phone'] = $data['phone'];
- $pay['name'] = $data['name'];
- }
- $res = $pay->save();
- if ($res) return app('json')->successful('修改成功');
- return app('json')->fail('修改失败');
- }else{
- if ($data['type'] == 1 ){
- // 微信收款方式
- if (!$data['payment']) return app('json')->fail('微信账号不能为空');
- if (!$data['image']) return app('json')->fail('二维码不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- }elseif ($data['type'] == 2){
- // 支付宝收款方式
- if (!$data['payment']) return app('json')->fail('支付宝账号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- }elseif ($data['type'] == 3){
- // 银行卡收款方式
- if (!$data['payment']) return app('json')->fail('银行卡号不能为空');
- if (!$data['name']) return app('json')->fail('姓名不能为空');
- if (!$data['bank']) return app('json')->fail('开户行不能为空');
- // if (!$data['bank_name']) return app('json')->fail('开户支行不能为空');
- if (!$data['phone']) return app('json')->fail('请填写手机号');
- }
- $res = $model->save($data);
- if ($res) return app('json')->successful('添加成功');
- return app('json')->fail('添加失败');
- }
- }
- /**
- * 收款方式详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function pay_list(Request $request)
- {
- $model = new UserPay();
- $list = $model->where('uid', $request->uid())->select();
- $list = empty($list)? []: $list->toArray();
- $data['wx'] = [];
- $data['zfb'] = [];
- $data['bank'] = [];
- foreach ($list as $k => $v){
- if ($v['type'] == 1){
- $data['wx'] = $v;
- }elseif ($v['type'] == 2){
- $data['zfb'] = $v;
- }elseif ($v['type'] == 3){
- $data['bank'] = $v;
- }
- }
- return app('json')->successful($data);
- }
- }
|