User.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\api;
  4. use app\model\api\City as CityModel;
  5. use app\model\api\ServiceType;
  6. use Closure;
  7. use library\basic\BaseModel;
  8. use think\db\exception\DbException;
  9. use app\model\api\ShowTemplate;
  10. use app\model\api\InfoAudit;
  11. use think\facade\Log;
  12. use think\Model;
  13. use think\facade\Db;
  14. use think\Request;
  15. /**
  16. * @mixin \think\Model
  17. */
  18. class User extends BaseModel
  19. {
  20. //
  21. protected $pk = 'uid';
  22. /**
  23. * 获取列表数据
  24. * @param $page
  25. * @param $where
  26. * @param $pageCount
  27. * @param $desc
  28. */
  29. public function getList($page, $where = [], $pageCount = 20, $filed = '*', $desc = '')
  30. {
  31. $today = strtotime(date("Y-m-d"));
  32. $data = $this
  33. ->alias("m")
  34. ->field("m.*,u.nickname as i_nickname,u.uuid as i_uuid,t.name as i_tname,t.tuuid as i_tuuid,s.day_surplus_talk,s.day_surplus_video,s.time as surplus_time"
  35. . ",(select count(*) from table_user_friends as p where p.uid = m.uid) as friendsCount"//朋友数
  36. . ",(select count(*) from table_user_attention as a where a.uid = m.uid) as attentionCount"//关注数
  37. . ",(select count(*) from table_user_attention as f where f.i_uid = m.uid) as fasCount"//粉丝数
  38. . ",(select count(*) from table_user_visitors as v where v.uid = m.uid) as visitorsCount"//访客数
  39. . "")
  40. ->leftJoin("user u", "u.uid = m.i_uid")
  41. ->leftJoin("user_surplus s", "s.uid = m.uid and s.time = {$today}")
  42. ->leftJoin("tuser t", "t.tuid = m.i_tuid")
  43. ->when(!empty($where), function ($query) use ($where) {
  44. if (!empty($where['mobile'])) {
  45. $query->where('m.mobile', $where['mobile']);
  46. }
  47. if (!empty($where['nickname'])) {
  48. $query->whereLike('m.nickname', "%{$where['nickname']}%");
  49. }
  50. if (is_numeric($where['levelid']) && in_array(strval($where['levelid']), ['0', '1', '2'])) {
  51. $query->where('m.levelid', intval($where['levelid']));
  52. }
  53. if (is_numeric($where['is_certification']) && in_array(strval($where['is_certification']), ['0', '1', '2'])) {
  54. $query->where('m.is_certification', intval($where['is_certification']));
  55. }
  56. if (is_numeric($where['is_goddess']) && in_array(strval($where['is_goddess']), ['0', '1', '2'])) {
  57. $query->where('m.is_goddess', intval($where['is_goddess']));
  58. }
  59. if (!empty($where['address'])) {
  60. $query->whereLike('m.address', "%{$where['address']}%");
  61. }
  62. if (!empty($where['now_address'])) {
  63. $query->whereLike('m.now_address', "%{$where['now_address']}%");
  64. }
  65. if (!empty($where['i_uid'])) {
  66. $query->where('m.i_uid', $where['i_uid']);
  67. }
  68. if (!empty($where['i_tuid'])) {
  69. $query->where('m.i_tuid', $where['i_tuid']);
  70. }
  71. if (is_numeric($where["sex"]) && in_array(strval($where["sex"]), ["0", "1", "2"])) {
  72. $query->where('m.sex', $where["sex"]);
  73. }
  74. if (is_numeric($where["is_im"]) && in_array(strval($where["is_im"]), ["0", "1"])) {
  75. $query->where('m.is_im', $where["is_im"]);
  76. }
  77. if (is_numeric($where["is_online"]) && in_array(strval($where["is_online"]), ["0", "1"])) {
  78. $query->where('m.is_online', $where["is_online"]);
  79. }
  80. if (is_numeric($where["status"]) && in_array(strval($where["status"]), ["0", "1", "-1"])) {
  81. $query->where('m.status', $where["status"]);
  82. }
  83. if (!empty($where['uid'])) {
  84. $query->where('m.uid', $where['uid']);
  85. }
  86. if (!empty($where['uuid'])) {
  87. $query->where('m.uuid', $where['uuid']);
  88. }
  89. //注册时间
  90. $startTime = "";
  91. $endTime = "";
  92. if (!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
  93. $startTime = strtotime($where['time'][0]);
  94. $endTime = strtotime($where['time'][1]);
  95. }
  96. if (!empty($startTime) && !empty($endTime)) {
  97. $query->whereBetween("m.regtime", "{$startTime},{$endTime}");
  98. }
  99. })
  100. ->order($desc)
  101. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  102. ->toArray();
  103. return [$data['total'], $data['data']];
  104. }
  105. /**
  106. * 保存数据
  107. * @param $data
  108. * @param $admin_id
  109. * @return array
  110. */
  111. public function saveUpdate($data, $admin_id)
  112. {
  113. try {
  114. self::beginTrans();
  115. if (!is_numeric($data["levelid"]) || !in_array(strval($data["levelid"]), ["0", "1", "2"])) {
  116. unset($data["levelid"]);
  117. }
  118. if (empty($data["mobile"])) {
  119. unset($data["mobile"]);
  120. } else {
  121. if (!isMobile($data["mobile"])) {
  122. return [0, '请输入正确的手机号码'];
  123. }
  124. }
  125. if (empty($data["nickname"])) {
  126. unset($data["nickname"]);
  127. }
  128. if (empty($data["address"])) {
  129. unset($data["address"]);
  130. }
  131. if (empty($data["now_address"])) {
  132. unset($data["now_address"]);
  133. }
  134. if (!is_numeric($data["status"]) || !in_array(strval($data["status"]), ["0", "1", "-1"])) {
  135. unset($data["status"]);
  136. }
  137. if (!is_numeric($data["is_shield"]) || !in_array(strval($data["is_shield"]), ["0", "1"])) {
  138. unset($data["is_shield"]);
  139. }
  140. //密码
  141. if (!empty($data['password'])) {
  142. $data['password'] = md5($data['password']);
  143. } else {
  144. unset($data["password"]);
  145. }
  146. if (empty($data["avatar"])) {
  147. unset($data["avatar"]);
  148. }
  149. if (!is_numeric($data["sex"]) || !in_array(strval($data["sex"]), ["0", "1", "2"])) {
  150. unset($data["sex"]);
  151. }
  152. //特殊设置
  153. if (empty($data["msg_price"]) || !is_numeric($data["msg_price"])) {
  154. unset($data["msg_price"]);
  155. }
  156. if (empty($data["video_price"]) || !is_numeric($data["video_price"])) {
  157. unset($data["video_price"]);
  158. }
  159. if (empty($data["audio_price"]) || !is_numeric($data["audio_price"])) {
  160. unset($data["audio_price"]);
  161. }
  162. if (empty($data["phone_price"]) || !is_numeric($data["phone_price"])) {
  163. unset($data["phone_price"]);
  164. }
  165. if (empty($data["wx_price"]) || !is_numeric($data["wx_price"])) {
  166. unset($data["wx_price"]);
  167. }
  168. if (empty($data["qq_price"]) || !is_numeric($data["qq_price"])) {
  169. unset($data["qq_price"]);
  170. }
  171. //开关设置
  172. if (!is_numeric($data["is_rank"]) || !in_array(strval($data["is_rank"]), ["0", "1"])) {
  173. unset($data["is_rank"]);
  174. }
  175. if (!is_numeric($data["is_gift"]) || !in_array(strval($data["is_gift"]), ["0", "1"])) {
  176. unset($data["is_gift"]);
  177. }
  178. if (!is_numeric($data["is_msg"]) || !in_array(strval($data["is_msg"]), ["0", "1"])) {
  179. unset($data["is_msg"]);
  180. }
  181. if (!is_numeric($data["is_video"]) || !in_array(strval($data["is_video"]), ["0", "1"])) {
  182. unset($data["is_video"]);
  183. }
  184. if (!is_numeric($data["is_audio"]) || !in_array(strval($data["is_audio"]), ["0", "1"])) {
  185. unset($data["is_audio"]);
  186. }
  187. //uid
  188. if (!empty($data['uid'])) {
  189. $uData = $this->where('uid', $data['uid'])->find()->toArray();
  190. if (empty($uData)) {
  191. return [0, '会员不存在'];
  192. }
  193. if (!empty($data["mobile"])) {
  194. //判断手机是否重复
  195. $count = $this
  196. ->where('mobile', $data['mobile'])
  197. ->where('uid', '<>', $data['uid'])
  198. ->count();
  199. if ($count > 0) {
  200. return [0, '手机号码已经存在'];
  201. }
  202. }
  203. } else {
  204. $data['regtime'] = time();
  205. }
  206. $this->saveModel($data);
  207. self::commitTrans();
  208. return [1, ''];
  209. } catch (DbException $db) {
  210. self::rollbackTrans();
  211. return [0, '操作失败1'];
  212. } catch (\Exception $e) {
  213. self::rollbackTrans();
  214. return [0, '操作失败2'];
  215. }
  216. return [0, '操作失败'];
  217. }
  218. /**
  219. * 获取列表数据
  220. * @param $page
  221. * @param $where
  222. * @param $pageCount
  223. * @param $desc
  224. */
  225. public function getItem($uid)
  226. {
  227. $data = $this
  228. ->field("m.*,(select mobile from table_user where uid = m.i_uid) as i_name")
  229. ->alias("m")
  230. ->where('uid', $uid)
  231. ->select();
  232. return $data;
  233. }
  234. // /**
  235. // * 获取小程序在职展示列表
  236. // * @return type
  237. // */
  238. // public function getApiWorkerList($post)
  239. // {
  240. // $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  241. // $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  242. // $where = [];
  243. // $where[] = ["u.work_type_id", ">", 0];
  244. // $where[] = ["u.status", "=", 1];
  245. // if (!empty($post['work_type_id'])) {
  246. // $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  247. // }
  248. // if ($post['servicePrice'] > 0) {
  249. // $where[] = ["a.service_min_price", "<=", $post['servicePrice']];
  250. // }
  251. // if (!empty($post['timetype'])) {
  252. // $where[] = ["a.service_type", "=", $post['timetype']];
  253. // }
  254. // if ($post['is_china'] != '') {
  255. // $where[] = ["a.is_china", '=', $post['is_china']];
  256. // }
  257. //
  258. // $cityModel = new CityModel();
  259. // $serviceAreaWhere = null;
  260. // if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  261. // $cityIds = [];
  262. // foreach ($post["service_area"] as $v) {
  263. // $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  264. // $str = str_replace(['辖'], ['市辖'], $stc);
  265. // $arr = explode(",", $str);
  266. // $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  267. // if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  268. // if ($city_id) $cityIds[] = $city_id;
  269. // }
  270. // if (!empty($cityIds)) {
  271. // $cityIdStr = implode(',', $cityIds);
  272. // $serviceAreaWhere = function ($query) use ($cityIdStr) {
  273. // foreach (explode(',', $cityIdStr) as $cid) {
  274. // $query->whereOr('find_in_set(' . intval($cid) . ',a.service_area)');
  275. // }
  276. // };
  277. // }
  278. // }
  279. //
  280. // $totalModel = $this->alias("u")->leftJoin("info_audit a", "u.uid=a.uid")->where($where);
  281. // if ($serviceAreaWhere) {
  282. // $totalModel->where($serviceAreaWhere);
  283. // }
  284. // $totalCount = $totalModel->count();
  285. //
  286. // $data = null;
  287. // if ($totalCount > 0) {
  288. // $dataModel = $this
  289. // ->alias("u")
  290. // ->field("u.uid,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit,a.name,a.avatar,a.age,a.birthday,a.service_project")
  291. // ->leftJoin("info_audit a", "u.uid=a.uid and a.status = 1 and a.is_show = 1")
  292. // ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")
  293. // ->where($where);
  294. // if ($serviceAreaWhere) {
  295. // $dataModel->where($serviceAreaWhere);
  296. // }
  297. // $data = $dataModel->order("u.show_temp_seq", "desc")
  298. // ->order("u.uid", "desc")
  299. // ->page($post["page"], $post["pageSize"])
  300. // ->select();
  301. // if (!empty($data)) {
  302. // $data = $data->toArray();
  303. // foreach ($data as $k => $v) {
  304. // $data[$k]['name'] = $v['name'] ?? '';
  305. // $data[$k]['avatar'] = $v['avatar'] ?? '';
  306. // $data[$k]['age'] = $v['age'] ?? '';
  307. // $data[$k]['birthday'] = !empty($v['birthday']) ? date('Y-m-d', $v['birthday']) : '';
  308. // $data[$k]['service_project_ar'] = [];
  309. // if (!empty($v['service_project'])) {
  310. // $projectIds = is_array($v['service_project']) ? $v['service_project'] : explode(',', $v['service_project']);
  311. // $data[$k]['service_project_ar'] = (new ServiceType())->where('id', 'in', $projectIds)->select()->toArray();
  312. // }
  313. // $data[$k]['user_work_type_title'] = '';
  314. // $data[$k]['service_area_all'] = [];
  315. // }
  316. // }
  317. // }
  318. // $data = empty($data) ? [] : $data;
  319. // return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  320. // }
  321. /**
  322. * 获取小程序在职展示列表
  323. * @return type
  324. */
  325. public function getApiWorkerList($post)
  326. {
  327. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  328. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  329. $where = [];
  330. $where[] = ["u.work_type_id", ">", 0];
  331. $where[] = ["u.status", "=", 1];
  332. if (!empty($post['work_type_id'])) {
  333. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  334. }
  335. $totalCount = $this->alias("u")->where($where)->count();
  336. $data = null;
  337. if ($totalCount > 0) {
  338. $data = $this
  339. ->alias("u")
  340. ->field("u.uid,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit")
  341. ->leftJoin("info_audit a", "u.uid=a.uid")
  342. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//默认模板
  343. ->where($where)
  344. ->order("u.show_temp_seq", "desc")
  345. ->order("u.uid", "desc")
  346. ->page($post["page"], $post["pageSize"])
  347. ->select();
  348. if (!empty($data)) {
  349. $data = $data->toArray();
  350. }
  351. $infoAuditDb = new InfoAudit();
  352. foreach ($data as $k => $v) {
  353. $item = [
  354. "name" => "",
  355. "avatar" => "",
  356. "age" => "",
  357. "service_project_ar" => [],
  358. "user_work_type_title" => "",
  359. "service_area_all" => [],
  360. "birthday" => ""
  361. ];
  362. $infoData = $infoAuditDb->getItem(["status" => 1, "uid" => $v["uid"]]);
  363. if (!empty($infoData)) {
  364. foreach ($item as $k2 => $v2) {
  365. $item[$k2] = $infoData[$k2];
  366. }
  367. }
  368. $data[$k] = array_merge($v, $item);
  369. }
  370. }
  371. // $datatwo = [];
  372. // foreach($data as $v){
  373. // if($v['is_show'] == 1){
  374. // $datatwo[] = $v;
  375. // }
  376. // }
  377. $data = empty($data) ? [] : $data;
  378. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  379. }
  380. /**
  381. * 获取小程序在职展示列表
  382. * @return type
  383. */
  384. public function getGoodApiWorkerList($post)
  385. {
  386. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  387. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  388. $where = [];
  389. $where[] = ["u.work_type_id", ">", 0];
  390. $where[] = ["u.show_temp_seq", ">", 0];
  391. $where[] = ["u.status", "=", 1];
  392. if (!empty($post['work_type_id'])) {
  393. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  394. }
  395. if ($post['servicePrice'] > 0) {
  396. $where[] = ["a.service_min_price", "<=", $post['servicePrice']];
  397. }
  398. if ($post['is_china'] != '') {
  399. $where[] = ["a.is_china", '=', $post['is_china']];
  400. }
  401. if (!empty($post['timetype'])) {
  402. $where[] = ["a.service_type", "=", $post['timetype']];
  403. }
  404. $cityModel = new CityModel();
  405. $totalModel = $this->alias("u")->leftJoin("info_audit a", "u.uid=a.uid")->where($where);
  406. if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  407. $totalModel->where(function ($query) use ($post, $cityModel) {
  408. foreach ($post["service_area"] as $v) {
  409. $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  410. $str = str_replace(['辖'], ['市辖'], $stc);
  411. $arr = explode(",", $str);
  412. $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  413. if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  414. @file_put_contents('quanju.txt', $city_id. "-城市id\r\n", 8);
  415. $query->whereOr('find_in_set(' . $city_id . ',a.service_area)');
  416. }
  417. });
  418. }
  419. $totalCount = $totalModel->count();
  420. $data = null;
  421. if ($totalCount > 0) {
  422. // Log::info('搜索条件:' . json_encode($where, JSON_UNESCAPED_UNICODE));
  423. $dataModel = $this
  424. ->alias("u")
  425. ->field("u.uid,ut.show_template_id,a.ancestral_place,a.status as is_type_audit")
  426. ->leftJoin("info_audit a", "u.uid=a.uid")
  427. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//默认模板
  428. ->where($where)->where('ut.show_template_id',7);
  429. if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  430. $dataModel->where(function ($query) use ($post, $cityModel) {
  431. foreach ($post["service_area"] as $v) {
  432. $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  433. $str = str_replace(['辖'], ['市辖'], $stc);
  434. $arr = explode(",", $str);
  435. $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  436. if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  437. @file_put_contents('quanju.txt', $city_id. "-城市id2222222\r\n", 8);
  438. $query->whereOr('find_in_set(' . $city_id . ',a.service_area)');
  439. }
  440. });
  441. }
  442. $data = $dataModel->order("u.show_temp_seq", "desc")
  443. ->order("u.uid", "desc")
  444. ->page($post["page"], $post["pageSize"])
  445. ->select();
  446. // Log::info('搜索语句:' . json_encode($this->getLastSql(), JSON_UNESCAPED_UNICODE));
  447. if (!empty($data)) {
  448. $data = $data->toArray();
  449. }
  450. $infoAuditDb = new InfoAudit();
  451. foreach ($data as $k => $v) {
  452. $item = [
  453. "name" => "",
  454. "avatar" => "",
  455. "age" => "",
  456. "service_project_ar" => [],
  457. "user_work_type_title" => "",
  458. "service_area_all" => [],
  459. "birthday" => ""
  460. ];
  461. $infoData = $infoAuditDb->getItem(["status" => 1, "uid" => $v["uid"]]);
  462. if (!empty($infoData)) {
  463. foreach ($item as $k2 => $v2) {
  464. $item[$k2] = $infoData[$k2];
  465. }
  466. }
  467. $data[$k] = array_merge($v, $item);
  468. }
  469. }
  470. // $datatwo = [];
  471. // foreach($data as $v){
  472. // if($v['is_show'] == 1){
  473. // $datatwo[] = $v;
  474. // }
  475. // }
  476. $data = empty($data) ? [] : $data;
  477. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  478. }
  479. /**
  480. * 获取小程序在职展示列表
  481. * @return type
  482. */
  483. public function getNewApiWorkerList($post)
  484. {
  485. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  486. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  487. $where = [];
  488. $where[] = ["u.work_type_id", ">", 0];
  489. $where[] = ["u.status", "=", 1];
  490. if (!empty($post['work_type_id'])) {
  491. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  492. }
  493. if ($post['servicePrice'] > 0) {
  494. $where[] = ["a.service_min_price", "<=", $post['servicePrice']];
  495. }
  496. if (!empty($post['timetype'])) {
  497. $where[] = ["a.service_type", "=", $post['timetype']];
  498. }
  499. if ($post['is_china'] != '') {
  500. $where[] = ["a.is_china", '=', $post['is_china']];
  501. }
  502. $cityModel = new CityModel();
  503. $serviceAreaWhere = null;
  504. if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  505. $cityIds = [];
  506. foreach ($post["service_area"] as $v) {
  507. $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  508. $str = str_replace(['辖'], ['市辖'], $stc);
  509. $arr = explode(",", $str);
  510. $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  511. if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  512. if ($city_id) $cityIds[] = $city_id;
  513. }
  514. if (!empty($cityIds)) {
  515. $cityIdStr = implode(',', $cityIds);
  516. $serviceAreaWhere = function ($query) use ($cityIdStr) {
  517. foreach (explode(',', $cityIdStr) as $cid) {
  518. $query->whereOr('find_in_set(' . intval($cid) . ',a.service_area)');
  519. }
  520. };
  521. }
  522. }
  523. $totalModel = $this->alias("u")->leftJoin("info_audit a", "u.uid=a.uid")->where($where);
  524. if ($serviceAreaWhere) {
  525. $totalModel->where($serviceAreaWhere);
  526. }
  527. $totalCount = $totalModel->count();
  528. $data = null;
  529. if ($totalCount > 0) {
  530. $dataModel = $this
  531. ->alias("u")
  532. ->field("u.uid,u.longitude,u.latitude,ut.show_template_id,a.ancestral_place,a.status as is_type_audit,a.name,a.avatar,a.age,a.birthday,a.service_project")
  533. ->leftJoin("info_audit a", "u.uid=a.uid and a.status = 1 and a.is_show = 1")
  534. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")
  535. ->where($where);
  536. if ($serviceAreaWhere) {
  537. $dataModel->where($serviceAreaWhere);
  538. }
  539. $data = $dataModel->order("u.uid", "desc")
  540. ->page($post["page"], $post["pageSize"])
  541. ->select();
  542. if (!empty($data)) {
  543. $data = $data->toArray();
  544. foreach ($data as $k => $v) {
  545. $data[$k]['name'] = $v['name'] ?? '';
  546. $data[$k]['avatar'] = $v['avatar'] ?? '';
  547. $data[$k]['age'] = $v['age'] ?? '';
  548. $data[$k]['birthday'] = !empty($v['birthday']) ? date('Y-m-d', $v['birthday']) : '';
  549. $data[$k]['service_project_ar'] = [];
  550. if (!empty($v['service_project'])) {
  551. $projectIds = is_array($v['service_project']) ? $v['service_project'] : explode(',', $v['service_project']);
  552. $data[$k]['service_project_ar'] = (new ServiceType())->where('id', 'in', $projectIds)->select()->toArray();
  553. }
  554. $data[$k]['user_work_type_title'] = '';
  555. $data[$k]['service_area_all'] = [];
  556. }
  557. }
  558. }
  559. $data = empty($data) ? [] : $data;
  560. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  561. }
  562. /**
  563. * 获取从业人员列表
  564. * @param type $post
  565. * @param type $field
  566. * @param type $is_admin
  567. */
  568. public function getWorkerList($post, $field = "*", $is_admin = 0)
  569. {
  570. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  571. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  572. $where = [];
  573. $where[] = ["u.work_type_id", ">", 0];
  574. //用户uid
  575. if (!empty($post['uid'])) {
  576. $where[] = ["u.uid", "=", $post['uid']];
  577. }
  578. if (!empty($post['work_type_id'])) {
  579. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  580. }
  581. //手机号码
  582. if (!empty($post["mobile"])) {
  583. $where[] = ["u.mobile", "=", $post["mobile"]];
  584. }
  585. //状态
  586. if (isset($post["status"]) && in_array((string)$post["status"], ["0", "1", "-1"])) {
  587. $where[] = ["u.status", "=", (int)$post["status"]];
  588. }
  589. //父级uid
  590. if (!empty($post["parent_uid"])) {
  591. $where[] = ["u.parent_uid", "=", $post["parent_uid"]];
  592. }
  593. //
  594. if ($is_admin == 1) {
  595. $field = "u.*"
  596. . ",ut.show_template_id"
  597. . ",(select is_show from table_info_audit where uid = u.uid and status = 1 limit 1 ) as is_show"
  598. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  599. . ",wt.title as work_type_title"
  600. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  601. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  602. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  603. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  604. }
  605. $totalCount = $this->alias("u")->where($where)->count();
  606. $data = null;
  607. if ($totalCount > 0) {
  608. $data = $this
  609. ->alias("u")
  610. ->field($field)
  611. ->leftJoin("user p", "p.uid = u.parent_uid")
  612. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  613. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//职称
  614. ->where($where)
  615. ->order("u.show_temp_seq", "desc")
  616. ->order("u.uid", "desc")
  617. ->page($post["page"], $post["pageSize"])
  618. ->select();
  619. // var_dump($this->getLastSql());
  620. if (!empty($data)) {
  621. $data = $data->toArray();
  622. }
  623. }
  624. $data = empty($data) ? [] : $data;
  625. $ShowTemplateDb = new ShowTemplate();
  626. foreach ($data as $k => $v) {
  627. if (!empty($v["regtime"])) {
  628. $data[$k]["regtime"] = date("Y-m-d H:i:s", $v["regtime"]);
  629. }
  630. if (!empty($v["parent_time"])) {
  631. $data[$k]["parent_time"] = date("Y-m-d H:i:s", $v["parent_time"]);
  632. }
  633. }
  634. // $datatwo = [];
  635. // foreach($data as $v){
  636. // if($v['is_show'] == 1){
  637. // $datatwo[] = $v;
  638. // }
  639. // }
  640. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  641. }
  642. /**
  643. * 前端获取用户列表
  644. * @param type $post
  645. * @param type $field
  646. * @return type
  647. */
  648. public function getDataList($post, $field = "*", $is_admin = 0)
  649. {
  650. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  651. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  652. $where = [];
  653. //用户uid
  654. if (!empty($post['uid'])) {
  655. $where[] = ["u.uid", "=", $post['uid']];
  656. }
  657. //状态
  658. if (isset($post["status"]) && in_array((string)$post["status"], ["0", "1", "-1"])) {
  659. $where[] = ["u.status", "=", (int)$post["status"]];
  660. }
  661. //昵称
  662. if (!empty($post["nickname"])) {
  663. $where[] = ["u.nickname", "like", "%{$post["nickname"]}%"];
  664. }
  665. //手机号码
  666. if (!empty($post["mobile"])) {
  667. $where[] = ["u.mobile", "=", $post["mobile"]];
  668. }
  669. //父级uid
  670. if (!empty($post["parent_uid"])) {
  671. $where[] = ["u.parent_uid", "=", $post["parent_uid"]];
  672. }
  673. //注册时间
  674. if (!empty($post['time']) && !empty($post['time'][0]) && !empty($post['time'][1])) {
  675. $startTime = strtotime($post['time'][0]);
  676. $endTime = strtotime($post['time'][1]);
  677. $where[] = ["u.regtime", "between", "{$startTime},{$endTime}"];
  678. }
  679. if ($is_admin == 1) {
  680. $field = "u.*"
  681. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  682. . ",wt.title as work_type_title"
  683. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  684. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  685. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  686. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  687. }
  688. $totalCount = $this->alias("u")->where($where)->count();
  689. $data = null;
  690. if ($totalCount > 0) {
  691. $data = $this
  692. ->alias("u")
  693. ->field($field)
  694. ->leftJoin("user p", "p.uid = u.parent_uid")
  695. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  696. ->where($where)
  697. ->order("u.uid", "desc")
  698. ->page($post["page"], $post["pageSize"])
  699. ->select();
  700. if (!empty($data)) {
  701. $data = $data->toArray();
  702. }
  703. }
  704. $data = empty($data) ? [] : $data;
  705. foreach ($data as $k => $v) {
  706. if (!empty($v["regtime"])) {
  707. $data[$k]["regtime"] = date("Y-m-d H:i:s", $v["regtime"]);
  708. }
  709. if (!empty($v["parent_time"])) {
  710. $data[$k]["parent_time"] = date("Y-m-d H:i:s", $v["parent_time"]);
  711. }
  712. }
  713. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  714. }
  715. }