FaceServiceImpl.java 31 KB

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