User.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. <?php
  2. namespace app\models\user;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreCouponUser;
  5. use app\models\system\SystemUserLevel;
  6. use crmeb\services\SystemConfigService;
  7. use Exception;
  8. use think\Collection;
  9. use think\db\exception\DataNotFoundException;
  10. use think\db\exception\DbException;
  11. use think\db\exception\ModelNotFoundException;
  12. use think\db\Query;
  13. use think\facade\Session;
  14. use crmeb\traits\ModelTrait;
  15. use crmeb\basic\BaseModel;
  16. use crmeb\traits\JwtAuthModelTrait;
  17. use think\facade\Db;
  18. use app\models\store\StoreProduct;
  19. use think\Model;
  20. /**
  21. * TODO 用户Model
  22. * Class User
  23. * @package app\models\user
  24. */
  25. class User extends BaseModel
  26. {
  27. use JwtAuthModelTrait;
  28. use ModelTrait;
  29. protected $pk = 'uid';
  30. protected $name = 'user';
  31. protected $insert = ['add_time', 'add_ip', 'last_time', 'last_ip'];
  32. protected $hidden = [
  33. 'add_ip', 'account', 'clean_time', 'last_ip', 'pwd', 'pwd'
  34. ];
  35. protected function setAddTimeAttr($value)
  36. {
  37. return time();
  38. }
  39. protected function setAddIpAttr($value)
  40. {
  41. return app('request')->ip();
  42. }
  43. protected function setLastTimeAttr($value)
  44. {
  45. return time();
  46. }
  47. protected function setLastIpAttr($value)
  48. {
  49. return app('request')->ip();
  50. }
  51. /**
  52. * @param $mer_id
  53. * @return User|Query
  54. */
  55. public static function merSet($mer_id)
  56. {
  57. return $mer_id ? self::where('mer_id', $mer_id) : new self;
  58. }
  59. public function getOneDE($uid)
  60. {
  61. return $this;
  62. }
  63. public static function setWechatUser($wechatUser, $spread_uid = 0)
  64. {
  65. return self::create([
  66. 'account' => 'wx' . $wechatUser['uid'] . time(),
  67. 'pwd' => md5(123456),
  68. 'nickname' => $wechatUser['nickname'] ?: '',
  69. 'avatar' => $wechatUser['headimgurl'] ?: '',
  70. 'spread_uid' => $spread_uid,
  71. 'add_time' => time(),
  72. 'add_ip' => app('request')->ip(),
  73. 'last_time' => time(),
  74. 'last_ip' => app('request')->ip(),
  75. 'uid' => $wechatUser['uid'],
  76. 'user_type' => 'wechat'
  77. ]);
  78. }
  79. /**
  80. * TODO 获取会员是否被清除过的时间
  81. * @param $uid
  82. * @return int|mixed
  83. * @throws DataNotFoundException
  84. * @throws ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public static function getCleanTime($uid)
  88. {
  89. $user = self::where('uid', $uid)->field(['add_time', 'clean_time'])->find();
  90. if (!$user) return 0;
  91. return $user['clean_time'] ? $user['clean_time'] : $user['add_time'];
  92. }
  93. public static function updateWechatUser($wechatUser, $uid)
  94. {
  95. $userInfo = self::where('uid', $uid)->find();
  96. if (!$userInfo) return;
  97. if ($userInfo->spread_uid) {
  98. return self::edit([
  99. 'nickname' => $wechatUser['nickname'] ?: '',
  100. 'avatar' => $wechatUser['headimgurl'] ?: '',
  101. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  102. ], $uid, 'uid');
  103. } else {
  104. $data = [
  105. 'nickname' => $wechatUser['nickname'] ?: '',
  106. 'avatar' => $wechatUser['headimgurl'] ?: '',
  107. 'is_promoter' => $userInfo->is_promoter,
  108. 'login_type' => isset($wechatUser['login_type']) ? $wechatUser['login_type'] : $userInfo->login_type,
  109. 'spread_uid' => 0,
  110. 'spread_time' => 0,
  111. 'last_time' => time(),
  112. 'last_ip' => request()->ip(),
  113. ];
  114. //TODO 获取后台分销类型
  115. $storeBrokerageStatus = SystemConfigService::get('store_brokerage_statu');
  116. $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  117. if (isset($wechatUser['code']) && $wechatUser['code'] && $wechatUser['code'] != $uid && $uid != self::where('uid', $wechatUser['code'])->value('spread_uid')) {
  118. if ($storeBrokerageStatus == 1) {
  119. $spreadCount = self::where('uid', $wechatUser['code'])->count();
  120. if ($spreadCount) {
  121. $spreadInfo = self::where('uid', $wechatUser['code'])->find();
  122. if ($spreadInfo->is_promoter) {
  123. //TODO 只有扫码才可以获得推广权限
  124. // if(isset($wechatUser['isPromoter'])) $data['is_promoter'] = $wechatUser['isPromoter'] ? 1 : 0;
  125. }
  126. }
  127. }
  128. $data['spread_uid'] = $wechatUser['code'];
  129. $data['spread_time'] = time();
  130. }
  131. return self::edit($data, $uid, 'uid');
  132. }
  133. }
  134. /**
  135. * 设置推广关系
  136. * @param $spread
  137. * @param $uid
  138. * @param bool $mer_id
  139. * @return bool
  140. * @throws DataNotFoundException
  141. * @throws DbException
  142. * @throws ModelNotFoundException
  143. */
  144. public static function setSpread($spread, $uid, $mer_id = false)
  145. {
  146. //当前用户信息
  147. $userInfo = self::merSet($mer_id)->where('uid', $uid)->find();
  148. $spUserInfo = self::merSet($mer_id)->where('uid', $spread)->find();
  149. if (!$userInfo) return true;
  150. if (!$spUserInfo) return true;
  151. //当前用户有上级直接返回
  152. if ($userInfo->spread_uid) return true;
  153. //没有推广编号直接返回
  154. if (!$spread) return true;
  155. if ($spread == $uid) return true;
  156. $sp = $spread;
  157. while ($sp) {
  158. if ($sp == $uid) {
  159. return true;
  160. }
  161. $sp = self::merSet($mer_id)->where('uid', $sp)->value('spread_uid');
  162. }
  163. //if ($uid == self::where('uid', $spread)->value('spread_uid')) return true;
  164. //TODO 获取后台分销类型
  165. $storeBrokerageStatus = sys_config('store_brokerage_statu', '', $mer_id);
  166. $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  167. if ($storeBrokerageStatus == 1) {
  168. $spreadCount = self::merSet($mer_id)->where('uid', $spread)->count();
  169. if ($spreadCount) {
  170. $spreadInfo = self::merSet($mer_id)->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') && self::setUserSpreadCount($spread, $mer_id);
  180. }
  181. /**
  182. * 小程序用户添加
  183. * @param $routineUser
  184. * @param int $spread_uid
  185. * @return object
  186. */
  187. public static function setRoutineUser($routineUser, $spread_uid = 0)
  188. {
  189. self::beginTrans();
  190. $res1 = true;
  191. if ($spread_uid) $res1 = self::where('uid', $spread_uid)->inc('spread_count', 1)->update();
  192. // $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ? : 1;//获取后台分销类型
  193. $res2 = self::create([
  194. 'account' => 'rt' . $routineUser['uid'] . time(),
  195. 'pwd' => md5(123456),
  196. 'nickname' => $routineUser['nickname'] ?: '',
  197. 'avatar' => $routineUser['headimgurl'] ?: '',
  198. 'spread_uid' => $spread_uid,
  199. // 'is_promoter'=>$spread_uid || $storeBrokerageStatu != 1 ? 1: 0,
  200. 'spread_time' => $spread_uid ? time() : 0,
  201. 'uid' => $routineUser['uid'],
  202. 'add_time' => $routineUser['add_time'],
  203. 'add_ip' => request()->ip(),
  204. 'last_time' => time(),
  205. 'last_ip' => request()->ip(),
  206. 'user_type' => $routineUser['user_type'],
  207. 'mer_id' => $routineUser['mer_id']
  208. ]);
  209. $res = $res1 && $res2;
  210. self::checkTrans($res);
  211. return $res2;
  212. }
  213. /**
  214. * 获得当前登陆用户UID
  215. * @return int $uid
  216. */
  217. public static function getActiveUid()
  218. {
  219. $uid = null;
  220. $uid = Session::get('LoginUid');
  221. if ($uid) return $uid;
  222. else return 0;
  223. }
  224. /**
  225. * TODO 查询当前用户信息
  226. * @param $uid $uid 用户编号
  227. * @param string $field $field 查询的字段
  228. * @param bool $mer_id
  229. * @return array
  230. * @throws DataNotFoundException
  231. * @throws DbException
  232. * @throws ModelNotFoundException
  233. */
  234. public static function getUserInfo($uid, $field = '', $mer_id = false)
  235. {
  236. if (strlen(trim($field))) $userInfo = self::merSet($mer_id)->where('uid', $uid)->field($field)->find();
  237. else $userInfo = self::merSet($mer_id)->where('uid', $uid)->find();
  238. if (!$userInfo) return [];
  239. return $userInfo->toArray();
  240. }
  241. /**
  242. * 判断当前用户是否推广员
  243. * @param int $uid
  244. * @return bool
  245. */
  246. public static function isUserSpread($uid = 0)
  247. {
  248. if (!$uid) return false;
  249. $status = (int)SystemConfigService::get('store_brokerage_statu');
  250. $isPromoter = true;
  251. if ($status == 1) $isPromoter = self::where('uid', $uid)->value('is_promoter');
  252. if ($isPromoter) return true;
  253. else return false;
  254. }
  255. /**
  256. * TODO 一级返佣
  257. * @param $orderInfo
  258. * @param bool $open
  259. * @return bool
  260. * @throws DataNotFoundException
  261. * @throws DbException
  262. * @throws ModelNotFoundException
  263. * @throws \think\Exception
  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. $brokeragePrice = StoreProduct::getProductBrokerage($cartId, true, $orderInfo['mer_id']);
  277. //TODO 返佣金额小于等于0 直接返回不返佣金
  278. if ($brokeragePrice <= 0) return true;
  279. //TODO 获取上级推广员信息
  280. $spreadUserInfo = User::getUserInfo($userInfo['spread_uid']);
  281. //TODO 上级推广员返佣之后的金额
  282. $balance = bcadd($spreadUserInfo['brokerage_price'], $brokeragePrice, 2);
  283. $mark = $userInfo['nickname'] . '成功消费' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokeragePrice);
  284. $open && self::beginTrans();
  285. //TODO 添加推广记录
  286. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $balance, $mark, 1, $orderInfo['mer_id']);
  287. //TODO 添加用户余额
  288. $res2 = self::bcInc($userInfo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  289. //TODO 一级返佣成功 跳转二级返佣
  290. $res = $res1 && $res2 && self::backOrderBrokerageTwo($orderInfo, $open);
  291. $open && self::checkTrans($res);
  292. // if($res) return self::backOrderBrokerageTwo($orderInfo);
  293. return $res;
  294. }
  295. /**
  296. * TODO 二级推广
  297. * @param $orderInfo
  298. * @return bool
  299. * @throws DataNotFoundException
  300. * @throws ModelNotFoundException
  301. * @throws DbException
  302. */
  303. public static function backOrderBrokerageTwo($orderInfo, bool $open = true)
  304. {
  305. //TODO 获取购买商品的用户
  306. $userInfo = User::getUserInfo($orderInfo['uid']);
  307. //TODO 获取上推广人
  308. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  309. //TODO 上推广人不存在 或者 上推广人没有上级 或者 当用用户上上级时自己 直接返回
  310. if (!$userInfoTwo || !$userInfoTwo['spread_uid'] || $userInfoTwo['spread_uid'] == $orderInfo['uid']) return true;
  311. //TODO 获取后台分销类型 1 指定分销 2 人人分销
  312. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  313. $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
  314. $brokeragePrice = StoreProduct::getProductBrokerage($cartId, false, $orderInfo['mer_id']);
  315. //TODO 返佣金额小于等于0 直接返回不返佣金
  316. if ($brokeragePrice <= 0) return true;
  317. //TODO 获取上上级推广员信息
  318. $spreadUserInfoTwo = User::getUserInfo($userInfoTwo['spread_uid']);
  319. //TODO 获取上上级推广员返佣之后余额
  320. $balance = bcadd($spreadUserInfoTwo['brokerage_price'], $brokeragePrice, 2);
  321. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokeragePrice);
  322. $open && self::beginTrans();
  323. //TODO 添加返佣记录
  324. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $balance, $mark, 1, $orderInfo['mer_id']);
  325. //TODO 添加用户余额
  326. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'brokerage_price', $brokeragePrice, 'uid');
  327. $res = $res1 && $res2;
  328. $open && self::checkTrans($res);
  329. return $res;
  330. }
  331. /*
  332. * 获取推荐人
  333. * @param int $two_uid
  334. * @param int $first
  335. * @param int $limit
  336. * @return array
  337. * */
  338. public static function getSpreadList($uid, $page, $limit, $mer_id)
  339. {
  340. $one_uids = self::merSet($mer_id)->where('spread_uid', $uid)->column('uid');
  341. $two_uids = self::merSet($mer_id)->whereIn('spread_uid', $one_uids)->where('spread_uid', '<>', 0)->column('uid');
  342. $uids = array_merge($one_uids, $two_uids);
  343. $list = self::merSet($mer_id)->whereIn('uid', $uids)->field('uid,nickname,real_name,avatar,add_time')->page($page, $limit)->order('add_time DESC')->select()->toArray();
  344. foreach ($list as $k => $user) {
  345. $list[$k]['type'] = in_array($user['uid'], $one_uids) ? '一级' : '二级';
  346. $list[$k]['add_time'] = date('Y-m-d', $user['add_time']);
  347. }
  348. $count = self::merSet($mer_id)->whereIn('uid', $uids)->count();
  349. $data['count'] = $count;
  350. $data['list'] = $list;
  351. return $data;
  352. }
  353. /*
  354. * 获取某个用户的下级uid
  355. * @param int $uid 用户uid
  356. * @return array
  357. * */
  358. public static function getOneSpreadUid($uid)
  359. {
  360. return self::where('spread_uid', $uid)->column('uid');
  361. }
  362. /*
  363. * 修改个人信息
  364. * */
  365. public static function editUser($avatar, $nickname, $uid)
  366. {
  367. return self::edit(['avatar' => $avatar, 'nickname' => $nickname], $uid, 'uid');
  368. }
  369. /**
  370. * TODO 获取推广人数 一级
  371. * @param int $uid
  372. * @param bool $mer_id
  373. * @return bool|int|string
  374. */
  375. public static function getSpreadCount($uid = 0, $mer_id = false)
  376. {
  377. if (!$uid) return false;
  378. return self::merSet($mer_id)->where('spread_uid', $uid)->count('uid');
  379. }
  380. public static function setUserSpreadCount($uid, $mer_id = false)
  381. {
  382. self::merSet($mer_id)->where('uid', $uid)->update(['spread_count' => self::getSpreadCount($uid, $mer_id)]);
  383. }
  384. /**
  385. * TODO 获取推广人数 二级
  386. * @param int $uid
  387. * @return bool|int|string
  388. */
  389. public static function getSpreadLevelCount($uid = 0)
  390. {
  391. if (!$uid) return false;
  392. $uidSubordinate = self::where('spread_uid', $uid)->column('uid');
  393. if (!count($uidSubordinate)) return 0;
  394. return self::where('spread_uid', 'IN', implode(',', $uidSubordinate))->count('uid');
  395. }
  396. /**
  397. * 获取用户下级推广人
  398. * @param int $uid 当前用户
  399. * @param int $grade 等级 0 一级 1 二级
  400. * @param string $orderBy 排序
  401. * @param string $keyword
  402. * @param int $page
  403. * @param int $limit
  404. * @return array|bool
  405. */
  406. public static function getUserSpreadGrade($uid = 0, $grade = 0, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  407. {
  408. if (!$uid) return [];
  409. $gradeGroup = [0, 1];
  410. if (!in_array($grade, $gradeGroup)) return self::setErrorInfo('等级错误');
  411. $userStair = self::where('spread_uid', $uid)->column('uid');
  412. if (!count($userStair)) return [];
  413. if ($grade == 0) return self::getUserSpreadCountList(implode(',', $userStair), $orderBy, $keyword, $page, $limit);
  414. $userSecondary = self::where('spread_uid', 'in', implode(',', $userStair))->column('uid');
  415. return self::getUserSpreadCountList(implode(',', $userSecondary), $orderBy, $keyword, $page, $limit);
  416. }
  417. /**
  418. * 获取团队信息
  419. * @param $uid
  420. * @param string $orderBy
  421. * @param string $keyword
  422. * @param int $page
  423. * @param int $limit
  424. * @return array
  425. */
  426. public static function getUserSpreadCountList($uid, $orderBy = '', $keyword = '', $page = 0, $limit = 20)
  427. {
  428. $model = new self;
  429. if ($orderBy === '') $orderBy = 'u.add_time desc';
  430. $model = $model->alias(' u');
  431. $sql = StoreOrder::where('o.paid', 1)->group('o.uid')->field(['SUM(o.pay_price) as numberCount', 'o.uid', 'o.order_id'])
  432. ->where('o.is_del', 0)->where('o.is_system_del', 0)->alias('o')->fetchSql(true)->select();
  433. $model = $model->join("(" . $sql . ") p", 'u.uid = p.uid', 'LEFT');
  434. $model = $model->where('u.uid', 'IN', $uid);
  435. $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");
  436. if (strlen(trim($keyword))) $model = $model->where('u.nickname|u.phone', 'like', "%$keyword%");
  437. $model = $model->group('u.uid');
  438. $model = $model->order($orderBy);
  439. $model = $model->page($page, $limit);
  440. $list = $model->select();
  441. if ($list) return $list->toArray();
  442. else return [];
  443. }
  444. public static function setSpreadUid($uid, $spreadUid)
  445. {
  446. // 自己不能绑定自己为上级
  447. if ($uid == $spreadUid) return false;
  448. //TODO 获取后台分销类型
  449. $storeBrokerageStatus = SystemConfigService::get('store_brokerage_statu');
  450. $storeBrokerageStatus = $storeBrokerageStatus ? $storeBrokerageStatus : 1;
  451. if ($storeBrokerageStatus == 1) {
  452. $spreadCount = self::where('uid', $spreadUid)->count();
  453. if ($spreadCount) {
  454. $spreadInfo = self::where('uid', $spreadUid)->find();
  455. if ($spreadInfo->is_promoter) {
  456. //TODO 只有扫码才可以获得推广权限
  457. if (isset($wechatUser['isPromoter'])) $data['is_promoter'] = 1;
  458. }
  459. }
  460. }
  461. return self::where('uid', $uid)->update(['spread_uid' => $spreadUid, 'spread_time' => time()]);
  462. }
  463. /**
  464. * 判断上下级关系是否存在
  465. * @param $uid
  466. * @param $spreadUid
  467. * @return bool|int
  468. */
  469. public static function validSpread($uid, $spreadUid)
  470. {
  471. if (!$uid || !$spreadUid) return false;
  472. return self::where('uid', $uid)->where('spread_uid', $spreadUid)->count();
  473. }
  474. /**
  475. * H5用户注册
  476. * @param $account
  477. * @param $password
  478. * @param $spread
  479. * @param bool $mer_id
  480. * @return bool
  481. * @throws DataNotFoundException
  482. * @throws DbException
  483. * @throws ModelNotFoundException
  484. */
  485. public static function register($account, $password, $spread, $mer_id = false)
  486. {
  487. if (self::checkPhone($account, $mer_id)) return self::setErrorInfo('用户已存在');
  488. $phone = $account;
  489. $data['account'] = $account;
  490. $data['pwd'] = md5($password);
  491. $data['phone'] = $phone;
  492. $data['mer_id'] = $mer_id ?? 0;
  493. if ($spread) {
  494. $data['spread_uid'] = $spread;
  495. $data['spread_time'] = time();
  496. }
  497. $data['real_name'] = '';
  498. $data['birthday'] = 0;
  499. $data['card_id'] = '';
  500. $data['mark'] = '';
  501. $data['addres'] = '';
  502. $data['user_type'] = 'h5';
  503. $data['add_time'] = time();
  504. $data['add_ip'] = app('request')->ip();
  505. $data['last_time'] = time();
  506. $data['last_ip'] = app('request')->ip();
  507. $data['nickname'] = substr(md5($account . time()), 0, 12);
  508. $data['avatar'] = $data['headimgurl'] = SystemConfigService::get('h5_avatar');
  509. $data['city'] = '';
  510. $data['language'] = '';
  511. $data['province'] = '';
  512. $data['country'] = '';
  513. self::beginTrans();
  514. $res2 = WechatUser::create($data);
  515. $data['uid'] = $res2->uid;
  516. $res1 = self::create($data);
  517. $res3 = true;
  518. if ($spread) {
  519. $res3 = self::setUserSpreadCount($spread, $mer_id);
  520. }
  521. $res = $res1 && $res2 && $res3;
  522. self::checkTrans($res);
  523. return $res;
  524. }
  525. /**
  526. * 密码修改
  527. * @param $account
  528. * @param $password
  529. * @param bool $mer_id
  530. * @return User|bool
  531. * @throws DataNotFoundException
  532. * @throws DbException
  533. * @throws ModelNotFoundException
  534. */
  535. public static function reset($account, $password, $mer_id = false)
  536. {
  537. if (!self::checkPhone($account, $mer_id)) return self::setErrorInfo('用户不存在');
  538. $count = self::merSet($mer_id)->where('account', $account)->where('pwd', md5($password))->count();
  539. if ($count) return true;
  540. return self::merSet($mer_id)->where('account', $account)->update(['pwd' => md5($password)]);
  541. }
  542. /**
  543. * 获取手机号是否注册
  544. * @param $phone
  545. * @param bool $mer_id
  546. * @return bool
  547. * @throws DataNotFoundException
  548. * @throws DbException
  549. * @throws ModelNotFoundException
  550. */
  551. public static function checkPhone($phone, $mer_id = false)
  552. {
  553. return false != self::merSet($mer_id)->where(['account' => $phone])->find();
  554. }
  555. /**
  556. * 获取推广人
  557. * @param array $data 查询条件
  558. * @return array
  559. */
  560. public static function getRankList($data, $mer_id = false)
  561. {
  562. switch ($data['type']) {
  563. case 'week':
  564. $startTime = strtotime('this week');
  565. $endTime = time();
  566. break;
  567. case 'month':
  568. $startTime = strtotime('last month');
  569. $endTime = time();
  570. break;
  571. }
  572. $list = self::alias('t0')
  573. ->field('t0.uid,t0.spread_uid,count(t1.spread_uid) AS count,t0.add_time,t0.nickname,t0.avatar');
  574. if ($mer_id) {
  575. $list = $list->where('t0.mer_id', 'in', [0, $mer_id])->where('t1.mer_id', 'in', [0, $mer_id]);
  576. }
  577. $list = $list->join('user t1', 't0.uid = t1.spread_uid', 'LEFT')
  578. ->where('t1.spread_uid', '<>', 0)
  579. ->order('count desc')
  580. ->order('t0.uid desc')
  581. ->where('t1.add_time', 'BETWEEN', [$startTime, $endTime])
  582. ->page($data['page'], $data['limit'])
  583. ->group('t0.uid')
  584. ->select();
  585. return count($list) ? $list->toArray() : [];
  586. }
  587. /**
  588. * 获取佣金排行
  589. * @param $data
  590. * @return array
  591. */
  592. public static function brokerageRank($data, $mer_id = false)
  593. {
  594. $model = UserBill::alias('b')->join('user u', 'b.uid = u.uid');
  595. $model = $model->where('b.category', 'now_money')->where('b.type', 'brokerage');
  596. switch ($data['type']) {
  597. case 'week':
  598. $model = $model->whereWeek('b.add_time');
  599. break;
  600. case 'month':
  601. $model = $model->whereMonth('b.add_time');
  602. break;
  603. }
  604. if ($mer_id) {
  605. $model->where('u.mer_id', 'in', [0, $mer_id]);
  606. }
  607. $users = $model->group('b.uid')
  608. ->field('b.uid,u.nickname,u.avatar,SUM(IF(pm=1,`number`,-`number`)) as brokerage_price')
  609. ->order('brokerage_price desc')
  610. ->page((int)$data['page'], (int)$data['limit'])
  611. ->select();
  612. return count($users) ? $users->toArray() : [];
  613. }
  614. /**
  615. * 获取当前用户的佣金排行位置
  616. * @param $uid
  617. * @return int
  618. */
  619. public static function currentUserRank($type, $brokerage_price)
  620. {
  621. $model = self::where('status', 1);
  622. switch ($type) {
  623. case 'week':
  624. $model = $model->whereIn('uid', function ($query) {
  625. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  626. ->whereWeek('add_time')->field('uid');
  627. });
  628. break;
  629. case 'month':
  630. $model = $model->whereIn('uid', function ($query) {
  631. $query->name('user_bill')->where('category', 'now_money')->where('type', 'brokerage')
  632. ->whereMonth('add_time')->field('uid');
  633. });
  634. break;
  635. }
  636. return $model->where('brokerage_price', '>', $brokerage_price)->count('uid');
  637. }
  638. /** 后台 获取用户某个时间段的消费信息
  639. * @param $where
  640. * @param string $status
  641. * @param string $keep
  642. * @return array|float|int
  643. * @throws DataNotFoundException
  644. * @throws DbException
  645. * @throws ModelNotFoundException
  646. */
  647. public static function consume($where, $status = '', $keep = '', $mer_id = '')
  648. {
  649. $model = new self;
  650. if ($mer_id) {
  651. $model = $model->merSet($mer_id);
  652. }
  653. $user_id = [];
  654. if (is_array($where)) {
  655. if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
  656. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  657. switch ($where['date']) {
  658. case null:
  659. case 'today':
  660. case 'week':
  661. case 'year':
  662. if ($where['date'] == null) {
  663. $where['date'] = 'month';
  664. }
  665. if ($keep) {
  666. $model = $model->whereTime('add_time', $where['date'])->whereTime('last_time', $where['date']);
  667. } else {
  668. $model = $model->whereTime('add_time', $where['date']);
  669. }
  670. break;
  671. case 'quarter':
  672. $quarter = self::getMonth('n');
  673. $startTime = strtotime($quarter[0]);
  674. $endTime = strtotime($quarter[1]);
  675. if ($keep) {
  676. $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime)->where('last_time', '>', $startTime)->where('last_time', '<', $endTime);
  677. } else {
  678. $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime);
  679. }
  680. break;
  681. default:
  682. //自定义时间
  683. if (strstr($where['date'], '-') !== FALSE) {
  684. list($startTime, $endTime) = explode('-', $where['date']);
  685. $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', bcadd(strtotime($endTime), 86400, 0));
  686. } else {
  687. $model = $model->whereTime('add_time', 'month');
  688. }
  689. break;
  690. }
  691. } else {
  692. if (is_array($status)) {
  693. $model = $model->where('add_time', '>', $status[0])->where('add_time', '<', $status[1]);
  694. }
  695. }
  696. if ($keep === true) {
  697. return $model->count();
  698. }
  699. if ($status === 'default') {
  700. return $model->group('from_unixtime(add_time,\'%Y-%m-%d\')')->field('count(uid) num,from_unixtime(add_time,\'%Y-%m-%d\') add_time,uid')->select()->toArray();
  701. }
  702. if ($status === 'grouping') {
  703. return $model->group('user_type')->field('user_type')->select()->toArray();
  704. }
  705. $uid = $model->field('uid')->select()->toArray();
  706. foreach ($uid as $val) {
  707. $user_id[] = $val['uid'];
  708. }
  709. if (empty($user_id)) {
  710. $user_id = [0];
  711. }
  712. if ($status === 'xiaofei') {
  713. $list = UserBill::where('uid', 'in', $user_id)
  714. ->group('type')
  715. ->field('sum(number) as top_number,title')
  716. ->select()
  717. ->toArray();
  718. $series = [
  719. 'name' => isset($list[0]['title']) ? $list[0]['title'] : '',
  720. 'type' => 'pie',
  721. 'radius' => ['40%', '50%'],
  722. 'data' => []
  723. ];
  724. foreach ($list as $key => $val) {
  725. $series['data'][$key]['value'] = $val['top_number'];
  726. $series['data'][$key]['name'] = $val['title'];
  727. }
  728. return $series;
  729. } else if ($status === 'form') {
  730. $list = WechatUser::where('uid', 'in', $user_id)->group('city')->field('count(city) as top_city,city')->limit(0, 10)->select()->toArray();
  731. $count = self::getcount();
  732. $option = [
  733. 'legend_date' => [],
  734. 'series_date' => []
  735. ];
  736. foreach ($list as $key => $val) {
  737. $num = $count != 0 ? (bcdiv($val['top_city'], $count, 2)) * 100 : 0;
  738. $t = ['name' => $num . '% ' . (empty($val['city']) ? '未知' : $val['city']), 'icon' => 'circle'];
  739. $option['legend_date'][$key] = $t;
  740. $option['series_date'][$key] = ['value' => $num, 'name' => $t['name']];
  741. }
  742. return $option;
  743. } else {
  744. $number = UserBill::where('uid', 'in', $user_id)->where('type', 'pay_product')->sum('number');
  745. return $number;
  746. }
  747. }
  748. /*
  749. * 获取 用户某个时间段的钱数或者TOP20排行
  750. *
  751. * return Array || number
  752. */
  753. public static function getUserSpend($date, $status = '', $mer_id = '')
  754. {
  755. $model = new self();
  756. if ($mer_id) {
  757. $model = $model->merSet($mer_id);
  758. }
  759. $model = $model->alias('A');
  760. switch ($date) {
  761. case null:
  762. case 'today':
  763. case 'week':
  764. case 'year':
  765. if ($date == null) $date = 'month';
  766. $model = $model->whereTime('A.add_time', $date);
  767. break;
  768. case 'quarter':
  769. list($startTime, $endTime) = User::getMonth('n');
  770. $model = $model->where('A.add_time', '>', strtotime($startTime));
  771. $model = $model->where('A.add_time', '<', bcadd(strtotime($endTime), 86400, 0));
  772. break;
  773. default:
  774. list($startTime, $endTime) = explode('-', $date);
  775. $model = $model->where('A.add_time', '>', strtotime($startTime));
  776. $model = $model->where('A.add_time', '<', bcadd(strtotime($endTime), 86400, 0));
  777. break;
  778. }
  779. if ($status === true) {
  780. return $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
  781. }
  782. $list = $model->join('user_bill B', 'B.uid=A.uid')
  783. ->where('B.type', 'pay_product')
  784. ->where('B.pm', 0)
  785. ->field('sum(B.number) as totel_number,A.nickname,A.avatar,A.now_money,A.uid,A.add_time')
  786. ->order('totel_number desc')
  787. ->limit(0, 20)
  788. ->select()
  789. ->toArray();
  790. if (!isset($list[0]['totel_number'])) {
  791. $list = [];
  792. }
  793. return $list;
  794. }
  795. /*
  796. * 获取 相对于上月或者其他的数据
  797. *
  798. * return Array
  799. */
  800. public static function getPostNumber($date, $status = false, $field = 'A.add_time', $t = '消费', $mer_id = '')
  801. {
  802. $model = new self();
  803. if ($mer_id) {
  804. $model = $model->merSet($mer_id);
  805. }
  806. if (!$status) $model = $model->alias('A');
  807. switch ($date) {
  808. case null:
  809. case 'today':
  810. case 'week':
  811. case 'year':
  812. if ($date == null) {
  813. $date = 'last month';
  814. $title = '相比上月用户' . $t . '增长';
  815. }
  816. if ($date == 'today') {
  817. $date = 'yesterday';
  818. $title = '相比昨天用户' . $t . '增长';
  819. }
  820. if ($date == 'week') {
  821. $date = 'last week';
  822. $title = '相比上周用户' . $t . '增长';
  823. }
  824. if ($date == 'year') {
  825. $date = 'last year';
  826. $title = '相比去年用户' . $t . '增长';
  827. }
  828. $model = $model->whereTime($field, $date);
  829. break;
  830. case 'quarter':
  831. $title = '相比上季度用户' . $t . '增长';
  832. list($startTime, $endTime) = User::getMonth('n', 1);
  833. $model = $model->where($field, '>', $startTime);
  834. $model = $model->where($field, '<', $endTime);
  835. break;
  836. default:
  837. list($startTime, $endTime) = explode('-', $date);
  838. $title = '相比' . $startTime . '-' . $endTime . '时间段用户' . $t . '增长';
  839. $Time = strtotime($endTime) - strtotime($startTime);
  840. $model = $model->where($field, '>', strtotime($startTime) + $Time);
  841. $model = $model->where($field, '<', strtotime($endTime) + $Time);
  842. break;
  843. }
  844. if ($status) {
  845. return [$model->count(), $title];
  846. }
  847. $number = $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
  848. return [$number, $title];
  849. }
  850. /**
  851. * 设置搜索条件
  852. * @param $where
  853. * @return $this
  854. */
  855. public static function setWhere($where)
  856. {
  857. if ($where['order'] != '') {
  858. $model = self::order(self::setOrder($where['order']));
  859. } else {
  860. $model = self::order('u.uid desc');
  861. }
  862. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  863. list($startTime, $endTime) = explode('-', $where['user_time']);
  864. $endTime = strtotime($endTime) + 24 * 3600;
  865. $model = $model->where("u.last_time < " . strtotime($startTime) . " OR u.last_time > " . $endTime);
  866. }
  867. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  868. list($startTime, $endTime) = explode('-', $where['user_time']);
  869. $model = $model->where('u.last_time', '>', strtotime($startTime));
  870. $model = $model->where('u.last_time', '<', strtotime($endTime) + 24 * 3600);
  871. }
  872. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  873. list($startTime, $endTime) = explode('-', $where['user_time']);
  874. $model = $model->where('u.add_time', '>', strtotime($startTime));
  875. $model = $model->where('u.add_time', '<', strtotime($endTime) + 24 * 3600);
  876. }
  877. if ($where['pay_count'] !== '') {
  878. if ($where['pay_count'] == '-1')
  879. $model = $model->where('pay_count', 0);
  880. else
  881. $model = $model->where('pay_count', '>', $where['pay_count']);
  882. }
  883. if ($where['user_type'] != '') {
  884. if ($where['user_type'] == 'routine')
  885. $model = $model->where('w.routine_openid', 'not null');
  886. else if ($where['user_type'] == 'wechat')
  887. $model = $model->where('w.openid', 'not null');
  888. else
  889. $model = $model->where('u.user_type', $where['user_type']);
  890. }
  891. if ($where['country'] != '') {
  892. if ($where['country'] == 'domestic')
  893. $model = $model->where('w.country', '中国');
  894. else if ($where['country'] == 'abroad')
  895. $model = $model->where('w.country', '<>', '中国');
  896. }
  897. if ($where['level'] != '') {
  898. $model = $model->where('level', $where['level'])->where('clean_time', 0);
  899. }
  900. if ($where['group_id'] != '') {
  901. $model = $model->where('group_id', $where['group_id']);
  902. }
  903. // if ($where['mer_id'] != '') {
  904. // $model = $model->where('mer_id', 'in', [0, $where['mer_id']]);
  905. // }
  906. if ($where['label_id'] != '') {
  907. $model = $model->whereIn('u.uid', function ($query) use ($where) {
  908. $query->name('user_label_relation')->where('label_id', $where['label_id'])->field('uid')->select();
  909. });
  910. }
  911. return $model;
  912. }
  913. /**
  914. * 异步获取当前用户 信息
  915. * @param $where
  916. * @return array
  917. */
  918. public static function getUserList($where)
  919. {
  920. $model = self::setWherePage(self::setWhere($where), $where, ['w.sex', 'w.province', 'w.city', 'u.status', 'u.is_promoter'], ['u.nickname', 'u.uid', 'u.phone']);
  921. $labels = UserLabel::column('label_name', 'id');
  922. $list = $model->alias('u')
  923. ->join('WechatUser w', 'u.uid=w.uid')
  924. ->field('u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.routine_openid,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time')
  925. ->page((int)$where['page'], (int)$where['limit'])
  926. ->select()
  927. ->each(function ($item) use ($labels) {
  928. $item['status'] = ($item['status'] == 1) ? '正常' : '禁止';
  929. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  930. if ($item['last_time']) $item['last_time'] = date('Y-m-d H:i:s', $item['last_time']);//最近一次访问日期
  931. else $item['last_time'] = '无访问';//最近一次访问日期
  932. //self::edit(['pay_count' => StoreOrder::getUserCountPay($item['uid'])], $item['uid']);
  933. //$item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现
  934. if ($item['spread_uid']) {
  935. $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname') . '/' . $item['spread_uid'];
  936. } else {
  937. $item['spread_uid_nickname'] = '无';
  938. }
  939. if ($item['openid'] != '' && $item['routine_openid'] != '') {
  940. $item['user_type'] = '通用';
  941. } else if ($item['openid'] == '' && $item['routine_openid'] != '') {
  942. $item['user_type'] = '小程序';
  943. } else if ($item['openid'] != '' && $item['routine_openid'] == '') {
  944. $item['user_type'] = '公众号';
  945. } else if ($item['user_type'] == 'h5') {
  946. $item['user_type'] = 'H5';
  947. } else $item['user_type'] = '其他';
  948. if ($item['sex'] == 1) {
  949. $item['sex'] = '男';
  950. } else if ($item['sex'] == 2) {
  951. $item['sex'] = '女';
  952. } else $item['sex'] = '保密';
  953. $item['level'] = SystemUserLevel::where('id', $item['level'])->value('name') ?? '无';
  954. $item['group_id'] = UserGroup::where('id', $item['group_id'])->value('group_name') ?? '无';
  955. $item['vip_name'] = false;
  956. $levelinfo = UserLevel::where('uid', $item['uid'])->where('level_id', $item['level'])->where('is_del', 0)->order('grade desc')->field('level_id,is_forever,valid_time')->find();
  957. if ($levelinfo) {
  958. if ($levelinfo['is_forever']) $item['vip_name'] = SystemUserLevel::where('id', $levelinfo['level_id'])->value('name');
  959. else if (time() < $levelinfo['valid_time']) $item['vip_name'] = SystemUserLevel::where('id', $levelinfo['level_id'])->value('name');
  960. }
  961. $item['birthday'] = date('Y-m-d', $item['birthday']);
  962. $lids = UserLabelRelation::where('uid', $item['uid'])->column('label_id') ?? [];
  963. $label = [];
  964. foreach ($lids as $k => $v) {
  965. if (!empty($labels)) {
  966. if (in_array($v, array_keys($labels))) $label[] = $labels[$v];
  967. }
  968. }
  969. $item['labels'] = implode(',', $label);
  970. });//->toArray();
  971. $count = self::setWherePage(self::setWhere($where), $where, ['w.sex', 'w.province', 'w.city', 'u.status', 'u.is_promoter'], ['u.nickname', 'u.uid'])->alias('u')->join('WechatUser w', 'u.uid=w.uid')->count();
  972. return ['count' => $count, 'list' => $list];
  973. }
  974. //获取某用户的详细信息
  975. public static function getUserDetailed($uid)
  976. {
  977. $key_field = ['real_name', 'phone', 'province', 'city', 'district', 'detail', 'post_code'];
  978. $Address = ($thisAddress = UserAddress::where('uid', $uid)->where('is_default', 1)->field($key_field)->find()) ?
  979. $thisAddress :
  980. UserAddress::where('uid', $uid)->field($key_field)->find();
  981. $UserInfo = self::get($uid);
  982. return [
  983. ['col' => 12, 'name' => '默认收货地址', 'value' => $thisAddress ? '收货人:' . $thisAddress['real_name'] . '邮编:' . $thisAddress['post_code'] . ' 收货人电话:' . $thisAddress['phone'] . ' 地址:' . $thisAddress['province'] . ' ' . $thisAddress['city'] . ' ' . $thisAddress['district'] . ' ' . $thisAddress['detail'] : ''],
  984. // ['name'=>'微信OpenID','value'=>WechatUser::where('uid', $uid)->value('openid'),'col'=>8],
  985. ['name' => '手机号码', 'value' => $UserInfo['phone']],
  986. // ['name'=>'ID','value'=>$uid],
  987. ['name' => '姓名', 'value' => ''],
  988. ['name' => '微信昵称', 'value' => $UserInfo['nickname']],
  989. ['name' => '头像', 'value' => $UserInfo['avatar']],
  990. ['name' => '邮箱', 'value' => ''],
  991. ['name' => '生日', 'value' => ''],
  992. ['name' => '积分', 'value' => $UserInfo['integral']],
  993. ['name' => '上级推广人', 'value' => $UserInfo['spread_uid'] ? self::where('uid', $UserInfo['spread_uid'])->value('nickname') : ''],
  994. ['name' => '账户余额', 'value' => $UserInfo['now_money']],
  995. ['name' => '佣金总收入', 'value' => UserBill::where('category', 'now_money')->where('type', 'brokerage')->where('uid', $uid)->sum('number')],
  996. ['name' => '提现总金额', 'value' => UserExtract::where('uid', $uid)->where('status', 1)->sum('extract_price')],
  997. ];
  998. }
  999. //获取某用户的订单个数,消费明细
  1000. public static function getHeaderList($uid)
  1001. {
  1002. return [
  1003. [
  1004. 'title' => '余额',
  1005. 'value' => self::where('uid', $uid)->value('now_money'),
  1006. 'key' => '元',
  1007. 'class' => '',
  1008. ],
  1009. [
  1010. 'title' => '总计订单',
  1011. 'value' => StoreOrder::where('uid', $uid)->count(),
  1012. 'key' => '笔',
  1013. 'class' => '',
  1014. ],
  1015. [
  1016. 'title' => '总消费金额',
  1017. 'value' => StoreOrder::where('uid', $uid)->where('paid', 1)->sum('total_price'),
  1018. 'key' => '元',
  1019. 'class' => '',
  1020. ],
  1021. [
  1022. 'title' => '积分',
  1023. 'value' => self::where('uid', $uid)->value('integral'),
  1024. 'key' => '',
  1025. 'class' => '',
  1026. ],
  1027. [
  1028. 'title' => '本月订单',
  1029. 'value' => StoreOrder::where('uid', $uid)->whereTime('add_time', 'month')->count(),
  1030. 'key' => '笔',
  1031. 'class' => '',
  1032. ],
  1033. [
  1034. 'title' => '本月消费金额',
  1035. 'value' => StoreOrder::where('uid', $uid)->where('paid', 1)->whereTime('add_time', 'month')->sum('total_price'),
  1036. 'key' => '元',
  1037. 'class' => '',
  1038. ]
  1039. ];
  1040. }
  1041. /*
  1042. * 获取 会员 订单个数,积分明细,优惠劵明细
  1043. *
  1044. * $uid 用户id;
  1045. *
  1046. * return array
  1047. */
  1048. public static function getCountInfo($uid)
  1049. {
  1050. $order_count = StoreOrder::where('uid', $uid)->count();
  1051. $integral_count = UserBill::where('uid', $uid)->where('category', 'integral')->where('type', 'in', 'deduction,system_add')->count();
  1052. $sign_count = UserBill::where('type', 'sign')->where('uid', $uid)->where('category', 'integral')->count();
  1053. $balanceChang_count = UserBill::where('category', 'now_money')->where('uid', $uid)
  1054. ->where('type', 'in', 'system_add,pay_product,extract,pay_product_refund,system_sub')
  1055. ->count();
  1056. $coupon_count = StoreCouponUser::where('uid', $uid)->count();
  1057. $spread_count = self::where('spread_uid', $uid)->count();
  1058. return compact('order_count', 'integral_count', 'sign_count', 'balanceChang_count', 'coupon_count', 'spread_count');
  1059. }
  1060. //获取佣金记录列表
  1061. public static function getCommissionList($where)
  1062. {
  1063. $model = self::setCommissionWhere($where);
  1064. $list = $model->page((int)$where['page'], (int)$where['limit'])->select();
  1065. count($list) && $list = $list->toArray();
  1066. foreach ($list as &$value) {
  1067. $value['ex_price'] = UserExtract::where('uid', $value['uid'])->sum('extract_price');
  1068. $value['extract_price'] = UserExtract::where('uid', $value['uid'])->where('status', 1)->sum('extract_price');
  1069. $cashPrice = UserExtract::where('uid', $value['uid'])->where('status', 0)->sum('extract_price');
  1070. $value['money'] = bcsub($value['ex_price'], $value['extract_price'], 2);
  1071. $value['money'] = bcsub($value['money'], $cashPrice, 2);
  1072. }
  1073. $count = self::setCommissionWhere($where)->count();
  1074. return ['data' => $list, 'count' => $count];
  1075. }
  1076. //获取佣金记录列表
  1077. public static function exportData($where)
  1078. {
  1079. $model = self::setCommissionWhere($where);
  1080. $list = $model->select();
  1081. count($list) && $list = $list->toArray();
  1082. foreach ($list as &$value) {
  1083. $value['ex_price'] = UserExtract::where('uid', $value['uid'])->sum('extract_price');
  1084. $value['extract_price'] = UserExtract::where('uid', $value['uid'])->where('status', 1)->sum('extract_price');
  1085. $cashPrice = UserExtract::where('uid', $value['uid'])->where('status', 0)->sum('extract_price');
  1086. $value['money'] = bcsub($value['ex_price'], $value['extract_price'], 2);
  1087. $value['money'] = bcsub($value['money'], $cashPrice, 2);
  1088. }
  1089. return $list;
  1090. }
  1091. //获取佣金记录列表的查询条件
  1092. public static function setCommissionWhere($where)
  1093. {
  1094. $models = self::setWherePage(self::alias('A'), $where, [], ['A.nickname', 'A.uid'])
  1095. ->join('user_bill B', 'B.uid=A.uid')
  1096. ->group('A.uid')
  1097. ->where('B.type', 'brokerage')
  1098. ->where('B.category', 'now_money')
  1099. ->field('sum(B.number) as sum_number,A.nickname,A.uid,A.now_money,A.brokerage_price');
  1100. if ($where['price_max'] != '' && $where['price_min'] != '') {
  1101. $models = $models->where('brokerage_price', 'between', [$where['price_min'], $where['price_max']]);
  1102. }
  1103. if ($where['mer_id']) {
  1104. $models = $models->where('A.mer_id', $where['mer_id']);
  1105. }
  1106. return $models;
  1107. }
  1108. //获取某人用户推广信息
  1109. public static function userInfo($uid)
  1110. {
  1111. $userinfo = self::where('uid', $uid)->field('nickname,spread_uid,now_money,add_time')->find()->toArray();
  1112. $userinfo['number'] = (float)UserBill::where('category', 'now_money')->where('uid', $uid)->where('type', 'brokerage')->sum('number');
  1113. $userinfo['spread_name'] = $userinfo['spread_uid'] ? self::where('uid', $userinfo['spread_uid'])->value('nickname') : '';
  1114. $userinfo['add_time'] = date('Y-m-d H:i:s', $userinfo['add_time']);
  1115. return $userinfo;
  1116. }
  1117. /*
  1118. * 获取和提现金额
  1119. * @param array $uid
  1120. * @return float
  1121. * */
  1122. public static function getextractPrice($uid, $where = [])
  1123. {
  1124. if (is_array($uid)) {
  1125. if (!count($uid)) return 0;
  1126. } else
  1127. $uid = [$uid];
  1128. $brokerage = UserBill::getAdminBrokerage($uid, 'now_money', 'brokerage', $where);//获取总佣金
  1129. $recharge = UserBill::getAdminBrokerage($uid, 'now_money', 'recharge', $where);//累计充值
  1130. $extractTotalPrice = UserExtract::userExtractAdminTotalPrice($uid, 1, $where);//累计提现
  1131. if ($brokerage > $extractTotalPrice) {
  1132. $orderYuePrice = self::getModelTime($where, StoreOrder::where('uid', 'in', $uid)->where(['is_del' => 0, 'paid' => 1]))->sum('pay_price');//余额累计消费
  1133. $systemAdd = UserBill::getAdminBrokerage($uid, 'now_money', 'system_add', $where);//后台添加余额
  1134. $yueCount = bcadd($recharge, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  1135. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
  1136. $brokerage = bcsub($brokerage, $extractTotalPrice, 2);//减去已提现金额
  1137. $extract_price = UserExtract::userExtractAdminTotalPrice($uid, 0, $where);
  1138. $brokerage = $extract_price < $brokerage ? bcsub($brokerage, $extract_price, 2) : 0;//减去审核中的提现金额
  1139. $brokerage = $brokerage > $orderYuePrice ? bcsub($brokerage, $orderYuePrice, 2) : 0;//减掉余额支付
  1140. } else {
  1141. $brokerage = 0;
  1142. }
  1143. $num = (float)bcsub($brokerage, $extractTotalPrice, 2);
  1144. return $num > 0 ? $num : 0;//可提现
  1145. }
  1146. /**获取用户详细信息
  1147. * @param $uid
  1148. * @param bool $mer_id
  1149. * @return array
  1150. * @throws DataNotFoundException
  1151. * @throws DbException
  1152. * @throws ModelNotFoundException
  1153. * @throws Exception
  1154. */
  1155. public static function getUserInfos($uid, $mer_id = false)
  1156. {
  1157. $userInfo = self::merSet($mer_id)->where('uid', $uid)->find();
  1158. if (!$userInfo) exception('读取用户信息失败!');
  1159. return $userInfo->toArray();
  1160. }
  1161. }