User.php 43 KB

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