User.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 as ServiceTypeModel;
  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. $totalCount = $this->alias("u")->where($where)->count();
  249. $data = null;
  250. if ($totalCount > 0) {
  251. $data = $this
  252. ->alias("u")
  253. ->field("u.uid,ut.show_template_id,a.ancestral_place,a.status as is_type_audit")
  254. ->leftJoin("info_audit a", "u.uid=a.uid")
  255. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//默认模板
  256. ->where($where)
  257. ->order("u.show_temp_seq", "desc")
  258. ->order("u.uid", "desc")
  259. ->page($post["page"], $post["pageSize"])
  260. ->select();
  261. if (!empty($data)) {
  262. $data = $data->toArray();
  263. }
  264. $infoAuditDb = new InfoAudit();
  265. foreach ($data as $k => $v) {
  266. $item = [
  267. "name" => "",
  268. "avatar" => "",
  269. "age" => "",
  270. "service_project_ar" => [],
  271. "user_work_type_title" => "",
  272. "service_area_all" => [],
  273. "birthday" => ""
  274. ];
  275. $infoData = $infoAuditDb->getItem(["status" => 1, "uid" => $v["uid"]]);
  276. if (!empty($infoData)) {
  277. foreach ($item as $k2 => $v2) {
  278. $item[$k2] = $infoData[$k2];
  279. }
  280. }
  281. $data[$k] = array_merge($v, $item);
  282. }
  283. }
  284. // $datatwo = [];
  285. // foreach($data as $v){
  286. // if($v['is_show'] == 1){
  287. // $datatwo[] = $v;
  288. // }
  289. // }
  290. $data = empty($data) ? [] : $data;
  291. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  292. }
  293. /**
  294. * 获取小程序在职展示列表
  295. * @return type
  296. */
  297. public function getGoodApiWorkerList($post)
  298. {
  299. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  300. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  301. $where = [];
  302. $where[] = ["u.work_type_id", ">", 0];
  303. $where[] = ["u.show_temp_seq", ">", 0];
  304. $where[] = ["u.status", "=", 1];
  305. if (!empty($post['work_type_id'])) {
  306. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  307. }
  308. if ($post['servicePrice'] > 0) {
  309. $where[] = ["a.service_min_price", "<=", $post['servicePrice']];
  310. }
  311. if ($post['is_china'] != '') {
  312. $where[] = ["a.is_china", '=', $post['is_china']];
  313. }
  314. if (!empty($post['timetype'])) {
  315. $where[] = ["a.service_type", "=", $post['timetype']];
  316. }
  317. $cityModel = new CityModel();
  318. $totalModel = $this->alias("u")->leftJoin("info_audit a", "u.uid=a.uid")->where($where);
  319. if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  320. $totalModel->where(function ($query) use ($post, $cityModel) {
  321. foreach ($post["service_area"] as $v) {
  322. $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  323. $str = str_replace(['辖'], ['市辖'], $stc);
  324. $arr = explode(",", $str);
  325. $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  326. if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  327. @file_put_contents('quanju.txt', $city_id. "-城市id\r\n", 8);
  328. $query->whereOr('find_in_set(' . $city_id . ',a.service_area)');
  329. }
  330. });
  331. }
  332. $totalCount = $totalModel->count();
  333. $data = null;
  334. if ($totalCount > 0) {
  335. // Log::info('搜索条件:' . json_encode($where, JSON_UNESCAPED_UNICODE));
  336. $dataModel = $this
  337. ->alias("u")
  338. ->field("u.uid,ut.show_template_id,a.ancestral_place,a.status as is_type_audit")
  339. ->leftJoin("info_audit a", "u.uid=a.uid")
  340. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//默认模板
  341. ->where($where)->where('ut.show_template_id',7);
  342. if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  343. $dataModel->where(function ($query) use ($post, $cityModel) {
  344. foreach ($post["service_area"] as $v) {
  345. $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  346. $str = str_replace(['辖'], ['市辖'], $stc);
  347. $arr = explode(",", $str);
  348. $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  349. if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  350. @file_put_contents('quanju.txt', $city_id. "-城市id2222222\r\n", 8);
  351. $query->whereOr('find_in_set(' . $city_id . ',a.service_area)');
  352. }
  353. });
  354. }
  355. $data = $dataModel->order("u.show_temp_seq", "desc")
  356. ->order("u.uid", "desc")
  357. ->page($post["page"], $post["pageSize"])
  358. ->select();
  359. // Log::info('搜索语句:' . json_encode($this->getLastSql(), JSON_UNESCAPED_UNICODE));
  360. if (!empty($data)) {
  361. $data = $data->toArray();
  362. }
  363. $infoAuditDb = new InfoAudit();
  364. foreach ($data as $k => $v) {
  365. $item = [
  366. "name" => "",
  367. "avatar" => "",
  368. "age" => "",
  369. "service_project_ar" => [],
  370. "user_work_type_title" => "",
  371. "service_area_all" => [],
  372. "birthday" => ""
  373. ];
  374. $infoData = $infoAuditDb->getItem(["status" => 1, "uid" => $v["uid"]]);
  375. if (!empty($infoData)) {
  376. foreach ($item as $k2 => $v2) {
  377. $item[$k2] = $infoData[$k2];
  378. }
  379. }
  380. $data[$k] = array_merge($v, $item);
  381. }
  382. }
  383. // $datatwo = [];
  384. // foreach($data as $v){
  385. // if($v['is_show'] == 1){
  386. // $datatwo[] = $v;
  387. // }
  388. // }
  389. $data = empty($data) ? [] : $data;
  390. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  391. }
  392. // /**
  393. // * 获取小程序在职展示列表
  394. // * @return type
  395. // */
  396. // public function getNewApiWorkerList($post)
  397. // {
  398. // $post["pageSize"] = $post["pageSize"] > 20 ? 20 : (int)$post["pageSize"];
  399. // $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  400. //
  401. // // user 表的基础筛选条件
  402. // $where = [["u.work_type_id", ">", 0], ["u.status", "=", 1]];
  403. // if (!empty($post['work_type_id'])) {
  404. // $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  405. // }
  406. //
  407. // // info_audit 表的筛选条件(需要在获取 uid 时应用)
  408. // $infoAuditWhere = [["status", "=", 1], ["is_show", "=", 1]];
  409. // if ($post['servicePrice'] > 0) {
  410. // $infoAuditWhere[] = ["service_min_price", "<=", $post['servicePrice']];
  411. // }
  412. // if (!empty($post['timetype'])) {
  413. // $infoAuditWhere[] = ["service_type", "=", $post['timetype']];
  414. // }
  415. // if ($post['is_china'] != '') {
  416. // $infoAuditWhere[] = ["is_china", '=', $post['is_china']];
  417. // }
  418. //
  419. // // 城市查询条件优化:预先获取 cityIds
  420. // $cityIds = [];
  421. // if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  422. // $cityModel = new CityModel();
  423. // $areaNames = [];
  424. // foreach ($post["service_area"] as $v) {
  425. // $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  426. // $str = str_replace(['辖'], ['市辖'], $stc);
  427. // $arr = explode(",", $str);
  428. // $areaNames[] = ["arr1" => $arr[1] ?? '', "arr2" => $arr[2] ?? '', "arr0" => $arr[0] ?? ''];
  429. // }
  430. // // 批量查询 city_ids
  431. // if (!empty($areaNames)) {
  432. // $cityIdMap = [];
  433. // foreach ($areaNames as $item) {
  434. // if (!isset($cityIdMap[$item['arr1']])) {
  435. // $id = $cityModel->where('merger_name', 'like', "%" . $item['arr1'] . "," . $item['arr2'])->value('id');
  436. // if (!$id) {
  437. // $id = $cityModel->where('merger_name', 'like', "%" . $item['arr0'] . "," . $item['arr1'])->value('id');
  438. // }
  439. // $cityIdMap[$item['arr1']] = $id;
  440. // }
  441. // if ($cityIdMap[$item['arr1']]) {
  442. // $cityIds[] = $cityIdMap[$item['arr1']];
  443. // }
  444. // }
  445. // $cityIds = array_unique($cityIds);
  446. // }
  447. // }
  448. //
  449. // // 构建 info_audit 查询条件(用于获取符合条件的 uid)
  450. // $infoAuditQuery = (new \app\model\api\InfoAudit())->where($infoAuditWhere);
  451. //
  452. // // 城市筛选:使用 find_in_set
  453. // if (!empty($cityIds)) {
  454. // $infoAuditQuery->where(function ($query) use ($cityIds) {
  455. // $orWhere = '';
  456. // foreach ($cityIds as $idx => $cid) {
  457. // if ($idx == 0) {
  458. // $orWhere .= "FIND_IN_SET({$cid}, service_area)";
  459. // } else {
  460. // $orWhere .= " OR FIND_IN_SET({$cid}, service_area)";
  461. // }
  462. // }
  463. // $query->whereRaw("({$orWhere})");
  464. // });
  465. // }
  466. //
  467. // // 直接在 info_audit 上筛选 uid(如果有 info_audit 筛选条件)
  468. // $hasInfoAuditFilter = $post['servicePrice'] > 0 || !empty($post['timetype']) || $post['is_china'] != '' || !empty($cityIds);
  469. //
  470. // if ($hasInfoAuditFilter) {
  471. // // 通过 info_audit 获取符合条件的 uid
  472. // $infoAuditUids = $infoAuditQuery->column('uid');
  473. //
  474. // if (empty($infoAuditUids)) {
  475. // return ["list" => [], "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => 0];
  476. // }
  477. //
  478. // // user 查询以此 uid 列表为基础
  479. // $userQuery = $this->alias("u")
  480. // ->where($where)
  481. // ->where('u.uid', 'in', $infoAuditUids);
  482. // } else {
  483. // // 没有 info_audit 筛选条件,直接查 user
  484. // $userQuery = $this->alias("u")
  485. // ->where($where)
  486. // ->whereRaw("EXISTS (SELECT 1 FROM info_audit ia WHERE ia.uid = u.uid AND ia.status = 1 AND ia.is_show = 1)");
  487. // }
  488. //
  489. // // 获取总数
  490. // $totalCount = $userQuery->count();
  491. //
  492. // // 分页查询
  493. // $data = [];
  494. // if ($totalCount > 0) {
  495. // $offset = ($post["page"] - 1) * $post["pageSize"];
  496. // $userList = $userQuery->order("u.uid", "desc")
  497. // ->limit($offset, $post["pageSize"])
  498. // ->column('u.uid');
  499. //
  500. // if (!empty($userList)) {
  501. // // 批量获取 info_audit 信息(带所有筛选条件)
  502. // $infoAuditQuery2 = (new \app\model\api\InfoAudit())->where($infoAuditWhere);
  503. // if (!empty($cityIds)) {
  504. // $infoAuditQuery2->where(function ($query) use ($cityIds) {
  505. // $orWhere = '';
  506. // foreach ($cityIds as $idx => $cid) {
  507. // $orWhere .= ($idx == 0 ? "" : " OR ") . "FIND_IN_SET({$cid}, service_area)";
  508. // }
  509. // $query->whereRaw("({$orWhere})");
  510. // });
  511. // }
  512. // $infoAuditList = $infoAuditQuery2->where('uid', 'in', $userList)->select()->toArray();
  513. //
  514. // $infoAuditMap = [];
  515. // foreach ($infoAuditList as $item) {
  516. // $infoAuditMap[$item['uid']] = $item;
  517. // }
  518. //
  519. // foreach ($userList as $uid) {
  520. // $audit = $infoAuditMap[$uid] ?? [];
  521. // $data[] = [
  522. // 'uid' => $uid,
  523. // 'show_template_id' => $audit['show_template_id'] ?? 0,
  524. // 'name' => $audit['name'] ?? '',
  525. // 'avatar' => $audit['avatar'] ?? '',
  526. // 'longitude' => $audit['longitude'] ?? 0,
  527. // 'latitude' => $audit['latitude'] ?? 0,
  528. // 'user_work_type_title' => ''
  529. // ];
  530. // }
  531. // }
  532. // }
  533. //
  534. // return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  535. // }
  536. /**
  537. * 获取小程序在职展示列表 备份
  538. * @return type
  539. */
  540. public function getNewApiWorkerList($post)
  541. {
  542. $post["pageSize"] = $post["pageSize"] > 20 ? 20 : (int)$post["pageSize"];
  543. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  544. $where = [];
  545. $where[] = ["u.work_type_id", ">", 0];
  546. $where[] = ["u.status", "=", 1];
  547. if (!empty($post['work_type_id'])) {
  548. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  549. }
  550. if ($post['servicePrice'] > 0) {
  551. $where[] = ["a.service_min_price", "<=", $post['servicePrice']];
  552. }
  553. if (!empty($post['timetype'])) {
  554. $where[] = ["a.service_type", "=", $post['timetype']];
  555. }
  556. if ($post['is_china'] != '') {
  557. $where[] = ["a.is_china", '=', $post['is_china']];
  558. }
  559. $cityModel = new CityModel();
  560. $serviceAreaWhere = null;
  561. if (!empty($post["service_area"]) && is_array($post["service_area"])) {
  562. $cityIds = [];
  563. foreach ($post["service_area"] as $v) {
  564. $stc = str_replace(['省', '市', '区', '县'], ['', '', '', ''], $v);
  565. $str = str_replace(['辖'], ['市辖'], $stc);
  566. $arr = explode(",", $str);
  567. $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[1] . "," . $arr[2])->value('id');
  568. if (!$city_id) $city_id = $cityModel->where('merger_name', 'like', "%" . $arr[0] . "," . $arr[1])->value('id');
  569. if ($city_id) $cityIds[] = $city_id;
  570. }
  571. if (!empty($cityIds)) {
  572. $cityIdStr = implode(',', $cityIds);
  573. $serviceAreaWhere = function ($query) use ($cityIdStr) {
  574. foreach (explode(',', $cityIdStr) as $cid) {
  575. $query->whereOr('find_in_set(' . intval($cid) . ',a.service_area)');
  576. }
  577. };
  578. }
  579. }
  580. $totalModel = $this->alias("u")->leftJoin("info_audit a", "u.uid=a.uid")->where($where);
  581. if ($serviceAreaWhere) {
  582. $totalModel->where($serviceAreaWhere);
  583. }
  584. $totalCount = $totalModel->count();
  585. $data = null;
  586. if ($totalCount > 0) {
  587. $dataModel = $this
  588. ->alias("u")
  589. ->field("u.uid,ut.show_template_id,a.ancestral_place,a.status as is_type_audit,a.name,a.avatar,a.birthday,a.service_project,IFNULL(u.longitude,0) as longitude,IFNULL(u.latitude,0) as latitude")
  590. // ->leftJoin("info_audit a", "u.uid=a.uid and a.status = 1 and a.is_show = 1")
  591. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")
  592. ->where($where);
  593. if ($serviceAreaWhere) {
  594. $dataModel->where($serviceAreaWhere);
  595. }
  596. $data = $dataModel->order("u.uid", "desc")
  597. ->page($post["page"], $post["pageSize"])
  598. ->select();
  599. if (!empty($data)) {
  600. $data = $data->toArray();
  601. /* 注释掉 service_project_ar 查询 start */
  602. // 一次性获取所有 service_project 的详情
  603. $allProjectIds = [];
  604. foreach ($data as $v) {
  605. if (!empty($v['service_project'])) {
  606. $projectIds = is_array($v['service_project']) ? $v['service_project'] : explode(',', $v['service_project']);
  607. $allProjectIds = array_merge($allProjectIds, $projectIds);
  608. }
  609. }
  610. $projectMap = [];
  611. if (!empty($allProjectIds)) {
  612. $allProjectIds = array_unique(array_map('intval', $allProjectIds));
  613. $projectList = (new ServiceTypeModel())->where('id', 'in', $allProjectIds)->select()->toArray();
  614. foreach ($projectList as $p) {
  615. $projectMap[$p['id']] = $p;
  616. }
  617. }
  618. /* 注释掉 service_project_ar 查询 end */
  619. foreach ($data as $k => $v) {
  620. $data[$k]['name'] = $v['name'] ?? '';
  621. $data[$k]['avatar'] = $v['avatar'] ?? '';
  622. // age 通过 birthday 计算得出
  623. $data[$k]['age'] = !empty($v['birthday']) ? (intval(date('Y')) - intval(date('Y', $v['birthday']))) : '';
  624. $data[$k]['birthday'] = !empty($v['birthday']) ? date('Y-m-d', $v['birthday']) : '';
  625. $data[$k]['longitude'] = $v['longitude'] ?? 0;
  626. $data[$k]['latitude'] = $v['latitude'] ?? 0;
  627. $data[$k]['service_project_ar'] = [];
  628. /* 注释掉 service_project 匹配逻辑 start */
  629. if (!empty($v['service_project'])) {
  630. $projectIds = is_array($v['service_project']) ? $v['service_project'] : explode(',', $v['service_project']);
  631. foreach ($projectIds as $pid) {
  632. if (isset($projectMap[$pid])) {
  633. $data[$k]['service_project_ar'][] = $projectMap[$pid];
  634. }
  635. }
  636. }
  637. /* 注释掉 service_project 匹配逻辑 end */
  638. $data[$k]['user_work_type_title'] = '';
  639. $data[$k]['service_area_all'] = [];
  640. }
  641. }
  642. }
  643. $data = empty($data) ? [] : $data;
  644. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  645. }
  646. /**
  647. * 获取从业人员列表
  648. * @param type $post
  649. * @param type $field
  650. * @param type $is_admin
  651. */
  652. public function getWorkerList($post, $field = "*", $is_admin = 0)
  653. {
  654. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  655. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  656. $where = [];
  657. $where[] = ["u.work_type_id", ">", 0];
  658. //用户uid
  659. if (!empty($post['uid'])) {
  660. $where[] = ["u.uid", "=", $post['uid']];
  661. }
  662. if (!empty($post['work_type_id'])) {
  663. $where[] = ["u.work_type_id", "=", $post['work_type_id']];
  664. }
  665. //手机号码
  666. if (!empty($post["mobile"])) {
  667. $where[] = ["u.mobile", "=", $post["mobile"]];
  668. }
  669. //状态
  670. if (isset($post["status"]) && in_array((string)$post["status"], ["0", "1", "-1"])) {
  671. $where[] = ["u.status", "=", (int)$post["status"]];
  672. }
  673. //父级uid
  674. if (!empty($post["parent_uid"])) {
  675. $where[] = ["u.parent_uid", "=", $post["parent_uid"]];
  676. }
  677. //
  678. if ($is_admin == 1) {
  679. $field = "u.*"
  680. . ",ut.show_template_id"
  681. . ",(select is_show from table_info_audit where uid = u.uid and status = 1 limit 1 ) as is_show"
  682. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  683. . ",wt.title as work_type_title"
  684. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  685. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  686. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  687. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  688. }
  689. $totalCount = $this->alias("u")->where($where)->count();
  690. $data = null;
  691. if ($totalCount > 0) {
  692. $data = $this
  693. ->alias("u")
  694. ->field($field)
  695. ->leftJoin("user p", "p.uid = u.parent_uid")
  696. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  697. ->leftJoin("user_show_template ut", "ut.uid = u.uid and ut.is_default = 1")//职称
  698. ->where($where)
  699. ->order("u.show_temp_seq", "desc")
  700. ->order("u.uid", "desc")
  701. ->page($post["page"], $post["pageSize"])
  702. ->select();
  703. // var_dump($this->getLastSql());
  704. if (!empty($data)) {
  705. $data = $data->toArray();
  706. }
  707. }
  708. $data = empty($data) ? [] : $data;
  709. $ShowTemplateDb = new ShowTemplate();
  710. foreach ($data as $k => $v) {
  711. if (!empty($v["regtime"])) {
  712. $data[$k]["regtime"] = date("Y-m-d H:i:s", $v["regtime"]);
  713. }
  714. if (!empty($v["parent_time"])) {
  715. $data[$k]["parent_time"] = date("Y-m-d H:i:s", $v["parent_time"]);
  716. }
  717. }
  718. // $datatwo = [];
  719. // foreach($data as $v){
  720. // if($v['is_show'] == 1){
  721. // $datatwo[] = $v;
  722. // }
  723. // }
  724. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  725. }
  726. /**
  727. * 前端获取用户列表
  728. * @param type $post
  729. * @param type $field
  730. * @return type
  731. */
  732. public function getDataList($post, $field = "*", $is_admin = 0)
  733. {
  734. $post["pageSize"] = $post["pageSize"] > 50 ? 50 : (int)$post["pageSize"];
  735. $post["page"] = $post["page"] <= 0 ? 1 : (int)$post["page"];
  736. $where = [];
  737. //用户uid
  738. if (!empty($post['uid'])) {
  739. $where[] = ["u.uid", "=", $post['uid']];
  740. }
  741. //状态
  742. if (isset($post["status"]) && in_array((string)$post["status"], ["0", "1", "-1"])) {
  743. $where[] = ["u.status", "=", (int)$post["status"]];
  744. }
  745. //昵称
  746. if (!empty($post["nickname"])) {
  747. $where[] = ["u.nickname", "like", "%{$post["nickname"]}%"];
  748. }
  749. //手机号码
  750. if (!empty($post["mobile"])) {
  751. $where[] = ["u.mobile", "=", $post["mobile"]];
  752. }
  753. //父级uid
  754. if (!empty($post["parent_uid"])) {
  755. $where[] = ["u.parent_uid", "=", $post["parent_uid"]];
  756. }
  757. //注册时间
  758. if (!empty($post['time']) && !empty($post['time'][0]) && !empty($post['time'][1])) {
  759. $startTime = strtotime($post['time'][0]);
  760. $endTime = strtotime($post['time'][1]);
  761. $where[] = ["u.regtime", "between", "{$startTime},{$endTime}"];
  762. }
  763. if ($is_admin == 1) {
  764. $field = "u.*"
  765. . ",p.nickname as p_nickname,p.mobile as p_mobile"
  766. . ",wt.title as work_type_title"
  767. . ",(select count(*) from table_user_show_template where uid = u.uid) as showTempCount"
  768. . ",(select count(*) from table_info_audit where uid = u.uid and status = 1) as is_info_audit"
  769. . ",(select count(*) from table_type_audit where uid = u.uid and status = 1) as is_type_audit"
  770. . ",(select count(*) from table_user where parent_uid = u.uid) as branchCount";
  771. }
  772. $totalCount = $this->alias("u")->where($where)->count();
  773. $data = null;
  774. if ($totalCount > 0) {
  775. $data = $this
  776. ->alias("u")
  777. ->field($field)
  778. ->leftJoin("user p", "p.uid = u.parent_uid")
  779. ->leftJoin("user_work_type wt", "wt.id = u.work_type_id")//职称
  780. ->where($where)
  781. ->order("u.uid", "desc")
  782. ->page($post["page"], $post["pageSize"])
  783. ->select();
  784. if (!empty($data)) {
  785. $data = $data->toArray();
  786. }
  787. }
  788. $data = empty($data) ? [] : $data;
  789. foreach ($data as $k => $v) {
  790. if (!empty($v["regtime"])) {
  791. $data[$k]["regtime"] = date("Y-m-d H:i:s", $v["regtime"]);
  792. }
  793. if (!empty($v["parent_time"])) {
  794. $data[$k]["parent_time"] = date("Y-m-d H:i:s", $v["parent_time"]);
  795. }
  796. }
  797. return ["list" => $data, "pageSize" => $post["pageSize"], "page" => $post["page"], "totalCount" => $totalCount];
  798. }
  799. }