User.php 37 KB

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