package com.cloud.user.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.cloud.model.common.Result; import com.cloud.user.model.*; import com.cloud.user.service.BbPersonToDevService; import com.cloud.user.utils.Base64Utils; import io.swagger.annotations.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.UnsupportedEncodingException; import java.util.*; /** * 人员分享 更新 设备节点查询 接口 * wp * 2018-11-29 */ @Slf4j @RestController @RequestMapping("/data/api-u/perDev") @Api(value = "BbPersonToDevController", description = "/data/api-u/perDev 人员分享 更新 设备节点查询 接口") public class BbPersonToDevController { @Autowired private BbPersonToDevService personToDevService; private final String SyncDataBaseSessionKey = "cluSyncDataBase"; // 将 底库数据存于 session 中 的key private final String updatePersonMap = "updatePersonMap"; // 待更新后 private final String sharePersonToDevList = "sharePersonToDevList"; private final String selectSharePerson = "selectSharePerson"; // 后台存储待分享人员 private final String dataBA = "dataBA";// 返回判断状态 private final String before = "before"; // 返回转台为之前 private final String after = "after"; // 返回状态为之后 private final String returnData = "returnData"; // 返回包装数据 private int baseTime = 0; // 轮询 底库ip 调用 次数增长值 @PostMapping("/getCluDataList") @ApiOperation(value = "getCluDataList 集群下设备列表查询", notes = "/getCluDataList", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParams({ }) public Result getCluDataList() { JSONArray allCluListData = personToDevService.getAllCluListData(); if (allCluListData != null) log.info("allCluListData 集群设备:" + allCluListData.size()); else { log.info("集群设备查询为空"); allCluListData = new JSONArray(); } return Result.ok("success", allCluListData); } @PostMapping("/getCluDataBaseList") @ApiOperation(value = "getCluDataBaseList 获取集群下所有底库", notes = "/getCluDataBaseList", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", paramType = "body", dataType = "Map", value = "{\"likeName\":\"\"}") public Result getCluDataBaseList(@RequestBody Map map, HttpServletRequest req) { List allCluDataBaseList = personToDevService.getAllCluDataBaseList(); List dbList = new ArrayList(); DBaseCluster dBaseCluster = null; for (int i = 0, item = allCluDataBaseList.size(); i < item; ++i) { dBaseCluster = allCluDataBaseList.get(i); boolean isFlag = true; String likeName = ""; if (map.get("likeName") != null) { likeName = map.get("likeName").toString().trim(); } if (!likeName.isEmpty()) { if (dBaseCluster.getCluName() == null || !dBaseCluster.getCluName().contains(likeName)) isFlag = false; } JSONObject jsonObj = new JSONObject(); jsonObj.put("id", dBaseCluster.getCluId()); jsonObj.put("parentId", "0"); jsonObj.put("label", dBaseCluster.getCluName()); List syncDataBase = dBaseCluster.getSyncDataBase(); List children = new ArrayList(); if (syncDataBase != null && syncDataBase.size() > 0) { for (SyncDataBase obj : syncDataBase) { if (isFlag || (obj.getTableName() != null && obj.getTableName().contains(likeName))) { JSONObject jsono = new JSONObject(); jsono.put("parentId", dBaseCluster.getCluId()); jsono.put("id", obj.getUuid()); jsono.put("label", obj.getTableName()); jsono.put("children", new JSONArray()); children.add(jsono); } } } if (isFlag || children.size() > 0) { jsonObj.put("children", children); dbList.add(jsonObj); } else { allCluDataBaseList.remove(i--); } }// 处理结束后再 存储 底库集群状态 HttpSession session = req.getSession(); session.setAttribute(SyncDataBaseSessionKey, JSONObject.toJSONString(allCluDataBaseList)); log.info("allCluDataBaseList底库集群:" + allCluDataBaseList.size()); return Result.ok("success", dbList); } @PostMapping("/sharePersonToDBNode") @ApiOperation(value = "sharePersonToDBNode 分享选中人员到选中底库", notes = "/sharePersonToDBNode", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"tableIds\":[\"底库uuid集合\"],\"persons\":[\"人员Id\"]}", dataType = "Map", paramType = "body") public Result addSomePersonToDataBase(@RequestBody Map map, HttpServletRequest req) { List persons = (List) map.get("persons"); List tableIds = (List) map.get("tableIds"); log.info("分享接受值:persons:" + persons + "tableIds:" + tableIds); if (persons != null && persons.size() > 0) { List perslist = personToDevService.findByPersonIds(persons); if (perslist.size() < 1) { log.info("分享前查询该人员失败" + perslist); return Result.error(new RuntimeException("该人员不存在。")); } // persons 的 feature 转换 for (CServerPerson cServerPerson : perslist) { if (cServerPerson.getByteFeature() != null) { byte[] byteFeature = cServerPerson.getByteFeature(); if (byteFeature.length > 3) { log.info("分享前查询该人员id:" + cServerPerson.getPersonId()); String s = Base64Utils.byteToBase64(cServerPerson.getByteFeature()); cServerPerson.setByteFeature(new byte[0]); cServerPerson.setFeature(s); } else { log.info("该人员没有特征值不能分享到集群,人员id:" + cServerPerson.getPersonId() + "特征值信息byteFeature:" + byteFeature); throw new RuntimeException(cServerPerson.getName() + "该人员没有特征值不能分享到该集群。"); } } else { log.info("该人员没有特征值不能分享到集群,人员id:" + cServerPerson.getPersonId()); } } List ipInfos = new ArrayList(); List tableId = tableIds; HttpSession session = req.getSession(); if (tableId != null && tableId.size() > 0) { String json = (String) session.getAttribute(SyncDataBaseSessionKey); List dBCluList = (List) JSONObject.parseArray(json, DBaseCluster.class); log.info(dBCluList.size() + "集群信息 session查询"); if (dBCluList == null) { return Result.error(new RuntimeException("请先查询底库集群列表")); } for (DBaseCluster dBaseCluster : dBCluList) { // 各集群 循环 log.info(dBaseCluster + "分享时查询集群信息"); List syncDBs = dBaseCluster.getSyncDataBase(); IpTable ipTable = new IpTable(); boolean flag = false; for (SyncDataBase sDB : syncDBs) { // 本集群 各同步底库 循环 if (tableId.contains(sDB.getUuid())) { // 待分享底库uuid集合 包含节点中 if (!flag) { List cluIPList = dBaseCluster.getCluIPList(); CluIP cluIP = cluIPList.get(baseTime % cluIPList.size()); ipTable.setIp(cluIP.getNode_ip()); // 获取节点 IP flag = true; ipTable.setCluId(dBaseCluster.getCluId()); // 获取集群id ipTable.setTableIds(new ArrayList()); } ipTable.getTableIds().add(sDB.getUuid()); // 将此节点 table uuid 加入集合 } } if (flag) { ipInfos.add(ipTable); } // 存在底库 在添加 } baseTime++; // 循环 节点 增加 List cSerResults = personToDevService.addSyncPersons(perslist, ipInfos); session.setAttribute(sharePersonToDevList, JSONObject.toJSONString(cSerResults)); // 更新 所有已分享成功的人员 for (PerInDBStatus cSerResult : cSerResults) { if (cSerResult.getIsSomeSuccess() != null && cSerResult.getIsSomeSuccess() && cSerResult.getPersonId() != null) { if (cSerResult.getCluList().size() > 0) { Set cluInfos = cSerResult.getCluList(); // 个集群id 获取 Integer integer = personToDevService.updatePersonForCluInfo(cluInfos, cSerResult.getPersonId()); // 集群id信息 加入数据库中 log.info(cSerResult.getPersonId() + "该人员已更新,status:" + integer); } } } // 将 选中人员 下发 到 es 索引中 便于 安卓 查询 使用 wp 19-03-19 代码编写 List padPersonInfos = personToDevService.addPersonsToEs(persons); log.info("并分享并存储到es为pad:" + padPersonInfos.toString()); return Result.ok("分享到底库成功。", cSerResults); } else { return Result.error(new RuntimeException("未选中分享集群底库。")); } } else { return Result.error(new RuntimeException("未选中分享人员。")); } } @PostMapping("/addPersonsToEs") @ApiOperation(value = "addPersonsToEs 分享选中人员加入es", notes = "/addPersonsToEs", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public Result addPersonsToEs(@RequestParam @ApiParam(value = "personId列表") List list, HttpServletRequest req) { log.info("后台存储分享人员信息" + list.size()); log.info("第一条人员信息" + (list.size() > 0 ? list.get(0) : 0)); List padPersonInfos = personToDevService.addPersonsToEs(list); return Result.ok("存储分享人员到es", padPersonInfos); } @PostMapping("/selectSharePersonToSession") @ApiOperation(value = "selectSharePersonToSession 分享选中人员后台暂存", notes = "/selectSharePersonToSession", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "人员信息", dataType = "Array", paramType = "body") public Result addSomePersonToDataBase(@RequestBody List> list, HttpServletRequest req) { log.info("后台存储分享人员信息" + list.size()); log.info("第一条人员信息" + (list.size() > 0 ? list.get(0) : 0)); if (list.size() > 0) { req.getSession().setAttribute(selectSharePerson, list); } return Result.ok("存储分享人员", "{\"perSize\":\"" + (list != null ? list.size() : 0) + "\"}"); } @PostMapping("/getSelectPersonFromSession") @ApiOperation(value = "getSelectPersonFromSession 分享选中人员后台获取", notes = "/getSelectPersonFromSession", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"contVal\":\"输入内容\"}", dataType = "Map", paramType = "body") public Result getSelectPersonFromSession(@RequestBody Map map, HttpServletRequest req) { String contVal = (String) map.get("contVal"); List> list = (List>) req.getSession().getAttribute(selectSharePerson); List> list1 = new ArrayList>(); if (list != null) { log.info("分享选中人员后台获取" + list.size()); log.info(list + "全部人员信息"); log.info("第一条人员信息" + (list.size() > 0 ? list.get(0) : 0)); list1.addAll(list); if (contVal != null && !contVal.isEmpty() && list1.size() > 0) { for (int i = 0; i < list1.size(); i++) { Map strMap = list1.get(i); if (strMap != null) { String name = (String) strMap.get("name"); String cardId = (String) strMap.get("cardId"); if ((name != null && name.contains(contVal)) || (cardId != null && cardId.contains(contVal))) { } else { list1.remove(i--); } } else { list1.remove(i--); } } } return Result.ok("获取待分享人员", list1); } else { return Result.error(new RuntimeException("人员不存在")); } } @PostMapping("/getSharePersonStatusBySession") @ApiOperation(value = "getSharePersonStatusBySession 获取分享人员后单人信息", notes = "/getSharePersonStatusBySession", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"personId\":\"人员id\"}", dataType = "Map", paramType = "body") public Result getSharePersonStatusBySession(@RequestBody(required = true) Map map, HttpServletRequest req) throws UnsupportedEncodingException { String personId = (String) map.get("personId"); if (personId != null && personId.isEmpty()) personId = null; HttpSession session = req.getSession(); String json = (String) session.getAttribute(sharePersonToDevList); List cSerResults = JSONObject.parseArray(json, PerInDBStatus.class); if (personId == null || "".equals(personId)) { return Result.error(new RuntimeException("请选择已分享人员")); } HashMap returnMap = new HashMap<>(); if (cSerResults == null) { Result personDataBaseSyncStatus = getPersonDataBaseSyncStatus(map, req); Object data = personDataBaseSyncStatus.getData(); returnMap.put(dataBA, before); returnMap.put(returnData, data); // throw new RuntimeException("分享人员后单人信息不存在,请重新分享。"); } else { boolean isGetSharePerson = false; PerInDBStatus getSharePerson = null; for (PerInDBStatus cSerResult : cSerResults) { if (personId.equals(cSerResult.getPersonId())) { isGetSharePerson = true; getSharePerson = cSerResult; break; } } if (isGetSharePerson) { returnMap.put(dataBA, after); returnMap.put(returnData, getSharePerson); } else { Result personDataBaseSyncStatus = getPersonDataBaseSyncStatus(map, req); Object data = personDataBaseSyncStatus.getData(); returnMap.put(dataBA, before); returnMap.put(returnData, data); Result.ok("查询分享单个人员成功。", returnMap); // return Result.error(new RuntimeException("查询分享单个人员失败。")); } } return Result.ok("查询分享单个人员成功。", returnMap); } @PostMapping("/getPersonDBSyncStatus") @ApiOperation(value = "getPersonDBSyncStatus 查询当前人员真实底库状态,分享前,待更新页面都可用", notes = "/getPersonDBSyncStatus", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"personId\":\"人员id\"}", required = true, dataType = "Map", paramType = "body") public Result getPersonDataBaseSyncStatus(@RequestBody Map map, HttpServletRequest req) throws UnsupportedEncodingException { String persons = (String) map.get("personId"); if (persons != null && persons.isEmpty()) persons = null; HttpSession session = req.getSession(); if (persons != null && !"".equals(persons)) { String json = (String) session.getAttribute(SyncDataBaseSessionKey); List dBCluList = JSONObject.parseArray(json, DBaseCluster.class); if (dBCluList == null) { throw new RuntimeException("请先查询集群列表是否存在。"); } List ips = new ArrayList(); for (DBaseCluster dBaseCluster : dBCluList) { ips.add(dBaseCluster.getCluIPList().get(0).getNode_ip()); } List exists = personToDevService.isExists(persons, ips); return Result.ok("success", exists); } return Result.error(new RuntimeException("未选中分享集群底库。")); } @PostMapping("/updatePersonBySyncStatus") @ApiOperation(value = "updatePersonBySyncStatus 更新待更新人员信息", notes = "/updatePersonBySyncStatus", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"persons\":[\"人员id\",\"4805\",\"4806\"]}", dataType = "Map", paramType = "body") public Result updatePersonBySyncStatus(@RequestBody Map map, HttpServletRequest req) throws UnsupportedEncodingException { List persons = (List) map.get("persons"); HttpSession session = req.getSession(); String json = (String) session.getAttribute(SyncDataBaseSessionKey); List dBCluList = JSONObject.parseArray(json, DBaseCluster.class); List ipInfos = new ArrayList(); if (dBCluList == null) { throw new RuntimeException("请先查询集群列表是否存在。"); } if (persons == null || persons.size() < 1) { return Result.error(new RuntimeException("请选择待更新人员")); } for (DBaseCluster dBaseCluster : dBCluList) { List cluIPList = dBaseCluster.getCluIPList(); String ip = cluIPList.get(baseTime % cluIPList.size()).getNode_ip(); ipInfos.add(ip); } ++baseTime; if (baseTime > 100) this.baseTime = 0; if (ipInfos.size() < 1) { return Result.error(new RuntimeException("集群信息为空,如若实际存在集群,请重试")); } log.info(ipInfos + "ip集合"); Map> cResultMap = personToDevService.updateSyncPersons(persons, ipInfos); session.setAttribute(updatePersonMap, JSONObject.toJSONString(cResultMap)); Collection> values = cResultMap.values(); boolean isAllSuccess = true; for (List value : values) { for (UpdatePerToViewResult result : value) { if ("-1".equals(result.getResult())) { isAllSuccess = false; break; } } if (!isAllSuccess) break; } if (isAllSuccess) { return Result.ok("更新人员成功。", cResultMap); } else { return Result.custom("更新人员失败。", 203, false, cResultMap); } } @PostMapping("/getUpdatePersonStatusBySession") @ApiOperation(value = "getUpdatePersonStatusBySession 获取待更新人员将要被覆盖人员被更新信息", notes = "/getUpdatePersonStatusBySession", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"personId\":\"被合并人员id\",\"fatherPersonId\":\"所合并人员id\"}", dataType = "Map", paramType = "body") public Result getUpdatePersonStatusBySession(@RequestBody(required = true) Map map, HttpServletRequest req) throws UnsupportedEncodingException { String personId = (String) map.get("personId"); String fatherPersonId = (String) map.get("fatherPersonId"); if (personId != null && personId.isEmpty()) personId = null; HttpSession session = req.getSession(); String json = (String) session.getAttribute(updatePersonMap); Map> cResultMap = JSONObject.parseObject(json, HashMap.class); ; List ips = new ArrayList<>(); HashMap returnMap = new HashMap<>(); if (cResultMap == null) { Result personDataBaseSyncStatus = getPersonDataBaseSyncStatus(map, req); Object data = personDataBaseSyncStatus.getData(); returnMap.put(dataBA, before); returnMap.put(returnData, data); // throw new RuntimeException("待更新人员信息不存在,请重新更新。"); } else { if (personId == null || "".equals(personId) || fatherPersonId == null) { return Result.error(new RuntimeException("请选择待更新人员")); } UpdatePerToViewResult cSerResult = null; if (cResultMap.get(fatherPersonId) != null) { List cSerResults = JSONArray.parseArray(cResultMap.get(fatherPersonId).toString(), UpdatePerToViewResult.class); if (cSerResults != null && cSerResults.size() > 0) { for (UpdatePerToViewResult updatePerToViewResult : cSerResults) { if (personId.equalsIgnoreCase(updatePerToViewResult.getPersonId())) { cSerResult = updatePerToViewResult; break; } } } } if (cSerResult == null) { Result personDataBaseSyncStatus = getPersonDataBaseSyncStatus(map, req); Object data = personDataBaseSyncStatus.getData(); returnMap.put(dataBA, before); returnMap.put(returnData, data); } else { List allDataBase = cSerResult.getAllDataBase(); List existDataBase = cSerResult.getExistDataBase(); List notDataBase = new ArrayList(); if (allDataBase != null && existDataBase != null) { notDataBase.addAll(allDataBase); boolean b = notDataBase.removeAll(existDataBase); } cSerResult.setNotDataBase(notDataBase); // 获取 成功底库列表 returnMap.put(dataBA, after); returnMap.put(returnData, cSerResult); } } return Result.ok("查询更新人员成功。", returnMap); } @PostMapping("/getUpdatePersonDetail") @ApiOperation(value = "getUpdatePersonDetail 获取待更新人员所需信息", notes = "/getUpdatePersonDetail", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"personLike\":\"人员idcard/姓名\"}", dataType = "Map", paramType = "body") public Result getUpdatePersonDetail(@RequestBody(required = true) Map map, HttpServletRequest req) { String personLike = (String) map.get("personLike"); List cServerPeople = personToDevService.queryUpdatingPerson(personLike); return Result.ok("查询待更新人员详情成功。", cServerPeople); } @PostMapping("/getUpdatePersonCount") @ApiOperation(value = "getUpdatePersonCount 获取待更新人员总数", notes = "/getUpdatePersonCount", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParams({ }) public Result getUpdatePersonCount(HttpServletRequest req) { Integer integer = personToDevService.queryUpdatingPersonCount(); return Result.ok("查询更新人员总数成功。", integer); } @PostMapping("/deleteMergePersonByPersonId") @ApiOperation(value = "deleteMergePersonByPersonId 从待更新人员列表删除", notes = "/deleteMergePersonByPersonId", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"personId\":\"合并人员id\"}", dataType = "Map", paramType = "body") public Result deleteMergePersonByPersonId(@RequestBody Map map, HttpServletRequest req) { String personId = (String) map.get("personId"); if (personId != null && personId.isEmpty()) personId = null; Integer integer = personToDevService.deleteMergePersonByPersonId(personId); return Result.ok("删除更新人员成功。", integer); } @PostMapping("/getPersonStatusForAddOrUpdateOrNow") @ApiOperation(value = "getPersonStatusForAddOrUpdateOrNow 查询人员真实底库状态或分享后 更新后页面三合一接口", notes = "/getPersonStatusForAddOrUpdateOrNow", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ApiImplicitParam(name = "map", value = "{\"personId\":\"人员id\",\"SDLType\":\"传输类型 update add\"}", dataType = "Map", paramType = "body") public Result getPersonStatusForAddOrUpdateOrNow(@RequestBody Map map, HttpServletRequest req) throws UnsupportedEncodingException { String personId = (String) map.get("personId"); String sdlType = (String) map.get("SDLType"); if (personId != null && personId.isEmpty()) personId = null; HttpSession session = req.getSession(); if (personId != null && !"".equals(personId)) { String json = (String) session.getAttribute(SyncDataBaseSessionKey); List dBCluList = JSONObject.parseArray(json, DBaseCluster.class); if (dBCluList == null) { throw new RuntimeException("请先查询集群列表是否存在。"); } // 判断三种状态 if ("update".equalsIgnoreCase(sdlType)) { Result updatePersonStatusBySession = getUpdatePersonStatusBySession(map, req); return updatePersonStatusBySession; } else if ("add".equalsIgnoreCase(sdlType)) { Result sharePersonStatusBySession = getSharePersonStatusBySession(map, req); return sharePersonStatusBySession; } else { Result personDataBaseSyncStatus = getPersonDataBaseSyncStatus(map, req); return personDataBaseSyncStatus; } /* List ips = new ArrayList(); for (DBaseCluster dBaseCluster : dBCluList){ ips.add(dBaseCluster.getCluIPList().get(0).getNode_ip()); } List exists = personToDevService.isExists(personId, ips); return Result.ok("success",exists);*/ } return Result.error(new RuntimeException("未选中分享集群底库。")); } }