12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013 |
- <?php
- namespace app\models\user;
- use app\models\store\StoreOrder;
- use app\models\store\StoreProduct;
- use crmeb\services\SystemConfigService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Exception;
- use think\facade\Session;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use crmeb\traits\JwtAuthModelTrait;
- /**
- * TODO 用户Model
- * Class User
- * @package app\models\user
- */
- class User extends BaseModel
- {
- use JwtAuthModelTrait;
- use ModelTrait;
- protected $pk = 'uid';
- protected $name = 'user';
- protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
- protected $hidden = [
- 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
- ];
- protected function setAddTimeAttr($value)
- {
- return time();
- }
- protected function setAddIpAttr($value)
- {
- return app('request')->ip();
- }
- protected function setLastTimeAttr($value)
- {
- return time();
- }
- protected function setLastIpAttr($value)
- {
- return app('request')->ip();
- }
- /**
- * @param $wechatUser
- * @param int $spread_uid
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function setWechatUser($wechatUser, $spread_uid = 0)
- {
- $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
- $res = self::create([
- 'account' => 'wx' . $wechatUser['uid'] . time(),
- 'pwd' => md5(123456),
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'add_time' => time(),
- 'add_ip' => app('request')->ip(),
- 'last_time' => time(),
- 'last_ip' => app('request')->ip(),
- 'uid' => $wechatUser['uid'],
- 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
- 'user_type' => 'wechat'
- ]);
- return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
- }
- /**
- * TODO 获取会员是否被清除过的时间
- * @param $uid
- * @return int|mixed
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function getCleanTime($uid)
- {
- $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
- if (!$user) return 0;
- return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
- }
- /**
- * 更新用户信息
- * @param array $wechatUser 用户信息
- * @param int $uid 用户uid
- * @return bool|void
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function updateWechatUser($wechatUser, $uid)
- {
- $userInfo = self::where('uid', $uid)->find();
- if (!$userInfo) return;
- //增加成为分销权限
- if (!$userInfo->is_promoter) {
- $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
- $status = is_brokerage_statu($price);
- } else {
- $status = false;
- }
- if ($userInfo->spread_uid) {
- return self::edit([
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
- 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
- ], $uid, 'uid');
- } else {
- $data = [
- 'nickname' => $wechatUser['nickname'] ?: '',
- 'avatar' => $wechatUser['headimgurl'] ?: '',
- 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
- 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
- // 'spread_uid' => 0,
- // 'spread_time' => 0,
- 'last_time' => time(),
- 'last_ip' => request()->ip(),
- ];
- //TODO 获取后台分销类型
- // $storeBrokerageStatus = sys_config('store_brokerage_statu');
- // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
- // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
- // if ($storeBrokerageStatus == 1) {
- // $spreadCount = self::where('uid', $wechatUser['code'])->count();
- // if ($spreadCount) {
- // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
- // if ($spreadInfo->is_promoter) {
- // //TODO 只有扫码才可以获得推广权限
- //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
- // }
- // }
- // }
- // $data['spread_uid'] = $wechatUser['code'];
- // $data['spread_time'] = time();
- // }
- return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
- }
- }
- /**
- * 设置推广关系
- * @param $spread
- * @param $uid
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function setSpread($spread, $uid)
- {
- return UserSpread::setSpread($uid, $spread);
- //当前用户信息
- // $userInfo = self::where('uid', $uid)->find();
- // if (!$userInfo) return true;
- // //当前用户有上级直接返回
- // if ($userInfo->spread_uid) return true;
- // //没有推广编号直接返回
- // if (!$spread) return true;
- // if ($spread == $uid) return true;
- // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
- // //TODO 获取后台分销类型
- // $storeBrokerageStatus = sys_config('store_brokerage_statu');
- // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
- // if ($storeBrokerageStatus == 1) {
- // $spreadCount = self::where('uid', $spread)->count();
- // if ($spreadCount) {
- // $spreadInfo = self::where('uid', $spread)->find();
- // if ($spreadInfo->is_promoter) {
- // //TODO 只有扫码才可以获得推广权限
- //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
- // }
- // }
- // }
- // $data['spread_uid'] = $spread;
- // $data['spread_time'] = time();
- // return self::edit($data, $uid, 'uid');
- }
- /**
- * 小程序用户添加
- * @param $routineUser
- * @param int $spread_uid
- * @return object
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function setRoutineUser($routineUser, $spread_uid = 0)
- {
- self::beginTrans();
- $res1 = true;
- if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
- $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
- $res2 = self::create([
- 'account' => 'rt' . $routineUser['uid'] . time(),
- 'pwd' => md5(123456),
- 'nickname' => $routineUser['nickname'] ?: '',
- 'avatar' => $routineUser['headimgurl'] ?: '',
- // 'spread_uid' => $spread_uid,
- 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
- // 'spread_time' => $spread_uid ? time() : 0,
- 'uid' => $routineUser['uid'],
- 'add_time' => $routineUser['add_time'],
- 'add_ip' => request()->ip(),
- 'last_time' => time(),
- 'last_ip' => request()->ip(),
- 'user_type' => $routineUser['user_type']
- ]);
- $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
- self::checkTrans($res);
- return $res2;
- }
- /**
- * 获得当前登陆用户UID
- * @return int $uid
- */
- public static function getActiveUid()
- {
- $uid = null;
- $uid = Session::get('LoginUid');
- if ($uid) return $uid;
- else return 0;
- }
- /**
- * TODO 查询当前用户信息
- * @param $uid $uid 用户编号
- * @param string $field $field 查询的字段
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function getUserInfo($uid, $field = '')
- {
- if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
- else $userInfo = self::where('uid', $uid)->find();
- if (!$userInfo) return [];
- return $userInfo->toArray();
- }
- /**
- * 判断当前用户是否推广员
- * @param int $uid
- * @return bool
- */
- public static function isUserSpread($uid = 0)
- {
- if (!$uid) return false;
- $status = (int)sys_config('store_brokerage_statu');
- $isPromoter = true;
- if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
- if ($isPromoter) return true;
- else return false;
- }
- /**
- * TODO 一级返佣
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function backOrderBrokerage($orderInfo, bool $open = true)
- {
- //TODO 营销产品不返佣金
- if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
- if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
- if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
- $userInfo = User::getUserInfo($orderInfo['uid']);
- //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
- if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
- if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
- $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
- list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($userInfo['uid'],$userInfo['spread_uid'],$cartId);
- //TODO 返佣金额小于等于0 直接返回不返佣金
- if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
- //TODO 获取上级推广员信息
- // $open && self::beginTrans();
- $res = true;
- if ($virtualBrokeragePrice > 0) {
- $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
- $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
- //TODO 添加推广记录
- $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
- //TODO 添加用户余额
- $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
- //TODO 一级返佣成功 跳转二级返佣
- $res = $res1 && $res2;
- }
- if ($realBrokeragePrice > 0) {
- $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
- $order_count = StoreOrder::where('uid', $userInfo['uid'])->where('paid' , 1)->count();
- //TODO 上级推广员返佣之后的金额
- //TODO 添加推广记录
- if ($order_count < 2){
- $brokerage_price = $realBrokeragePrice * 0.95;// 到账佣金
- $integral = $realBrokeragePrice * 0.05;// 到账积分
- $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokerage_price);
- $mark1 = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广积分' . floatval($integral);
- $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokerage_price, $orderInfo['id'], $spreadUserInfo['brokerage_price'] + $brokerage_price, $mark);
- $res = UserBill::income('获得推广积分', $userInfo['spread_uid'], 'integral', 'push', $integral, $orderInfo['id'], $spreadUserInfo['integral'] + $integral, $mark1);
- $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokerage_price, 'uid');
- $res2 = self::bcInc($userInfo['spread_uid'], 'integral', $integral, 'uid');
- if ($spreadUserInfo['identity'] == 1){
- $uid = getParent($userInfo['spread_uid'],User::select()); //找到最上级
- if (count($uid) > 0){
- $uid1 = array_pop($uid);
- $user1 = User::where('uid', $uid1)->find();
- if ($user1['line'] >= 3){
- $amministrazione = User::where('uid', $user1['spread_uid'])->find();// 发放管理奖
- if ($amministrazione){
- $brokerage_price = $realBrokeragePrice * 0.95;// 到账佣金
- $integral = $realBrokeragePrice * 0.05;// 到账积分
- UserBill::income('获得推广佣金', $uid1, 'now_money', 'brokerage', $brokerage_price, $orderInfo['id'], $amministrazione['brokerage_price'] + $brokerage_price, '管理佣金奖励');
- UserBill::income('获得推广积分', $uid1, 'integral', 'Level', $integral, $orderInfo['id'], $amministrazione['integral'] + $integral, '管理积分奖励');
- self::bcInc($uid1, 'brokerage_price', $brokerage_price, 'uid'); //发放管理奖
- self::bcInc($uid1, 'integral', $integral, 'uid'); //发放管理奖
- self::livello($amministrazione['spread_uid'], $realBrokeragePrice);
- }
- }
- }
- }elseif ($spreadUserInfo['identity'] == 2){
- self::livello($spreadUserInfo['spread_uid'], $realBrokeragePrice);
- }
- }else{
- $brokerage_price = $realBrokeragePrice * 0.95;// 到账佣金
- $integral = $realBrokeragePrice * 0.05;// 到账积分
- $mark = $userInfo['nickname'] . '复购成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokerage_price);
- $mark1 = $userInfo['nickname'] . '复购成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广积分' . floatval($integral);
- $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokerage_price, $orderInfo['id'], $spreadUserInfo['brokerage_price'] + $brokerage_price, $mark);
- $res = UserBill::income('获得推广积分', $userInfo['spread_uid'], 'integral', 'push', $integral, $orderInfo['id'], $spreadUserInfo['integral'] + $integral, $mark1);
- $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokerage_price, 'uid');
- $res2 = self::bcInc($userInfo['spread_uid'], 'integral', $integral, 'uid');
- }
- }
- // $res = $res && self::backOrderBrokerageTwo($orderInfo);
- // $open && self::checkTrans($res);
- return $res;
- }
- /**
- * 平级奖励
- * @param $uid
- * @param $price
- * @return void
- */
- public static function livello($uid, $price)
- {
- $uids = getParent_livello( $uid, User::select());
- if (count($uids) > 0){
- // 第一级店长
- if (isset($uids[0])){
- $user = User::where('uid', $uids[0])->find();
- $price1 = $price * 0.7;
- $brokerage_price = $price1 * 0.95;// 到账佣金
- $integral = $price1 * 0.05;// 到账积分
- UserBill::income('获得推广佣金', $user['uid'], 'now_money', 'brokerage', $brokerage_price, '', $user['brokerage_price'] + $brokerage_price, '店长平级佣金奖励');
- UserBill::income('获得推广积分', $user['uid'], 'integral', 'Level', $integral, '', $user['integral'] + $integral, '店长平级积分奖励');
- self::bcInc($user['uid'], 'brokerage_price', $brokerage_price, 'uid'); //发放管理奖
- self::bcInc($user['uid'], 'integral', $integral, 'uid'); //发放管理奖
- }
- if (isset($uids[1])){
- // 第二级店长
- $user = User::where('uid', $uids[1])->find();
- $price2 = $price * 0.3;
- $brokerage_price = $price2 * 0.95;// 到账佣金
- $integral = $price2 * 0.05;// 到账积分
- UserBill::income('获得推广佣金', $user['uid'], 'now_money', 'brokerage', $brokerage_price, '', $user['brokerage_price'] + $brokerage_price, '店长平级佣金奖励');
- UserBill::income('获得推广积分', $user['uid'], 'integral', 'Level', $integral, '', $user['integral'] + $integral, '店长平级积分奖励');
- self::bcInc($user['uid'], 'brokerage_price', $brokerage_price, 'uid'); //发放管理奖
- self::bcInc($user['uid'], 'integral', $integral, 'uid'); //发放管理奖
- }
- }
- }
- /**
- * TODO 二级推广
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
- {
- //TODO 获取购买商品的用户
- $userInfo = User::getUserInfo($orderInfo['uid']);
- //TODO 获取上推广人
- $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
- //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
- if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
- //TODO 获取后台分销类型 1 指定分销 2 人人分销
- if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
- $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
- list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
- //TODO 返佣金额小于等于0 直接返回不返佣金
- if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
- // $open && self::beginTrans();
- $res = true;
- if ($virtualBrokeragePrice > 0) {
- $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
- $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
- //TODO 添加推广记录
- $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
- //TODO 添加用户余额
- $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
- //TODO 一级返佣成功 跳转二级返佣
- $res = $res1 && $res2;
- }
- if ($realBrokeragePrice > 0) {
- $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
- //TODO 上级推广员返佣之后的金额
- $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
- //TODO 添加推广记录
- $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
- }
- // $res = $res && self::backOrderBrokerageTwo($orderInfo);
- // $open && self::checkTrans($res);
- return $res;
- }
- /**
- * 发放实际商品佣金
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function sendBackOrderBrokerage($orderInfo)
- {
- $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
- if ($list) {
- self::beginTrans();
- try {
- foreach ($list as $v) {
- self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
- $balance = self::getUserInfo($v['uid'])['brokerage_price'];
- UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
- }
- self::commitTrans();
- return true;
- } catch (Exception $e) {
- self::rollbackTrans();
- return false;
- }
- }
- return true;
- }
- /**
- * 获取推荐人 暂无使用
- * @param $uid
- * @param $page
- * @param $limit
- * @return mixed
- */
- public static function getSpreadList($uid, $page, $limit)
- {
- $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
- foreach ($list as $k => $user) {
- $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
- $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
- }
- $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
- $data['count'] = $count;
- $data['list'] = $list;
- return $data;
- }
- /**
- * 获取某个用户的下级uid
- * @param $uid
- * @return array
- */
- public static function getOneSpreadUid($uid)
- {
- return self::where('spread_uid', $uid)->column('uid');
- }
- /**
- * 修改个人信息
- * @param $avatar 头像
- * @param $nickname 昵称
- * @param $uid 用户uid
- * @return bool
- */
- public static function editUser($avatar, $nickname, $uid)
- {
- return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
- }
- /**
- * TODO 获取推广人数 一级
- * @param int $uid
- * @return bool|int|string
- */
- public static function getSpreadCount($uid = 0)
- {
- if (!$uid) return false;
- return self::where('spread_uid', $uid)->count();
- }
- /**
- * 修改当前用户的推广人数
- * @param $uid
- */
- public static function setUserSpreadCount($uid)
- {
- if (!$uid) return true;
- if (self::getSpreadCount($uid) > 0) {
- self::where('uid', $uid)->update(['spread_count' => 0]);
- } else {
- self::where('uid', $uid)->update(['spread_count' => 0]);
- return true;
- }
- return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
- }
- /**
- * TODO 获取推广人数 二级
- * @param int $uid
- * @return bool|int|string
- */
- public static function getSpreadLevelCount($uid = 0)
- {
- if (!$uid) return false;
- $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
- if (!count($uidSubordinate)) return 0;
- return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
- }
- /**
- * 获取用户下级推广人
- * @param int $uid 当前用户
- * @param int $grade 等级 0 一级 1 二级
- * @param string $orderBy 排序
- * @param string $keyword
- * @param int $page
- * @param int $limit
- * @return array|bool
- */
- public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
- {
- if (!$uid) return [];
- $gradeGroup = [0, 1];
- if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
- $userStair = self::where('spread_uid', $uid)->column('uid');
- if (!count($userStair)) return [];
- if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
- $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
- return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
- }
- /**
- * 获取团队信息
- * @param $uid
- * @param string $orderBy
- * @param string $keyword
- * @param int $page
- * @param int $limit
- * @return array
- */
- public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
- {
- $model = new self;
- if ($orderBy === '') $orderBy = 'u.add_time desc';
- $model = $model->alias(' u');
- $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
- ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
- $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
- $model = $model->where('u.uid', 'IN', $uid);
- $model = $model->field("u.uid,u.nickname,u.avatar,from_unixtime(u.add_time,'%Y/%m/%d') as time,u.spread_count as childCount,u.pay_count as orderCount,p.numberCount");
- if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
- $model = $model->group('u.uid');
- $model = $model->order($orderBy);
- $model = $model->page($page, $limit);
- $list = $model->select();
- if ($list) return $list->toArray();
- else return [];
- }
- /**
- * 设置用户的上级关系
- * @param $uid 用户uid
- * @param $spreadUid 上级用户uid
- * @return User|bool
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws DbException
- */
- public static function setSpreadUid($uid, $spreadUid)
- {
- return UserSpread::setSpread($uid, $spreadUid);
- // 自己不能绑定自己为上级
- // if ($uid == $spreadUid) return false;
- // //TODO 获取后台分销类型
- // $storeBrokerageStatus = sys_config('store_brokerage_statu');
- // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
- // if ($storeBrokerageStatus == 1) {
- // $spreadCount = self::where('uid', $spreadUid)->count();
- // if ($spreadCount) {
- // $spreadInfo = self::where('uid', $spreadUid)->find();
- // if ($spreadInfo->is_promoter) {
- // //TODO 只有扫码才可以获得推广权限
- // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
- // }
- // }
- // }
- // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
- }
- /**
- * 判断上下级关系是否存在
- * @param $uid
- * @param $spreadUid
- * @return bool|int
- */
- public static function validSpread($uid, $spreadUid)
- {
- if (!$uid || !$spreadUid) return false;
- return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
- }
- /**
- * H5用户注册
- * @param $account
- * @param $password
- * @param $spread
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function register($account, $password, $spread)
- {
- $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
- if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
- $phone = $account;
- $data['account'] = $account;
- $data['pwd'] = md5($password);
- $data['phone'] = $phone;
- // if ($spread) {
- // $data['spread_uid'] = $spread;
- // $data['spread_time'] = time();
- // }
- $data['real_name'] = '';
- $data['birthday'] = 0;
- $data['card_id'] = '';
- $data['mark'] = '';
- $data['addres'] = '';
- $data['user_type'] = 'h5';
- $data['add_time'] = time();
- $data['add_ip'] = app('request')->ip();
- $data['last_time'] = time();
- $data['last_ip'] = app('request')->ip();
- $data['nickname'] = substr(md5($account . time()), 0, 12);
- $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
- $data['city'] = '';
- $data['language'] = '';
- $data['province'] = '';
- $data['country'] = '';
- $data['is_promoter'] = $storeBrokerageStatu != 1 ? 1 : 0;
- self::beginTrans();
- $res2 = WechatUser::create($data);
- $data['uid'] = $res2->uid;
- $res1 = self::create($data);
- $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
- self::checkTrans($res);
- return $res;
- }
- /**
- * 密码修改
- * @param $account
- * @param $password
- * @return User|bool
- */
- public static function reset($account, $password)
- {
- if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
- $count = self::where('account', $account)->where('pwd', md5($password))->count();
- if ($count) return true;
- return self::where('account', $account)->update(['pwd' => md5($password)]);
- }
- /**
- * 获取手机号是否注册
- * @param $phone
- * @return bool
- */
- public static function checkPhone($phone)
- {
- return self::be(['account' => $phone]);
- }
- /**
- * 获取推广人
- * @param $data 查询条件
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getRankList($data)
- {
- switch ($data['type']) {
- case 'week':
- $startTime = strtotime('this week');
- $endTime = time();
- break;
- case 'month':
- $startTime = strtotime('last month');
- $endTime = time();
- break;
- }
- $list = self::alias('t0')
- ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
- ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
- ->where('t1.spread_uid', '<>', 0)
- ->order('count desc')
- ->order('t0.uid desc')
- ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
- ->page($data['page'], $data['limit'])
- ->group('t0.uid')
- ->select();
- return count($list) ? $list->toArray() : [];
- }
- /**
- * 获取佣金排行
- * @param $data
- * @return array
- */
- public static function brokerageRank($data)
- {
- $model = self::where('status', 1);
- switch ($data['type']) {
- case 'week':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereWeek('add_time')->field('uid');
- });
- break;
- case 'month':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereMonth('add_time')->field('uid');
- });
- break;
- }
- $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
- ->page((int)$data['page'], (int)$data['limit'])->select();
- return count($users) ? $users->toArray() : [];
- }
- /**
- * 获取当前用户的佣金排行位置
- * @param $uid
- * @return int
- */
- public static function currentUserRank($type, $brokerage_price)
- {
- $model = self::where('status', 1);
- switch ($type) {
- case 'week':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereWeek('add_time')->field('uid');
- });
- break;
- case 'month':
- $model = $model->whereIn('uid', function ($query) {
- $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
- ->whereMonth('add_time')->field('uid');
- });
- break;
- }
- return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
- }
- /**
- * 升级店长店员
- * @param $uid
- * @return void
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function upgrade($uid)
- {
- $user = User::where('uid', $uid)->find();
- if ($user['identity'] == 0){
- $price = StoreOrder::where('uid', $uid)->where('paid', 1)->sum('pay_price');
- if ($price >= sys_config('cumulative')){
- $user['identity'] = 1;
- if ($user['spread_uid']){
- $spd = User::where('uid', $user['spread_uid'])->find();
- $user['line'] = $spd['lines'] + 1;
- $spd['lines'] += 1;
- $spd->save();
- }
- }
- }elseif ($user['identity'] == 1){
- $count = User::where('spread_uid', $user['uid'])->where('identity', 1)->count();
- if ($count >= sys_config('push_clerk')){
- $user['identity'] = 2;
- }
- }
- $res = $user->save();
- if ($user['spread_uid']){
- $spd = User::where('uid', $user['spread_uid'])->find();
- $count = User::where('spread_uid', $spd['uid'])->where('identity', 1)->count();
- if ($count >= sys_config('push_clerk')){
- $spd['identity'] = 2;
- }
- $spd->save();
- }
- if ($res) return true;
- return false;
- }
- /**
- * 团队奖励分红
- * @return void
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function bonus()
- {
- if (!cache('bonus')){
- $user = User::select();
- foreach ($user as $item)
- {
- $start_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
- $end_time = date('Y-m-d 23:59:59', strtotime(-date('d').'day'));
- $price = StoreOrder::whereBetweenTime('add_time', $start_time, $end_time)->where('paid', 1)->where('uid', $item['uid'])->sum('pay_price');// 月结
- if ($item['spread_uid'] > 0 and $price > 0){
- $spread = getParents($item['spread_uid'], $user->toArray());// 找到所有上级
- $v1 = 0;
- $v2 = 0;
- $v3 = 0;
- $one = sys_config('area')/100; // 区代
- $tow = sys_config('city')/100; // 市代
- $three = sys_config('partner')/100;// 合伙人
- foreach ($spread as $value) {
- $details = User::where('uid', $value)->find();
- if ($details['level'] > 0){
- if ($details['level'] == 1){
- if ($v2 == 0 and $v3 == 0){
- // 没有发放市代和合伙人的奖励
- if ($v1 == 0) { // 没有发放v1的奖励
- $jl = $price * $one;
- $details['dynamic_integral'] += $jl; // 区代团队奖励
- $v1++;
- }
- }
- }elseif ($details['level'] == 2){
- if ($v3 == 0){
- // 没有发放合伙人的奖励
- if ($v1 == 0 and $v2 == 0) { // 没有发放区代和市代的奖励的奖励
- $jl = $price * $tow;// 市代团队奖励
- $v2++;
- }elseif ($v1 > 0 and $v2 == 0){// 发放区代奖励,没有发放市代的奖励
- $jl = $price * ($tow - $one); // 市代团队奖励
- $v2++;
- }
- }
- }elseif ($details['level'] == 3){
- if ($v1 == 0 and $v2 == 0 and $v3 == 0) { // 没有发放区代和市代,合伙人的奖励的奖励
- $jl = $price * $three; // 合伙人团队奖励
- $v3++;
- }elseif ($v1 > 0 and $v2 == 0 and $v3 == 0){// 发放区代奖励,没有发放市代,合伙人的奖励
- $jl = $price * ($three - $one); // 合伙人团队奖励
- $v3++;
- }elseif ($v1 == 0 and $v2 > 0 and $v3 == 0){// 没有发放区代奖励,发放市代的奖励,没有发放合伙人的奖励
- $jl = ($price * ($three - $tow)); // 合伙人团队奖励
- $v3++;
- }elseif ($v1 > 0 and $v2 > 0 and $v3 == 0){// 发放区代奖励,发放市代的奖励,发放合伙人的奖励
- $jl = ($price * ($three - $tow)); // 合伙人团队奖励
- $v3++;
- }
- }
- if ($jl > 0){
- $brokerage_price = $jl * 0.95;// 到账佣金
- $integral = $jl * 0.05;// 到账积分
- User::where('uid', $value)->inc('brokerage_price', $brokerage_price)->update();
- User::where('uid', $value)->inc('integral', $integral)->update();
- if ($details['level'] == 3) {
- $bl = $three;
- if ($v2 > 0 and $v1 > 0) $bl -= $tow;
- if ($v2 > 0 and $v1 == 0) $bl -= $tow;
- if ($v2 == 0 and $v1 > 0) $bl -= $one;
- }
- if ($details['level'] == 2){
- $bl = $tow;
- if ($v1 > 0) $bl -= $one;
- }
- if ($details['level'] == 1) $bl = $one;
- UserBill::income('获得推广佣金', $details['uid'], 'now_money', 'brokerage', $brokerage_price, $item['uid'],$details['brokerage_price'] + $brokerage_price, '用户'.$item['uid'].'月结'.$price.'团队奖励比例'.$bl.'的95%佣金');
- UserBill::income('获得推广积分', $details['uid'], 'integral', 'bonus', $integral, $item['uid'],$details['integral'] + $integral, '用户'.$item['uid'].'月结'.$price.'团队奖励比例'.$bl.'的5%积分');
- }
- $jl = 0;
- }
- }
- }
- }
- cache('bonus', 1, 86400);
- }
- }
- /**
- * 获取ID
- * @param $key
- * @return mixed
- */
- public static function getkeytoid($key)
- {
- $rs = self::order('uid DESC')->find();
- return $rs['uid']+1;
- }
- /**
- * 联创分红
- * @return void
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function creation()
- {
- $user = User::where('level', 4)->select();
- if (count($user) > 0){
- $start_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
- $end_time = date('Y-m-d 23:59:59', strtotime(-date('d').'day'));
- $price = StoreOrder::whereBetweenTime('add_time',$start_time, $end_time)->where('paid', 1)->sum('pay_price');// 月结
- if ($price > 0){
- foreach ($user as $item)
- {
- $brokerage_price = ($price * sys_config('link')/100) * 0.95;// 到账佣金
- $integral = ($price * sys_config('link')/100) * 0.05;// 到账积分
- User::where('uid', $item['uid'])->inc('brokerage_price', $brokerage_price)->update();
- User::where('uid', $item['uid'])->inc('integral', $integral)->update();
- UserBill::income('联创分红佣金', $item['uid'], 'now_money', 'brokerage', $brokerage_price, $item['uid'],$item['brokerage_price'] + $brokerage_price, '联创分红奖励比例'.(sys_config('link')/100).'的95%佣金');
- UserBill::income('联创分红积分', $item['uid'], 'integral', 'creation', $integral, $item['uid'],$item['integral'] + $integral, '联创分红奖励比例'.(sys_config('link')/100).'的5%积分');
- }
- }
- }
- }
- }
|