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