平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersonApi.cs 22 KiB

4 months ago
4 months ago
4 months ago
3 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 month ago
1 month ago
1 month ago
4 months ago
1 month ago
4 months ago
4 months ago
1 month ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
1 month ago
4 months ago
4 months ago
4 months ago
1 month ago
4 months ago
4 months ago
1 month ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. using MoYu.FriendlyException;
  2. using MoYu.RemoteRequest;
  3. using MoYu.RemoteRequest.Extensions;
  4. using Newtonsoft.Json.Linq;
  5. using SafeCampus.Application.Manager.DeepelephManager;
  6. using SafeCampus.Application.Services.Business.PersonFacesService;
  7. using SafeCampus.Application.Services.Business.PersonInfoService;
  8. using SafeCampus.Web.Core.Controllers.Application.Business.Dto.Person;
  9. namespace SafeCampus.Web.Core.Controllers.Application.Business;
  10. /// <summary>
  11. /// 人员管理接口
  12. /// </summary>
  13. [Route("/business/[controller]")]
  14. [ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 98,Tag = "人员管理")]
  15. public class PersonApi : IDynamicApiController
  16. {
  17. private readonly IDeepelephManager _deepelephManager;
  18. private readonly IPersonInfoService _personInfoService;
  19. private readonly IPersonFacesService _personFacesService;
  20. public PersonApi(IDeepelephManager deepelephManager, IPersonInfoService personInfoService, IPersonFacesService personFacesService)
  21. {
  22. _deepelephManager = deepelephManager;
  23. _personInfoService = personInfoService;
  24. _personFacesService = personFacesService;
  25. }
  26. /// <summary>
  27. /// 新增人员
  28. /// </summary>
  29. /// <param name="info">人员信息</param>
  30. /// <returns></returns>
  31. public async Task<dynamic> CreatePersonA(PersonModel info)
  32. {
  33. info.Faces = info.Faces.Select(x => new Faces {faceId = Guid.NewGuid().ToString("N"),faceUrl = x.faceUrl}).ToList();
  34. var personId = Guid.NewGuid().ToString("N");
  35. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  36. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/create"
  37. .SetBody(new
  38. {
  39. token = _deepelephManager.GetToken(),
  40. tenantCode = appSettings.TenantCode,
  41. poiId = appSettings.PoiId,
  42. personId = personId,
  43. age = info.Age,
  44. name = info.Name,
  45. gender = info.Gender,
  46. phone = info.Phone,
  47. exData = info.ExData,
  48. faces = info.Faces
  49. })
  50. .SetContentType("application/json")
  51. .PostAsAsync<string>();
  52. var model = JsonConvert.DeserializeObject<JObject>(str);
  53. if ((bool)model["success"])
  54. {
  55. var person = new PersonInfoDto
  56. {
  57. PersonId = personId,
  58. Age = info.Age,
  59. Name = info.Name,
  60. Gender = info.Gender,
  61. Phone = info.Phone,
  62. CreateTime = DateTime.Now,
  63. ExtData = info.ExData,
  64. FaceSizeLimit = 60,
  65. QualityScore = 30,
  66. };
  67. if (!string.IsNullOrEmpty(info.PersonSetId))
  68. {
  69. await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/bind"
  70. .SetBody(new
  71. {
  72. token = _deepelephManager.GetToken(),
  73. tenantCode = appSettings.TenantCode,
  74. poiId = appSettings.PoiId,
  75. personSetId = info.PersonSetId,
  76. personIds = new List<string> { model["data"].ToString() }
  77. })
  78. .SetContentType("application/json")
  79. .PostAsAsync<string>();
  80. person.PersonSetId=info.PersonSetId;
  81. }
  82. await _personInfoService.Add(person);
  83. await _personFacesService.BatchAdd(info.Faces.Select(x => new PersonFaces
  84. {
  85. FaceId = x.faceId,
  86. FaceUrl = x.faceUrl,
  87. PersonId = personId
  88. }).ToList());
  89. return model["data"];
  90. }
  91. throw Oops.Oh(model["message"].ToString());
  92. }
  93. /// <summary>
  94. /// 人脸图片上传
  95. /// </summary>
  96. /// <param name="input"></param>
  97. /// <returns></returns>
  98. public async Task<dynamic> UploadFile([FromForm] BaseFileInput input)
  99. {
  100. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  101. var jsonparam = JsonConvert.SerializeObject(new
  102. {
  103. token = _deepelephManager.GetToken(),
  104. tenantCode = appSettings.TenantCode,
  105. poiId = appSettings.PoiId,
  106. usageScenario = "personSet"
  107. });
  108. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/common/upload-file"
  109. .SetFiles(HttpFile.Create("file", input.File.OpenReadStream(), input.File.FileName))
  110. .SetBody(new
  111. {
  112. param = jsonparam
  113. })
  114. .SetContentType("multipart/form-data")
  115. .PostAsAsync<string>();
  116. var model = JsonConvert.DeserializeObject<JObject>(str);
  117. if ((bool)model["success"])
  118. {
  119. var strCheck = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/faceQuality/check"
  120. .SetBody(new
  121. {
  122. token = _deepelephManager.GetToken(),
  123. tenantCode = appSettings.TenantCode,
  124. poiId = appSettings.PoiId,
  125. faceSizeLimit = 60,
  126. qualityScore = 30,
  127. faceUrl = model["data"]["httpUrl"].ToString()
  128. })
  129. .SetContentType("application/json")
  130. .PostAsAsync<string>();
  131. var modelCheck = JsonConvert.DeserializeObject<JObject>(strCheck);
  132. if ((bool)modelCheck["success"])
  133. {
  134. if ((bool)modelCheck["data"]["pass"])
  135. {
  136. return model["data"]["httpUrl"].ToString();
  137. }
  138. throw Oops.Oh(modelCheck["data"]["message"].ToString());
  139. }
  140. else
  141. {
  142. throw Oops.Oh("人脸效验失败!");
  143. }
  144. }
  145. throw Oops.Oh(model["message"].ToString());
  146. }
  147. /// <summary>
  148. /// 删除人员
  149. /// </summary>
  150. /// <param name="personId"></param>
  151. /// <returns></returns>
  152. public async Task<dynamic> DeletePersonD(string id)
  153. {
  154. var list = new List<string>();
  155. if (id.Contains(","))
  156. {
  157. list = id.Split(",").ToList();
  158. }
  159. else
  160. {
  161. list.Add(id);
  162. }
  163. foreach (var ids in list)
  164. {
  165. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  166. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/delete"
  167. .SetBody(new
  168. {
  169. token = _deepelephManager.GetToken(),
  170. tenantCode = appSettings.TenantCode,
  171. poiId = appSettings.PoiId,
  172. personId = ids,
  173. })
  174. .SetContentType("application/json")
  175. .PostAsAsync<string>();
  176. var model = JsonConvert.DeserializeObject<JObject>(str);
  177. if ((bool)model["success"])
  178. {
  179. var isOk = model["data"].ToString() == ids;
  180. if (isOk)
  181. {
  182. await _personInfoService.Delete(ids);
  183. }
  184. return isOk;
  185. }
  186. throw Oops.Oh(model["message"].ToString());
  187. }
  188. return false;
  189. }
  190. /// <summary>
  191. /// 更新人员信息
  192. /// </summary>
  193. /// <param name="info"></param>
  194. /// <returns></returns>
  195. public async Task<dynamic> UpdatePersionU(PersonModel info)
  196. {
  197. info.Faces = info.Faces.Select(x => new Faces { faceId = Guid.NewGuid().ToString("N"), faceUrl = x.faceUrl }).ToList();
  198. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  199. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/createOrOverride"
  200. .SetBody(new
  201. {
  202. token = _deepelephManager.GetToken(),
  203. tenantCode = appSettings.TenantCode,
  204. poiId = appSettings.PoiId,
  205. personId = info.PersonId,
  206. age = info.Age,
  207. name = info.Name,
  208. gender = info.Gender,
  209. phone = info.Phone,
  210. exData = info.ExData,
  211. faces = info.Faces
  212. })
  213. .SetContentType("application/json")
  214. .PostAsAsync<string>();
  215. var model = JsonConvert.DeserializeObject<JObject>(str);
  216. if ((bool)model["success"])
  217. {
  218. var person = new PersonInfoDto
  219. {
  220. PersonId = info.PersonId,
  221. Age = info.Age,
  222. Name = info.Name,
  223. Gender = info.Gender,
  224. Phone = info.Phone,
  225. CreateTime = DateTime.Now,
  226. ExtData = info.ExData,
  227. FaceSizeLimit = 60,
  228. QualityScore = 30,
  229. };
  230. if (!string.IsNullOrEmpty(info.PersonSetId))
  231. {
  232. person.PersonSetId = info.PersonSetId;
  233. var perInfo = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/query"
  234. .SetBody(new
  235. {
  236. token = _deepelephManager.GetToken(),
  237. tenantCode = appSettings.TenantCode,
  238. poiId = appSettings.PoiId,
  239. personId = info.PersonId,
  240. })
  241. .SetContentType("application/json")
  242. .PostAsAsync<string>();
  243. var permodel = JsonConvert.DeserializeObject<JObject>(perInfo);
  244. if ((bool)permodel["success"])
  245. {
  246. if (permodel["data"]["personSets"].Count() > 0)
  247. {
  248. var old = permodel["data"]["personSets"][0]["personSetId"].ToString();
  249. if (old != info.PersonSetId)
  250. {
  251. //解绑旧班级
  252. await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/unbind"
  253. .SetBody(new
  254. {
  255. token = _deepelephManager.GetToken(),
  256. tenantCode = appSettings.TenantCode,
  257. poiId = appSettings.PoiId,
  258. personId = info.PersonId,
  259. personSetId = old,
  260. })
  261. .SetContentType("application/json")
  262. .PostAsAsync<string>();
  263. //绑定新班级
  264. await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/bind"
  265. .SetBody(new
  266. {
  267. token = _deepelephManager.GetToken(),
  268. tenantCode = appSettings.TenantCode,
  269. poiId = appSettings.PoiId,
  270. personSetId = info.PersonSetId,
  271. personIds = new List<string> { info.PersonId }
  272. })
  273. .SetContentType("application/json")
  274. .PostAsAsync<string>();
  275. }
  276. }
  277. else
  278. {
  279. //绑定新班级
  280. await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/bind"
  281. .SetBody(new
  282. {
  283. token = _deepelephManager.GetToken(),
  284. tenantCode = appSettings.TenantCode,
  285. poiId = appSettings.PoiId,
  286. personSetId = info.PersonSetId,
  287. personIds = new List<string> { model["data"].ToString() }
  288. })
  289. .SetContentType("application/json")
  290. .PostAsAsync<string>();
  291. }
  292. }
  293. }
  294. await _personInfoService.Update(person);
  295. await _personFacesService.BatchAdd(info.Faces.Select(x => new PersonFaces
  296. {
  297. FaceId = x.faceId,
  298. FaceUrl = x.faceUrl,
  299. PersonId = info.PersonId
  300. }).ToList());
  301. return model["data"];
  302. }
  303. throw Oops.Oh(model["message"].ToString());
  304. }
  305. /// <summary>
  306. /// 分页查询人员信息
  307. /// </summary>
  308. /// <param name="search">查询条件</param>
  309. /// <returns></returns>
  310. public async Task<dynamic> PageQuery(PersonInfoSearch search)
  311. {
  312. return await _personInfoService.GetPageList(search);
  313. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  314. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/pageQuery"
  315. .SetBody(new
  316. {
  317. token = _deepelephManager.GetToken(),
  318. tenantCode = appSettings.TenantCode,
  319. poiId = appSettings.PoiId,
  320. personSetId = search.PersonSetId,
  321. //personId = search.PersonId,
  322. personName = search.PersonName,
  323. pageIndex = search.PageNum,
  324. pageSize = search.PageSize,
  325. })
  326. .SetContentType("application/json")
  327. .PostAsAsync<string>();
  328. var model = JsonConvert.DeserializeObject<JObject>(str);
  329. if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
  330. if (model["data"] != null)
  331. {
  332. return new
  333. {
  334. pageNum = search.PageNum,
  335. pageSize = model["pageSize"],
  336. total = model["totalCount"],
  337. pages = model["pageIndex"],
  338. list = model["data"]
  339. };
  340. }
  341. return new
  342. {
  343. pageNum = search.PageNum,
  344. pageSize = 0,
  345. total = 0,
  346. pages = 0,
  347. list = new List<string>()
  348. };
  349. }
  350. /// <summary>
  351. /// 查看人员详情
  352. /// </summary>
  353. /// <param name="personId">人员id</param>
  354. /// <returns></returns>
  355. public async Task<dynamic> GetPersionById(ControllersIdInput input)
  356. {
  357. var perModel= await _personInfoService.GetInfo(input.Id);
  358. var appSettings = App.GetOptionsMonitor < AppInfoOptions>();
  359. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/query"
  360. .SetBody(new
  361. {
  362. token = _deepelephManager.GetToken(),
  363. tenantCode = appSettings.TenantCode,
  364. poiId = appSettings.PoiId,
  365. personId = input.Id,
  366. })
  367. .SetContentType("application/json")
  368. .PostAsAsync<string>();
  369. var model = JsonConvert.DeserializeObject<JObject>(str);
  370. if ((bool)model["success"])
  371. {
  372. if (model["data"]["personSets"].Count() > 0)
  373. {
  374. model["data"]["personSetId"] = model["data"]["personSets"][0]["personSetId"];
  375. model["data"]["personSetName"] = model["data"]["personSets"][0]["personSetName"];
  376. }
  377. else
  378. {
  379. model["data"]["personSetId"] = null;
  380. model["data"]["personSetName"] = null;
  381. }
  382. perModel.Faces = JsonConvert.DeserializeObject<List<PersonFaces>>(model["data"]["faces"].ToString());
  383. return perModel;
  384. }
  385. throw Oops.Oh(model["message"].ToString());
  386. }
  387. /// <summary>
  388. /// 添加人脸
  389. /// </summary>
  390. /// <param name="info">人脸信息</param>
  391. /// <returns></returns>
  392. public async Task<dynamic> AddFaceA(PersonFaceInfo info)
  393. {
  394. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  395. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/addFace"
  396. .SetBody(new
  397. {
  398. token = _deepelephManager.GetToken(),
  399. tenantCode = appSettings.TenantCode,
  400. poiId = appSettings.PoiId,
  401. personId = info.PersonId,
  402. info.faceSizeLimit,
  403. info.qualityScore,
  404. faces = new List<Faces>
  405. {
  406. new Faces
  407. {
  408. faceUrl = info.faceUrl,
  409. faceId=Guid.NewGuid().ToString("N"),
  410. }
  411. }
  412. })
  413. .SetContentType("application/json")
  414. .PostAsAsync<string>();
  415. var model = JsonConvert.DeserializeObject<JObject>(str);
  416. if ((bool)model["success"])
  417. {
  418. var faceModel = new PersonFaces { PersonId = info.PersonId, FaceId = Guid.NewGuid().ToString("N"), FaceUrl = info.faceUrl };
  419. await _personFacesService.Add(faceModel);
  420. return true;
  421. }
  422. throw Oops.Oh(model["message"].ToString());
  423. }
  424. /// <summary>
  425. /// 删除人脸
  426. /// </summary>
  427. /// <param name="info">删除信息</param>
  428. /// <returns></returns>
  429. [HttpPost]
  430. public async Task<dynamic> DeleteFaceD(PersonFaceDel info)
  431. {
  432. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  433. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/removeFace"
  434. .SetBody(new
  435. {
  436. token = _deepelephManager.GetToken(),
  437. tenantCode = appSettings.TenantCode,
  438. poiId = appSettings.PoiId,
  439. info.personId,
  440. info.faceIds
  441. })
  442. .SetContentType("application/json")
  443. .PostAsAsync<string>();
  444. var model = JsonConvert.DeserializeObject<JObject>(str);
  445. if ((bool)model["success"])
  446. {
  447. await _personFacesService.Delete(info.personId, info.faceIds);
  448. return true;
  449. }
  450. throw Oops.Oh(model["message"].ToString());
  451. }
  452. /// <summary>
  453. /// 人员和底库绑定
  454. /// </summary>
  455. /// <param name="info"></param>
  456. /// <returns></returns>
  457. public async Task<dynamic> PersonBindDfie(PersonDfieDfie info)
  458. {
  459. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  460. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/bind"
  461. .SetBody(new
  462. {
  463. token = _deepelephManager.GetToken(),
  464. tenantCode = appSettings.TenantCode,
  465. poiId = appSettings.PoiId,
  466. personSetId = info.PersonSetId,
  467. personIds = info.PersonIds
  468. })
  469. .SetContentType("application/json")
  470. .PostAsAsync<string>();
  471. var model = JsonConvert.DeserializeObject<JObject>(str);
  472. if ((bool)model["success"])
  473. {
  474. await _personInfoService.UpdateSet(info.PersonIds, info.PersonSetId);
  475. return true;
  476. }
  477. throw Oops.Oh(model["message"].ToString());
  478. }
  479. /// <summary>
  480. /// 人员与底库解绑
  481. /// </summary>
  482. /// <returns></returns>
  483. public async Task<dynamic> PersonUnBindDfie(PersonUnBindDfieInput input)
  484. {
  485. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  486. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/set/unbind"
  487. .SetBody(new
  488. {
  489. token = _deepelephManager.GetToken(),
  490. tenantCode = appSettings.TenantCode,
  491. poiId = appSettings.PoiId,
  492. personId = input.PersonId,
  493. personSetId = input.PersonSetId,
  494. })
  495. .SetContentType("application/json")
  496. .PostAsAsync<string>();
  497. var model = JsonConvert.DeserializeObject<JObject>(str);
  498. if ((bool)model["success"])
  499. {
  500. await _personInfoService.UpdateSet(new List<string>{ input.PersonId}, null);
  501. return true;
  502. }
  503. throw Oops.Oh(model["message"].ToString());
  504. }
  505. /// <summary>
  506. /// 人员数据同步
  507. /// </summary>
  508. /// <returns></returns>
  509. public async Task<bool> SyncData()
  510. {
  511. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  512. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/person/pageQuery"
  513. .SetBody(new
  514. {
  515. token = _deepelephManager.GetToken(),
  516. tenantCode = appSettings.TenantCode,
  517. poiId = appSettings.PoiId,
  518. pageIndex =1,
  519. pageSize = 1000,
  520. })
  521. .SetContentType("application/json")
  522. .PostAsAsync<string>();
  523. var model = JsonConvert.DeserializeObject<JObject>(str);
  524. if (!(bool)model["success"]) throw Oops.Oh(model["message"].ToString());
  525. if (model["data"] != null)
  526. {
  527. foreach (var item in model["data"])
  528. {
  529. var personModel = new PersonInfoDto
  530. {
  531. PersonId = item["personId"]?.ToString(),
  532. Age = (int)item["age"],
  533. Name = item["name"]?.ToString(),
  534. Gender = item["gender"]?.ToString(),
  535. Phone = item["phone"]?.ToString(),
  536. CreateTime = DateTime.Now,
  537. ExtData = item["extData"]?.ToString(),
  538. FaceSizeLimit = 60,
  539. QualityScore = 30,
  540. PersonSetId = item["personSets"]?[0]?["personSetId"]?.ToString()
  541. };
  542. if (item["faces"]?.Count()>0)
  543. {
  544. var list = new List<PersonFaces>();
  545. foreach (var faceItem in item["faces"])
  546. {
  547. var face = new PersonFaces
  548. {
  549. FaceId = faceItem["faceId"]?.ToString(),
  550. FaceUrl = faceItem["faceUrl"]?.ToString(),
  551. PersonId = item["personId"]?.ToString(),
  552. Tag = faceItem["tag"]?.ToString(),
  553. };
  554. var signImg = Path.Combine(Directory.GetCurrentDirectory(), "Files", App.Configuration["AppInfo:PersonImg"], face.FaceId + ".jpg");
  555. await face.FaceUrl.GetToSaveAsync(signImg);
  556. face.FaceUrl = $"/Files/{App.Configuration["AppInfo:PersonImg"]}/{face.FaceId}.jpg";
  557. list.Add(face);
  558. }
  559. await _personFacesService.BatchAdd(list);
  560. }
  561. var oldmodel = await _personInfoService.GetInfo(personModel.PersonId);
  562. if (oldmodel==null)
  563. {
  564. await _personInfoService.Add(personModel);
  565. }
  566. }
  567. }
  568. return true;
  569. }
  570. }