User.php 37 KB

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