User.php 37 KB

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