User.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. namespace app\models\user;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreProduct;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\Exception;
  9. use think\facade\Session;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. use crmeb\traits\JwtAuthModelTrait;
  13. /**
  14. * TODO 用户Model
  15. * Class User
  16. * @package app\models\user
  17. */
  18. class User extends BaseModel
  19. {
  20. use JwtAuthModelTrait;
  21. use ModelTrait;
  22. protected $pk = 'uid';
  23. protected $name = 'user';
  24. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  25. protected $hidden = [
  26. 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
  27. ];
  28. protected function setAddTimeAttr($value)
  29. {
  30. return time();
  31. }
  32. protected function setAddIpAttr($value)
  33. {
  34. return app('request')->ip();
  35. }
  36. protected function setLastTimeAttr($value)
  37. {
  38. return time();
  39. }
  40. protected function setLastIpAttr($value)
  41. {
  42. return app('request')->ip();
  43. }
  44. /**
  45. * @param $wechatUser
  46. * @param int $spread_uid
  47. * @return bool
  48. * @throws DataNotFoundException
  49. * @throws DbException
  50. * @throws ModelNotFoundException
  51. */
  52. public static function setWechatUser($wechatUser, $spread_uid = 0)
  53. {
  54. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  55. $res = self::create([
  56. 'account' => 'wx' . $wechatUser['uid'] . time(),
  57. 'pwd' => md5(123456),
  58. 'nickname' => $wechatUser['nickname'] ?: '',
  59. 'avatar' => $wechatUser['headimgurl'] ?: '',
  60. 'add_time' => time(),
  61. 'add_ip' => app('request')->ip(),
  62. 'last_time' => time(),
  63. 'last_ip' => app('request')->ip(),
  64. 'uid' => $wechatUser['uid'],
  65. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  66. 'user_type' => 'wechat'
  67. ]);
  68. return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
  69. }
  70. /**
  71. * TODO 获取会员是否被清除过的时间
  72. * @param $uid
  73. * @return int|mixed
  74. * @throws DataNotFoundException
  75. * @throws ModelNotFoundException
  76. * @throws DbException
  77. */
  78. public static function getCleanTime($uid)
  79. {
  80. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  81. if (!$user) return 0;
  82. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  83. }
  84. /**
  85. * 更新用户信息
  86. * @param array $wechatUser 用户信息
  87. * @param int $uid 用户uid
  88. * @return bool|void
  89. * @throws DataNotFoundException
  90. * @throws ModelNotFoundException
  91. * @throws DbException
  92. */
  93. public static function updateWechatUser($wechatUser, $uid)
  94. {
  95. $userInfo = self::where('uid', $uid)->find();
  96. if (!$userInfo) return;
  97. //增加成为分销权限
  98. if (!$userInfo->is_promoter) {
  99. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  100. $status = is_brokerage_statu($price);
  101. } else {
  102. $status = false;
  103. }
  104. if ($userInfo->spread_uid) {
  105. $data = [
  106. // 'avatar' => $wechatUser['headimgurl'] ?: '',
  107. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  108. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  109. 'last_time' => time(),
  110. 'last_ip' => request()->ip(),
  111. ];
  112. if (!$userInfo['nickname']) {
  113. $data['nickname'] = $wechatUser['nickname'] ?: '';
  114. }
  115. if (!$userInfo['avatar']) {
  116. $data['avatar'] = $wechatUser['headimgurl'] ?: '';
  117. }
  118. // if ($data['nickname'] == '微信用户') unset($data['avatar']);
  119. // if ($data['nickname'] == '微信用户') unset($data['nickname']);
  120. return self::edit($data, $uid, 'uid');
  121. } else {
  122. $data = [
  123. // 'nickname' => $wechatUser['nickname'] ?: '',
  124. // 'avatar' => $wechatUser['headimgurl'] ?: '',
  125. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  126. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  127. // 'spread_uid' => 0,
  128. // 'spread_time' => 0,
  129. 'last_time' => time(),
  130. 'last_ip' => request()->ip(),
  131. ];
  132. if (!$userInfo['nickname']) {
  133. $data['nickname'] = $wechatUser['nickname'] ?: '';
  134. }
  135. if (!$userInfo['avatar']) {
  136. $data['avatar'] = $wechatUser['headimgurl'] ?: '';
  137. }
  138. // if ($data['nickname'] == '微信用户') unset($data['avatar']);
  139. // if ($data['nickname'] == '微信用户') unset($data['nickname']);
  140. //TODO 获取后台分销类型
  141. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  142. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  143. // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  144. // if ($storeBrokerageStatus == 1) {
  145. // $spreadCount = self::where('uid', $wechatUser['code'])->count();
  146. // if ($spreadCount) {
  147. // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  148. // if ($spreadInfo->is_promoter) {
  149. // //TODO 只有扫码才可以获得推广权限
  150. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  151. // }
  152. // }
  153. // }
  154. // $data['spread_uid'] = $wechatUser['code'];
  155. // $data['spread_time'] = time();
  156. // }
  157. return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
  158. }
  159. }
  160. /**
  161. * 设置推广关系
  162. * @param $spread
  163. * @param $uid
  164. * @return bool
  165. * @throws DataNotFoundException
  166. * @throws ModelNotFoundException
  167. * @throws DbException
  168. */
  169. public static function setSpread($spread, $uid)
  170. {
  171. return UserSpread::setSpread($uid, $spread);
  172. //当前用户信息
  173. // $userInfo = self::where('uid', $uid)->find();
  174. // if (!$userInfo) return true;
  175. // //当前用户有上级直接返回
  176. // if ($userInfo->spread_uid) return true;
  177. // //没有推广编号直接返回
  178. // if (!$spread) return true;
  179. // if ($spread == $uid) return true;
  180. // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  181. // //TODO 获取后台分销类型
  182. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  183. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  184. // if ($storeBrokerageStatus == 1) {
  185. // $spreadCount = self::where('uid', $spread)->count();
  186. // if ($spreadCount) {
  187. // $spreadInfo = self::where('uid', $spread)->find();
  188. // if ($spreadInfo->is_promoter) {
  189. // //TODO 只有扫码才可以获得推广权限
  190. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  191. // }
  192. // }
  193. // }
  194. // $data['spread_uid'] = $spread;
  195. // $data['spread_time'] = time();
  196. // return self::edit($data, $uid, 'uid');
  197. }
  198. /**
  199. * 小程序用户添加
  200. * @param $routineUser
  201. * @param int $spread_uid
  202. * @return object
  203. * @throws DataNotFoundException
  204. * @throws DbException
  205. * @throws ModelNotFoundException
  206. */
  207. public static function setRoutineUser($routineUser, $spread_uid = 0)
  208. {
  209. self::beginTrans();
  210. $res1 = true;
  211. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  212. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  213. $res2 = self::create([
  214. 'account' => 'rt' . $routineUser['uid'] . time(),
  215. 'pwd' => md5(123456),
  216. 'nickname' => $routineUser['nickname'] ?: '',
  217. 'avatar' => $routineUser['headimgurl'] ?: '',
  218. // 'spread_uid' => $spread_uid,
  219. 'is_promoter' => $storeBrokerageStatu != 1 ? 1 : 0,
  220. // 'spread_time' => $spread_uid ? time() : 0,
  221. 'uid' => $routineUser['uid'],
  222. 'add_time' => $routineUser['add_time'],
  223. 'add_ip' => request()->ip(),
  224. 'last_time' => time(),
  225. 'last_ip' => request()->ip(),
  226. 'user_type' => $routineUser['user_type']
  227. ]);
  228. $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
  229. self::checkTrans($res);
  230. return $res2;
  231. }
  232. /**
  233. * 获得当前登陆用户UID
  234. * @return int $uid
  235. */
  236. public static function getActiveUid()
  237. {
  238. $uid = null;
  239. $uid = Session::get('LoginUid');
  240. if ($uid) return $uid;
  241. else return 0;
  242. }
  243. /**
  244. * TODO 查询当前用户信息
  245. * @param $uid $uid 用户编号
  246. * @param string $field $field 查询的字段
  247. * @return array
  248. * @throws DataNotFoundException
  249. * @throws ModelNotFoundException
  250. * @throws DbException
  251. */
  252. public static function getUserInfo($uid, $field = '')
  253. {
  254. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  255. else $userInfo = self::where('uid', $uid)->find();
  256. if (!$userInfo) return [];
  257. return $userInfo->toArray();
  258. }
  259. /**
  260. * 判断当前用户是否推广员
  261. * @param int $uid
  262. * @return bool
  263. */
  264. public static function isUserSpread($uid = 0)
  265. {
  266. if (!$uid) return false;
  267. $status = (int)sys_config('store_brokerage_statu');
  268. $isPromoter = true;
  269. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  270. if ($isPromoter) return true;
  271. else return false;
  272. }
  273. /**
  274. * TODO 一级返佣
  275. * @param $orderInfo
  276. * @return bool
  277. * @throws DataNotFoundException
  278. * @throws ModelNotFoundException
  279. * @throws DbException
  280. */
  281. public static function backOrderBrokerage($orderInfo, bool $open = true)
  282. {
  283. //TODO 营销产品不返佣金
  284. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  285. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  286. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  287. $userInfo = User::getUserInfo($orderInfo['uid']);
  288. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  289. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  290. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  291. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  292. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
  293. //TODO 返佣金额小于等于0 直接返回不返佣金
  294. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  295. //TODO 获取上级推广员信息
  296. // $open && self::beginTrans();
  297. $res = true;
  298. if ($virtualBrokeragePrice > 0) {
  299. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  300. //TODO 上级推广员返佣之后的金额
  301. $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
  302. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  303. //TODO 添加推广记录
  304. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'brokerage_price', 'spread_award', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  305. //TODO 添加用户余额
  306. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  307. //TODO 一级返佣成功 跳转二级返佣
  308. $res = $res1 && $res2;
  309. }
  310. if ($realBrokeragePrice > 0) {
  311. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  312. //TODO 上级推广员返佣之后的金额
  313. $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  314. //TODO 添加推广记录
  315. $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'brokerage_price', 'spread_award', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfo['brokerage_price'], $mark, 0);
  316. }
  317. $res = $res && self::backOrderBrokerageTwo($orderInfo);
  318. // $open && self::checkTrans($res);
  319. return $res;
  320. }
  321. /**
  322. * TODO 二级推广
  323. * @param $orderInfo
  324. * @return bool
  325. * @throws DataNotFoundException
  326. * @throws ModelNotFoundException
  327. * @throws DbException
  328. */
  329. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  330. {
  331. //TODO 获取购买商品的用户
  332. $userInfo = User::getUserInfo($orderInfo['uid']);
  333. //TODO 获取上推广人
  334. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  335. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  336. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  337. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  338. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  339. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  340. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  341. //TODO 返佣金额小于等于0 直接返回不返佣金
  342. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  343. // $open && self::beginTrans();
  344. $res = true;
  345. if ($virtualBrokeragePrice > 0) {
  346. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  347. //TODO 上级推广员返佣之后的金额
  348. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  349. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  350. //TODO 添加推广记录
  351. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'brokerage_price', 'spread_award', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  352. //TODO 添加用户余额
  353. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  354. //TODO 一级返佣成功 跳转二级返佣
  355. $res = $res1 && $res2;
  356. }
  357. if ($realBrokeragePrice > 0) {
  358. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  359. //TODO 上级推广员返佣之后的金额
  360. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  361. //TODO 添加推广记录
  362. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'brokerage_price', 'spread_award', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  363. }
  364. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  365. // $open && self::checkTrans($res);
  366. return $res;
  367. }
  368. /**
  369. * 发放实际商品佣金
  370. * @param $orderInfo
  371. * @return bool
  372. * @throws DataNotFoundException
  373. * @throws DbException
  374. * @throws ModelNotFoundException
  375. */
  376. public static function sendBackOrderBrokerage($orderInfo)
  377. {
  378. $list = UserBill::where(['link_id' => $orderInfo['id'], 'category' => 'brokerage_price', 'pm' => 1, 'status' => 0])->select();
  379. if ($list) {
  380. self::beginTrans();
  381. try {
  382. foreach ($list as $v) {
  383. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  384. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  385. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  386. }
  387. self::commitTrans();
  388. return true;
  389. } catch (Exception $e) {
  390. self::rollbackTrans();
  391. return false;
  392. }
  393. }
  394. return true;
  395. }
  396. /**
  397. * 获取推荐人 暂无使用
  398. * @param $uid
  399. * @param $page
  400. * @param $limit
  401. * @return mixed
  402. */
  403. public static function getSpreadList($uid, $page, $limit)
  404. {
  405. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  406. foreach ($list as $k => $user) {
  407. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  408. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  409. }
  410. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  411. $data['count'] = $count;
  412. $data['list'] = $list;
  413. return $data;
  414. }
  415. /**
  416. * 获取某个用户的下级uid
  417. * @param $uid
  418. * @return array
  419. */
  420. public static function getOneSpreadUid($uid)
  421. {
  422. return self::where('spread_uid', $uid)->column('uid');
  423. }
  424. /**
  425. * 修改个人信息
  426. * @param $avatar 头像
  427. * @param $nickname 昵称
  428. * @param $uid 用户uid
  429. * @return bool
  430. */
  431. public static function editUser($avatar, $nickname, $uid)
  432. {
  433. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  434. }
  435. /**
  436. * TODO 获取推广人数 一级
  437. * @param int $uid
  438. * @return bool|int|string
  439. */
  440. public static function getSpreadCount($uid = 0)
  441. {
  442. if (!$uid) return false;
  443. return self::where('spread_uid', $uid)->count();
  444. }
  445. /**
  446. * 修改当前用户的推广人数
  447. * @param $uid
  448. */
  449. public static function setUserSpreadCount($uid)
  450. {
  451. if (!$uid) return true;
  452. if (self::getSpreadCount($uid) > 0) {
  453. self::where('uid', $uid)->update(['spread_count' => 0]);
  454. } else {
  455. self::where('uid', $uid)->update(['spread_count' => 0]);
  456. return true;
  457. }
  458. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  459. }
  460. /**
  461. * TODO 获取推广人数 二级
  462. * @param int $uid
  463. * @return bool|int|string
  464. */
  465. public static function getSpreadLevelCount($uid = 0)
  466. {
  467. if (!$uid) return false;
  468. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  469. if (!count($uidSubordinate)) return 0;
  470. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  471. }
  472. /**
  473. * TODO 获取推广人数 二级
  474. * @param int $uid
  475. * @return bool|int|string
  476. */
  477. public static function getTeamPerformance($uid = 0)
  478. {
  479. $group_users=[];
  480. $group_users = \app\admin\model\user\User::get_group_user($uid);
  481. // $achievement1 = \app\admin\model\order\StoreOrder::where('uid','in', $group_users)->where('paid', 1)->whereNotIn('status',-1)->sum('pay_price');
  482. $achievement1 = StoreOrder::where('uid','in', $group_users)->where('paid', 1)->whereNotIn('status',[-1])->sum('pay_price');
  483. $achievement2 = \app\admin\model\order\StoreOrder::where('uid','in', $group_users)->where('is_brokerage',1)->where('paid', 1)->whereNotIn('status',[-1])->sum('use_integral');
  484. @file_put_contents('quanju.txt', $achievement1 . "-团体支付金额\r\n", 8);
  485. @file_put_contents('quanju.txt', $achievement2 . "-团体支付健康币\r\n", 8);
  486. $achievement= bcadd($achievement1, $achievement2, 2);
  487. return $achievement;
  488. }
  489. /**
  490. * 获取用户下级推广人
  491. * @param int $uid 当前用户
  492. * @param int $grade 等级 0 一级 1 二级
  493. * @param string $orderBy 排序
  494. * @param string $keyword
  495. * @param int $page
  496. * @param int $limit
  497. * @return array|bool
  498. */
  499. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  500. {
  501. if (!$uid) return [];
  502. $gradeGroup = [0, 1];
  503. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  504. $userStair = self::where('spread_uid', $uid)->column('uid');
  505. if (!count($userStair)) return [];
  506. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  507. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  508. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  509. }
  510. /**
  511. * 获取团队信息
  512. * @param $uid
  513. * @param string $orderBy
  514. * @param string $keyword
  515. * @param int $page
  516. * @param int $limit
  517. * @return array
  518. */
  519. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  520. {
  521. $model = new self;
  522. if ($orderBy === '') $orderBy = 'u.add_time desc';
  523. $model = $model->alias(' u');
  524. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  525. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  526. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  527. $model = $model->where('u.uid', 'IN', $uid);
  528. $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");
  529. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  530. $model = $model->group('u.uid');
  531. $model = $model->order($orderBy);
  532. $model = $model->page($page, $limit);
  533. $list = $model->select();
  534. if ($list) $list = $list->toArray();
  535. else $list = [];
  536. foreach ($list as &$v) {
  537. if (!sys_config('vip_open'))
  538. $user['vip'] = false;
  539. else {
  540. $vipId = UserLevel::getUserLevel($v['uid']);
  541. $v['vip'] = $vipId !== false ? true : false;
  542. if ($v['vip']) {
  543. $v['vip_id'] = $vipId;
  544. $v['vip_icon'] = UserLevel::getUserLevelInfo($vipId, 'icon');
  545. $v['vip_name'] = UserLevel::getUserLevelInfo($vipId, 'name');
  546. }
  547. }
  548. }
  549. return $list;
  550. }
  551. /**
  552. * 设置用户的上级关系
  553. * @param $uid 用户uid
  554. * @param $spreadUid 上级用户uid
  555. * @return User|bool
  556. * @throws DataNotFoundException
  557. * @throws ModelNotFoundException
  558. * @throws DbException
  559. */
  560. public static function setSpreadUid($uid, $spreadUid)
  561. {
  562. return UserSpread::setSpread($uid, $spreadUid);
  563. // 自己不能绑定自己为上级
  564. // if ($uid == $spreadUid) return false;
  565. // //TODO 获取后台分销类型
  566. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  567. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  568. // if ($storeBrokerageStatus == 1) {
  569. // $spreadCount = self::where('uid', $spreadUid)->count();
  570. // if ($spreadCount) {
  571. // $spreadInfo = self::where('uid', $spreadUid)->find();
  572. // if ($spreadInfo->is_promoter) {
  573. // //TODO 只有扫码才可以获得推广权限
  574. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  575. // }
  576. // }
  577. // }
  578. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  579. }
  580. /**
  581. * 判断上下级关系是否存在
  582. * @param $uid
  583. * @param $spreadUid
  584. * @return bool|int
  585. */
  586. public static function validSpread($uid, $spreadUid)
  587. {
  588. if (!$uid || !$spreadUid) return false;
  589. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  590. }
  591. /**
  592. * H5用户注册
  593. * @param $account
  594. * @param $password
  595. * @param $spread
  596. * @return bool
  597. * @throws DataNotFoundException
  598. * @throws DbException
  599. * @throws ModelNotFoundException
  600. */
  601. public static function register($account, $password, $spread)
  602. {
  603. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  604. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  605. $phone = $account;
  606. $data['account'] = $account;
  607. $data['pwd'] = md5($password);
  608. $data['phone'] = $phone;
  609. // if ($spread) {
  610. // $data['spread_uid'] = $spread;
  611. // $data['spread_time'] = time();
  612. // }
  613. $data['real_name'] = '';
  614. $data['birthday'] = 0;
  615. $data['card_id'] = '';
  616. $data['mark'] = '';
  617. $data['addres'] = '';
  618. $data['user_type'] = 'h5';
  619. $data['add_time'] = time();
  620. $data['add_ip'] = app('request')->ip();
  621. $data['last_time'] = time();
  622. $data['last_ip'] = app('request')->ip();
  623. $data['nickname'] = substr(md5($account . time()), 0, 12);
  624. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  625. $data['city'] = '';
  626. $data['language'] = '';
  627. $data['province'] = '';
  628. $data['country'] = '';
  629. $data['is_promoter'] = $storeBrokerageStatu != 1 ? 1 : 0;
  630. self::beginTrans();
  631. $res2 = WechatUser::create($data);
  632. $data['uid'] = $res2->uid;
  633. $res1 = self::create($data);
  634. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  635. self::checkTrans($res);
  636. return $res;
  637. }
  638. /**
  639. * 密码修改
  640. * @param $account
  641. * @param $password
  642. * @return User|bool
  643. */
  644. public static function reset($account, $password)
  645. {
  646. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  647. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  648. if ($count) return true;
  649. return self::where('account', $account)->update(['pwd' => md5($password)]);
  650. }
  651. /**
  652. * 获取手机号是否注册
  653. * @param $phone
  654. * @return bool
  655. */
  656. public static function checkPhone($phone)
  657. {
  658. return self::be(['account' => $phone]);
  659. }
  660. /**
  661. * 获取推广人
  662. * @param $data 查询条件
  663. * @return array
  664. * @throws DataNotFoundException
  665. * @throws ModelNotFoundException
  666. * @throws \think\exception\DbException
  667. */
  668. public static function getRankList($data)
  669. {
  670. switch ($data['type']) {
  671. case 'week':
  672. $startTime = strtotime('this week');
  673. $endTime = time();
  674. break;
  675. case 'month':
  676. $startTime = strtotime('last month');
  677. $endTime = time();
  678. break;
  679. }
  680. $list = self::alias('t0')
  681. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  682. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  683. ->where('t1.spread_uid', '<>', 0)
  684. ->order('count desc')
  685. ->order('t0.uid desc')
  686. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  687. ->page($data['page'], $data['limit'])
  688. ->group('t0.uid')
  689. ->select();
  690. return count($list) ? $list->toArray() : [];
  691. }
  692. /**
  693. * 获取佣金排行
  694. * @param $data
  695. * @return array
  696. */
  697. public static function brokerageRank($data)
  698. {
  699. $model = self::where('status', 1);
  700. switch ($data['type']) {
  701. case 'week':
  702. $model = $model->whereIn('uid', function ($query) {
  703. $query->name('user_bill')->where('category', 'brokerage_price')->where('pm', 1)
  704. ->whereWeek('add_time')->field('uid');
  705. });
  706. break;
  707. case 'month':
  708. $model = $model->whereIn('uid', function ($query) {
  709. $query->name('user_bill')->where('category', 'brokerage_price')->where('pm', 1)
  710. ->whereMonth('add_time')->field('uid');
  711. });
  712. break;
  713. }
  714. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  715. ->page((int)$data['page'], (int)$data['limit'])->select();
  716. return count($users) ? $users->toArray() : [];
  717. }
  718. /**
  719. * 获取当前用户的佣金排行位置
  720. * @param $uid
  721. * @return int
  722. */
  723. public static function currentUserRank($type, $brokerage_price)
  724. {
  725. $model = self::where('status', 1);
  726. switch ($type) {
  727. case 'week':
  728. $model = $model->whereIn('uid', function ($query) {
  729. $query->name('user_bill')->where('category', 'brokerage_price')->where('pm', 1)
  730. ->whereWeek('add_time')->field('uid');
  731. });
  732. break;
  733. case 'month':
  734. $model = $model->whereIn('uid', function ($query) {
  735. $query->name('user_bill')->where('category', 'brokerage_price')->where('pm', 1)
  736. ->whereMonth('add_time')->field('uid');
  737. });
  738. break;
  739. }
  740. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  741. }
  742. }