|
|
@@ -0,0 +1,652 @@
|
|
|
+package biz.xy;
|
|
|
+
|
|
|
+import biz.kq.HkConnU;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import core.co.haikang.HkApiClient;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.Response;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+import ss.AppB;
|
|
|
+import ss.db.Db;
|
|
|
+import ss.db.FaceCollectDb;
|
|
|
+import ss.db.SqlCmd;
|
|
|
+import ss.http.HttpU;
|
|
|
+import ss.obj.ObjC;
|
|
|
+import ss.serv.Serv;
|
|
|
+import ss.serv.ServC;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.sql.Timestamp;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 海康头像同步服务:
|
|
|
+ * 1. 用 PMS 学号/人员号匹配海康 jobNo;
|
|
|
+ * 2. 下载海康当前头像到本地图片目录;
|
|
|
+ * 3. 将快照落到 FACE_COLLECT.XY_HK_FACE_SNAPSHOT;
|
|
|
+ * 4. 将当前头像路径回写到主库 RY.ZJZWJ。
|
|
|
+ */
|
|
|
+public class HkFaceSnapshotServ extends Serv {
|
|
|
+
|
|
|
+ private static final Logger log4j = LogManager.getLogger(HkFaceSnapshotServ.class);
|
|
|
+ private static final Gson GSON = new Gson();
|
|
|
+ private static final HkApiClient HK_API_CLIENT = new HkApiClient();
|
|
|
+ private static final OkHttpClient DOWNLOAD_HTTP_CLIENT = HttpU.createTrustAllOkHttpClient();
|
|
|
+
|
|
|
+ private static final String TAB_SNAPSHOT = "XY_HK_FACE_SNAPSHOT";
|
|
|
+ private static final String SEQ_SNAPSHOT = "SEQ_XY_HK_FACE_SNAPSHOT";
|
|
|
+ private static final String FACE_DIR = "hk_face";
|
|
|
+ private static final int DOWNLOAD_STATUS_PENDING = 0;
|
|
|
+ private static final int DOWNLOAD_STATUS_OK = 1;
|
|
|
+ private static final int DOWNLOAD_STATUS_FAIL = 2;
|
|
|
+ private static final int DOWNLOAD_STATUS_NO_PHOTO = 3;
|
|
|
+ private static final int DOWNLOAD_STATUS_UNCHANGED = 4;
|
|
|
+ private static final int SYNC_RY_PENDING = 0;
|
|
|
+ private static final int SYNC_RY_OK = 1;
|
|
|
+ private static final int SYNC_RY_FAIL = 2;
|
|
|
+
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public String syncByRyid() {
|
|
|
+ super.wrJsonByResp(syncByRyidResult());
|
|
|
+ return ServC.SERV_notByA;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public String syncBatchByPage() {
|
|
|
+ super.wrJsonByResp(syncBatchByPageResult());
|
|
|
+ return ServC.SERV_notByA;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ public String selCurrentSnapshotByRyid() {
|
|
|
+ super.wrJsonByResp(selCurrentSnapshotByRyidResult());
|
|
|
+ return ServC.SERV_notByA;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> syncByRyidResult() {
|
|
|
+ Long ryid = rdReqLong("ryid");
|
|
|
+ if (ryid == null || ryid <= 0)
|
|
|
+ return mkFail("HK_FACE_PARAM_INVALID", "ryid 不能为空。");
|
|
|
+ return syncOneByRyid(ryid);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> syncBatchByPageResult() {
|
|
|
+ int pageNo = rdReqInt("pageNo", 1);
|
|
|
+ int pageSize = rdReqInt("pageSize", 20);
|
|
|
+ if (pageNo <= 0)
|
|
|
+ pageNo = 1;
|
|
|
+ if (pageSize <= 0)
|
|
|
+ pageSize = 20;
|
|
|
+ if (pageSize > 100)
|
|
|
+ pageSize = 100;
|
|
|
+
|
|
|
+ try (Db gdba = Db.makeConn()) {
|
|
|
+ if (gdba == null)
|
|
|
+ return mkFail("HK_FACE_DB_ERROR", "主业务库不可用。");
|
|
|
+
|
|
|
+ List<Map<String, Object>> studentRowList = selActiveStudentPage(gdba, pageNo, pageSize);
|
|
|
+ List<Map<String, Object>> itemList = new ArrayList<>();
|
|
|
+ int okCount = 0;
|
|
|
+ int failCount = 0;
|
|
|
+ for (Map<String, Object> studentRow : studentRowList) {
|
|
|
+ Long ryid = rdLong(studentRow, "ryid");
|
|
|
+ Map<String, Object> item = syncOneByRyid(ryid == null ? 0L : ryid);
|
|
|
+ itemList.add(item);
|
|
|
+ if (rdBool(item.get("ok")))
|
|
|
+ okCount++;
|
|
|
+ else
|
|
|
+ failCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> result = mkOk("批量同步完成。");
|
|
|
+ Map<String, Object> data = castData(result);
|
|
|
+ data.put("pageNo", pageNo);
|
|
|
+ data.put("pageSize", pageSize);
|
|
|
+ data.put("count", studentRowList.size());
|
|
|
+ data.put("okCount", okCount);
|
|
|
+ data.put("failCount", failCount);
|
|
|
+ data.put("items", itemList);
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log4j.error("HkFaceSnapshotServ.syncBatchByPageResult() 异常", e);
|
|
|
+ return mkFail("HK_FACE_SYNC_FAIL", rdErrMsg(e, "批量同步海康头像失败。"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> selCurrentSnapshotByRyidResult() {
|
|
|
+ Long ryid = rdReqLong("ryid");
|
|
|
+ if (ryid == null || ryid <= 0)
|
|
|
+ return mkFail("HK_FACE_PARAM_INVALID", "ryid 不能为空。");
|
|
|
+
|
|
|
+ try (Db gdba = FaceCollectDb.makeConn()) {
|
|
|
+ if (gdba == null)
|
|
|
+ return mkFail("HK_FACE_DB_ERROR", "FACE_COLLECT 数据源不可用。");
|
|
|
+
|
|
|
+ Map<String, Object> snapshotRow = selCurrentSnapshotRow(gdba, ryid);
|
|
|
+ if (snapshotRow == null || snapshotRow.isEmpty())
|
|
|
+ return mkFail("HK_FACE_NOT_FOUND", "未找到当前头像快照。");
|
|
|
+
|
|
|
+ Map<String, Object> result = mkOk("查询成功。");
|
|
|
+ castData(result).put("snapshot", snapshotRow);
|
|
|
+ gdba.commit();
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log4j.error("HkFaceSnapshotServ.selCurrentSnapshotByRyidResult() 异常", e);
|
|
|
+ return mkFail("HK_FACE_QUERY_FAIL", rdErrMsg(e, "查询当前头像快照失败。"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> syncOneByRyid(long ryid) {
|
|
|
+ if (ryid <= 0)
|
|
|
+ return mkFail("HK_FACE_PARAM_INVALID", "ryid 不合法。");
|
|
|
+ if (StringUtils.isBlank(AppB.imgBaseDir))
|
|
|
+ return mkFail("HK_FACE_CFG_ERROR", "未配置图片根目录 AppB.imgBaseDir。");
|
|
|
+
|
|
|
+ try (Db mainDb = Db.makeConn(); Db faceDb = FaceCollectDb.makeConn()) {
|
|
|
+ if (mainDb == null)
|
|
|
+ return mkFail("HK_FACE_DB_ERROR", "主业务库不可用。");
|
|
|
+ if (faceDb == null)
|
|
|
+ return mkFail("HK_FACE_DB_ERROR", "FACE_COLLECT 数据源不可用。");
|
|
|
+
|
|
|
+ Map<String, Object> studentRow = selActiveStudentByRyid(mainDb, ryid);
|
|
|
+ if (studentRow == null || studentRow.isEmpty())
|
|
|
+ return mkFail("HK_FACE_STUDENT_NOT_FOUND", "未找到在用学员记录。");
|
|
|
+
|
|
|
+ Map<String, Object> result = syncStudentFace(mainDb, faceDb, studentRow);
|
|
|
+ mainDb.commit();
|
|
|
+ faceDb.commit();
|
|
|
+ return result;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log4j.error("HkFaceSnapshotServ.syncOneByRyid() 异常, ryid=" + ryid, e);
|
|
|
+ return mkFail("HK_FACE_SYNC_FAIL", rdErrMsg(e, "同步海康头像失败。"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> syncStudentFace(Db mainDb, Db faceDb, Map<String, Object> studentRow) throws Exception {
|
|
|
+ long ryid = rdLongVal(studentRow, "ryid");
|
|
|
+ String ryh = rdStr(studentRow, "ryh");
|
|
|
+ String xm = rdStr(studentRow, "xm");
|
|
|
+ String sfzh = rdStr(studentRow, "sfzh");
|
|
|
+ String currentRyZjzwj = rdStr(studentRow, "zjzwj");
|
|
|
+ Timestamp now = new Timestamp(System.currentTimeMillis());
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(ryh))
|
|
|
+ return persistFailure(faceDb, studentRow, null, null, DOWNLOAD_STATUS_FAIL, "学员号为空,无法匹配海康 jobNo。", now);
|
|
|
+
|
|
|
+ Map<String, Object> hkConnParm = HkConnU.makeHkConnParm();
|
|
|
+ Map<String, Object> hkPersonInfo = loadHkPersonInfoByJobNo(hkConnParm, ryh);
|
|
|
+ if (hkPersonInfo == null)
|
|
|
+ return persistFailure(faceDb, studentRow, null, null, DOWNLOAD_STATUS_FAIL,
|
|
|
+ "海康未找到 jobNo=" + ryh + " 的人员。", now);
|
|
|
+
|
|
|
+ String hkPersonId = rdStr(hkPersonInfo, "personId");
|
|
|
+ List<Map<String, Object>> photoList = castMapList(rdRowVal(hkPersonInfo, "personPhoto"));
|
|
|
+ Map<String, Object> photoRow = photoList.isEmpty() ? null : photoList.get(0);
|
|
|
+ if (photoRow == null)
|
|
|
+ return persistFailure(faceDb, studentRow, hkPersonInfo, null, DOWNLOAD_STATUS_NO_PHOTO,
|
|
|
+ "海康人员存在,但未配置 personPhoto。", now);
|
|
|
+
|
|
|
+ String personPhotoIndexCode = rdStr(photoRow, "personPhotoIndexCode");
|
|
|
+ String picUri = rdStr(photoRow, "picUri");
|
|
|
+ String serverIndexCode = rdStr(photoRow, "serverIndexCode");
|
|
|
+ if (StringUtils.isAnyBlank(picUri, serverIndexCode))
|
|
|
+ return persistFailure(faceDb, studentRow, hkPersonInfo, photoRow, DOWNLOAD_STATUS_NO_PHOTO,
|
|
|
+ "海康头像缺少 picUri 或 serverIndexCode。", now);
|
|
|
+
|
|
|
+ Map<String, Object> currentSnapshotRow = selCurrentSnapshotRow(faceDb, ryid);
|
|
|
+ String currentPhotoIndexCode = rdStr(currentSnapshotRow, "hk_person_photo_index_code");
|
|
|
+ String currentLocalImgPath = rdStr(currentSnapshotRow, "local_img_path");
|
|
|
+ if (StringUtils.isNotBlank(currentPhotoIndexCode)
|
|
|
+ && currentPhotoIndexCode.equalsIgnoreCase(personPhotoIndexCode)
|
|
|
+ && localImgExists(currentLocalImgPath)) {
|
|
|
+ String syncMsg = ensureRyZjzwj(mainDb, faceDb, currentSnapshotRow, ryid, currentLocalImgPath, currentRyZjzwj, now);
|
|
|
+ updCurrentSnapshotKeepAlive(faceDb, currentSnapshotRow, now, DOWNLOAD_STATUS_UNCHANGED,
|
|
|
+ "海康头像未变化,沿用当前快照。", syncMsg == null ? SYNC_RY_PENDING : SYNC_RY_OK, syncMsg);
|
|
|
+
|
|
|
+ Map<String, Object> result = mkOk("海康头像未变化,已复用当前快照。");
|
|
|
+ Map<String, Object> data = castData(result);
|
|
|
+ data.put("ryid", ryid);
|
|
|
+ data.put("ryh", ryh);
|
|
|
+ data.put("xm", xm);
|
|
|
+ data.put("hkPersonId", hkPersonId);
|
|
|
+ data.put("snapshotId", rdLong(currentSnapshotRow, "snapshot_id"));
|
|
|
+ data.put("localImgPath", currentLocalImgPath);
|
|
|
+ data.put("syncMsg", syncMsg);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ String downloadUrl = HK_API_CLIENT.resolvePersonPictureDownloadUrl(hkConnParm, picUri, serverIndexCode);
|
|
|
+ String relPath = buildFaceRelPath(ryh, personPhotoIndexCode, now);
|
|
|
+ File imgFile = new File(AppB.imgBaseDir, relPath.replace("/", File.separator));
|
|
|
+ downloadToFile(downloadUrl, imgFile);
|
|
|
+
|
|
|
+ ImgMeta imgMeta = readImgMeta(imgFile);
|
|
|
+ if (currentSnapshotRow != null && !currentSnapshotRow.isEmpty())
|
|
|
+ markSnapshotHistory(faceDb, currentSnapshotRow, now);
|
|
|
+
|
|
|
+ long snapshotId = insertSnapshotRow(faceDb, studentRow, hkPersonInfo, photoRow, relPath, imgMeta,
|
|
|
+ DOWNLOAD_STATUS_OK, "海康头像下载成功。", SYNC_RY_PENDING, null, 1, now);
|
|
|
+ String syncMsg = writeBackRyZjzwj(mainDb, ryid, relPath, currentRyZjzwj);
|
|
|
+ updSnapshotSyncStatus(faceDb, snapshotId, SYNC_RY_OK, syncMsg, now);
|
|
|
+
|
|
|
+ Map<String, Object> result = mkOk("海康头像同步成功。");
|
|
|
+ Map<String, Object> data = castData(result);
|
|
|
+ data.put("ryid", ryid);
|
|
|
+ data.put("ryh", ryh);
|
|
|
+ data.put("xm", xm);
|
|
|
+ data.put("sfzh", sfzh);
|
|
|
+ data.put("hkPersonId", hkPersonId);
|
|
|
+ data.put("snapshotId", snapshotId);
|
|
|
+ data.put("personPhotoIndexCode", personPhotoIndexCode);
|
|
|
+ data.put("localImgPath", relPath);
|
|
|
+ data.put("imgWidth", imgMeta.width);
|
|
|
+ data.put("imgHeight", imgMeta.height);
|
|
|
+ data.put("imgSize", imgMeta.size);
|
|
|
+ data.put("imgMd5", imgMeta.md5);
|
|
|
+ data.put("syncMsg", syncMsg);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> persistFailure(Db faceDb,
|
|
|
+ Map<String, Object> studentRow,
|
|
|
+ Map<String, Object> hkPersonInfo,
|
|
|
+ Map<String, Object> photoRow,
|
|
|
+ int downloadStatus,
|
|
|
+ String msg,
|
|
|
+ Timestamp now) throws Exception {
|
|
|
+ Map<String, Object> currentSnapshotRow = selCurrentSnapshotRow(faceDb, rdLongVal(studentRow, "ryid"));
|
|
|
+ String localImgPath = rdStr(currentSnapshotRow, "local_img_path");
|
|
|
+ insertSnapshotRow(faceDb, studentRow, hkPersonInfo, photoRow, StringUtils.defaultString(localImgPath), null,
|
|
|
+ downloadStatus, msg, SYNC_RY_PENDING, null, 0, now);
|
|
|
+
|
|
|
+ Map<String, Object> result = mkFail("HK_FACE_SYNC_FAIL", msg);
|
|
|
+ Map<String, Object> data = castData(result);
|
|
|
+ data.put("ryid", rdLongVal(studentRow, "ryid"));
|
|
|
+ data.put("ryh", rdStr(studentRow, "ryh"));
|
|
|
+ data.put("xm", rdStr(studentRow, "xm"));
|
|
|
+ data.put("hkPersonId", rdStr(hkPersonInfo, "personId"));
|
|
|
+ data.put("personPhotoIndexCode", rdStr(photoRow, "personPhotoIndexCode"));
|
|
|
+ data.put("downloadStatus", downloadStatus);
|
|
|
+ data.put("localImgPath", localImgPath);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> loadHkPersonInfoByJobNo(Map<String, Object> hkConnParm, String jobNo) throws Exception {
|
|
|
+ Map<String, Map<String, Object>> personInfoMap = HK_API_CLIENT.loadPersonInfoMapByJobNo(hkConnParm, List.of(jobNo));
|
|
|
+ if (personInfoMap == null || personInfoMap.isEmpty())
|
|
|
+ return null;
|
|
|
+
|
|
|
+ Map<String, Object> exactRow = personInfoMap.get(StringUtils.upperCase(StringUtils.trimToEmpty(jobNo)));
|
|
|
+ if (exactRow != null)
|
|
|
+ return exactRow;
|
|
|
+
|
|
|
+ for (Map<String, Object> row : personInfoMap.values()) {
|
|
|
+ if (jobNo.equalsIgnoreCase(rdStr(row, "jobNo")))
|
|
|
+ return row;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String ensureRyZjzwj(Db mainDb,
|
|
|
+ Db faceDb,
|
|
|
+ Map<String, Object> currentSnapshotRow,
|
|
|
+ long ryid,
|
|
|
+ String localImgPath,
|
|
|
+ String currentRyZjzwj,
|
|
|
+ Timestamp now) throws Exception {
|
|
|
+ if (StringUtils.isBlank(localImgPath))
|
|
|
+ return "当前快照未保存本地图片路径,未回写 RY.ZJZWJ。";
|
|
|
+ if (StringUtils.equals(currentRyZjzwj, localImgPath))
|
|
|
+ return "RY.ZJZWJ 已与当前快照一致。";
|
|
|
+
|
|
|
+ String syncMsg = writeBackRyZjzwj(mainDb, ryid, localImgPath, currentRyZjzwj);
|
|
|
+ updCurrentSnapshotKeepAlive(faceDb, currentSnapshotRow, now, DOWNLOAD_STATUS_UNCHANGED,
|
|
|
+ "海康头像未变化,补齐 RY.ZJZWJ 回写。", SYNC_RY_OK, syncMsg);
|
|
|
+ return syncMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updCurrentSnapshotKeepAlive(Db faceDb,
|
|
|
+ Map<String, Object> snapshotRow,
|
|
|
+ Timestamp now,
|
|
|
+ int downloadStatus,
|
|
|
+ String downloadMsg,
|
|
|
+ int syncFlag,
|
|
|
+ String syncMsg) throws Exception {
|
|
|
+ Long snapshotId = rdLong(snapshotRow, "snapshot_id");
|
|
|
+ if (snapshotId == null || snapshotId <= 0)
|
|
|
+ return;
|
|
|
+
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("update " + TAB_SNAPSHOT +
|
|
|
+ " set DOWNLOAD_STATUS=:downloadStatus,DOWNLOAD_MSG=:downloadMsg,LAST_SYNC_TIME=:lastSyncTime," +
|
|
|
+ " SYNC_TO_RY_FLAG=:syncFlag,SYNC_TO_RY_TIME=:syncTime,SYNC_TO_RY_MSG=:syncMsg,XGSJ=:xgsj" +
|
|
|
+ " where SNAPSHOT_ID=:snapshotId");
|
|
|
+ sqlCmd.wrIntParm("downloadStatus", downloadStatus);
|
|
|
+ sqlCmd.wrStrParm("downloadMsg", downloadMsg);
|
|
|
+ sqlCmd.wrTimeParm("lastSyncTime", now);
|
|
|
+ sqlCmd.wrIntParm("syncFlag", syncFlag);
|
|
|
+ sqlCmd.wrTimeParm("syncTime", now);
|
|
|
+ sqlCmd.wrStrParm("syncMsg", syncMsg);
|
|
|
+ sqlCmd.wrTimeParm("xgsj", now);
|
|
|
+ sqlCmd.wrLongParm("snapshotId", snapshotId);
|
|
|
+ faceDb.doModByC(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void markSnapshotHistory(Db faceDb, Map<String, Object> currentSnapshotRow, Timestamp now) throws Exception {
|
|
|
+ Long snapshotId = rdLong(currentSnapshotRow, "snapshot_id");
|
|
|
+ if (snapshotId == null || snapshotId <= 0)
|
|
|
+ return;
|
|
|
+
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("update " + TAB_SNAPSHOT + " set IS_CURRENT=0,XGSJ=:xgsj where SNAPSHOT_ID=:snapshotId");
|
|
|
+ sqlCmd.wrTimeParm("xgsj", now);
|
|
|
+ sqlCmd.wrLongParm("snapshotId", snapshotId);
|
|
|
+ faceDb.doModByC(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ private long insertSnapshotRow(Db faceDb,
|
|
|
+ Map<String, Object> studentRow,
|
|
|
+ Map<String, Object> hkPersonInfo,
|
|
|
+ Map<String, Object> photoRow,
|
|
|
+ String localImgPath,
|
|
|
+ ImgMeta imgMeta,
|
|
|
+ int downloadStatus,
|
|
|
+ String downloadMsg,
|
|
|
+ int syncFlag,
|
|
|
+ String syncMsg,
|
|
|
+ int isCurrent,
|
|
|
+ Timestamp now) throws Exception {
|
|
|
+ long snapshotId = faceDb.rdNextSeqVal(SEQ_SNAPSHOT);
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("insert into " + TAB_SNAPSHOT +
|
|
|
+ " (SNAPSHOT_ID,RYID,RYH,XM,SFZH,HK_PERSON_ID,HK_JOB_NO,HK_PERSON_PHOTO_INDEX_CODE,HK_PIC_URI,HK_SERVER_INDEX_CODE," +
|
|
|
+ " LOCAL_IMG_PATH,IMG_MD5,IMG_SIZE,IMG_WIDTH,IMG_HEIGHT,DOWNLOAD_STATUS,DOWNLOAD_MSG,SYNC_TO_RY_FLAG,SYNC_TO_RY_TIME," +
|
|
|
+ " SYNC_TO_RY_MSG,LAST_SYNC_TIME,IS_CURRENT,CJSJ,XGSJ,JLZTM)" +
|
|
|
+ " values (:snapshotId,:ryid,:ryh,:xm,:sfzh,:hkPersonId,:hkJobNo,:photoIndexCode,:picUri,:serverIndexCode," +
|
|
|
+ " :localImgPath,:imgMd5,:imgSize,:imgWidth,:imgHeight,:downloadStatus,:downloadMsg,:syncFlag,:syncTime," +
|
|
|
+ " :syncMsg,:lastSyncTime,:isCurrent,:cjsj,:xgsj,1)");
|
|
|
+ sqlCmd.wrLongParm("snapshotId", snapshotId);
|
|
|
+ sqlCmd.wrLongParm("ryid", rdLongVal(studentRow, "ryid"));
|
|
|
+ sqlCmd.wrStrParm("ryh", rdStr(studentRow, "ryh"));
|
|
|
+ sqlCmd.wrStrParm("xm", rdStr(studentRow, "xm"));
|
|
|
+ sqlCmd.wrStrParm("sfzh", rdStr(studentRow, "sfzh"));
|
|
|
+ sqlCmd.wrStrParm("hkPersonId", rdStr(hkPersonInfo, "personId"));
|
|
|
+ sqlCmd.wrStrParm("hkJobNo", rdStr(hkPersonInfo, "jobNo"));
|
|
|
+ sqlCmd.wrStrParm("photoIndexCode", rdStr(photoRow, "personPhotoIndexCode"));
|
|
|
+ sqlCmd.wrStrParm("picUri", rdStr(photoRow, "picUri"));
|
|
|
+ sqlCmd.wrStrParm("serverIndexCode", rdStr(photoRow, "serverIndexCode"));
|
|
|
+ sqlCmd.wrStrParm("localImgPath", StringUtils.defaultString(localImgPath));
|
|
|
+ sqlCmd.wrStrParm("imgMd5", imgMeta == null ? null : imgMeta.md5);
|
|
|
+ sqlCmd.wrLongParm("imgSize", imgMeta == null ? null : imgMeta.size);
|
|
|
+ sqlCmd.wrIntParm("imgWidth", imgMeta == null ? null : imgMeta.width);
|
|
|
+ sqlCmd.wrIntParm("imgHeight", imgMeta == null ? null : imgMeta.height);
|
|
|
+ sqlCmd.wrIntParm("downloadStatus", downloadStatus);
|
|
|
+ sqlCmd.wrStrParm("downloadMsg", StringUtils.left(downloadMsg, 1000));
|
|
|
+ sqlCmd.wrIntParm("syncFlag", syncFlag);
|
|
|
+ sqlCmd.wrTimeParm("syncTime", syncFlag == SYNC_RY_PENDING ? null : now);
|
|
|
+ sqlCmd.wrStrParm("syncMsg", StringUtils.left(syncMsg, 1000));
|
|
|
+ sqlCmd.wrTimeParm("lastSyncTime", now);
|
|
|
+ sqlCmd.wrIntParm("isCurrent", isCurrent);
|
|
|
+ sqlCmd.wrTimeParm("cjsj", now);
|
|
|
+ sqlCmd.wrTimeParm("xgsj", now);
|
|
|
+ faceDb.doModByC(sqlCmd);
|
|
|
+ return snapshotId;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updSnapshotSyncStatus(Db faceDb, long snapshotId, int syncFlag, String syncMsg, Timestamp now) throws Exception {
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("update " + TAB_SNAPSHOT +
|
|
|
+ " set SYNC_TO_RY_FLAG=:syncFlag,SYNC_TO_RY_TIME=:syncTime,SYNC_TO_RY_MSG=:syncMsg,XGSJ=:xgsj" +
|
|
|
+ " where SNAPSHOT_ID=:snapshotId");
|
|
|
+ sqlCmd.wrIntParm("syncFlag", syncFlag);
|
|
|
+ sqlCmd.wrTimeParm("syncTime", now);
|
|
|
+ sqlCmd.wrStrParm("syncMsg", StringUtils.left(syncMsg, 1000));
|
|
|
+ sqlCmd.wrTimeParm("xgsj", now);
|
|
|
+ sqlCmd.wrLongParm("snapshotId", snapshotId);
|
|
|
+ faceDb.doModByC(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String writeBackRyZjzwj(Db mainDb, long ryid, String newImgPath, String currentRyZjzwj) throws Exception {
|
|
|
+ if (StringUtils.equals(currentRyZjzwj, newImgPath))
|
|
|
+ return "RY.ZJZWJ 已是最新路径,无需更新。";
|
|
|
+
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("update RY set ZJZWJ=:zjzwj where RYID=:ryid");
|
|
|
+ sqlCmd.wrStrParm("zjzwj", newImgPath);
|
|
|
+ sqlCmd.wrLongParm("ryid", ryid);
|
|
|
+ int updCount = mainDb.doModByC(sqlCmd);
|
|
|
+ if (updCount <= 0)
|
|
|
+ throw new RuntimeException("回写 RY.ZJZWJ 失败,未命中 ryid=" + ryid + " 的记录。");
|
|
|
+ return "已回写 RY.ZJZWJ。";
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> selActiveStudentByRyid(Db gdba, long ryid) throws Exception {
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("select distinct ry.RYID,ry.RYH,ry.XM,ry.SFZH,ry.ZJZWJ" +
|
|
|
+ " from RY join BJCY on bjcy.RYID=ry.RYID" +
|
|
|
+ " where ry.RYID=:ryid and bjcy.SFBY=0 and ry." + ObjC.JLZT_useStr + " and bjcy." + ObjC.JLZT_useStr);
|
|
|
+ sqlCmd.wrLongParm("ryid", ryid);
|
|
|
+ return gdba.doSelMapByC(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Map<String, Object>> selActiveStudentPage(Db gdba, int pageNo, int pageSize) throws Exception {
|
|
|
+ int startNo = (pageNo - 1) * pageSize + 1;
|
|
|
+ int endNo = pageNo * pageSize;
|
|
|
+ String sql = "select * from (" +
|
|
|
+ " select a.*, rownum rn from (" +
|
|
|
+ " select distinct ry.RYID,ry.RYH,ry.XM,ry.SFZH,ry.ZJZWJ" +
|
|
|
+ " from RY join BJCY on bjcy.RYID=ry.RYID" +
|
|
|
+ " where bjcy.SFBY=0 and ry." + ObjC.JLZT_useStr + " and bjcy." + ObjC.JLZT_useStr +
|
|
|
+ " order by ry.RYID" +
|
|
|
+ " ) a where rownum<=:endNo" +
|
|
|
+ ") where rn>=:startNo";
|
|
|
+ SqlCmd sqlCmd = new SqlCmd(sql);
|
|
|
+ sqlCmd.wrIntParm("endNo", endNo);
|
|
|
+ sqlCmd.wrIntParm("startNo", startNo);
|
|
|
+ return gdba.doSelMapListByC(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> selCurrentSnapshotRow(Db gdba, long ryid) throws Exception {
|
|
|
+ SqlCmd sqlCmd = new SqlCmd("select * from " + TAB_SNAPSHOT +
|
|
|
+ " where RYID=:ryid and IS_CURRENT=1 and JLZTM=1 order by SNAPSHOT_ID desc");
|
|
|
+ sqlCmd.wrLongParm("ryid", ryid);
|
|
|
+ List<Map<String, Object>> rowList = gdba.doSelMapListByC(sqlCmd);
|
|
|
+ return rowList.isEmpty() ? null : rowList.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String buildFaceRelPath(String ryh, String personPhotoIndexCode, Timestamp now) {
|
|
|
+ String dateDir = new SimpleDateFormat("yyyy/MM/dd").format(new Date(now.getTime()));
|
|
|
+ String ts = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date(now.getTime()));
|
|
|
+ String safeRyh = safeFilePart(ryh);
|
|
|
+ String safePhotoCode = safeFilePart(StringUtils.defaultIfBlank(personPhotoIndexCode, "nopho"));
|
|
|
+ return FACE_DIR + "/" + dateDir + "/" + safeRyh + "_" + safePhotoCode + "_" + ts + ".jpg";
|
|
|
+ }
|
|
|
+
|
|
|
+ private void downloadToFile(String downloadUrl, File imgFile) throws Exception {
|
|
|
+ File parent = imgFile.getParentFile();
|
|
|
+ if (parent != null && !parent.exists() && !parent.mkdirs())
|
|
|
+ throw new RuntimeException("头像目录创建失败:" + parent.getAbsolutePath());
|
|
|
+
|
|
|
+ Request request = new Request.Builder().url(downloadUrl).get().build();
|
|
|
+ try (Response response = DOWNLOAD_HTTP_CLIENT.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful())
|
|
|
+ throw new RuntimeException("下载海康头像失败,HTTP=" + response.code());
|
|
|
+ if (response.body() == null)
|
|
|
+ throw new RuntimeException("下载海康头像失败,响应体为空。");
|
|
|
+ try (InputStream inputStream = response.body().byteStream()) {
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, imgFile);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ImgMeta readImgMeta(File imgFile) throws Exception {
|
|
|
+ BufferedImage bufferedImage = ImageIO.read(imgFile);
|
|
|
+ if (bufferedImage == null)
|
|
|
+ throw new RuntimeException("下载的海康头像不是有效图片:" + imgFile.getAbsolutePath());
|
|
|
+
|
|
|
+ ImgMeta imgMeta = new ImgMeta();
|
|
|
+ imgMeta.size = imgFile.length();
|
|
|
+ imgMeta.width = bufferedImage.getWidth();
|
|
|
+ imgMeta.height = bufferedImage.getHeight();
|
|
|
+ imgMeta.md5 = calcMd5(imgFile);
|
|
|
+ return imgMeta;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String calcMd5(File imgFile) throws Exception {
|
|
|
+ MessageDigest md5 = MessageDigest.getInstance("MD5");
|
|
|
+ byte[] buf = new byte[8192];
|
|
|
+ try (InputStream inputStream = new FileInputStream(imgFile)) {
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buf)) > 0)
|
|
|
+ md5.update(buf, 0, len);
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte b : md5.digest())
|
|
|
+ sb.append(String.format("%02x", b));
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean localImgExists(String relPath) {
|
|
|
+ if (StringUtils.isBlank(relPath) || StringUtils.isBlank(AppB.imgBaseDir))
|
|
|
+ return false;
|
|
|
+ File file = new File(AppB.imgBaseDir, relPath.replace("/", File.separator));
|
|
|
+ return file.isFile() && file.exists();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object rdRowVal(Map<String, Object> row, String key) {
|
|
|
+ if (row == null || row.isEmpty() || StringUtils.isBlank(key))
|
|
|
+ return null;
|
|
|
+ if (row.containsKey(key))
|
|
|
+ return row.get(key);
|
|
|
+ String lowerKey = StringUtils.lowerCase(key);
|
|
|
+ if (row.containsKey(lowerKey))
|
|
|
+ return row.get(lowerKey);
|
|
|
+ String upperKey = StringUtils.upperCase(key);
|
|
|
+ if (row.containsKey(upperKey))
|
|
|
+ return row.get(upperKey);
|
|
|
+ for (Map.Entry<String, Object> entry : row.entrySet()) {
|
|
|
+ if (key.equalsIgnoreCase(entry.getKey()))
|
|
|
+ return entry.getValue();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private List<Map<String, Object>> castMapList(Object obj) {
|
|
|
+ if (!(obj instanceof List<?> list) || list.isEmpty())
|
|
|
+ return List.of();
|
|
|
+ List<Map<String, Object>> rowList = new ArrayList<>();
|
|
|
+ for (Object item : list) {
|
|
|
+ if (item instanceof Map<?, ?> map)
|
|
|
+ rowList.add((Map<String, Object>) map);
|
|
|
+ }
|
|
|
+ return rowList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> mkOk(String msg) {
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("ok", true);
|
|
|
+ result.put("msg", msg);
|
|
|
+ result.put("data", new LinkedHashMap<String, Object>());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> mkFail(String code, String msg) {
|
|
|
+ Map<String, Object> result = new LinkedHashMap<>();
|
|
|
+ result.put("ok", false);
|
|
|
+ result.put("code", code);
|
|
|
+ result.put("msg", msg);
|
|
|
+ result.put("data", new LinkedHashMap<String, Object>());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private Map<String, Object> castData(Map<String, Object> result) {
|
|
|
+ Object data = result.get("data");
|
|
|
+ if (data instanceof Map<?, ?> map)
|
|
|
+ return (Map<String, Object>) map;
|
|
|
+ Map<String, Object> newData = new LinkedHashMap<>();
|
|
|
+ result.put("data", newData);
|
|
|
+ return newData;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String rdErrMsg(Exception e, String defMsg) {
|
|
|
+ String errMsg = e == null ? null : StringUtils.trimToNull(e.getMessage());
|
|
|
+ return errMsg == null ? defMsg : errMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String rdReqTrim(String name) {
|
|
|
+ return super.req == null ? null : StringUtils.trimToNull(super.req.getParameter(name));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long rdReqLong(String name) {
|
|
|
+ String val = rdReqTrim(name);
|
|
|
+ if (val == null)
|
|
|
+ return null;
|
|
|
+ try {
|
|
|
+ return Long.parseLong(val);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int rdReqInt(String name, int defVal) {
|
|
|
+ String val = rdReqTrim(name);
|
|
|
+ if (val == null)
|
|
|
+ return defVal;
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(val);
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ return defVal;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean rdBool(Object value) {
|
|
|
+ if (value instanceof Boolean bool)
|
|
|
+ return bool;
|
|
|
+ return "true".equalsIgnoreCase(String.valueOf(value));
|
|
|
+ }
|
|
|
+
|
|
|
+ private String rdStr(Map<String, Object> row, String key) {
|
|
|
+ Object value = rdRowVal(row, key);
|
|
|
+ return value == null ? null : StringUtils.trimToNull(String.valueOf(value));
|
|
|
+ }
|
|
|
+
|
|
|
+ private Long rdLong(Map<String, Object> row, String key) {
|
|
|
+ Object value = rdRowVal(row, key);
|
|
|
+ if (value == null)
|
|
|
+ return null;
|
|
|
+ if (value instanceof Number num)
|
|
|
+ return num.longValue();
|
|
|
+ try {
|
|
|
+ return Long.parseLong(String.valueOf(value));
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private long rdLongVal(Map<String, Object> row, String key) {
|
|
|
+ Long value = rdLong(row, key);
|
|
|
+ return value == null ? 0L : value;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String safeFilePart(String text) {
|
|
|
+ String value = StringUtils.defaultIfBlank(text, "unknown");
|
|
|
+ return value.replaceAll("[^0-9A-Za-z\\u4e00-\\u9fa5_-]", "_");
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final class ImgMeta {
|
|
|
+ private String md5;
|
|
|
+ private long size;
|
|
|
+ private int width;
|
|
|
+ private int height;
|
|
|
+ }
|
|
|
+}
|