User.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. $realBrokeragePrice = $realBrokeragePrice * (sys_config('administration')/100);
  316. $brokerage_price = $realBrokeragePrice * 0.95;// 到账佣金
  317. $integral = $realBrokeragePrice * 0.05;// 到账积分
  318. UserBill::income('获得推广佣金', $amministrazione['uid'], 'now_money', 'brokerage', $brokerage_price, $orderInfo['id'], $amministrazione['brokerage_price'] + $brokerage_price, '管理佣金奖励');
  319. UserBill::income('获得推广积分', $amministrazione['uid'], 'integral', 'Level', $integral, $orderInfo['id'], $amministrazione['integral'] + $integral, '管理积分奖励');
  320. self::bcInc($amministrazione['uid'], 'brokerage_price', $brokerage_price, 'uid'); //发放管理奖
  321. self::bcInc($amministrazione['uid'], 'integral', $integral, 'uid'); //发放管理奖
  322. self::livello($amministrazione['spread_uid'], $realBrokeragePrice);
  323. }
  324. }
  325. }
  326. }elseif ($spreadUserInfo['identity'] == 2){
  327. self::livello($spreadUserInfo['spread_uid'], $realBrokeragePrice);
  328. }
  329. }else{
  330. $brokerage_price = $realBrokeragePrice * 0.95;// 到账佣金
  331. $integral = $realBrokeragePrice * 0.05;// 到账积分
  332. $mark = $userInfo['nickname'] . '复购成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokerage_price);
  333. $mark1 = $userInfo['nickname'] . '复购成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广积分' . floatval($integral);
  334. $res = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokerage_price, $orderInfo['id'], $spreadUserInfo['brokerage_price'] + $brokerage_price, $mark);
  335. $res = UserBill::income('获得推广积分', $userInfo['spread_uid'], 'integral', 'push', $integral, $orderInfo['id'], $spreadUserInfo['integral'] + $integral, $mark1);
  336. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokerage_price, 'uid');
  337. $res2 = self::bcInc($userInfo['spread_uid'], 'integral', $integral, 'uid');
  338. }
  339. }
  340. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  341. // $open && self::checkTrans($res);
  342. return $res;
  343. }
  344. /**
  345. * 平级奖励
  346. * @param $uid
  347. * @param $price
  348. * @return void
  349. */
  350. public static function livello($uid, $price)
  351. {
  352. $uids = getParent_livello( $uid, User::select());
  353. if (count($uids) > 0){
  354. // 第一级店长
  355. if (isset($uids[0])){
  356. $user = User::where('uid', $uids[0])->find();
  357. $price1 = $price * 0.7;
  358. $brokerage_price = $price1 * 0.95;// 到账佣金
  359. $integral = $price1 * 0.05;// 到账积分
  360. UserBill::income('获得推广佣金', $user['uid'], 'now_money', 'brokerage', $brokerage_price, '', $user['brokerage_price'] + $brokerage_price, '店长平级佣金奖励');
  361. UserBill::income('获得推广积分', $user['uid'], 'integral', 'Level', $integral, '', $user['integral'] + $integral, '店长平级积分奖励');
  362. self::bcInc($user['uid'], 'brokerage_price', $brokerage_price, 'uid'); //发放管理奖
  363. self::bcInc($user['uid'], 'integral', $integral, 'uid'); //发放管理奖
  364. }
  365. if (isset($uids[1])){
  366. // 第二级店长
  367. $user = User::where('uid', $uids[1])->find();
  368. $price2 = $price * 0.3;
  369. $brokerage_price = $price2 * 0.95;// 到账佣金
  370. $integral = $price2 * 0.05;// 到账积分
  371. UserBill::income('获得推广佣金', $user['uid'], 'now_money', 'brokerage', $brokerage_price, '', $user['brokerage_price'] + $brokerage_price, '店长平级佣金奖励');
  372. UserBill::income('获得推广积分', $user['uid'], 'integral', 'Level', $integral, '', $user['integral'] + $integral, '店长平级积分奖励');
  373. self::bcInc($user['uid'], 'brokerage_price', $brokerage_price, 'uid'); //发放管理奖
  374. self::bcInc($user['uid'], 'integral', $integral, 'uid'); //发放管理奖
  375. }
  376. }
  377. }
  378. /**
  379. * TODO 二级推广
  380. * @param $orderInfo
  381. * @return bool
  382. * @throws DataNotFoundException
  383. * @throws ModelNotFoundException
  384. * @throws DbException
  385. */
  386. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  387. {
  388. //TODO 获取购买商品的用户
  389. $userInfo = User::getUserInfo($orderInfo['uid']);
  390. //TODO 获取上推广人
  391. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  392. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  393. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  394. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  395. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  396. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  397. list($realBrokeragePrice, $virtualBrokeragePrice) = StoreProduct::getProductBrokerage($cartId, false);
  398. //TODO 返佣金额小于等于0 直接返回不返佣金
  399. if ($realBrokeragePrice <= 0 && $virtualBrokeragePrice <= 0) return true;
  400. // $open && self::beginTrans();
  401. $res = true;
  402. if ($virtualBrokeragePrice > 0) {
  403. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  404. //TODO 上级推广员返佣之后的金额
  405. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $virtualBrokeragePrice, 2);
  406. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[虚拟产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($virtualBrokeragePrice);
  407. //TODO 添加推广记录
  408. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $virtualBrokeragePrice, $orderInfo['id'], $balance, $mark);
  409. //TODO 添加用户余额
  410. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $virtualBrokeragePrice, 'uid');
  411. //TODO 一级返佣成功 跳转二级返佣
  412. $res = $res1 && $res2;
  413. }
  414. if ($realBrokeragePrice > 0) {
  415. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  416. //TODO 上级推广员返佣之后的金额
  417. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费[实体产品]' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($realBrokeragePrice);
  418. //TODO 添加推广记录
  419. $res = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $realBrokeragePrice, $orderInfo['id'], $spreadUserInfoTwo['brokerage_price'], $mark, 0);
  420. }
  421. // $res = $res && self::backOrderBrokerageTwo($orderInfo);
  422. // $open && self::checkTrans($res);
  423. return $res;
  424. }
  425. /**
  426. * 发放实际商品佣金
  427. * @param $orderInfo
  428. * @return bool
  429. * @throws DataNotFoundException
  430. * @throws DbException
  431. * @throws ModelNotFoundException
  432. */
  433. public static function sendBackOrderBrokerage($orderInfo)
  434. {
  435. $list = UserBill::where(['link_id' => $orderInfo['id'], 'type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0])->select();
  436. if ($list) {
  437. self::beginTrans();
  438. try {
  439. foreach ($list as $v) {
  440. self::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  441. $balance = self::getUserInfo($v['uid'])['brokerage_price'];
  442. UserBill::where('id', $v['id'])->update(['add_time' => time(), 'status' => 1, 'balance' => $balance]);
  443. }
  444. self::commitTrans();
  445. return true;
  446. } catch (Exception $e) {
  447. self::rollbackTrans();
  448. return false;
  449. }
  450. }
  451. return true;
  452. }
  453. /**
  454. * 获取推荐人 暂无使用
  455. * @param $uid
  456. * @param $page
  457. * @param $limit
  458. * @return mixed
  459. */
  460. public static function getSpreadList($uid, $page, $limit)
  461. {
  462. $list = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select();
  463. foreach ($list as $k => $user) {
  464. $list[$k]['add_time'] = date('Y/m/d', $user['add_time']);
  465. $list[$k]['price'] = StoreOrder::getUserPrice($user['uid']);
  466. }
  467. $count = self::where('spread_uid', $uid)->field('uid,nickname,avatar,add_time')->count();
  468. $data['count'] = $count;
  469. $data['list'] = $list;
  470. return $data;
  471. }
  472. /**
  473. * 获取某个用户的下级uid
  474. * @param $uid
  475. * @return array
  476. */
  477. public static function getOneSpreadUid($uid)
  478. {
  479. return self::where('spread_uid', $uid)->column('uid');
  480. }
  481. /**
  482. * 修改个人信息
  483. * @param $avatar 头像
  484. * @param $nickname 昵称
  485. * @param $uid 用户uid
  486. * @return bool
  487. */
  488. public static function editUser($avatar, $nickname, $uid)
  489. {
  490. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  491. }
  492. /**
  493. * TODO 获取推广人数 一级
  494. * @param int $uid
  495. * @return bool|int|string
  496. */
  497. public static function getSpreadCount($uid = 0)
  498. {
  499. if (!$uid) return false;
  500. return self::where('spread_uid', $uid)->count();
  501. }
  502. /**
  503. * 修改当前用户的推广人数
  504. * @param $uid
  505. */
  506. public static function setUserSpreadCount($uid)
  507. {
  508. if (!$uid) return true;
  509. if (self::getSpreadCount($uid) > 0) {
  510. self::where('uid', $uid)->update(['spread_count' => 0]);
  511. } else {
  512. self::where('uid', $uid)->update(['spread_count' => 0]);
  513. return true;
  514. }
  515. return self::where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid)]);
  516. }
  517. /**
  518. * TODO 获取推广人数 二级
  519. * @param int $uid
  520. * @return bool|int|string
  521. */
  522. public static function getSpreadLevelCount($uid = 0)
  523. {
  524. if (!$uid) return false;
  525. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  526. if (!count($uidSubordinate)) return 0;
  527. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count();
  528. }
  529. /**
  530. * 获取用户下级推广人
  531. * @param int $uid 当前用户
  532. * @param int $grade 等级 0 一级 1 二级
  533. * @param string $orderBy 排序
  534. * @param string $keyword
  535. * @param int $page
  536. * @param int $limit
  537. * @return array|bool
  538. */
  539. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  540. {
  541. if (!$uid) return [];
  542. $gradeGroup = [0, 1];
  543. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  544. $userStair = self::where('spread_uid', $uid)->column('uid');
  545. if (!count($userStair)) return [];
  546. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  547. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  548. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  549. }
  550. /**
  551. * 获取团队信息
  552. * @param $uid
  553. * @param string $orderBy
  554. * @param string $keyword
  555. * @param int $page
  556. * @param int $limit
  557. * @return array
  558. */
  559. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  560. {
  561. $model = new self;
  562. if ($orderBy === '') $orderBy = 'u.add_time desc';
  563. $model = $model->alias(' u');
  564. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  565. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  566. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  567. $model = $model->where('u.uid', 'IN', $uid);
  568. $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");
  569. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  570. $model = $model->group('u.uid');
  571. $model = $model->order($orderBy);
  572. $model = $model->page($page, $limit);
  573. $list = $model->select();
  574. if ($list) return $list->toArray();
  575. else return [];
  576. }
  577. /**
  578. * 设置用户的上级关系
  579. * @param $uid 用户uid
  580. * @param $spreadUid 上级用户uid
  581. * @return User|bool
  582. * @throws DataNotFoundException
  583. * @throws ModelNotFoundException
  584. * @throws DbException
  585. */
  586. public static function setSpreadUid($uid, $spreadUid)
  587. {
  588. return UserSpread::setSpread($uid, $spreadUid);
  589. // 自己不能绑定自己为上级
  590. // if ($uid == $spreadUid) return false;
  591. // //TODO 获取后台分销类型
  592. // $storeBrokerageStatus = sys_config('store_brokerage_statu');
  593. // $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  594. // if ($storeBrokerageStatus == 1) {
  595. // $spreadCount = self::where('uid', $spreadUid)->count();
  596. // if ($spreadCount) {
  597. // $spreadInfo = self::where('uid', $spreadUid)->find();
  598. // if ($spreadInfo->is_promoter) {
  599. // //TODO 只有扫码才可以获得推广权限
  600. // if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  601. // }
  602. // }
  603. // }
  604. // return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  605. }
  606. /**
  607. * 判断上下级关系是否存在
  608. * @param $uid
  609. * @param $spreadUid
  610. * @return bool|int
  611. */
  612. public static function validSpread($uid, $spreadUid)
  613. {
  614. if (!$uid || !$spreadUid) return false;
  615. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  616. }
  617. /**
  618. * H5用户注册
  619. * @param $account
  620. * @param $password
  621. * @param $spread
  622. * @return bool
  623. * @throws DataNotFoundException
  624. * @throws DbException
  625. * @throws ModelNotFoundException
  626. */
  627. public static function register($account, $password, $spread)
  628. {
  629. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  630. if (self::be(['account' => $account])) return self::setErrorInfo('用户已存在');
  631. $phone = $account;
  632. $data['account'] = $account;
  633. $data['pwd'] = md5($password);
  634. $data['phone'] = $phone;
  635. // if ($spread) {
  636. // $data['spread_uid'] = $spread;
  637. // $data['spread_time'] = time();
  638. // }
  639. $data['real_name'] = '';
  640. $data['birthday'] = 0;
  641. $data['card_id'] = '';
  642. $data['mark'] = '';
  643. $data['addres'] = '';
  644. $data['user_type'] = 'h5';
  645. $data['add_time'] = time();
  646. $data['add_ip'] = app('request')->ip();
  647. $data['last_time'] = time();
  648. $data['last_ip'] = app('request')->ip();
  649. $data['nickname'] = substr(md5($account . time()), 0, 12);
  650. $data['avatar'] = $data['headimgurl'] = sys_config('h5_avatar');
  651. $data['city'] = '';
  652. $data['language'] = '';
  653. $data['province'] = '';
  654. $data['country'] = '';
  655. $data['is_promoter'] = $storeBrokerageStatu != 1 ? 1 : 0;
  656. self::beginTrans();
  657. $res2 = WechatUser::create($data);
  658. $data['uid'] = $res2->uid;
  659. $res1 = self::create($data);
  660. $res = $res1 && $res2 && UserSpread::setSpread($data['uid'], $spread);
  661. self::checkTrans($res);
  662. return $res;
  663. }
  664. /**
  665. * 密码修改
  666. * @param $account
  667. * @param $password
  668. * @return User|bool
  669. */
  670. public static function reset($account, $password)
  671. {
  672. if (!self::be(['account' => $account])) return self::setErrorInfo('用户不存在');
  673. $count = self::where('account', $account)->where('pwd', md5($password))->count();
  674. if ($count) return true;
  675. return self::where('account', $account)->update(['pwd' => md5($password)]);
  676. }
  677. /**
  678. * 获取手机号是否注册
  679. * @param $phone
  680. * @return bool
  681. */
  682. public static function checkPhone($phone)
  683. {
  684. return self::be(['account' => $phone]);
  685. }
  686. /**
  687. * 获取推广人
  688. * @param $data 查询条件
  689. * @return array
  690. * @throws DataNotFoundException
  691. * @throws ModelNotFoundException
  692. * @throws \think\exception\DbException
  693. */
  694. public static function getRankList($data)
  695. {
  696. switch ($data['type']) {
  697. case 'week':
  698. $startTime = strtotime('this week');
  699. $endTime = time();
  700. break;
  701. case 'month':
  702. $startTime = strtotime('last month');
  703. $endTime = time();
  704. break;
  705. }
  706. $list = self::alias('t0')
  707. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar')
  708. ->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  709. ->where('t1.spread_uid', '<>', 0)
  710. ->order('count desc')
  711. ->order('t0.uid desc')
  712. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  713. ->page($data['page'], $data['limit'])
  714. ->group('t0.uid')
  715. ->select();
  716. return count($list) ? $list->toArray() : [];
  717. }
  718. /**
  719. * 获取佣金排行
  720. * @param $data
  721. * @return array
  722. */
  723. public static function brokerageRank($data)
  724. {
  725. $model = self::where('status', 1);
  726. switch ($data['type']) {
  727. case 'week':
  728. $model = $model->whereIn('uid', function ($query) {
  729. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  730. ->whereWeek('add_time')->field('uid');
  731. });
  732. break;
  733. case 'month':
  734. $model = $model->whereIn('uid', function ($query) {
  735. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  736. ->whereMonth('add_time')->field('uid');
  737. });
  738. break;
  739. }
  740. $users = $model->field('uid,nickname,avatar,brokerage_price')->order('brokerage_price desc')
  741. ->page((int)$data['page'], (int)$data['limit'])->select();
  742. return count($users) ? $users->toArray() : [];
  743. }
  744. /**
  745. * 获取当前用户的佣金排行位置
  746. * @param $uid
  747. * @return int
  748. */
  749. public static function currentUserRank($type, $brokerage_price)
  750. {
  751. $model = self::where('status', 1);
  752. switch ($type) {
  753. case 'week':
  754. $model = $model->whereIn('uid', function ($query) {
  755. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  756. ->whereWeek('add_time')->field('uid');
  757. });
  758. break;
  759. case 'month':
  760. $model = $model->whereIn('uid', function ($query) {
  761. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  762. ->whereMonth('add_time')->field('uid');
  763. });
  764. break;
  765. }
  766. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  767. }
  768. /**
  769. * 升级店长店员
  770. * @param $uid
  771. * @return void
  772. * @throws DataNotFoundException
  773. * @throws DbException
  774. * @throws ModelNotFoundException
  775. */
  776. public static function upgrade($uid)
  777. {
  778. $user = User::where('uid', $uid)->find();
  779. if ($user['identity'] == 0){
  780. $price = StoreOrder::where('uid', $uid)->where('paid', 1)->sum('pay_price');
  781. if ($price >= sys_config('cumulative')){
  782. $user['identity'] = 1;
  783. if ($user['spread_uid']){
  784. $spd = User::where('uid', $user['spread_uid'])->find();
  785. $user['line'] = $spd['lines'] + 1;
  786. $spd['lines'] += 1;
  787. $spd->save();
  788. }
  789. }
  790. }elseif ($user['identity'] == 1){
  791. $count = User::where('spread_uid', $user['uid'])->where('identity', 1)->count();
  792. if ($count >= sys_config('push_clerk')){
  793. $user['identity'] = 2;
  794. }
  795. }
  796. $res = $user->save();
  797. if ($user['spread_uid']){
  798. $spd = User::where('uid', $user['spread_uid'])->find();
  799. $count = User::where('spread_uid', $spd['uid'])->where('identity', 1)->count();
  800. if ($count >= sys_config('push_clerk')){
  801. $spd['identity'] = 2;
  802. }
  803. $spd->save();
  804. }
  805. if ($res) return true;
  806. return false;
  807. }
  808. /**
  809. * 团队奖励分红
  810. * @return void
  811. * @throws DataNotFoundException
  812. * @throws DbException
  813. * @throws ModelNotFoundException
  814. */
  815. public static function bonus()
  816. {
  817. if (!cache('bonus')){
  818. $user = User::select();
  819. foreach ($user as $item)
  820. {
  821. $start_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
  822. $end_time = date('Y-m-d 23:59:59', strtotime(-date('d').'day'));
  823. $price = StoreOrder::whereBetweenTime('add_time', $start_time, $end_time)->where('paid', 1)->where('uid', $item['uid'])->sum('pay_price');// 月结
  824. if ($item['spread_uid'] > 0 and $price > 0){
  825. $spread = getParents($item['spread_uid'], $user->toArray());// 找到所有上级
  826. $v1 = 0;
  827. $v2 = 0;
  828. $v3 = 0;
  829. $one = sys_config('area')/100; // 区代
  830. $tow = sys_config('city')/100; // 市代
  831. $three = sys_config('partner')/100;// 合伙人
  832. foreach ($spread as $value) {
  833. $details = User::where('uid', $value)->find();
  834. if ($details['level'] > 0){
  835. if ($details['level'] == 1){
  836. if ($v2 == 0 and $v3 == 0){
  837. // 没有发放市代和合伙人的奖励
  838. if ($v1 == 0) { // 没有发放v1的奖励
  839. $jl = $price * $one;
  840. $details['dynamic_integral'] += $jl; // 区代团队奖励
  841. $v1++;
  842. }
  843. }
  844. }elseif ($details['level'] == 2){
  845. if ($v3 == 0){
  846. // 没有发放合伙人的奖励
  847. if ($v1 == 0 and $v2 == 0) { // 没有发放区代和市代的奖励的奖励
  848. $jl = $price * $tow;// 市代团队奖励
  849. $v2++;
  850. }elseif ($v1 > 0 and $v2 == 0){// 发放区代奖励,没有发放市代的奖励
  851. $jl = $price * ($tow - $one); // 市代团队奖励
  852. $v2++;
  853. }
  854. }
  855. }elseif ($details['level'] == 3){
  856. if ($v1 == 0 and $v2 == 0 and $v3 == 0) { // 没有发放区代和市代,合伙人的奖励的奖励
  857. $jl = $price * $three; // 合伙人团队奖励
  858. $v3++;
  859. }elseif ($v1 > 0 and $v2 == 0 and $v3 == 0){// 发放区代奖励,没有发放市代,合伙人的奖励
  860. $jl = $price * ($three - $one); // 合伙人团队奖励
  861. $v3++;
  862. }elseif ($v1 == 0 and $v2 > 0 and $v3 == 0){// 没有发放区代奖励,发放市代的奖励,没有发放合伙人的奖励
  863. $jl = ($price * ($three - $tow)); // 合伙人团队奖励
  864. $v3++;
  865. }elseif ($v1 > 0 and $v2 > 0 and $v3 == 0){// 发放区代奖励,发放市代的奖励,发放合伙人的奖励
  866. $jl = ($price * ($three - $tow)); // 合伙人团队奖励
  867. $v3++;
  868. }
  869. }
  870. if ($jl > 0){
  871. $brokerage_price = $jl * 0.95;// 到账佣金
  872. $integral = $jl * 0.05;// 到账积分
  873. User::where('uid', $value)->inc('brokerage_price', $brokerage_price)->update();
  874. User::where('uid', $value)->inc('integral', $integral)->update();
  875. if ($details['level'] == 3) {
  876. $bl = $three;
  877. if ($v2 > 0 and $v1 > 0) $bl -= $tow;
  878. if ($v2 > 0 and $v1 == 0) $bl -= $tow;
  879. if ($v2 == 0 and $v1 > 0) $bl -= $one;
  880. }
  881. if ($details['level'] == 2){
  882. $bl = $tow;
  883. if ($v1 > 0) $bl -= $one;
  884. }
  885. if ($details['level'] == 1) $bl = $one;
  886. UserBill::income('获得推广佣金', $details['uid'], 'now_money', 'brokerage', $brokerage_price, $item['uid'],$details['brokerage_price'] + $brokerage_price, '用户'.$item['uid'].'月结'.$price.'团队奖励比例'.$bl.'的95%佣金');
  887. UserBill::income('获得推广积分', $details['uid'], 'integral', 'bonus', $integral, $item['uid'],$details['integral'] + $integral, '用户'.$item['uid'].'月结'.$price.'团队奖励比例'.$bl.'的5%积分');
  888. }
  889. $jl = 0;
  890. }
  891. }
  892. }
  893. }
  894. cache('bonus', 1, 86400);
  895. }
  896. }
  897. /**
  898. * 获取ID
  899. * @param $key
  900. * @return mixed
  901. */
  902. public static function getkeytoid($key)
  903. {
  904. $rs = self::order('uid DESC')->find();
  905. return $rs['uid']+1;
  906. }
  907. /**
  908. * 联创分红
  909. * @return void
  910. * @throws DataNotFoundException
  911. * @throws DbException
  912. * @throws ModelNotFoundException
  913. */
  914. public static function creation()
  915. {
  916. $user = User::where('level', 4)->select();
  917. if (count($user) > 0){
  918. $start_time = date('Y-m-01 00:00:00', strtotime('-1 month'));
  919. $end_time = date('Y-m-d 23:59:59', strtotime(-date('d').'day'));
  920. $price = StoreOrder::whereBetweenTime('add_time',$start_time, $end_time)->where('paid', 1)->sum('pay_price');// 月结
  921. if ($price > 0){
  922. foreach ($user as $item)
  923. {
  924. $brokerage_price = ($price * sys_config('link')/100) * 0.95;// 到账佣金
  925. $integral = ($price * sys_config('link')/100) * 0.05;// 到账积分
  926. User::where('uid', $item['uid'])->inc('brokerage_price', $brokerage_price)->update();
  927. User::where('uid', $item['uid'])->inc('integral', $integral)->update();
  928. UserBill::income('联创分红佣金', $item['uid'], 'now_money', 'brokerage', $brokerage_price, $item['uid'],$item['brokerage_price'] + $brokerage_price, '联创分红奖励比例'.(sys_config('link')/100).'的95%佣金');
  929. UserBill::income('联创分红积分', $item['uid'], 'integral', 'creation', $integral, $item['uid'],$item['integral'] + $integral, '联创分红奖励比例'.(sys_config('link')/100).'的5%积分');
  930. }
  931. }
  932. }
  933. }
  934. }