User.php 41 KB

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