User.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. <?php
  2. namespace app\models\user;
  3. use app\models\auction\AuctionBooking;
  4. use app\models\auction\AuctionOrder;
  5. use app\models\auction\AuctionReward;
  6. use app\models\store\StoreOrder;
  7. use app\models\store\StoreProduct;
  8. use crmeb\services\SystemConfigService;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\Exception;
  13. use think\facade\Session;
  14. use crmeb\traits\ModelTrait;
  15. use crmeb\basic\BaseModel;
  16. use crmeb\traits\JwtAuthModelTrait;
  17. /**
  18. * TODO 用户Model
  19. * Class User
  20. * @package app\models\user
  21. */
  22. class User extends BaseModel
  23. {
  24. use JwtAuthModelTrait;
  25. use ModelTrait;
  26. protected $pk = 'uid';
  27. protected $name = 'user';
  28. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  29. protected $hidden = [
  30. 'add_ip', 'add_time', 'account', 'clean_time', 'last_ip', 'last_time', 'pwd', 'status', 'mark', 'pwd'
  31. ];
  32. protected function setAddTimeAttr($value)
  33. {
  34. return time();
  35. }
  36. protected function setAddIpAttr($value)
  37. {
  38. return app('request')->ip();
  39. }
  40. protected function setLastTimeAttr($value)
  41. {
  42. return time();
  43. }
  44. protected function setLastIpAttr($value)
  45. {
  46. return app('request')->ip();
  47. }
  48. /**
  49. * @param $wechatUser
  50. * @param int $spread_uid
  51. * @return bool
  52. * @throws DataNotFoundException
  53. * @throws DbException
  54. * @throws ModelNotFoundException
  55. */
  56. public static function setWechatUser($wechatUser, $spread_uid = 0)
  57. {
  58. $res = self::create([
  59. 'account' => 'wx' . $wechatUser['uid'] . time(),
  60. 'pwd' => md5(123456),
  61. 'nickname' => $wechatUser['nickname'] ?: '',
  62. 'avatar' => $wechatUser['headimgurl'] ?: '',
  63. 'add_time' => time(),
  64. 'add_ip' => app('request')->ip(),
  65. 'last_time' => time(),
  66. 'last_ip' => app('request')->ip(),
  67. 'uid' => $wechatUser['uid'],
  68. 'user_type' => 'wechat'
  69. ]);
  70. return $res && UserSpread::setSpread($wechatUser['uid'], $spread_uid);
  71. }
  72. /**
  73. * TODO 获取会员是否被清除过的时间
  74. * @param $uid
  75. * @return int|mixed
  76. * @throws DataNotFoundException
  77. * @throws ModelNotFoundException
  78. * @throws DbException
  79. */
  80. public static function getCleanTime($uid)
  81. {
  82. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  83. if (!$user) return 0;
  84. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  85. }
  86. /**
  87. * 更新用户信息
  88. * @param array $wechatUser 用户信息
  89. * @param int $uid 用户uid
  90. * @return bool|void
  91. * @throws DataNotFoundException
  92. * @throws ModelNotFoundException
  93. * @throws DbException
  94. */
  95. public static function updateWechatUser($wechatUser, $uid)
  96. {
  97. $userInfo = self::where('uid', $uid)->find();
  98. if (!$userInfo) return;
  99. //增加成为分销权限
  100. if (!$userInfo->is_promoter) {
  101. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $uid])->sum('pay_price');
  102. $status = is_brokerage_statu($price);
  103. } else {
  104. $status = false;
  105. }
  106. if ($userInfo->spread_uid) {
  107. return self::edit([
  108. 'nickname' => $wechatUser['nickname'] ?: '',
  109. 'avatar' => $wechatUser['headimgurl'] ?: '',
  110. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  111. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  112. ], $uid, 'uid');
  113. } else {
  114. $data = [
  115. 'nickname' => $wechatUser['nickname'] ?: '',
  116. 'avatar' => $wechatUser['headimgurl'] ?: '',
  117. 'is_promoter' => $status ? 1 : $userInfo->is_promoter,
  118. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  119. // 'spread_uid' => 0,
  120. // 'spread_time' => 0,
  121. 'last_time' => time(),
  122. 'last_ip' => request()->ip(),
  123. ];
  124. //TODO 获取后台分销类型
  125. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  126. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  127. // if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  128. // if ($storeBrokerageStatus == 1) {
  129. // $spreadCount = self::where('uid', $wechatUser['code'])->count();
  130. // if ($spreadCount) {
  131. // $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  132. // if ($spreadInfo->is_promoter) {
  133. // //TODO 只有扫码才可以获得推广权限
  134. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  135. // }
  136. // }
  137. // }
  138. // $data['spread_uid'] = $wechatUser['code'];
  139. // $data['spread_time'] = time();
  140. // }
  141. return self::edit($data, $uid, 'uid') && UserSpread::setSpread($uid, $wechatUser['code'] ?? 0);
  142. }
  143. }
  144. /**
  145. * 设置推广关系
  146. * @param $spread
  147. * @param $uid
  148. * @return bool
  149. * @throws DataNotFoundException
  150. * @throws ModelNotFoundException
  151. * @throws DbException
  152. */
  153. public static function setSpread($spread, $uid)
  154. {
  155. return UserSpread::setSpread($uid, $spread);
  156. //当前用户信息
  157. // $userInfo = self::where('uid', $uid)->find();
  158. // if (!$userInfo) return true;
  159. // //当前用户有上级直接返回
  160. // if ($userInfo->spread_uid) return true;
  161. // //没有推广编号直接返回
  162. // if (!$spread) return true;
  163. // if ($spread == $uid) return true;
  164. // if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  165. // //TODO 获取后台分销类型
  166. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  167. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  168. // if ($storeBrokerageStatus == 1) {
  169. // $spreadCount = self::where('uid', $spread)->count();
  170. // if ($spreadCount) {
  171. // $spreadInfo = self::where('uid', $spread)->find();
  172. // if ($spreadInfo->is_promoter) {
  173. // //TODO 只有扫码才可以获得推广权限
  174. //// if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  175. // }
  176. // }
  177. // }
  178. // $data['spread_uid'] = $spread;
  179. // $data['spread_time'] = time();
  180. // return self::edit($data, $uid, 'uid');
  181. }
  182. /**
  183. * 小程序用户添加
  184. * @param $routineUser
  185. * @param int $spread_uid
  186. * @return object
  187. * @throws DataNotFoundException
  188. * @throws DbException
  189. * @throws ModelNotFoundException
  190. */
  191. public static function setRoutineUser($routineUser, $spread_uid = 0)
  192. {
  193. self::beginTrans();
  194. $res1 = true;
  195. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  196. // $storeBrokerageStatu = sys_config('store_brokerage_statu') ? : 1;//获取后台分销类型
  197. $res2 = self::create([
  198. 'account' => 'rt' . $routineUser['uid'] . time(),
  199. 'pwd' => md5(123456),
  200. 'nickname' => $routineUser['nickname'] ?: '',
  201. 'avatar' => $routineUser['headimgurl'] ?: '',
  202. // 'spread_uid' => $spread_uid,
  203. // 'is_promoter'=>$spread_uid || $storeBrokerageStatu != 1 ? 1: 0,
  204. // 'spread_time' => $spread_uid ? time() : 0,
  205. 'uid' => $routineUser['uid'],
  206. 'add_time' => $routineUser['add_time'],
  207. 'add_ip' => request()->ip(),
  208. 'last_time' => time(),
  209. 'last_ip' => request()->ip(),
  210. 'user_type' => $routineUser['user_type']
  211. ]);
  212. $res = $res1 && $res2 && UserSpread::setSpread($routineUser['uid'], $spread_uid);
  213. self::checkTrans($res);
  214. return $res2;
  215. }
  216. /**
  217. * 获得当前登陆用户UID
  218. * @return int $uid
  219. */
  220. public static function getActiveUid()
  221. {
  222. $uid = null;
  223. $uid = Session::get('LoginUid');
  224. if ($uid) return $uid;
  225. else return 0;
  226. }
  227. /**
  228. * TODO 查询当前用户信息
  229. * @param $uid $uid 用户编号
  230. * @param string $field $field 查询的字段
  231. * @return array
  232. * @throws DataNotFoundException
  233. * @throws ModelNotFoundException
  234. * @throws DbException
  235. */
  236. public static function getUserInfo($uid, $field = '')
  237. {
  238. if (strlen(trim($field))) $userInfo = self::where('uid', $uid)->field($field)->find();
  239. else $userInfo = self::where('uid', $uid)->find();
  240. if (!$userInfo) return [];
  241. return $userInfo->toArray();
  242. }
  243. /**
  244. * 判断当前用户是否推广员
  245. * @param int $uid
  246. * @return bool
  247. */
  248. public static function isUserSpread($uid = 0)
  249. {
  250. if (!$uid) return false;
  251. $status = (int)sys_config('store_brokerage_statu');
  252. $isPromoter = true;
  253. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  254. if ($isPromoter) return true;
  255. else return false;
  256. }
  257. /**
  258. * TODO 一级返佣
  259. * @param $orderInfo
  260. * @return bool
  261. * @throws DataNotFoundException
  262. * @throws ModelNotFoundException
  263. * @throws DbException
  264. */
  265. public static function backOrderBrokerage($orderInfo, bool $open = true)
  266. {
  267. //TODO 营销产品不返佣金
  268. if (isset($orderInfo['combination_id']) && $orderInfo['combination_id']) return true;
  269. if (isset($orderInfo['seckill_id']) && $orderInfo['seckill_id']) return true;
  270. if (isset($orderInfo['bargain_id']) && $orderInfo['bargain_id']) return true;
  271. $userInfo = User::getUserInfo($orderInfo['uid']);
  272. //TODO 当前用户不存在 没有上级 或者 当用用户上级时自己 直接返回
  273. if (!$userInfo || !$userInfo['spread_uid'] || $userInfo['spread_uid'] == $orderInfo['uid']) return true;
  274. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return self::backOrderBrokerageTwo($orderInfo, $open);
  275. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  276. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId);
  277. //TODO 返佣金额小于等于0 直接返回不返佣金
  278. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  279. //TODO 获取上级推广员信息
  280. // $open && self::beginTrans();
  281. $res = true;
  282. if ($virtualBrokeragePrice > 0) {
  283. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  284. //TODO 上级推广员返佣之后的金额
  285. $balance = bcadd($spreadUserInfo['brokerage_price'], $virtualBrokeragePrice, 2);
  286. $mark = $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  287. //TODO 添加推广记录
  288. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  289. //TODO 添加用户余额
  290. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  291. //TODO 一级返佣成功 跳转二级返佣
  292. $res = $res1 && $res2;
  293. }
  294. if ($realBrokeragePrice > 0) {
  295. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  296. //TODO 上级推广员返佣之后的金额
  297. $mark = $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  298. //TODO 添加推广记录
  299. $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfo['brokerage_price'], $mark, 0);
  300. }
  301. $res = $res && self::backOrderBrokerageTwo($orderInfo);
  302. // $open && self::checkTrans($res);
  303. return $res;
  304. }
  305. /**
  306. * TODO 二级推广
  307. * @param $orderInfo
  308. * @return bool
  309. * @throws DataNotFoundException
  310. * @throws ModelNotFoundException
  311. * @throws DbException
  312. */
  313. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  314. {
  315. //TODO 获取购买商品的用户
  316. $userInfo = User::getUserInfo($orderInfo['uid']);
  317. //TODO 获取上推广人
  318. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  319. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  320. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  321. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  322. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  323. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  324. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  325. //TODO 返佣金额小于等于0 直接返回不返佣金
  326. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  327. // $open && self::beginTrans();
  328. $res = true;
  329. if ($virtualBrokeragePrice > 0) {
  330. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  331. //TODO 上级推广员返佣之后的金额
  332. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  333. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  334. //TODO 添加推广记录
  335. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  336. //TODO 添加用户余额
  337. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  338. //TODO 一级返佣成功 跳转二级返佣
  339. $res = $res1 && $res2;
  340. }
  341. if ($realBrokeragePrice > 0) {
  342. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  343. //TODO 上级推广员返佣之后的金额
  344. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  345. //TODO 添加推广记录
  346. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  347. }
  348. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  349. // $open && self::checkTrans($res);
  350. return $res;
  351. }
  352. /**
  353. * 发放实际商品佣金
  354. * @param $orderInfo
  355. * @return bool
  356. * @throws DataNotFoundException
  357. * @throws DbException
  358. * @throws ModelNotFoundException
  359. */
  360. public static function sendBackOrderBrokerage($orderInfo)
  361. {
  362. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  363. if ($list) {
  364. self::beginTrans();
  365. try {
  366. foreach ($list as $v) {
  367. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  368. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  369. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  370. }
  371. self::commitTrans();
  372. return true;
  373. } catch (Exception $e) {
  374. self::rollbackTrans();
  375. return false;
  376. }
  377. }
  378. return true;
  379. }
  380. /**
  381. * 获取推荐人 暂无使用
  382. * @param $uid
  383. * @param $page
  384. * @param $limit
  385. * @return mixed
  386. */
  387. public static function getSpreadList($uid, $page, $limit)
  388. {
  389. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  390. foreach ($list as $k => $user) {
  391. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  392. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  393. }
  394. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  395. $data['count'] = $count;
  396. $data['list'] = $list;
  397. return $data;
  398. }
  399. /**
  400. * 获取某个用户的下级uid
  401. * @param $uid
  402. * @return array
  403. */
  404. public static function getOneSpreadUid($uid)
  405. {
  406. return self::where('spread_uid', $uid)->column('uid');
  407. }
  408. /**
  409. * 修改个人信息
  410. * @param $avatar 头像
  411. * @param $nickname 昵称
  412. * @param $uid 用户uid
  413. * @return bool
  414. */
  415. public static function editUser($avatar, $nickname, $uid)
  416. {
  417. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  418. }
  419. /**
  420. * TODO 获取推广人数 一级
  421. * @param int $uid
  422. * @return bool|int|string
  423. */
  424. public static function getSpreadCount($uid = 0)
  425. {
  426. if (!$uid) return false;
  427. return self::where('spread_uid', $uid)->count();
  428. }
  429. /**
  430. * 修改当前用户的推广人数
  431. * @param $uid
  432. */
  433. public static function setUserSpreadCount($uid)
  434. {
  435. if (!$uid) return true;
  436. if (self::getSpreadCount($uid) > 0) {
  437. self::where('uid', $uid)->update(['spread_count' => 0]);
  438. } else {
  439. self::where('uid', $uid)->update(['spread_count' => 0]);
  440. return true;
  441. }
  442. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  443. }
  444. /**
  445. * TODO 获取推广人数 二级
  446. * @param int $uid
  447. * @return bool|int|string
  448. */
  449. public static function getSpreadLevelCount($uid = 0)
  450. {
  451. if (!$uid) return false;
  452. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  453. if (!count($uidSubordinate)) return 0;
  454. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  455. }
  456. /**
  457. * 获取用户下级推广人
  458. * @param int $uid 当前用户
  459. * @param int $grade 等级 0 一级 1 二级
  460. * @param string $orderBy 排序
  461. * @param string $keyword
  462. * @param int $page
  463. * @param int $limit
  464. * @return array|bool
  465. */
  466. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  467. {
  468. if (!$uid) return [];
  469. $gradeGroup = [0, 1];
  470. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  471. $userStair = self::where('spread_uid', $uid)->column('uid');
  472. if (!count($userStair)) return [];
  473. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  474. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  475. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  476. }
  477. /**
  478. * 获取团队信息
  479. * @param $uid
  480. * @param string $orderBy
  481. * @param string $keyword
  482. * @param int $page
  483. * @param int $limit
  484. * @return array
  485. */
  486. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  487. {
  488. $model = new self;
  489. if ($orderBy === '') $orderBy = 'u.add_time desc';
  490. $model = $model->alias(' u');
  491. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  492. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  493. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  494. $model = $model->where('u.uid', 'IN', $uid);
  495. $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");
  496. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  497. $model = $model->group('u.uid');
  498. $model = $model->order($orderBy);
  499. $model = $model->page($page, $limit);
  500. $list = $model->select();
  501. if ($list) return $list->toArray();
  502. else return [];
  503. }
  504. /**
  505. * 设置用户的上级关系
  506. * @param $uid 用户uid
  507. * @param $spreadUid 上级用户uid
  508. * @return User|bool
  509. * @throws DataNotFoundException
  510. * @throws ModelNotFoundException
  511. * @throws DbException
  512. */
  513. public static function setSpreadUid($uid, $spreadUid)
  514. {
  515. return UserSpread::setSpread($uid, $spreadUid);
  516. // 自己不能绑定自己为上级
  517. // if ($uid == $spreadUid) return false;
  518. // //TODO 获取后台分销类型
  519. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  520. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  521. // if ($storeBrokerageStatus == 1) {
  522. // $spreadCount = self::where('uid', $spreadUid)->count();
  523. // if ($spreadCount) {
  524. // $spreadInfo = self::where('uid', $spreadUid)->find();
  525. // if ($spreadInfo->is_promoter) {
  526. // //TODO 只有扫码才可以获得推广权限
  527. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  528. // }
  529. // }
  530. // }
  531. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  532. }
  533. /**
  534. * 判断上下级关系是否存在
  535. * @param $uid
  536. * @param $spreadUid
  537. * @return bool|int
  538. */
  539. public static function validSpread($uid, $spreadUid)
  540. {
  541. if (!$uid || !$spreadUid) return false;
  542. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  543. }
  544. /**
  545. * H5用户注册
  546. * @param $account
  547. * @param $password
  548. * @param $spread
  549. * @return bool
  550. * @throws DataNotFoundException
  551. * @throws DbException
  552. * @throws ModelNotFoundException
  553. */
  554. public static function register($account, $password, $spread, $payment_pas)
  555. {
  556. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  557. $phone = $account;
  558. $data['account'] = $account;
  559. $data['pwd'] = md5($password);
  560. $data['phone'] = $phone;
  561. // if ($spread) {
  562. // $data['spread_uid'] = $spread;
  563. // $data['spread_time'] = time();
  564. // }
  565. $data['real_name'] = '';
  566. $data['birthday'] = 0;
  567. $data['card_id'] = '';
  568. $data['mark'] = '';
  569. $data['addres'] = '';
  570. $data['user_type'] = 'h5';
  571. $data['add_time'] = time();
  572. $data['add_ip'] = app('request')->ip();
  573. $data['last_time'] = time();
  574. $data['last_ip'] = app('request')->ip();
  575. $data['nickname'] = substr(md5($account . time()), 0, 12);
  576. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  577. $data['city'] = '';
  578. $data['language'] = '';
  579. $data['province'] = '';
  580. $data['country'] = '';
  581. $data['payment_pas'] = md5($payment_pas.'sxg');
  582. self::beginTrans();
  583. $res2 = WechatUser::create($data);
  584. $data['uid'] = $res2->uid;
  585. $res1 = self::create($data);
  586. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  587. self::checkTrans($res);
  588. return $res;
  589. }
  590. /**
  591. * 密码修改
  592. * @param $account
  593. * @param $password
  594. * @return User|bool
  595. */
  596. public static function reset($account, $password)
  597. {
  598. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  599. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  600. if ($count) return true;
  601. return self::where('account', $account)->update(['pwd' => md5($password)]);
  602. }
  603. /**
  604. * 获取手机号是否注册
  605. * @param $phone
  606. * @return bool
  607. */
  608. public static function checkPhone($phone)
  609. {
  610. return self::be(['account' => $phone]);
  611. }
  612. /**
  613. * 获取推广人
  614. * @param $data 查询条件
  615. * @return array
  616. * @throws DataNotFoundException
  617. * @throws ModelNotFoundException
  618. * @throws \think\exception\DbException
  619. */
  620. public static function getRankList($data)
  621. {
  622. switch ($data['type']) {
  623. case 'week':
  624. $startTime = strtotime('this week');
  625. $endTime = time();
  626. break;
  627. case 'month':
  628. $startTime = strtotime('last month');
  629. $endTime = time();
  630. break;
  631. }
  632. $list = self::alias('t0')
  633. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  634. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  635. ->where('t1.spread_uid', '<>', 0)
  636. ->order('count desc')
  637. ->order('t0.uid desc')
  638. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  639. ->page($data['page'], $data['limit'])
  640. ->group('t0.uid')
  641. ->select();
  642. return count($list) ? $list->toArray() : [];
  643. }
  644. /**
  645. * 获取佣金排行
  646. * @param $data
  647. * @return array
  648. */
  649. public static function brokerageRank($data)
  650. {
  651. $model = self::where('status', 1);
  652. switch ($data['type']) {
  653. case 'week':
  654. $model = $model->whereIn('uid', function ($query) {
  655. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  656. ->whereWeek('add_time')->field('uid');
  657. });
  658. break;
  659. case 'month':
  660. $model = $model->whereIn('uid', function ($query) {
  661. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  662. ->whereMonth('add_time')->field('uid');
  663. });
  664. break;
  665. }
  666. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  667. ->page((int)$data['page'], (int)$data['limit'])->select();
  668. return count($users) ? $users->toArray() : [];
  669. }
  670. /**
  671. * 获取当前用户的佣金排行位置
  672. * @param $uid
  673. * @return int
  674. */
  675. public static function currentUserRank($type, $brokerage_price)
  676. {
  677. $model = self::where('status', 1);
  678. switch ($type) {
  679. case 'week':
  680. $model = $model->whereIn('uid', function ($query) {
  681. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  682. ->whereWeek('add_time')->field('uid');
  683. });
  684. break;
  685. case 'month':
  686. $model = $model->whereIn('uid', function ($query) {
  687. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  688. ->whereMonth('add_time')->field('uid');
  689. });
  690. break;
  691. }
  692. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  693. }
  694. /**
  695. * 直推奖励
  696. * @return void
  697. */
  698. public static function direct_push()
  699. {
  700. if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
  701. // 查找到今天没有分发奖励
  702. $time = strtotime(date('Y-m-d', strtotime('-1 day'))); // 昨天的时间
  703. $timet = strtotime(date('Y-m-d', time())); // 今天时间
  704. $order = AuctionOrder::where('status', '=',2)->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->select();
  705. if ($order){
  706. foreach ($order as $k => $v) {
  707. $user = User::where('uid', $v['uid'])->find();
  708. if ($user['spread_uid']){
  709. $spread = User::where('uid', $user['spread_uid'])->find();
  710. $bo = AuctionBooking::where('uid', '=', $spread['uid'])->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->find();
  711. if ($bo){
  712. $money = round($v['price'] * 0.03, 2);
  713. $spread['sp_final'] += $money;
  714. $spread->save();
  715. UserBill::income('奖励购物积分', $spread['uid'], 'sp_final', 'add_sp_final', $money, $user['uid'], $spread['sp_final'], '奖励购物积分');
  716. }
  717. }
  718. }
  719. }
  720. }
  721. }
  722. /**
  723. * 分红奖励
  724. * @return void
  725. * @throws DataNotFoundException
  726. * @throws DbException
  727. * @throws ModelNotFoundException
  728. */
  729. public static function bonus(){
  730. if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
  731. $config = SystemConfigService::more(['bonus', 'bonus_standard']);
  732. $user = self::field('uid,spread_uid, sp_final')->select();
  733. $user = empty($user) ? [] : $user->toArray();
  734. $time = strtotime(date('Y-m-d', strtotime('-1 day'))); // 昨天的时间
  735. $timet = strtotime(date('Y-m-d', time())); // 今天时间
  736. $orderMoney = AuctionOrder::where('status', '=',2)->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->sum('price');// 找到昨天的总流水金额
  737. $moneys = [];
  738. foreach ($user as $k => $v){
  739. $bo = AuctionBooking::where('uid', '=', $v['uid'])->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->find(); // 查看昨天有预约记录
  740. if ($bo){
  741. $dow = User::where('spread_uid', $v['uid'])->select()->toArray(); // 是否有下级
  742. if (!empty($dow)){
  743. foreach ($dow as $key => $value){
  744. $userDow[$value['uid']] = get_downline($user,$value['uid']);
  745. }
  746. foreach ($userDow as $dk => $dv){
  747. //查询出下级每个分支昨天流水多少
  748. $money = AuctionOrder::where('uid', $dk)->where('status',2)->sum('price');
  749. if ($dv){
  750. $money += AuctionOrder::where('uid', 'in', $dv)->where('status', '=', 2)->sum('price');
  751. }
  752. $moneys[] = $money;
  753. }
  754. sort($moneys); // 排序
  755. array_pop($moneys); // 删除最大的流水
  756. $sum = array_sum($moneys);
  757. if ($sum > (float)$config['bonus_standard']){
  758. $reward = round($orderMoney * ($config['bonus']/100), 2); // 奖励分红额度
  759. $v['sp_final'] += $reward;
  760. User::where('uid', $v['uid'])
  761. ->update(['sp_final' => $v['sp_final']]);
  762. UserBill::income('分红奖励', $v['uid'], 'sp_final', 'add_sp_final', $reward, 0, $v['sp_final'], '分红奖励');
  763. }
  764. unset($moneys);
  765. unset($userDow);
  766. }
  767. }
  768. }
  769. }
  770. }
  771. /**
  772. * KPI奖励
  773. * @return void
  774. * @throws DataNotFoundException
  775. * @throws DbException
  776. * @throws ModelNotFoundException
  777. */
  778. public static function kpi()
  779. {
  780. if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
  781. $user = self::field('uid,spread_uid, sp_final')->select();
  782. $user = empty($user) ? [] : $user->toArray();
  783. $moneys = [];
  784. $time = strtotime(date('Y-m-d', strtotime('-1 day'))); // 昨天的时间
  785. $timet = strtotime(date('Y-m-d', time())); // 今天时间
  786. $userl = [];
  787. foreach ($user as $k => $v){
  788. $bo = AuctionBooking::where('uid', '=', $v['uid'])->where('create_time', '>=', $time)->where('create_time', '<=', $timet)->find(); // 查看昨天有预约记录
  789. if ($bo){
  790. $dow = User::where('spread_uid', $v['uid'])->select()->toArray(); // 是否有下级
  791. if (!empty($dow)){
  792. foreach ($dow as $key => $value){
  793. $userDow[$value['uid']] = get_downline($user,$value['uid']); // 获取到下级
  794. }
  795. foreach ($userDow as $dk => $dv){
  796. //查询出下级每个分支昨天流水多少
  797. $money = AuctionOrder::where('uid', $dk)->where('status',2)->sum('price');
  798. if ($dv){
  799. $money += AuctionOrder::where('uid', 'in', $dv)->where('status', '=', 2)->sum('price');
  800. }
  801. $moneys[$dk] = $money; // 查询到支线下级总额度
  802. }
  803. $userl = $moneys;
  804. sort($moneys); // 排序
  805. array_pop($moneys); // 删除最大的流水
  806. $sum = array_sum($moneys);
  807. $level = self::con($sum);
  808. if ($level > 0){
  809. if (empty($v['spread_uid'])){
  810. // 没有上级
  811. foreach ($userl as $uk => $uv){
  812. $towdow = self::dow($uk);
  813. if ($level > $towdow){
  814. $reward = ($level - $towdow)/100; // 减掉下级获得的倍率
  815. $userl[$uk] = $uv * $reward;
  816. }elseif ($level == $towdow){
  817. $reward = $level/100; // 如果等级相同就只拿百分之十
  818. $userl[$uk] = ($uv * $reward) * 0.1;
  819. }
  820. }
  821. $userMoney = array_sum($userl);
  822. $v['sp_final'] += $userMoney;
  823. User::where('uid', $v['uid'])
  824. ->update(['sp_final' => $v['sp_final']]);
  825. UserBill::income('KPI奖励', $v['uid'], 'sp_final', 'add_sp_final_kpi', $userMoney, 0, $v['sp_final'], 'KPI奖励');
  826. }else{
  827. //有上级
  828. foreach ($userl as $uk => $uv){
  829. $towdow = self::dow($uk);
  830. if ($level > $towdow){
  831. $reward = $level/100; // 减掉下级获得的倍率
  832. }elseif ($level == $towdow){
  833. $reward = ($level* 0.1)/100; // 如果下级等级相同就只拿百分之十
  834. }
  835. $userl[$uk] = $uv * $reward;
  836. }
  837. $plevel = self::dow($v['spread_uid']);
  838. $userMoney = array_sum($userl);
  839. if ($plevel == $level) {
  840. $userMoney = $userMoney * 0.9; // 如果和上级相同等级只拿百分之九十
  841. }
  842. $v['sp_final'] += $userMoney;
  843. User::where('uid', $v['uid'])
  844. ->update(['sp_final' => $v['sp_final']]);
  845. UserBill::income('KPI奖励', $v['uid'], 'sp_final', 'add_sp_final_kpi', $userMoney, 0, $v['sp_final'], 'KPI奖励');
  846. }
  847. }
  848. unset($userl);
  849. unset($moneys);
  850. unset($userDow);
  851. }
  852. }
  853. }
  854. }
  855. }
  856. public static function dow($id)
  857. {
  858. $user = self::field('uid,spread_uid, sp_final')->select();
  859. $dow = User::where('spread_uid', $id)->select()->toArray(); // 是否有下级
  860. if (!empty($dow)) {
  861. foreach ($dow as $key => $value) {
  862. $userDow[$value['uid']] = get_downline($user, $value['uid']);
  863. }
  864. foreach ($userDow as $dk => $dv) {
  865. //查询出下级每个分支昨天流水多少
  866. $money = AuctionOrder::where('uid', $dk)->where('status', 2)->sum('price');
  867. if ($dv) {
  868. $money += AuctionOrder::where('uid', 'in', $dv)->where('status', '=', 2)->sum('price');
  869. }
  870. $moneys[$dk] = $money;
  871. }
  872. sort($moneys); // 排序
  873. array_pop($moneys); // 删除最大的流水
  874. $sum = array_sum($moneys);
  875. $level = self::con($sum);
  876. return $level;
  877. }
  878. return 0;
  879. }
  880. public static function con($sum){
  881. $config = SystemConfigService::more([
  882. 'level_one','one_standard','level_two','two_standard','level_three','three_standard','level_four','four_standard','level_five','five_standard'
  883. ]);
  884. $status = '';
  885. if ($sum < $config['one_standard']) $status = 0;
  886. if ($sum >= $config['one_standard'] and $sum < $config['two_standard']) $status = $config['level_one']; // 达标1级
  887. if ($sum >= $config['two_standard'] and $sum < $config['three_standard']) $status = $config['level_two'];// 达标2级
  888. if ($sum >= $config['three_standard'] and $sum < $config['four_standard']) $status = $config['level_three'];// 达标3级
  889. if ($sum >= $config['four_standard'] and $sum < $config['five_standard']) $status = $config['level_four'];// 达标4级
  890. if ($sum >= $config['five_standard']) $status = $config['level_five'];// 达标5级
  891. return $status;
  892. }
  893. public static function up_time()
  894. {
  895. if (!AuctionReward::where('create_time', strtotime(date('Y-m-d', time())))->find()){
  896. AuctionReward::create([
  897. 'create_time' => strtotime(date('Y-m-d', time()))
  898. ]);
  899. }
  900. }
  901. }