User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model;
  13. use app\api\model\shop\Shops;
  14. use app\api\model\user\CommissionsDetail;
  15. use app\common\library\helper;
  16. use think\facade\Cache;
  17. use app\common\model\User as UserModel;
  18. use think\facade\Log;
  19. use app\api\service\User as UserService;
  20. /**
  21. * 用户模型类
  22. * Class User
  23. * @package app\api\model
  24. */
  25. class User extends UserModel
  26. {
  27. /**
  28. * 隐藏字段
  29. * @var array
  30. */
  31. protected $hidden = [
  32. 'open_id',
  33. 'is_delete',
  34. 'store_id',
  35. 'update_time'
  36. ];
  37. /**
  38. * 获取器:隐藏手机号中间四位
  39. * @param $value
  40. * @return mixed
  41. */
  42. public function getMobileAttr($value)
  43. {
  44. /* if (strlen($value) === 11) {
  45. return hide_mobile($value);
  46. }*/
  47. return $value;
  48. }
  49. /**
  50. * 获取用户信息
  51. * @param $token
  52. * @return bool|static
  53. */
  54. public static function getUserByToken(string $token)
  55. {
  56. if (Cache::has($token)) {
  57. // 获取微信用户openid
  58. $userId = Cache::get($token)['user']['user_id']??0;
  59. // 获取用户信息s
  60. if($userId>0){
  61. return self::detail($userId);
  62. }
  63. return false;
  64. }
  65. return false;
  66. }
  67. /**
  68. * 切换门店
  69. * @param $userId
  70. * @param $shopId
  71. * @return bool
  72. */
  73. public static function switchBindShop($userId,$shopId){
  74. Log::error($userId);
  75. Log::error($shopId);
  76. if ($userId && $shopId){
  77. self::where('user_id',$userId)->update(['shop_id'=>$shopId]);
  78. return true;
  79. }
  80. return false;
  81. }
  82. /**
  83. * 店长邀请店员加入
  84. * @param $managerId
  85. * @return bool
  86. * @throws \app\common\exception\BaseException
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public static function updateShopId($managerId){
  92. $userId = UserService::getCurrentLoginUserId();
  93. $staff = User::find($userId);
  94. //$manager = User::find($managerId);
  95. $mobileM = StaffMobile::where('mobile',$staff->getAttr('mobile'))->where('user_id',$managerId)->find();
  96. if (!$mobileM || $mobileM->isEmpty()){
  97. Log::error(__METHOD__."此用户的手机号没有被店长记录");
  98. return false;
  99. }
  100. $mobile = $mobileM->mobile??'';
  101. $shopId = $mobileM->shop_id??0;
  102. if ($shopId){
  103. if ($staff->getAttr('mobile') != $mobile){
  104. Log::error('staff_mobile::'.$staff->getAttr('mobile'));
  105. Log::error('手机号不一致staff_mobile invite::'.$mobile);
  106. return false;
  107. }
  108. User::where('user_id',$userId)->update(['shop_id'=>$shopId,'role'=>User::SHOP_SELLER,'upper_user_id'=>0,'bind_shop_id'=>0]);
  109. StaffMobile::where('mobile_id',$mobileM->mobile_id)->delete();
  110. }
  111. return true;
  112. }
  113. public static function getShopInfo($managerId){
  114. $userId = UserService::getCurrentLoginUserId();
  115. $staff = User::find($userId);
  116. $mobileM = StaffMobile::where('mobile',$staff->getAttr('mobile'))->where('user_id',$managerId)->find();
  117. $shopId = $mobileM->shop_id??0;
  118. $shop_name = '';
  119. if ($shopId){
  120. $shop = Shops::where('shop_id',$shopId)->field('shop_name')->find();
  121. $shop_name = $shop?$shop['shop_name']:'';
  122. }
  123. return $shop_name;
  124. }
  125. //更新生日
  126. public function updateBirthday($birthday){
  127. $userId = UserService::getCurrentLoginUserId();
  128. $user = User::find($userId);
  129. if(empty($user->birthday)&&!empty($birthday)){
  130. User::where("user_id",$userId)->update(['birthday'=>$birthday]);
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * 分享注册的或者,自然注册的用户首次扫码门店的,更新绑定的门店
  137. * @param $sharerId int 分享者
  138. * @param int $scanQrcode 是否扫码登录
  139. * @return bool
  140. * @throws \app\common\exception\BaseException
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public static function updateNormalUserRelation($sharerId,$scanQrcode=0){
  146. $userId = UserService::getCurrentLoginUserId();
  147. $user = User::find($userId);
  148. if ($userId == $sharerId){
  149. Log::error('T');
  150. return true;
  151. }
  152. $sharer = User::find($sharerId);
  153. if(!$sharer){
  154. Log::error('TT');
  155. return false;
  156. }
  157. $need = false;$shopId = 0;
  158. if ($sharer->role == User::SHOP_SELLER){
  159. //无上级的普通用户
  160. if ($user->role == User::NORMAL_USER && $user->upper_user_id == 0){
  161. $need = true;
  162. $shopId = $sharer->shop_id??0;
  163. }
  164. //无上级的分销员
  165. if ($user->role == 99 && $user->upper_user_id == 0){
  166. $need = true;
  167. $shopId = $sharer->shop_id??0;
  168. }
  169. }
  170. if($sharer->role == 99 ){
  171. //无上级的普通用户
  172. if ($user->role == User::NORMAL_USER && $user->upper_user_id == 0){
  173. $need = true;
  174. }
  175. //分销员不能绑定上级
  176. if ($user->role == 99){
  177. $need = false;
  178. }
  179. /* //无上级的分销员
  180. if ($user->role == 99 && $user->upper_user_id == 0){
  181. $need = true;
  182. }*/
  183. }
  184. if ($scanQrcode == 1 && $need == true){
  185. User::where('user_id',$userId)->update(['bind_shop_id'=>$shopId,'upper_user_id'=>$sharerId]);
  186. Log::error('TTT');
  187. return true;
  188. }
  189. return true;
  190. }
  191. public function shop(){
  192. return $this->hasOne(Shops::class,'shop_id','shop_id')->field('shop_id,shop_name,province_id,city_id,region_id');
  193. }
  194. public function shops(){
  195. return $this->hasMany(Shops::class,'boss_user_id','user_id')->field('shop_id,shop_name,province_id,city_id,region_id');
  196. }
  197. /**
  198. * 统计直推人数
  199. * @param $userId
  200. * @param $shopId
  201. * @param $role
  202. * @return int
  203. */
  204. public static function countSubordinates($userId,$shopId,$role){
  205. if($role == self::COMMISSION_USER){
  206. return self::where(['upper_user_id'=>$userId])->count();
  207. }
  208. return self::where(['upper_user_id'=>$userId,'bind_shop_id'=>$shopId])->count();
  209. }
  210. /**
  211. * 统计绑定门店人数
  212. * @param $shopId
  213. * @return int
  214. */
  215. public static function countShopBinder($shopId){
  216. return self::where(['bind_shop_id'=>$shopId])->whereIn('role',[self::NORMAL_USER,user::COMMISSION_USER])->count();
  217. }
  218. public static function countShopSellers($shopId){
  219. return self::where(['shop_id'=>$shopId,'role'=>User::SHOP_SELLER])->count();
  220. }
  221. public function orders(){
  222. return $this->hasMany(Order::class,'user_id','user_id');
  223. }
  224. /**
  225. * 直推用户统计数据
  226. * @param $userId
  227. * @param int $type
  228. * @param string $keyword
  229. * @return array
  230. * @throws \think\db\exception\DataNotFoundException
  231. * @throws \think\db\exception\DbException
  232. * @throws \think\db\exception\ModelNotFoundException
  233. */
  234. public static function getBindersDetailOld($userId,$type = 1,$keyword = ''){
  235. //找到本人推广的订单ID
  236. $m = self::with('avatar')->field('user_id,mobile,nick_name,avatar_id,create_time');
  237. if ($type == 1){
  238. $m = $m->where('upper_user_id',$userId);
  239. $orderIds = CommissionsDetail::where('user_id',$userId)->select()->column('order_id');
  240. }else{
  241. $user = self::find($userId);
  242. if ($user->shop_id > 0){
  243. $m = $m->where('bind_shop_id',$user->shop_id)->whereIn('role',[self::NORMAL_USER,self::COMMISSION_USER]);
  244. }else{
  245. return null;
  246. }
  247. //找到本店推广的订单ID
  248. $orderIds = CommissionsDetail::where('shop_id',$user->shop_id)->select()->column('order_id');
  249. }
  250. if($keyword){
  251. $m = $m->where('nick_name','like',$keyword)->whereOr('mobile','like',$keyword);
  252. }
  253. return $m->withCount(['orders'=>function($query) use ($orderIds){
  254. //$query->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20);
  255. if (count($orderIds)){
  256. $query->whereIn('order_id',$orderIds)->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20);
  257. }else{
  258. $query->where('order_id',0);
  259. }
  260. }])
  261. ->withSum(['orders'=>function($query) use ($orderIds){
  262. //$query->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20);
  263. if (count($orderIds)){
  264. $query->whereIn('order_id',$orderIds)->where('pay_status',20)->where('pay_type','=',20)->where('receipt_status',20);
  265. }else{
  266. $query->where('order_id',0);
  267. }
  268. }],'total_price')->paginate(10)->toArray();
  269. }
  270. public static function getBindersDetail($userId,$type = 1,$keyword = '',$shopId=null){
  271. //找到本人推广的订单ID
  272. $user = self::find($userId);
  273. $m = self::with('avatar')->field('user_id,mobile,nick_name,avatar_id,create_time');
  274. $realShopId = 0;
  275. if ($type == 1){
  276. //我的顾客
  277. $m = $m->where('upper_user_id',$userId);
  278. }else{
  279. //门店关联消费者,门店顾客
  280. if ($user->role == User::SHOP_BOSS){
  281. $m = $m->where('bind_shop_id',$shopId)->whereIn('role',[self::NORMAL_USER,self::COMMISSION_USER]);
  282. $realShopId = $shopId;
  283. }else{
  284. if ($user->shop_id > 0){
  285. $m = $m->where('bind_shop_id',$user->shop_id)->whereIn('role',[self::NORMAL_USER,self::COMMISSION_USER]);
  286. $realShopId = $user->shop_id;
  287. }else{
  288. return null;
  289. }
  290. }
  291. }
  292. if($keyword){
  293. $m = $m->where(function ($query) use($keyword) {
  294. $query->where('nick_name', 'like','%'.$keyword.'%')
  295. ->whereOr('mobile','like','%'.$keyword.'%');
  296. });
  297. }
  298. //orders_count,orders_sum
  299. $users = $m->paginate(10)->toArray();
  300. //$userArr = &$users['data'];
  301. foreach ($users['data'] as &$u){
  302. $u['orders_count'] = 0;
  303. $u['orders_sum'] = '0.00';
  304. if ($type == 1){
  305. if($user->role == User::SHOP_SELLER){
  306. $u['orders_count'] = CommissionsDetail::where('shop_id',$user->shop_id)
  307. ->where('user_id',$userId)
  308. ->where('buyer_user_id',$u['user_id'])
  309. ->where('clearing_status','<',2)
  310. ->field('distinct order_id')->select()->count();
  311. $u['orders_sum'] = OrderGoods::sumShopBuyerGiveOutOrder($user->shop_id,1577808000,time(),$u['user_id'],$userId);
  312. }
  313. if ($user->role == User::COMMISSION_USER){
  314. $u['orders_count'] = CommissionsDetail::where('user_id',$userId)
  315. ->where('buyer_user_id',$u['user_id'])
  316. ->where('clearing_status','<',2)
  317. ->field('distinct order_id')->select()->count();
  318. $u['orders_sum'] = OrderGoods::sumComerGiveOutOrder(1577808000,time(),$u['user_id'],$userId);
  319. }
  320. }else{
  321. $u['orders_count'] = CommissionsDetail::where('shop_id',$realShopId)
  322. ->where('buyer_user_id',$u['user_id'])
  323. ->where('clearing_status','<',2)
  324. ->field('distinct order_id')->select()->count();
  325. $u['orders_sum'] = OrderGoods::sumShopBuyerGiveOutOrder($realShopId,1577808000,time(),$u['user_id']);
  326. }
  327. }
  328. return $users;
  329. }
  330. public function avatar(){
  331. return $this->hasOne(UploadFile::class,'file_id','avatar_id')->field('file_id,file_type,storage,domain,file_path');
  332. }
  333. public static function getShopStaffs($shopId){
  334. return self::where('shop_id',$shopId)->column('user_id');
  335. }
  336. public function commission(){
  337. return $this->hasMany(CommissionsDetail::class,'user_id','user_id')->field('commission_id');
  338. }
  339. /**
  340. * 统计员工业绩
  341. * @param $shopId
  342. * @param $from
  343. * @param $to
  344. * @return array
  345. * @throws \think\db\exception\DbException
  346. */
  347. public static function staffAches($shopId,$from,$to): array
  348. {
  349. return self::field('user_id,nick_name,avatar_id,mobile,can_withdraw_money')->with(['avatar'])
  350. ->where('shop_id',$shopId)->where('role',User::SHOP_SELLER)
  351. ->withCount(['commission'=>function($query) use ($shopId,$from,$to){
  352. $query->where('shop_id',$shopId)->where('clearing_status','<',2)->whereBetweenTime('create_time', $from, $to);
  353. }])->withSum(['commission'=>function($query) use ($shopId,$from,$to){
  354. $query->where('shop_id',$shopId)->where('clearing_status','<',2)->whereBetweenTime('create_time', $from, $to);
  355. }],'clearing_money')->order('commission_count','desc')->order('user_id','asc')
  356. ->paginate(15)->each(function ($item){
  357. $item->can_withdraw_money = helper::bcadd($item->commission_sum ,0,2);
  358. })->toArray();
  359. }
  360. public function mgShopJoin($userId,$rid,$shopId){
  361. try {
  362. $this->transaction(function () use ($userId,$rid,$shopId){
  363. User::where('user_id',$userId)->update(['role'=>User::SHOP_MG,'shop_id'=>$shopId]);
  364. Shops::where('shop_id',$shopId)->update(['manager_user_id'=>$userId]);
  365. HireMgs::where('id',$rid)->update(['status'=>1]);
  366. });
  367. }catch (\Exception $e){
  368. Log::error(__METHOD__.'::'.$e->getMessage());
  369. return false;
  370. }
  371. return true;
  372. }
  373. }