FaceServiceImpl.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. package com.yx.face.service.impl;
  2. import cn.hutool.core.codec.Base64;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.github.pagehelper.PageHelper;
  5. import com.github.pagehelper.PageInfo;
  6. import com.google.common.collect.Maps;
  7. import com.yx.face.boot.restful.RestDTO;
  8. import com.yx.face.boot.restful.ServiceException;
  9. import com.yx.face.boot.uitls.*;
  10. import com.yx.face.dao.*;
  11. import com.yx.face.model.dto.FaceDeviceDTO;
  12. import com.yx.face.model.entity.Admin;
  13. import com.yx.face.model.entity.FaceDevice;
  14. import com.yx.face.model.entity.FaceDeviceLogo;
  15. import com.yx.face.model.entity.FaceRequestLog;
  16. import com.yx.face.model.excel.FaceLogVoExcel;
  17. import com.yx.face.model.excel.UploadExcel;
  18. import com.yx.face.model.excel.UserWhitelistExcel;
  19. import com.yx.face.model.query.FaceServerLogQu;
  20. import com.yx.face.model.query.QueryUser;
  21. import com.yx.face.model.search.FaceDeviceSearch;
  22. import com.yx.face.model.search.FaceServerLogSearch;
  23. import com.yx.face.model.vo.*;
  24. import com.yx.face.service.AdminService;
  25. import com.yx.face.service.FaceService;
  26. import com.yx.face.service.TbDeviceWhiteListService;
  27. import com.yx.face.service.feign.TBDeviceFaceService;
  28. import lombok.SneakyThrows;
  29. import lombok.extern.slf4j.Slf4j;
  30. import net.coobird.thumbnailator.Thumbnails;
  31. import org.apache.commons.lang3.StringUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Value;
  34. import org.springframework.context.annotation.Bean;
  35. import org.springframework.stereotype.Service;
  36. import sun.misc.BASE64Decoder;
  37. import sun.misc.BASE64Encoder;
  38. import javax.annotation.Resource;
  39. import javax.imageio.ImageIO;
  40. import java.awt.image.BufferedImage;
  41. import java.io.*;
  42. import java.net.MalformedURLException;
  43. import java.net.URL;
  44. import java.text.SimpleDateFormat;
  45. import java.util.*;
  46. import java.util.stream.Collectors;
  47. /**
  48. * @description: FaceServiceImpl <br>
  49. * @date: 2021/6/2 14:49 <br>
  50. * @author: PWB <br>
  51. */
  52. @Slf4j
  53. @Service
  54. public class FaceServiceImpl implements FaceService {
  55. @Value("${face.service.tb}")
  56. private String ip;
  57. @Resource
  58. private FacePassDao facePassDao;
  59. @Resource
  60. private FaceDeviceDao faceDeviceDao;
  61. @Resource
  62. private FaceLogDao faceLogDao;
  63. @Resource
  64. private FaceRequestLogDao faceRequestLogDao;
  65. @Autowired
  66. private TBDeviceFaceService tbDeviceFaceService;
  67. @Autowired
  68. private AdminService adminService;
  69. @Resource
  70. private FaceDeviceLogoDao faceDeviceLogoDao;
  71. @Resource
  72. private SystemConfigDao systemConfigDao;
  73. @Resource
  74. private TbDeviceWhiteListService tbDeviceWhiteListService;
  75. @Override
  76. public Boolean authFaceDevice(Integer deviceId) {
  77. FaceDevice faceDevice = faceDeviceDao.selectByPrimaryKey(deviceId);
  78. if (faceDevice == null) {
  79. throw new ServiceException("无此设备");
  80. }
  81. FaceDevice device = new FaceDevice();
  82. device.setId(deviceId);
  83. device.setAuth(1);
  84. device.setUpdateTime(new Date());
  85. Boolean aBoolean = tbDeviceWhiteListService.addOrDeleteTbDeviceWhiteList(faceDevice.getSn(), "00");
  86. if (aBoolean) {
  87. return faceDeviceDao.updateByPrimaryKeySelective(device) == 1;
  88. }
  89. return aBoolean;
  90. }
  91. @Override
  92. public Boolean stopFaceDevice(Integer deviceId) {
  93. FaceDevice faceDevice = faceDeviceDao.selectByPrimaryKey(deviceId);
  94. if (faceDevice == null) {
  95. throw new ServiceException("无此设备");
  96. }
  97. FaceDevice device = new FaceDevice();
  98. device.setId(deviceId);
  99. device.setAuth(0);
  100. device.setUpdateTime(new Date());
  101. Boolean aBoolean = tbDeviceWhiteListService.addOrDeleteTbDeviceWhiteList(faceDevice.getSn(), "01");
  102. if (aBoolean) {
  103. return faceDeviceDao.updateByPrimaryKeySelective(device) == 1;
  104. }
  105. return aBoolean;
  106. }
  107. @Override
  108. public Boolean addFaceDevice(FaceDeviceDTO deviceDTO) {
  109. FaceDevice device = faceDeviceDao.getFaceDeviceBySn(deviceDTO.getSn());
  110. if (device != null) {
  111. if (!device.getAdminId().equals(0)) {
  112. if (!device.getAdminId().equals(deviceDTO.getAdminId())) {
  113. throw new ServiceException("该设备也绑定别的账号");
  114. }
  115. }
  116. device.setName(deviceDTO.getName());
  117. device.setEnable(deviceDTO.getEnable());
  118. device.setAdminId(deviceDTO.getAdminId());
  119. return faceDeviceDao.updateByPrimaryKeySelective(device) == 1;
  120. }
  121. AdminVO info = adminService.getInfo(deviceDTO.getAdminId());
  122. if (info.getType() != 5) {
  123. throw new ServiceException("不能将设备添加到区级以外的账号上");
  124. }
  125. FaceDevice faceDevice = new FaceDevice();
  126. // faceDevice.setId();
  127. faceDevice.setSn(deviceDTO.getSn());
  128. faceDevice.setName(deviceDTO.getName());
  129. faceDevice.setEnable(deviceDTO.getEnable());
  130. faceDevice.setAdminId(deviceDTO.getAdminId());
  131. faceDevice.setPass(deviceDTO.getPass());
  132. faceDevice.setRemark(deviceDTO.getRemark());
  133. Date now = new Date();
  134. faceDevice.setUpdateTime(now);
  135. faceDevice.setCreateTime(now);
  136. return faceDeviceDao.insertSelective(faceDevice) == 1;
  137. }
  138. @Override
  139. public Boolean modifyFaceDevice(FaceDeviceDTO deviceDTO) {
  140. FaceDevice device = faceDeviceDao.selectByPrimaryKey(deviceDTO.getDeviceId());
  141. if (device == null) {
  142. throw new ServiceException("没有发现该设备");
  143. }
  144. if (deviceDTO.getSn() != null) {
  145. device = faceDeviceDao.getFaceDeviceBySn(deviceDTO.getSn());
  146. if (device != null) {
  147. if (!device.getId().equals(deviceDTO.getDeviceId())) {
  148. throw new ServiceException("也存在该设备");
  149. }
  150. }
  151. }
  152. FaceDevice faceDevice = new FaceDevice();
  153. faceDevice.setId(deviceDTO.getDeviceId());
  154. faceDevice.setSn(deviceDTO.getSn());
  155. faceDevice.setName(deviceDTO.getName());
  156. faceDevice.setEnable(deviceDTO.getEnable());
  157. faceDevice.setPass(deviceDTO.getPass());
  158. faceDevice.setRemark(deviceDTO.getRemark());
  159. faceDevice.setUpdateTime(new Date());
  160. faceDevice.setAdminId(deviceDTO.getAdminId());
  161. return faceDeviceDao.updateByPrimaryKeySelective(faceDevice) == 1;
  162. }
  163. @Override
  164. public Boolean deleteDevice(Integer deviceId) {
  165. FaceDevice device = faceDeviceDao.selectByPrimaryKey(deviceId);
  166. if (device == null) {
  167. throw new ServiceException("无此数据");
  168. }
  169. Boolean aBoolean = tbDeviceWhiteListService.addOrDeleteTbDeviceWhiteList(device.getSn(), "01");
  170. if (!aBoolean) {
  171. throw new ServiceException("删除失败");
  172. }
  173. FaceRequestLog requestLog = new FaceRequestLog();
  174. requestLog.setName("删除人员人脸信息");
  175. requestLog.setContext("/delUser");
  176. requestLog.setParams("");
  177. requestLog.setStatus(0);
  178. requestLog.setUsedTime(0L);
  179. Date now = new Date();
  180. requestLog.setUpdateTime(now);
  181. requestLog.setCreateTime(now);
  182. faceRequestLogDao.insertSelective(requestLog);
  183. faceDeviceDao.deleteByPrimaryKey(deviceId);
  184. Map<String, Object> map = Maps.newHashMap();
  185. map.put("SN", device.getSn());
  186. map.put("taskId", requestLog.getId());
  187. map.put("Data", "all");
  188. try {
  189. tbDeviceFaceService.delUser(map);
  190. } catch (Exception e) {
  191. log.info("删除人脸异常 {}", e.getMessage());
  192. }
  193. return true;
  194. }
  195. @Override
  196. public PageInfo<FaceDeviceVO> getFaceDevices(RestDTO<FaceDeviceSearch> dto) {
  197. FaceDeviceSearch data = dto.getData();
  198. AdminVO info = adminService.getInfo();
  199. Map<String, Object> map = new HashMap<>();
  200. map.put("adminVo", info);
  201. map.put("fde", data);
  202. // data.setAdminId(info.getAdminId());
  203. // data.setType(info.getType().intValue());
  204. // data.setProvinceId(info.getProvinceId());
  205. // data.setCityId(info.getCityId());
  206. // data.setAreaId(info.getAreaId());
  207. PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
  208. return new PageInfo<>(faceDeviceDao.getFaceDevices(map));
  209. }
  210. @Override
  211. public PageInfo<FacePassVO> getFacePasses(RestDTO<QueryUser> dto) {
  212. AdminVO info = adminService.getInfo();
  213. Map<String, Object> map = new HashMap<>();
  214. map.put("adminVo", info);
  215. map.put("query", dto.getData());
  216. PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
  217. List<FacePassVO> list = facePassDao.getFacePasses(map);
  218. for (int i = 0; i < list.size(); i++) {
  219. if (list.get(i).getPhone() != null)
  220. list.get(i).setPhone(SMSOrIdCardUtils.hidePhone(list.get(i).getPhone()));
  221. if (list.get(i).getUsername() != null)
  222. list.get(i).setUsername(SMSOrIdCardUtils.hideName(list.get(i).getUsername()));
  223. }
  224. return new PageInfo<>(list);
  225. }
  226. @SneakyThrows
  227. @Override
  228. public PageInfo<FaceLogVO> getFaceLogs(RestDTO<FaceServerLogSearch> dto) {
  229. PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
  230. FaceServerLogSearch data = dto.getData();
  231. //时间减去8小时
  232. // if (StringUtils.isNotBlank(data.getEndTime()) && StringUtils.isNotBlank(data.getStartTime())) {
  233. // String startTime = DateUtils.getReduceEightTime(data.getStartTime());
  234. // String endTime = DateUtils.getReduceEightTime(data.getEndTime());
  235. // data.setStartTime(startTime);
  236. // data.setEndTime(endTime);
  237. // log.info("startTime;{} ====== endTime; {}", startTime, endTime);
  238. // }
  239. // if (StringUtils.isNotBlank(data.getStartTime()) && StringUtils.isBlank(data.getEndTime())) {
  240. // String startTime = DateUtils.getReduceEightTime(data.getStartTime());
  241. // String endTime = DateUtils.getReduceEightTime(data.getEndTime());
  242. // data.setStartTime(startTime);
  243. // data.setEndTime(endTime);
  244. // log.info("startTime;{} ====== endTime; {}", startTime, endTime);
  245. // }
  246. AdminVO info = adminService.getInfo();
  247. Map<String, Object> map = new HashMap<>();
  248. map.put("adminVo", info);
  249. map.put("query", data);
  250. /*1.获取所有满足条件的adminId*/
  251. List<Long> adminIds = adminService.getAdminListSearch(map);
  252. map.put("adminIds", adminIds);
  253. /*2.获取所有满足条件的设备编号
  254. * 注:因为没有设备的相关查询条件所以不需要查询 所有满足条件的设备编号
  255. * */
  256. /*3.通过查询回来的id值 插入map中重新查询face_log表 进行单表查询*/
  257. PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
  258. List<FaceLogVO> list = faceLogDao.getFaceLogsNew(map);
  259. /*4.查询完成之后应当对其中的数据包括进行填入
  260. * 1.对admin中的数据字段进行填入
  261. * 2.对face_device中的数据字段进行填入
  262. * */
  263. /*4.1 获取所有adminId并把获取相关的admin*/
  264. List<Long> adminList = list.stream().collect(Collectors.groupingBy(FaceLogVO::getAdminId)).keySet().stream().map(key -> key).collect(Collectors.toList());
  265. List<AdminVO> adminVOList = adminService.getByAdminlist(adminList);
  266. Map<Integer, AdminVO> adminVOMap = adminVOList.stream().collect(Collectors.toMap(AdminVO::getAdminId, AdminVO -> AdminVO));
  267. List<String> snList = list.stream().collect(Collectors.groupingBy(FaceLogVO::getDeviceSn)).keySet().stream().map(key -> key).collect(Collectors.toList());
  268. List<FaceDeviceVO> faceDeviceVOList = faceDeviceDao.getBySnList(snList);
  269. Map<String, FaceDeviceVO> faceDeviceVOMap = faceDeviceVOList.stream().collect(Collectors.toMap(FaceDeviceVO::getSn, FaceDeviceVO -> FaceDeviceVO));
  270. // PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
  271. // List<FaceLogVO> list = faceLogDao.getFaceLogs(map);
  272. for (FaceLogVO faceLog : list) {
  273. AdminVO adminVO = adminVOMap.get(faceLog.getAdminId());
  274. if (Objects.nonNull(adminVO)) {
  275. faceLog.setUsername(adminVO.getUsername());
  276. }
  277. FaceDeviceVO faceDeviceVO = faceDeviceVOMap.get(faceLog.getDeviceSn());
  278. if (Objects.nonNull(faceDeviceVO)) {
  279. faceLog.setDeviceName(faceDeviceVO.getName());
  280. faceLog.setOutType(faceDeviceVO.getPass());
  281. }
  282. if (!faceLog.getJkmStatus().equals("01") && !faceLog.getJkmStatus().equals("10")) {
  283. if (faceLog.getPhone() != null)
  284. faceLog.setPhone(SMSOrIdCardUtils.hidePhone(faceLog.getPhone()));
  285. if (faceLog.getCardid() != null)
  286. faceLog.setCardid(SMSOrIdCardUtils.hideIdNumber(faceLog.getCardid()));
  287. }
  288. if (faceLog.getName() != null)
  289. faceLog.setName(faceLog.getName().trim());
  290. if (StringUtils.isNotBlank(faceLog.getYmStatus())) {
  291. String[] split = faceLog.getYmStatus().split(",");
  292. faceLog.setYmStatus(split[1]);
  293. }
  294. if (StringUtils.isNotBlank(faceLog.getHsStatus())) {
  295. String[] split = faceLog.getHsStatus().split(";");
  296. faceLog.setHsStatus(split[0]);
  297. }
  298. }
  299. return new PageInfo<>(list);
  300. }
  301. @Override
  302. public Map<String, Object> queryFaceSuccess(String taskId) {
  303. Map<String, Object> facePassByTaskId = facePassDao.getFacePassByTaskId(taskId);
  304. HashMap<String, Object> map = new HashMap<>();
  305. if (facePassByTaskId != null) {
  306. FacePassUserInFoVO facePassVO = facePassDao.getUserName((Long) facePassByTaskId.get("userId"));
  307. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  308. try {
  309. Calendar cal = Calendar.getInstance();
  310. // map.get("createTime");
  311. // String s = map.get("createTime").toString().replace("T", " ");
  312. // String format = sdf.format(facePassVO.getCreateTime());
  313. // Date parse = sdf.parse(s);
  314. cal.setTime(facePassVO.getCreateTime());
  315. cal.add(Calendar.HOUR_OF_DAY, +24);
  316. Date time = cal.getTime();
  317. String format = sdf.format(time);
  318. map.putAll(facePassByTaskId);
  319. map.put("avatar", facePassVO.getAvatar());
  320. map.put("username", facePassVO.getUsername());
  321. map.put("phone", facePassVO.getPhone());
  322. map.put("idNumber", facePassVO.getIdNumber());
  323. map.put("createTime", format);
  324. map.put("status", true);
  325. } catch (Exception e) {
  326. e.printStackTrace();
  327. log.info("数据异常", e.getMessage());
  328. String format = sdf.format(new Date());
  329. map.put("avatar", facePassVO.getAvatar());
  330. map.put("username", facePassVO.getUsername());
  331. map.put("phone", facePassVO.getPhone());
  332. map.put("idNumber", facePassVO.getIdNumber());
  333. map.put("createTime", format);
  334. map.put("status", true);
  335. return map;
  336. }
  337. } else {
  338. map.put("status", false);
  339. }
  340. return map;
  341. }
  342. @Override
  343. public List<FaceLogVoExcel> getFaceLogsExcel(FaceServerLogSearch data) {
  344. // //时间减去8小时
  345. // if (StringUtils.isNotBlank(data.getEndTime()) && StringUtils.isNotBlank(data.getStartTime())) {
  346. // String startTime = DateUtils.getReduceEightTime(data.getStartTime());
  347. // String endTime = DateUtils.getReduceEightTime(data.getEndTime());
  348. // data.setStartTime(startTime);
  349. // data.setEndTime(endTime);
  350. // log.info("startTime;{} ====== endTime; {}", startTime, endTime);
  351. // }
  352. // if (StringUtils.isNotBlank(data.getStartTime()) && StringUtils.isBlank(data.getEndTime())) {
  353. // String startTime = DateUtils.getReduceEightTime(data.getStartTime());
  354. // String endTime = DateUtils.getReduceEightTime(data.getEndTime());
  355. // data.setStartTime(startTime);
  356. // data.setEndTime(endTime);
  357. // log.info("startTime;{} ====== endTime; {}", startTime, endTime);
  358. // }
  359. AdminVO info = adminService.getInfo();
  360. if (info.getIsOut() != 1) {
  361. throw new ServiceException("你没有导出的权限");
  362. }
  363. Map<String, Object> map = new HashMap<>();
  364. map.put("adminVo", info);
  365. map.put("query", data);
  366. List<FaceLogVO> list = faceLogDao.getFaceLogs(map);
  367. List<FaceLogVoExcel> faceLogs = new ArrayList<>();
  368. for (int i = 0; i < list.size(); i++) {
  369. if (info.getIsHideImportant() == 1) {
  370. if (list.get(i).getPhone() != null)
  371. list.get(i).setPhone(SMSOrIdCardUtils.hidePhone(list.get(i).getPhone()));
  372. if (list.get(i).getCardid() != null)
  373. list.get(i).setCardid(SMSOrIdCardUtils.hideIdNumber(list.get(i).getCardid()));
  374. if (list.get(i).getName() != null)
  375. list.get(i).setName(list.get(i).getName().trim());
  376. }
  377. FaceLogVO fv = list.get(i);
  378. FaceLogVoExcel fle = new FaceLogVoExcel();
  379. fle.setDeviceSn(fv.getDeviceSn() == null ? null : fv.getDeviceSn());
  380. fle.setDeviceName(fv.getDeviceName() == null ? null : fv.getDeviceName());
  381. fle.setName(fv.getName() == null ? null : fv.getName());
  382. fle.setPhoto(fv.getPhoto() == null ? null : fv.getPhoto());
  383. fle.setCardid(fv.getCardid() == null ? null : fv.getCardid());
  384. fle.setPhone(fv.getPhone() == null ? null : fv.getPhone());
  385. if (fv.getJkmStatus() != null) {
  386. String jkmStatus = fv.getJkmStatus();
  387. if (jkmStatus.equals("00")) fle.setJkmStatus("绿码");
  388. else if (jkmStatus.equals("01")) fle.setJkmStatus("黄码");
  389. else if (jkmStatus.equals("10")) fle.setJkmStatus("红码");
  390. else {
  391. fle.setJkmStatus("未知");
  392. }
  393. }
  394. if (fv.getTwStatus() != null) {
  395. String twStatus = fv.getTwStatus();
  396. if (twStatus.equals("0")) fle.setTwStatus("正常");
  397. else if (twStatus.equals("1")) fle.setTwStatus("异常");
  398. else {
  399. fle.setTwStatus("未知");
  400. }
  401. }
  402. fle.setExtend3(fv.getExtend3() == null ? null : fv.getExtend3());
  403. fle.setHsStatus(fv.getHsStatus() == null ? null : fv.getHsStatus());
  404. fle.setXcInfo(fv.getXcInfo() == null ? null : fv.getXcInfo());
  405. fle.setYmStatus(fv.getYmStatus() == null ? null : fv.getYmStatus());
  406. if (fv.getOutType() != null) {
  407. Integer outType = fv.getOutType();
  408. if (outType == 0) fle.setOutType("通用");
  409. else if (outType == 1) fle.setOutType("进门");
  410. else if (outType == 2) fle.setOutType("出门");
  411. else {
  412. fle.setJkmStatus("未知");
  413. }
  414. }
  415. if (fv.getVerifyType() != null) {
  416. Integer verifyType = fv.getVerifyType();
  417. if (verifyType == 0) fle.setVerifyType("人脸");
  418. else if (verifyType == 1) fle.setVerifyType("卡");
  419. else if (verifyType == 2) fle.setVerifyType("身份证");
  420. else if (verifyType == 3) fle.setVerifyType("二维码");
  421. else if (verifyType == 4) fle.setVerifyType("远程");
  422. else if (verifyType == 6) fle.setVerifyType("IC卡+人脸");
  423. else {
  424. fle.setJkmStatus("未知");
  425. }
  426. }
  427. if (fv.getRightno() != null) {
  428. Integer rightno = fv.getRightno();
  429. if (rightno == 106) fle.setRightno("管理员");
  430. else if (rightno == 105) fle.setRightno("操作员");
  431. else if (rightno == 104) fle.setRightno("普通员工");
  432. else if (rightno == 103) fle.setRightno("访客");
  433. else if (rightno == 102) fle.setRightno("禁止");
  434. else if (rightno == 101) fle.setRightno("黑名单");
  435. else {
  436. fle.setJkmStatus("未知");
  437. }
  438. }
  439. fle.setCreateTime(fv.getCreateTime());
  440. faceLogs.add(fle);
  441. }
  442. return faceLogs;
  443. }
  444. @Override
  445. public List<FaceDeviceVO> getFaceDeviceList(JSONObject adminId) {
  446. AdminVO info;
  447. Map<String, Object> map = new HashMap<>();
  448. if (adminId != null && !adminId.isEmpty()) {
  449. info = adminService.getInfo(adminId.getInteger("adminId"));
  450. } else {
  451. info = adminService.getInfo();
  452. }
  453. map.put("adminVo", info);
  454. return faceDeviceDao.getFaceDeviceList(map);
  455. }
  456. /**
  457. * 设备 log 上传
  458. *
  459. * @param file
  460. * @param adminId
  461. * @return
  462. */
  463. @Override
  464. public String addFaceDeviceLogImg(InputStream file, Integer adminId) {
  465. String path = null;
  466. //按指定大小把图片进行缩和放(会遵循原图高宽比例)
  467. //此处把图片压成60*60的缩略图
  468. try {
  469. BufferedImage image = Thumbnails.of(file).size(60, 60).asBufferedImage();//变为400*300,遵循原图比例缩或放到400*某个高度
  470. //输出流
  471. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  472. ImageIO.write(image, "png", stream);
  473. String base64 = Base64.encode(stream.toByteArray());
  474. stream.flush();
  475. stream.close();
  476. String name = UUID.randomUUID().toString().concat(".jpg");
  477. path = FileUploadUtil.uploadLogImgUtil(base64, name);
  478. //判断logo 是否存在
  479. FaceDeviceLogo faceDeviceLogo = faceDeviceLogoDao.selectList(adminId);
  480. if (faceDeviceLogo == null) {
  481. faceDeviceLogo = new FaceDeviceLogo();
  482. faceDeviceLogo.setLogoImg(path);
  483. faceDeviceLogo.setAdminId(adminId);
  484. faceDeviceLogoDao.insert(faceDeviceLogo);
  485. } else {
  486. faceDeviceLogo.setLogoImg(path);
  487. faceDeviceLogoDao.updateByPrimaryKeySelective(faceDeviceLogo);
  488. }
  489. } catch (IOException e) {
  490. e.printStackTrace();
  491. }
  492. return path;
  493. }
  494. @Override
  495. public Boolean issuedAll(Integer type, Integer adminId, String sn) {
  496. List<String> list = new ArrayList<>();
  497. AdminVO info = adminService.getInfo();
  498. if (info == null) {
  499. list.add("数据匹配失败未找到此账号");
  500. throw new ServiceException(list.toString());
  501. }
  502. //设备列表
  503. List<FaceDevice> listSn = faceDeviceDao.selectSnList(type, sn, info.getProvinceId(), adminId);
  504. FaceDeviceLogo faceDeviceLogo = faceDeviceLogoDao.selectList(info.getAdminId());
  505. if (faceDeviceLogo == null) {
  506. list.add("LOGO失效下发失败请重新上传下发");
  507. throw new ServiceException(list.toString());
  508. }
  509. String base64 = FileUploadUtil.downloadFile(faceDeviceLogo.getLogoImg());
  510. if (StringUtils.isBlank(base64)) {
  511. list.add("LOGO失效下发失败请重新上传下发");
  512. throw new ServiceException(list.toString());
  513. }
  514. for (FaceDevice faceDevice : listSn) {
  515. //停用-0|启用-1
  516. Integer enable = faceDevice.getEnable();
  517. //离线-0|在线-1
  518. Integer online = faceDevice.getOnline();
  519. //未授权-0|已授权-1
  520. Integer auth = faceDevice.getAuth();
  521. if (enable == 0 || online == 0 || auth == 0) {
  522. list.add(faceDevice.getSn() + ":离线未授权停用状态LOGO下发失败请检查设备状态");
  523. continue;
  524. } else {
  525. List<String> stringList = new ArrayList<>();
  526. stringList.add(faceDevice.getSn());
  527. JSONObject img = new JSONObject();
  528. img.put("logo", base64);
  529. JSONObject json = new JSONObject();
  530. json.put("SN", stringList);
  531. json.put("type", "downLogo");
  532. json.put("taskId", faceDevice.getId());
  533. json.put("Data", img);
  534. log.info("log下发参数; {}", json);
  535. String sync = OkHttpUtils.builder().url(ip + "/Face/sendControl").localPost(json.toJSONString()).sync();
  536. log.info("log下发返回参数;{}", sync);
  537. JSONObject jsonObject = JSONObject.parseObject(sync);
  538. String msg = jsonObject.getString("msg");
  539. if ("命令下发成功".equals(msg)) {
  540. log.info("logo命令下发成功" + jsonObject);
  541. } else {
  542. list.add(faceDevice.getSn() + ":离线未授权停用状态LOGO下发失败请检查设备状态");
  543. }
  544. }
  545. }
  546. if (!list.isEmpty()) {
  547. throw new ServiceException(list.toString());
  548. }
  549. return true;
  550. }
  551. @Override
  552. public String getLogo(Integer adminId) {
  553. FaceDeviceLogo faceDeviceLogo = faceDeviceLogoDao.selectList(adminId);
  554. if (faceDeviceLogo == null) {
  555. return "";
  556. }
  557. return faceDeviceLogo.getLogoImg();
  558. }
  559. @Override
  560. public Boolean doReboot(String sn) {
  561. FaceDevice faceDeviceBySn = faceDeviceDao.getFaceDeviceBySn(sn);
  562. if (faceDeviceBySn == null) {
  563. throw new ServiceException("此设备不存在");
  564. }
  565. List<String> sns = new ArrayList<>();
  566. sns.add(sn);
  567. Map<String, Object> map = Maps.newHashMap();
  568. Map<String, Object> data = Maps.newHashMap();
  569. data.put("rebootType", "2");
  570. data.put("time", "");
  571. map.put("SN", sns);
  572. map.put("taskId", System.currentTimeMillis());
  573. map.put("type", "reboot");
  574. map.put("Data", data);
  575. try {
  576. Map<String, Object> sendControl = tbDeviceFaceService.sendControl(map);
  577. log.info("重启设备返回参数;{}", sendControl);
  578. if (sendControl.isEmpty()) {
  579. throw new ServiceException("设备重启失败请检查是否离线");
  580. }
  581. if (!"00".equals((String) sendControl.get("errCode"))) {
  582. //设备不在线 重试 3次
  583. // Boolean retry = triggerRetry(map);
  584. throw new ServiceException("设备重启失败请检查是否离线");
  585. }
  586. } catch (Exception e) {
  587. log.info("下发失败 原因 {}", e.getMessage());
  588. throw new ServiceException("设备重启失败请检查是否离线");
  589. //设备不在线 重试 3次
  590. // Boolean retry = triggerRetry(map);
  591. }
  592. return true;
  593. }
  594. @Override
  595. public FaceLogVO getDetails(FaceServerLogQu qu, Integer adminId) {
  596. String pwd = PasswordUtils.buildPw(qu.getPassword());
  597. if (StringUtils.equals(qu.getPassword(), pwd)) {
  598. throw new ServiceException("密码有误哦~");
  599. }
  600. SystemConfigVO vo = systemConfigDao.sysConfiguration(adminId);
  601. if (vo == null) {
  602. throw new ServiceException("未配置此权限哦~");
  603. }
  604. if (vo.getStatus() != 1) {
  605. throw new ServiceException("此权限未开启哦~");
  606. }
  607. return faceLogDao.getDetails(qu.getId());
  608. }
  609. @Override
  610. public void uploadExcel(List<Object> datas) {
  611. List<String> list = new ArrayList<>();
  612. for (int i = 0; i < datas.size(); i++) {
  613. UploadExcel uploadExcel = (UploadExcel) datas.get(i);
  614. if (StringUtils.isBlank(uploadExcel.getIdNumber()) || StringUtils.isBlank(uploadExcel.getPhoto())) {
  615. continue;
  616. }
  617. URL urlfile = null;
  618. try {
  619. urlfile = new URL(uploadExcel.getPhoto());
  620. } catch (MalformedURLException e) {
  621. e.printStackTrace();
  622. }
  623. byte[] data = null;
  624. try {
  625. InputStream inputStream = urlfile.openStream();
  626. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  627. byte[] buff = new byte[100];
  628. int rc = 0;
  629. while ((rc = inputStream.read(buff, 0, 100)) > 0) {
  630. swapStream.write(buff, 0, rc);
  631. }
  632. data = swapStream.toByteArray();
  633. swapStream.close();
  634. inputStream.close();
  635. } catch (Exception e) {
  636. e.printStackTrace();
  637. }
  638. try {
  639. String encode = new BASE64Encoder().encode(data);
  640. BASE64Decoder decoder = new BASE64Decoder();
  641. byte[] bytes1 = decoder.decodeBuffer(encode);
  642. for (int j = 0; j < bytes1.length; ++j) {
  643. if (bytes1[j] < 0) {// 调整异常数据
  644. bytes1[j] += 256;
  645. }
  646. }
  647. // 生成jpeg图片
  648. OutputStream out = new FileOutputStream("D:\\3\\"+uploadExcel.getIdNumber()+"-"+ (int)(Math.random()*100000)+".jpg");
  649. out.write(bytes1);
  650. out.flush();
  651. out.close();
  652. } catch (Exception e) {
  653. e.printStackTrace();
  654. }
  655. }
  656. }
  657. //发送失败重试三次
  658. public Boolean triggerRetry(Map<String, Object> params) {
  659. Map<String, Object> result = new HashMap<>();
  660. int times = 1;
  661. while (times <= 3) {
  662. try {
  663. Thread.sleep(1000 * 20); // 休眠20秒
  664. result = tbDeviceFaceService.downUser(params);
  665. log.info("下发人脸重试downFace = {} 重试次数 {}", params, times);
  666. if (result.get("msg") != null) {
  667. String msg = result.get("msg").toString();
  668. if (msg.contains("不在线")) {
  669. times++;
  670. } else {
  671. //代表成功
  672. times = 6;
  673. }
  674. } else {
  675. times++;
  676. }
  677. } catch (Exception e) {
  678. times++;
  679. log.info("下发失败 原因 {}", e.getMessage());
  680. }
  681. }
  682. //成功
  683. if (times == 6) {
  684. return true;
  685. }
  686. return false;
  687. }
  688. }