WechatUser.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/28
  5. */
  6. namespace app\models\wechat;
  7. use app\models\store\StoreOrder;
  8. use app\models\user\User;
  9. use app\models\user\UserBill;
  10. use app\models\user\UserExtract;
  11. use think\db\exception\DataNotFoundException;
  12. use think\db\exception\DbException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\facade\Cache;
  15. use think\facade\Config;
  16. use crmeb\traits\ModelTrait;
  17. use crmeb\basic\BaseModel;
  18. use crmeb\services\WechatService;
  19. use crmeb\services\SystemConfigService;
  20. use app\models\store\StoreOrderStatus;
  21. use app\models\wechat\WechatUser as UserModel;
  22. /**
  23. * 微信用户 model
  24. * Class WechatUser
  25. * @package app\models\wechat
  26. */
  27. class WechatUser extends BaseModel
  28. {
  29. /**
  30. * 数据表主键
  31. * @var string
  32. */
  33. protected $pk = 'uid';
  34. /**
  35. * 模型名称
  36. * @var string
  37. */
  38. protected $name = 'wechat_user';
  39. use ModelTrait;
  40. protected $insert = ['add_time'];
  41. /**
  42. * 用uid获得 微信openid
  43. * @param $uid
  44. * @return mixed
  45. * @throws \Exception
  46. */
  47. public static function uidToOpenid($uid)
  48. {
  49. $openid = self::where('uid', $uid)->value('openid');
  50. if (!$openid) exception('对应的openid不存在!');
  51. return $openid;
  52. }
  53. /**
  54. * 用uid获得 小程序 openid
  55. * @param $uid
  56. * @return mixed
  57. * @throws \Exception
  58. */
  59. public static function uidToRoutineOpenid($uid)
  60. {
  61. $openid = self::where('uid', $uid)->value('routine_openid');
  62. if (!$openid) exception('对应的routine_openid不存在!');
  63. return $openid;
  64. }
  65. public static function setAddTimeAttr($value)
  66. {
  67. return time();
  68. }
  69. protected function getAddTimeAttr($value)
  70. {
  71. if ($value) return date('Y-m-d H:i', $value);
  72. return $value;
  73. }
  74. /**
  75. * .添加新用户
  76. * @param $openid
  77. * @return object
  78. */
  79. public static function setNewUser($openid)
  80. {
  81. $userInfo = WechatService::getUserInfo($openid);
  82. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  83. return self::create($userInfo);
  84. }
  85. /**
  86. * 更新用户信息
  87. * @param $openid
  88. * @return bool
  89. */
  90. public static function updateUser($openid)
  91. {
  92. $userInfo = WechatService::getUserInfo($openid);
  93. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  94. return self::edit($userInfo, $openid, 'openid');
  95. }
  96. /**
  97. * 用户存在就更新 不存在就添加
  98. * @param $openid
  99. */
  100. public static function saveUser($openid)
  101. {
  102. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  103. }
  104. /**
  105. * 用户取消关注
  106. * @param $openid
  107. * @return bool
  108. */
  109. public static function unSubscribe($openid)
  110. {
  111. return self::edit(['subscribe' => 0], $openid, 'openid');
  112. }
  113. /**
  114. * 获取微信用户
  115. * @param array $where
  116. * @return array
  117. */
  118. public static function systemPage($where = array(), $isall = false)
  119. {
  120. $model = new self;
  121. $model = $model->alias('w');
  122. $model = $model->where('w.openid|routine_openid', 'NOT NULL');
  123. if ($where['nickname'] !== '') $model = $model->where('w.nickname', 'LIKE', "%$where[nickname]%");
  124. $model = self::getModelTime($where, $model, "w.add_time");
  125. if (isset($where['tagid_list']) && $where['tagid_list'] !== '') {
  126. $tagid_list = explode(',', $where['tagid_list']);
  127. foreach ($tagid_list as $v) {
  128. $model = $model->where('w.tagid_list', 'LIKE', "%$v%");
  129. }
  130. }
  131. if (isset($where['groupid']) && $where['groupid'] !== '-1') $model = $model->where('w.groupid', "$where[groupid]");
  132. if (isset($where['sex']) && $where['sex'] !== '') $model = $model->where('w.sex', "$where[sex]");
  133. if (isset($where['subscribe']) && $where['subscribe'] !== '') $model = $model->where('w.subscribe', "$where[subscribe]");
  134. if (isset($where['type']) && $where['type'] !== '') $model = $model->where('w.user_type', $where['type']);
  135. if (isset($where['mer_id']) && $where['mer_id'] !== '') $model = $model->where('u.mer_id', $where['mer_id']);
  136. $model = $model->join('user u', 'u.uid = w.uid');
  137. $model = $model->order('w.uid desc');
  138. $count = $model->count();
  139. $list = $model->page((int)$where['page'], (int)$where['limit'])
  140. ->field('w.uid,w.openid,w.nickname,w.headimgurl,w.sex,w.country,w.province,w.city,w.subscribe')
  141. ->select()
  142. ->each(function ($item) {
  143. // $groupList = UserModel::getUserGroup();
  144. // $tagList = UserModel::getUserTag();
  145. // $groupList = [2=>'222'];//测试
  146. // $tagList = [2=>'222'];//测试
  147. // if (!is_array($tagList)){
  148. // $item['tag_name'] = [];
  149. // }else{
  150. // $tagId = explode(',',$item['tagid_list']);
  151. // if(!$tagList || $item['tagid_list'] == ''|| !$tagId){
  152. // $item['tag_name'] = [];
  153. // }else{
  154. // foreach ($tagId as $k=>$vo){
  155. // if (isset($tagList[$vo])){
  156. // $item['tag_name'][$k] = $tagList[$vo];
  157. //// $item['tag_name'][$k]['name'] = $tagList[$vo];
  158. // }
  159. // }
  160. // }
  161. // }
  162. // if (!is_array($groupList)){
  163. // $item['group_name'] = '无';
  164. // }else{
  165. // if(!$groupList || $item['groupid'] == 0 || !isset($groupList[$item['groupid']])){
  166. // $item['group_name'] = '无';
  167. // }else{
  168. // $item['group_name'] = $groupList[$item['groupid']];
  169. // }
  170. // }
  171. $item['subscribe_time'] = $item['subscribe_time'] ? date('Y-m-d H:i', $item['subscribe_time']) : '';
  172. });
  173. return compact('count', 'list');
  174. }
  175. /**
  176. * 获取微信用户
  177. * @param array $where
  178. * @return array
  179. */
  180. public static function exportData($where = array(), $isall = false)
  181. {
  182. $model = new self;
  183. $model = $model->where('openid|routine_openid', 'NOT NULL');
  184. if ($where['nickname'] !== '') $model = $model->where('nickname', 'LIKE', "%$where[nickname]%");
  185. $model = self::getModelTime($where, $model, "add_time");
  186. if (isset($where['tagid_list']) && $where['tagid_list'] !== '') {
  187. $tagid_list = explode(',', $where['tagid_list']);
  188. foreach ($tagid_list as $v) {
  189. $model = $model->where('tagid_list', 'LIKE', "%$v%");
  190. }
  191. }
  192. if (isset($where['groupid']) && $where['groupid'] !== '-1') $model = $model->where('groupid', "$where[groupid]");
  193. if (isset($where['sex']) && $where['sex'] !== '') $model = $model->where('sex', "$where[sex]");
  194. if (isset($where['subscribe']) && $where['subscribe'] !== '') $model = $model->where('subscribe', "$where[subscribe]");
  195. $model = $model->order('uid desc');
  196. $list = $model->select()->toArray();
  197. return $list;
  198. }
  199. public static function setSpreadWhere($where = [], $alias = 'a', $model = null)
  200. {
  201. $model = is_null($model) ? new self() : $model;
  202. if ($alias) {
  203. $model = $model->alias($alias)->join('user u', 'a.uid=u.uid')->order('u.uid desc');
  204. $alias .= '.';
  205. }
  206. $status = (int)SystemConfigService::get('store_brokerage_statu');
  207. if ($status == 1) {
  208. if ($Listuids = User::where(['is_promoter' => 1])->field('uid')->select()) {
  209. $newUids = [];
  210. foreach ($Listuids as $item) {
  211. $newUids[] = $item['uid'];
  212. }
  213. $uids = $newUids;
  214. unset($uid, $newUids);
  215. $model = $model->where($alias . 'uid', 'in', implode(',', $uids));
  216. } else
  217. $model = $model->where($alias . 'uid', -1);
  218. }
  219. if ($where['nickname'] !== '') $model = $model->where("{$alias}nickname|{$alias}uid|u.phone", 'LIKE', "%$where[nickname]%");
  220. $model = self::getModelTime($where, $model, "{$alias}add_time");
  221. if (isset($where['sex']) && $where['sex'] !== '') $model = $model->where($alias . 'sex', $where['sex']);
  222. if (isset($where['subscribe']) && $where['subscribe'] !== '') $model = $model->where($alias . 'subscribe', $where['subscribe']);
  223. if (isset($where['order']) && $where['order'] != '') $model = $model->order($where['order']);
  224. if (isset($where['user_type']) && $where['user_type'] != '') {
  225. if ($where['user_type'] == 1) {
  226. $model = $model->where($alias . 'unionid', 'neq', 'NULL');
  227. } else if ($where['user_type'] == 2)
  228. $model = $model->where($alias . 'openid', 'neq', 'NULL')->where($alias . 'unionid', 'NULL');
  229. else if ($where['user_type'] == 3)
  230. $model = $model->where($alias . 'routine_openid', 'neq', 'NULL')->where($alias . 'unionid', 'NULL');
  231. }
  232. if (isset($where['is_time']) && isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, $alias . 'add_time');
  233. return $model;
  234. }
  235. /**
  236. * 获取分销用户
  237. * @param array $where
  238. * @return array
  239. * @throws DataNotFoundException
  240. * @throws DbException
  241. * @throws ModelNotFoundException
  242. */
  243. public static function agentSystemPage($where = [])
  244. {
  245. //提现数据
  246. $exteactSql = UserExtract::merSet($where['mer_id'] ?? false)->where(['status' => 1])
  247. ->group('uid')
  248. ->field(['sum(extract_price) as extract_count_price', 'count(id) as extract_count_num', 'uid as euid'])
  249. ->fetchSql(true)
  250. ->select();
  251. //订单数据
  252. $orderSql = StoreOrder::merSet($where['mer_id'] ?? false)->alias('o')
  253. ->where(['o.paid' => 1, 'o.refund_status' => 0])
  254. ->field(['sum(o.pay_price) as order_price', 'count(o.id) as order_count', 'o.uid as ouid'])
  255. ->group('o.uid')
  256. ->fetchSql(true)
  257. ->select();
  258. //佣金数据
  259. $billSql = UserBill::merSet($where['mer_id'] ?? false)->where(['status' => 1])
  260. ->where('type', 'brokerage')
  261. ->where('pm', 1)
  262. ->group('uid')
  263. ->field(['sum(number) as brokerage_money', 'uid as buid'])
  264. ->fetchSql(true)
  265. ->select();
  266. $model = User::merSet($where['mer_id'] ?? false)->where(['status' => 1])->where('is_promoter', 1);
  267. $model = $model->alias('u')
  268. ->join('(' . $orderSql . ') o', 'o.ouid = u.uid', 'left')
  269. ->join('(' . $billSql . ') b', 'b.buid = u.uid', 'left')
  270. ->join('(' . $exteactSql . ') e', 'e.euid = u.uid', 'left');
  271. if ($where['nickname'] !== '') {
  272. $model = $model->where("u.nickname|u.uid|u.phone", 'LIKE', "%$where[nickname]%");
  273. }
  274. if ($where['data']) $model = self::getModelTime($where, $model, 'u.add_time');
  275. $count = $model->count();
  276. $list = $model->field(['avatar as headimgurl', 'brokerage_price as new_money', 'u.*', 'o.*', 'e.*', 'b.*'])
  277. ->page((int)$where['page'], (int)$where['limit'])
  278. ->order('u.uid desc')
  279. ->select();
  280. $list = count($list) ? $list->toArray() : [];
  281. $broken_time = intval(sys_config('extract_time'));
  282. $search_time = time() - 86400 * $broken_time;
  283. foreach ($list as &$item) {
  284. if ($spread_uid = User::where('uid', $item['uid'])->value('spread_uid')) {
  285. if ($user = User::where('uid', $spread_uid)->field(['uid', 'nickname'])->find()) {
  286. $item['spread_name'] = $user['nickname'] . '/' . $user['uid'];
  287. }
  288. }
  289. $item['spread_count'] = User::where('spread_uid', $item['uid'])->count();
  290. //返佣 +
  291. $brokerage_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  292. ->where('add_time', '>', $search_time)
  293. ->where('pm', 1)
  294. ->sum('number');
  295. //退款退的佣金 -
  296. $refund_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  297. ->where('add_time', '>', $search_time)
  298. ->where('pm', 0)
  299. ->sum('number');
  300. $item['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  301. if ($item['broken_commission'] < 0)
  302. $item['broken_commission'] = 0;
  303. if ($item['brokerage_money'] > $refund_commission)
  304. $item['brokerage_money'] = bcsub($item['brokerage_money'], $refund_commission, 2);
  305. else
  306. $item['brokerage_money'] = 0;
  307. }
  308. return compact('list', 'count');
  309. }
  310. /**
  311. * 获取导出数据
  312. * @param array $where
  313. * @return array
  314. * @throws DataNotFoundException
  315. * @throws DbException
  316. * @throws ModelNotFoundException
  317. */
  318. public static function exportAgentData($where = [])
  319. {
  320. //提现数据
  321. $exteactSql = UserExtract::where(['status' => 1])
  322. ->group('uid')
  323. ->field(['sum(extract_price) as extract_count_price', 'count(id) as extract_count_num', 'uid as euid'])
  324. ->fetchSql(true)
  325. ->select();
  326. //订单数据
  327. $orderSql = StoreOrder::alias('o')
  328. ->where(['o.paid' => 1, 'o.refund_status' => 0])
  329. ->field(['sum(o.pay_price) as order_price', 'count(o.id) as order_count', 'o.uid as ouid'])
  330. ->group('o.uid')
  331. ->fetchSql(true)
  332. ->select();
  333. //佣金数据
  334. $billSql = UserBill::where(['status' => 1])
  335. ->where('type', 'brokerage')
  336. ->where('pm', 1)
  337. ->group('uid')
  338. ->field(['sum(number) as brokerage_money', 'uid as buid'])
  339. ->fetchSql(true)
  340. ->select();
  341. $model = User::where(['status' => 1])->where('is_promoter', 1);
  342. $model = $model->alias('u')
  343. ->join('(' . $orderSql . ') o', 'o.ouid = u.uid', 'left')
  344. ->join('(' . $billSql . ') b', 'b.buid = u.uid', 'left')
  345. ->join('(' . $exteactSql . ') e', 'e.euid = u.uid', 'left');
  346. if ($where['nickname'] !== '') {
  347. $model = $model->where("u.nickname|u.uid|u.phone", 'LIKE', "%$where[nickname]%");
  348. }
  349. if ($where['data']) $model = self::getModelTime($where, $model, 'u.add_time');
  350. if ($where['mer_id']) $model = $model->where('u.mer_id', $where['mer_id']);
  351. $count = $model->count();
  352. $list = $model->field(['avatar as headimgurl', 'brokerage_price as new_money', 'u.*', 'o.*', 'e.*', 'b.*'])
  353. ->page((int)$where['page'], (int)$where['limit'])
  354. ->order('u.uid desc')
  355. ->select();
  356. $list = count($list) ? $list->toArray() : [];
  357. $export = [];
  358. $broken_time = intval(sys_config('extract_time'));
  359. $search_time = time() - 86400 * $broken_time;
  360. foreach ($list as &$item) {
  361. $item['spread_name'] = '';
  362. if ($spread_uid = User::where('uid', $item['uid'])->value('spread_uid')) {
  363. if ($user = User::where('uid', $spread_uid)->field(['uid', 'nickname'])->find()) {
  364. $item['spread_name'] = $user['nickname'] . '/' . $user['uid'];
  365. }
  366. }
  367. $item['spread_count'] = User::where('spread_uid', $item['uid'])->count();
  368. //返佣 +
  369. $brokerage_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  370. ->where('add_time', '>', $search_time)
  371. ->where('pm', 1)
  372. ->sum('number');
  373. //退款退的佣金 -
  374. $refund_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  375. ->where('add_time', '>', $search_time)
  376. ->where('pm', 0)
  377. ->sum('number');
  378. $item['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  379. if ($item['broken_commission'] < 0)
  380. $item['broken_commission'] = 0;
  381. if ($item['brokerage_money'] > $refund_commission)
  382. $item['brokerage_money'] = bcsub($item['brokerage_money'], $refund_commission, 2);
  383. else
  384. $item['brokerage_money'] = 0;
  385. }
  386. return $list;
  387. }
  388. public static function setSairOrderWhere($where, $model = null, $alias = '')
  389. {
  390. $model = $model === null ? new self() : $model;
  391. if (!isset($where['uid'])) return $model;
  392. if ($alias) {
  393. $model = $model->alias($alias);
  394. $alias .= '.';
  395. }
  396. if (isset($where['type'])) {
  397. switch ((int)$where['type']) {
  398. case 1:
  399. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  400. if (count($uids))
  401. $model = $model->where("{$alias}uid", 'in', $uids);
  402. else
  403. $model = $model->where("{$alias}uid", 0);
  404. break;
  405. case 2:
  406. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  407. if (count($uids))
  408. $spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid');
  409. else
  410. $spread_uid_two = [0];
  411. if (count($spread_uid_two))
  412. $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
  413. else
  414. $model = $model->where("{$alias}uid", 0);
  415. break;
  416. default:
  417. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  418. if (count($uids)) {
  419. if ($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')) {
  420. $uids = array_merge($uids, $spread_uid_two);
  421. $uids = array_unique($uids);
  422. $uids = array_merge($uids);
  423. }
  424. $model = $model->where("{$alias}uid", 'in', $uids);
  425. } else
  426. $model = $model->where("{$alias}uid", 0);
  427. break;
  428. }
  429. }
  430. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  431. return $model->where("{$alias}is_del", 0)->where("{$alias}is_system_del", 0)->where($alias . 'paid', 1);
  432. }
  433. /*
  434. * 推广订单统计
  435. * @param array $where
  436. * @return array
  437. * */
  438. public static function getStairOrderBadge($where)
  439. {
  440. if (!isset($where['uid'])) return [];
  441. $data['order_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  442. $data['order_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  443. $ids = self::setSairOrderWhere($where, new StoreOrder())->where(['paid' => 1, 'is_del' => 0, 'refund_status' => 0])->where('status', '>', 1)->column('id');
  444. $data['number_price'] = 0;
  445. if (count($ids)) $data['number_price'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'uid' => $where['uid']])->where('link_id', 'in', $ids)->sum('number');
  446. $where['type'] = 1;
  447. $data['one_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  448. $data['one_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  449. $where['type'] = 2;
  450. $data['two_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  451. $data['two_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  452. return [
  453. [
  454. 'name' => '总金额(元)',
  455. 'count' => $data['order_price'],
  456. 'col' => 6,
  457. ],
  458. [
  459. 'name' => '订单总数(单)',
  460. 'count' => $data['order_count'],
  461. 'col' => 6,
  462. ],
  463. [
  464. 'name' => '返佣总金额(元)',
  465. 'count' => $data['number_price'],
  466. 'col' => 6,
  467. ],
  468. [
  469. 'name' => '一级总金额(元)',
  470. 'count' => $data['one_price'],
  471. 'col' => 6,
  472. ],
  473. [
  474. 'name' => '一级订单数(单)',
  475. 'count' => $data['one_count'],
  476. 'col' => 6,
  477. ],
  478. [
  479. 'name' => '二级总金额(元)',
  480. 'count' => $data['two_price'],
  481. 'col' => 6,
  482. ],
  483. [
  484. 'name' => '二级订单数(单)',
  485. 'count' => $data['two_count'],
  486. 'col' => 6,
  487. ],
  488. ];
  489. }
  490. /**
  491. * 设置查询条件
  492. * @param array $where
  493. * @param object $model
  494. * @param string $alias
  495. *
  496. * @return WechatUser|object|null
  497. */
  498. public static function setSairWhere($where, $model = null, $alias = '')
  499. {
  500. $model = $model === null ? new self() : $model;
  501. if (!isset($where['uid'])) return $model;
  502. if ($alias) {
  503. $model = $model->alias($alias);
  504. $alias .= '.';
  505. }
  506. if (isset($where['type'])) {
  507. switch ((int)$where['type']) {
  508. case 1:
  509. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  510. if (count($uids))
  511. $model = $model->where("{$alias}uid", 'in', $uids);
  512. else
  513. $model = $model->where("{$alias}uid", 0);
  514. break;
  515. case 2:
  516. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  517. if (count($uids))
  518. $spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid');
  519. else
  520. $spread_uid_two = [0];
  521. if (count($spread_uid_two))
  522. $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
  523. else
  524. $model = $model->where("{$alias}uid", 0);
  525. break;
  526. default:
  527. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  528. if (count($uids)) {
  529. if ($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')) {
  530. $uids = array_merge($uids, $spread_uid_two);
  531. $uids = array_unique($uids);
  532. $uids = array_merge($uids);
  533. }
  534. $model = $model->where("{$alias}uid", 'in', $uids);
  535. } else
  536. $model = $model->where("{$alias}uid", 0);
  537. break;
  538. }
  539. }
  540. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  541. if (isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}phone|{$alias}nickname|{$alias}real_name|{$alias}uid", 'LIKE', "%$where[nickname]%");
  542. return $model->where($alias . 'status', 1);
  543. }
  544. public static function getSairBadge($where)
  545. {
  546. $data['number'] = self::setSairWhere($where, new User())->count();
  547. $where['type'] = 1;
  548. $data['one_number'] = self::setSairWhere($where, new User())->count();
  549. $where['type'] = 2;
  550. $data['two_number'] = self::setSairWhere($where, new User())->count();
  551. $col = $data['two_number'] > 0 ? 4 : 6;
  552. return [
  553. [
  554. 'name' => '总人数(人)',
  555. 'count' => $data['number'],
  556. 'col' => $col,
  557. ],
  558. [
  559. 'name' => '一级人数(人)',
  560. 'count' => $data['one_number'],
  561. 'col' => $col,
  562. ],
  563. [
  564. 'name' => '二级人数(人)',
  565. 'count' => $data['two_number'],
  566. 'col' => $col,
  567. ],
  568. ];
  569. }
  570. public static function getStairList($where)
  571. {
  572. if (!isset($where['uid'])) return [];
  573. $data = self::setSairWhere($where, new User())->page((int)$where['page'], (int)$where['limit'])->select();
  574. $data = count($data) ? $data->toArray() : [];
  575. $userInfo = User::where('uid', $where['uid'])->find();
  576. foreach ($data as &$item) {
  577. $item['spread_count'] = User::where('spread_uid', $item['uid'])->count();
  578. $item['order_count'] = StoreOrder::where('uid', $item['uid'])->where(['paid' => 1, 'is_del' => 0])->count();
  579. $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
  580. $item['add_time'] = date("Y-m-d H:i:s", $item['add_time']);
  581. }
  582. $count = self::setSairWhere($where, new User())->count();
  583. return ['count' => $count, 'list' => $data];
  584. }
  585. /*
  586. * 推广订单
  587. * @param array $where
  588. * @return array
  589. * */
  590. public static function getStairOrderList($where)
  591. {
  592. if (!isset($where['uid'])) return [];
  593. $data = self::setSairOrderWhere($where, new StoreOrder())->page((int)$where['page'], (int)$where['limit'])->select();
  594. $data = count($data) ? $data->toArray() : [];
  595. $Info = User::where('uid', $where['uid'])->find();
  596. foreach ($data as &$item) {
  597. $userInfo = User::where('uid', $item['uid'])->find();
  598. $item['user_info'] = '';
  599. $item['avatar'] = '';
  600. if (count($userInfo->toArray())) {
  601. $item['user_info'] = $userInfo->nickname . '|' . ($userInfo->phone ? $userInfo->phone . '|' : '') . $userInfo->real_name;
  602. $item['avatar'] = $userInfo->avatar;
  603. }
  604. if (count($Info->toArray()))
  605. $item['spread_info'] = $Info->nickname . "|" . ($Info->phone ? $Info->phone . "|" : '') . $Info->uid;
  606. else
  607. $item['spread_info'] = '';
  608. $item['number_price'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'link_id' => $item['id']])->value('number');
  609. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  610. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  611. $item['take_time'] = ($change_time = StoreOrderStatus::where(['change_type' => 'user_take_delivery', 'oid' => $item['id']])->value('change_time')) ?
  612. date('Y-m-d H:i:s', $change_time) : '暂无';
  613. }
  614. $count = self::setSairOrderWhere($where, new StoreOrder())->count();
  615. return ['count' => $count, 'list' => $data];
  616. }
  617. /**
  618. * @param $where
  619. * @return array
  620. * @throws DataNotFoundException
  621. * @throws DbException
  622. * @throws ModelNotFoundException
  623. */
  624. public static function getSpreadBadge($where)
  625. {
  626. $model = self::alias('a')->join('user u', 'a.uid = u.uid')->order('u.uid desc')->where('u.is_promoter', 1);
  627. if ($where) {
  628. if (isset($where['mer_id']) && $where['mer_id']) {
  629. $model = $model->where('u.mer_id', $where['mer_id']);
  630. }
  631. if (isset($where['data']) && $where['data'])
  632. $model = self::getModelTime($where, $model, 'a.add_time');
  633. if (isset($where['nickname']) && $where['nickname']) {
  634. $model = $model->where('u.nickname|a.uid|u.phone', 'like', "%$where[nickname]%");
  635. }
  636. }
  637. $uids = $model->column('u.uid');
  638. //分销员人数
  639. $data['sum_count'] = count($uids);
  640. $data['spread_sum'] = 0;
  641. $data['order_count'] = 0;
  642. $data['pay_price'] = 0;
  643. $data['number'] = 0;
  644. $data['extract_count'] = 0;
  645. $data['extract_price'] = 0;
  646. if ($data['sum_count']) {
  647. //发展会员人数
  648. $data['spread_sum'] = User::where('spread_uid', 'in', $uids)->count();
  649. //订单总数
  650. $data['order_count'] = StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0])->count();
  651. //订单金额
  652. $data['pay_price'] = StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price');
  653. //可提现金额
  654. $data['number'] = User::where('uid', 'in', $uids)->sum('brokerage_price');
  655. //提现次数
  656. $data['extract_count'] = UserExtract::where('uid', 'in', $uids)->count();
  657. //获取某个用户可提现金额
  658. $data['extract_price'] = User::getextractPrice($uids, $where);
  659. }
  660. return [
  661. [
  662. 'name' => '分销员人数(人)',
  663. 'count' => $data['sum_count'],
  664. 'className' => 'md-contacts',
  665. 'col' => 6,
  666. ],
  667. [
  668. 'name' => '发展会员人数(人)',
  669. 'count' => $data['spread_sum'],
  670. 'className' => 'md-contact',
  671. 'col' => 6,
  672. ],
  673. [
  674. 'name' => '分销订单数(单)',
  675. 'count' => $data['order_count'],
  676. 'className' => 'md-cart',
  677. 'col' => 6,
  678. ],
  679. [
  680. 'name' => '订单金额(元)',
  681. 'count' => $data['pay_price'],
  682. 'className' => 'md-bug',
  683. 'col' => 6,
  684. ],
  685. [
  686. 'name' => '提现金额(元)',
  687. 'count' => $data['number'],
  688. 'className' => 'ios-beaker',
  689. 'col' => 6,
  690. ],
  691. [
  692. 'name' => '提现次数(次)',
  693. 'count' => $data['extract_count'],
  694. 'className' => 'md-basket',
  695. 'col' => 6,
  696. ],
  697. [
  698. 'name' => '未提现金额(元)',
  699. 'count' => $data['extract_price'],
  700. 'className' => 'ios-at-outline',
  701. 'col' => 6,
  702. ],
  703. ];
  704. }
  705. /**
  706. * 获取筛选后的所有用户uid
  707. * @param array $where
  708. * @return array
  709. */
  710. public static function getAll($where = array())
  711. {
  712. $model = new self;
  713. if ($where['nickname'] !== '') $model = $model->where('nickname', 'LIKE', "%$where[nickname]%");
  714. // if ($where['data'] !== '') {
  715. // list($startTime, $endTime) = explode(' - ', $where['data']);
  716. // $model = $model->where('add_time', '>', strtotime($startTime));
  717. // $model = $model->where('add_time', '<', strtotime($endTime));
  718. // }
  719. $model = self::getModelTime($where, $model, "add_time");
  720. if ($where['tagid_list'] !== '') {
  721. $model = $model->where('tagid_list', 'LIKE', "%$where[tagid_list]%");
  722. }
  723. if ($where['groupid'] !== '-1') $model = $model->where('groupid', "$where[groupid]");
  724. if ($where['sex'] !== '') $model = $model->where('sex', "$where[sex]");
  725. return $model->column('uid', 'uid');
  726. }
  727. /**
  728. * 获取已关注的用户
  729. * @param $field
  730. */
  731. public static function getSubscribe($field)
  732. {
  733. return self::where('subscribe', 1)->column($field);
  734. }
  735. public static function getUserAll($field)
  736. {
  737. return self::column($field);
  738. }
  739. public static function getUserTag()
  740. {
  741. $tagName = Config::get('system_wechat_tag');
  742. return Cache::tag($tagName)->remember('_wechat_tag', function () use ($tagName) {
  743. Cache::tag($tagName, ['_wechat_tag']);
  744. $tag = WechatService::userTagService()->lists()->toArray()['tags'] ?: array();
  745. $list = [];
  746. foreach ($tag as $g) {
  747. $list[] = $g;
  748. }
  749. return $list;
  750. });
  751. }
  752. public static function clearUserTag()
  753. {
  754. Cache::delete('_wechat_tag');
  755. }
  756. public static function getUserGroup()
  757. {
  758. $tagName = Config::get('system_wechat_tag');
  759. return Cache::tag($tagName)->remember('_wechat_group', function () use ($tagName) {
  760. Cache::tag($tagName, ['_wechat_group']);
  761. $tag = WechatService::userGroupService()->lists()->toArray()['groups'] ?: array();
  762. $list = [];
  763. foreach ($tag as $g) {
  764. $list[] = $g;
  765. }
  766. return $list;
  767. });
  768. }
  769. public static function clearUserGroup()
  770. {
  771. Cache::delete('_wechat_group');
  772. }
  773. /**
  774. * 获取推广人数
  775. * @param $uid //用户的uid
  776. * @param int $spread
  777. * $spread 0 一级推广人数 1 二级推广人数
  778. * @return int|string
  779. */
  780. public static function getUserSpreadUidCount($uid, $spread = 1)
  781. {
  782. $userStair = User::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人
  783. if ($userStair) {
  784. if (!$spread) return count($userStair);//返回一级推人人数
  785. else return User::where('spread_uid', 'IN', implode(',', $userStair))->count();//二级推荐人数
  786. } else return 0;
  787. }
  788. /**
  789. * 获取推广人的订单
  790. * @param $uid
  791. * @param int $spread
  792. * $spread 0 一级推广总订单 1 所有推广总订单
  793. * @return int|string
  794. */
  795. public static function getUserSpreadOrderCount($uid, $spread = 1)
  796. {
  797. $userStair = User::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人uid
  798. if ($userStair) {
  799. if (!$spread) {
  800. return StoreOrder::where('uid', 'IN', implode(',', $userStair))->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();//获取一级推广人订单数
  801. } else {
  802. $userSecond = User::where('spread_uid', 'IN', implode(',', $userStair))->column('uid', 'uid');//二级推广人的uid
  803. if ($userSecond) {
  804. return StoreOrder::where('uid', 'IN', implode(',', $userSecond))->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();//获取二级推广人订单数
  805. } else return 0;
  806. }
  807. } else return 0;
  808. }
  809. }