// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\model; use app\api\model\shop\Shops; use app\api\model\user\CommissionsDetail; use app\common\library\helper; use think\facade\Cache; use app\common\model\User as UserModel; use think\facade\Log; use app\api\service\User as UserService; /** * 用户模型类 * Class User * @package app\api\model */ class User extends UserModel { /** * 隐藏字段 * @var array */ protected $hidden = [ 'open_id', 'is_delete', 'store_id', 'update_time' ]; /** * 获取器:隐藏手机号中间四位 * @param $value * @return mixed */ public function getMobileAttr($value) { /* if (strlen($value) === 11) { return hide_mobile($value); }*/ return $value; } /** * 获取用户信息 * @param $token * @return bool|static */ public static function getUserByToken(string $token) { if (Cache::has($token)) { // 获取微信用户openid $userId = Cache::get($token)['user']['user_id']??0; // 获取用户信息s if($userId>0){ return self::detail($userId); } return false; } return false; } /** * 切换门店 * @param $userId * @param $shopId * @return bool */ public static function switchBindShop($userId,$shopId){ Log::error($userId); Log::error($shopId); if ($userId && $shopId){ self::where('user_id',$userId)->update(['shop_id'=>$shopId]); return true; } return false; } /** * 店长邀请店员加入 * @param $managerId * @return bool * @throws \app\common\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function updateShopId($managerId){ $userId = UserService::getCurrentLoginUserId(); $staff = User::find($userId); //$manager = User::find($managerId); $mobileM = StaffMobile::where('mobile',$staff->getAttr('mobile'))->where('user_id',$managerId)->find(); if (!$mobileM || $mobileM->isEmpty()){ Log::error(__METHOD__."此用户的手机号没有被店长记录"); return false; } $mobile = $mobileM->mobile??''; $shopId = $mobileM->shop_id??0; if ($shopId){ if ($staff->getAttr('mobile') != $mobile){ Log::error('staff_mobile::'.$staff->getAttr('mobile')); Log::error('手机号不一致staff_mobile invite::'.$mobile); return false; } User::where('user_id',$userId)->update(['shop_id'=>$shopId,'role'=>User::SHOP_SELLER,'upper_user_id'=>0,'bind_shop_id'=>0]); StaffMobile::where('mobile_id',$mobileM->mobile_id)->delete(); } return true; } public static function getShopInfo($managerId){ $userId = UserService::getCurrentLoginUserId(); $staff = User::find($userId); $mobileM = StaffMobile::where('mobile',$staff->getAttr('mobile'))->where('user_id',$managerId)->find(); $shopId = $mobileM->shop_id??0; $shop_name = ''; if ($shopId){ $shop = Shops::where('shop_id',$shopId)->field('shop_name')->find(); $shop_name = $shop?$shop['shop_name']:''; } return $shop_name; } //更新生日 public function updateBirthday($birthday){ $userId = UserService::getCurrentLoginUserId(); $user = User::find($userId); if(empty($user->birthday)&&!empty($birthday)){ User::where("user_id",$userId)->update(['birthday'=>$birthday]); return true; } return false; } /** * 分享注册的或者,自然注册的用户首次扫码门店的,更新绑定的门店 * @param $sharerId int 分享者 * @param int $scanQrcode 是否扫码登录 * @return bool * @throws \app\common\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function updateNormalUserRelation($sharerId,$scanQrcode=0){ $userId = UserService::getCurrentLoginUserId(); $user = User::find($userId); if ($userId == $sharerId){ Log::error('T'); return true; } $sharer = User::find($sharerId); if(!$sharer){ Log::error('TT'); return false; } $need = false;$shopId = 0; if ($sharer->role == User::SHOP_SELLER){ //无上级的普通用户 if ($user->role == User::NORMAL_USER && $user->upper_user_id == 0){ $need = true; $shopId = $sharer->shop_id??0; } //无上级的分销员 if ($user->role == 99 && $user->upper_user_id == 0){ $need = true; $shopId = $sharer->shop_id??0; } } if($sharer->role == 99 ){ //无上级的普通用户 if ($user->role == User::NORMAL_USER && $user->upper_user_id == 0){ $need = true; } //分销员不能绑定上级 if ($user->role == 99){ $need = false; } /* //无上级的分销员 if ($user->role == 99 && $user->upper_user_id == 0){ $need = true; }*/ } if ($scanQrcode == 1 && $need == true){ User::where('user_id',$userId)->update(['bind_shop_id'=>$shopId,'upper_user_id'=>$sharerId]); Log::error('TTT'); return true; } return true; } public function shop(){ return $this->hasOne(Shops::class,'shop_id','shop_id')->field('shop_id,shop_name,province_id,city_id,region_id'); } public function shops(){ return $this->hasMany(Shops::class,'boss_user_id','user_id')->field('shop_id,shop_name,province_id,city_id,region_id'); } /** * 统计直推人数 * @param $userId * @param $shopId * @param $role * @return int */ public static function countSubordinates($userId,$shopId,$role){ if($role == self::COMMISSION_USER){ return self::where(['upper_user_id'=>$userId])->count(); } return self::where(['upper_user_id'=>$userId,'bind_shop_id'=>$shopId])->count(); } /** * 统计绑定门店人数 * @param $shopId * @return int */ public static function countShopBinder($shopId){ return self::where(['bind_shop_id'=>$shopId])->whereIn('role',[self::NORMAL_USER,user::COMMISSION_USER])->count(); } public static function countShopSellers($shopId){ return self::where(['shop_id'=>$shopId,'role'=>User::SHOP_SELLER])->count(); } public function orders(){ return $this->hasMany(Order::class,'user_id','user_id'); } /** * 直推用户统计数据 * @param $userId * @param int $type * @param string $keyword * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getBindersDetailOld($userId,$type = 1,$keyword = ''){ //找到本人推广的订单ID $m = self::with('avatar')->field('user_id,mobile,nick_name,avatar_id,create_time'); if ($type == 1){ $m = $m->where('upper_user_id',$userId); $orderIds = CommissionsDetail::where('user_id',$userId)->select()->column('order_id'); }else{ $user = self::find($userId); if ($user->shop_id > 0){ $m = $m->where('bind_shop_id',$user->shop_id)->whereIn('role',[self::NORMAL_USER,self::COMMISSION_USER]); }else{ return null; } //找到本店推广的订单ID $orderIds = CommissionsDetail::where('shop_id',$user->shop_id)->select()->column('order_id'); } if($keyword){ $m = $m->where('nick_name','like',$keyword)->whereOr('mobile','like',$keyword); } return $m->withCount(['orders'=>function($query) use ($orderIds){ //$query->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20); if (count($orderIds)){ $query->whereIn('order_id',$orderIds)->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20); }else{ $query->where('order_id',0); } }]) ->withSum(['orders'=>function($query) use ($orderIds){ //$query->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20); if (count($orderIds)){ $query->whereIn('order_id',$orderIds)->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20); }else{ $query->where('order_id',0); } }],'total_price')->paginate(10)->toArray(); } public static function getBindersDetail($userId,$type = 1,$keyword = '',$shopId=null){ //找到本人推广的订单ID $user = self::find($userId); $m = self::with('avatar')->field('user_id,mobile,nick_name,avatar_id,create_time'); $realShopId = 0; if ($type == 1){ //我的顾客 $m = $m->where('upper_user_id',$userId); }else{ //门店关联消费者,门店顾客 if ($user->role == User::SHOP_BOSS){ $m = $m->where('bind_shop_id',$shopId)->whereIn('role',[self::NORMAL_USER,self::COMMISSION_USER]); $realShopId = $shopId; }else{ if ($user->shop_id > 0){ $m = $m->where('bind_shop_id',$user->shop_id)->whereIn('role',[self::NORMAL_USER,self::COMMISSION_USER]); $realShopId = $user->shop_id; }else{ return null; } } } if($keyword){ $m = $m->where(function ($query) use($keyword) { $query->where('nick_name', 'like','%'.$keyword.'%') ->whereOr('mobile','like','%'.$keyword.'%'); }); } //orders_count,orders_sum $users = $m->paginate(10)->toArray(); //$userArr = &$users['data']; foreach ($users['data'] as &$u){ $u['orders_count'] = 0; $u['orders_sum'] = '0.00'; if ($type == 1){ if($user->role == User::SHOP_SELLER){ $u['orders_count'] = CommissionsDetail::where('shop_id',$user->shop_id) ->where('user_id',$userId) ->where('buyer_user_id',$u['user_id']) ->where('clearing_status','<',2) ->field('distinct order_id')->select()->count(); $u['orders_sum'] = OrderGoods::sumShopBuyerGiveOutOrder($user->shop_id,1577808000,time(),$u['user_id'],$userId); } if ($user->role == User::COMMISSION_USER){ $u['orders_count'] = CommissionsDetail::where('user_id',$userId) ->where('buyer_user_id',$u['user_id']) ->where('clearing_status','<',2) ->field('distinct order_id')->select()->count(); $u['orders_sum'] = OrderGoods::sumComerGiveOutOrder(1577808000,time(),$u['user_id'],$userId); } }else{ $u['orders_count'] = CommissionsDetail::where('shop_id',$realShopId) ->where('buyer_user_id',$u['user_id']) ->where('clearing_status','<',2) ->field('distinct order_id')->select()->count(); $u['orders_sum'] = OrderGoods::sumShopBuyerGiveOutOrder($realShopId,1577808000,time(),$u['user_id']); } } return $users; } public function avatar(){ return $this->hasOne(UploadFile::class,'file_id','avatar_id')->field('file_id,file_type,storage,domain,file_path'); } public static function getShopStaffs($shopId){ return self::where('shop_id',$shopId)->column('user_id'); } public function commission(){ return $this->hasMany(CommissionsDetail::class,'user_id','user_id')->field('commission_id'); } /** * 统计员工业绩 * @param $shopId * @param $from * @param $to * @return array * @throws \think\db\exception\DbException */ public static function staffAches($shopId,$from,$to): array { return self::field('user_id,nick_name,avatar_id,mobile,can_withdraw_money')->with(['avatar']) ->where('shop_id',$shopId)->where('role',User::SHOP_SELLER) ->withCount(['commission'=>function($query) use ($shopId,$from,$to){ $query->where('shop_id',$shopId)->where('clearing_status','<',2)->whereBetweenTime('create_time', $from, $to); }])->withSum(['commission'=>function($query) use ($shopId,$from,$to){ $query->where('shop_id',$shopId)->where('clearing_status','<',2)->whereBetweenTime('create_time', $from, $to); }],'clearing_money')->order('commission_count','desc')->order('user_id','asc') ->paginate(15)->each(function ($item){ $item->can_withdraw_money = helper::bcadd($item->commission_sum ,0,2); })->toArray(); } public function mgShopJoin($userId,$rid,$shopId){ try { $this->transaction(function () use ($userId,$rid,$shopId){ User::where('user_id',$userId)->update(['role'=>User::SHOP_MG,'shop_id'=>$shopId]); Shops::where('shop_id',$shopId)->update(['manager_user_id'=>$userId]); HireMgs::where('id',$rid)->update(['status'=>1]); }); }catch (\Exception $e){ Log::error(__METHOD__.'::'.$e->getMessage()); return false; } return true; } }