User.php 42 KB

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