User.php 37 KB

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