Create_by_fileOSFService.java 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. package com.kingdee.eas.custom.esign.osf;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.alibaba.fastjson.TypeReference;
  6. import com.alibaba.fastjson.serializer.SerializerFeature;
  7. import com.cloudera.impala.jdbc4.internal.fasterxml.jackson.core.JsonProcessingException;
  8. import com.cloudera.impala.jdbc4.internal.fasterxml.jackson.databind.ObjectMapper;
  9. import com.google.common.collect.Lists;
  10. import com.google.common.collect.Maps;
  11. import com.google.common.collect.Sets;
  12. import com.kingdee.bos.BOSException;
  13. import com.kingdee.bos.Context;
  14. import com.kingdee.bos.bsf.service.app.IHRMsfService;
  15. import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
  16. import com.kingdee.bos.metadata.entity.SelectorItemCollection;
  17. import com.kingdee.bos.metadata.entity.SelectorItemInfo;
  18. import com.kingdee.eas.common.EASBizException;
  19. import com.kingdee.eas.custom.esign.*;
  20. import com.kingdee.eas.custom.esign.bizEnum.ComponentTypeEnum;
  21. import com.kingdee.eas.custom.esign.bizEnum.EsignConfigEnum;
  22. import com.kingdee.eas.custom.esign.bizEnum.EsignStatusEnum;
  23. import com.kingdee.eas.custom.esign.tsign.hz.comm.EsignHttpResponse;
  24. import com.kingdee.eas.custom.esign.tsign.hz.exception.EsignException;
  25. import com.kingdee.eas.custom.esign.util.EsignConfig;
  26. import com.kingdee.eas.custom.esign.util.EsignHttpUtil;
  27. import com.kingdee.eas.util.app.DbUtil;
  28. import com.kingdee.jdbc.rowset.IRowSet;
  29. import org.apache.commons.lang3.StringUtils;
  30. import java.net.URISyntaxException;
  31. import java.sql.SQLException;
  32. import java.util.*;
  33. import java.util.concurrent.TimeUnit;
  34. /**
  35. * description: Create_by_fileOSFService <br>
  36. * date: 28/11/2025 上午 9:46 <br>
  37. * author: lhbj <br>
  38. * version: 1.0 <br>
  39. */
  40. public class Create_by_fileOSFService implements IHRMsfService {
  41. private static final String SUCCESS_CODE = "0";
  42. private static final String PREVIEW_OPERATION = "预览";
  43. private static final int MAX_RETRY_COUNT = 20;
  44. private static final int RETRY_INTERVAL_SECONDS = 3;
  45. public Map<String, Object> getSign_templates_detail(Context context, String signTemplateId) throws EsignException, URISyntaxException {
  46. Map<String, Object> resul = Maps.newHashMap();
  47. String orgName = EsignConfig.getInstance().get("orgName");
  48. EsignHttpResponse response = EsignHttpUtil.getOrgIdentity_infoByOrgName(context, orgName);
  49. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  50. JSONObject body = JSON.parseObject(response.getBody());
  51. if ("0".equals(String.valueOf(body.get("code")))) {
  52. JSONObject orgPsn = body.getJSONObject("data");
  53. String orgId = orgPsn.getString("orgId");
  54. orgName = orgPsn.getString("orgName");
  55. //自动落章只能传orgId
  56. EsignHttpResponse std = EsignHttpUtil.getSign_templates_detail(context, signTemplateId, orgId, false, null);
  57. if (std.getStatus() >= 200 && std.getStatus() < 300) {
  58. JSONObject stdbody = JSON.parseObject(std.getBody());
  59. if (SUCCESS_CODE.equals(String.valueOf(stdbody.get("code")))) {
  60. JSONObject stdData = stdbody.getJSONObject("data");
  61. JSONArray participants = stdData.getJSONArray("participants");
  62. JSONArray copiers = stdData.getJSONArray("copiers");
  63. for (int x = 0; null != copiers && x < copiers.size(); x++) {
  64. JSONObject stdCopier = copiers.getJSONObject(x);
  65. stdCopier.put("participantType", 3);
  66. participants.add(stdCopier);
  67. }
  68. resul.put("data", participants);
  69. } else {
  70. resul.put("code", response.getStatus());
  71. resul.put("message", "网络异常");
  72. resul.put("data", null);
  73. }
  74. } else {
  75. resul.put("code", response.getStatus());
  76. resul.put("message", "网络异常");
  77. resul.put("data", null);
  78. }
  79. }
  80. } else {
  81. resul.put("code", response.getStatus());
  82. resul.put("message", "网络异常");
  83. resul.put("data", null);
  84. }
  85. return resul;
  86. }
  87. @Override
  88. public Object process(Context context, Map map) throws EASBizException, BOSException {
  89. String mack = (String) map.get("mack");
  90. String data = (String) map.get("data");
  91. String signTemplateId = (String) map.get("eSignTemplateNum");
  92. Map<String, Object> resul = Maps.newHashMap();
  93. try {
  94. JSONObject jsonObject = JSON.parseObject(data);
  95. if ("preview".equals(mack)) {
  96. for (Map.Entry<String, Object> fileEntry : jsonObject.entrySet()) {
  97. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  98. EsignHttpResponse response = this.previewFile(context, fieldObject, "预览");
  99. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  100. resul.putAll(this.processFileResponse(response, context));
  101. } else {
  102. resul.put("code", response.getStatus());
  103. resul.put("message", "网络异常");
  104. resul.put("data", null);
  105. }
  106. }
  107. } else if ("getTemplate".equals(mack)) {
  108. resul.putAll(this.getSign_templates_detail(context, signTemplateId));
  109. } else {
  110. Map<String, Object> signMap = Maps.newHashMap();
  111. String sourceId = jsonObject.getString("sourceId");
  112. String signFlowTitle = jsonObject.getString("signFlowTitle");
  113. String personId = jsonObject.getString("personId");
  114. String operatorId = jsonObject.getString("operatorId");
  115. //文件
  116. JSONObject templateInfo = jsonObject.getJSONObject("templateInfo");
  117. //签署方信息
  118. JSONObject signInfo = jsonObject.getJSONObject("signInfo");
  119. JSONArray signs = signInfo.getJSONArray("signers");
  120. Boolean isRadio = false;
  121. for (int i = 0; i < signs.size(); i++) {
  122. Map<String, Object> signMapInfo = Maps.newHashMap();
  123. JSONObject sign = signs.getJSONObject(i);
  124. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  125. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  126. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  127. //企业/机构签署方信息
  128. if (null != orgSignerInfo) {
  129. //isRadio = orgSignerInfo.getBoolean("isRadio");
  130. }
  131. }
  132. if (isRadio) {
  133. List<Map<String, Object>> docs = this.addDocs(context, jsonObject, templateInfo);
  134. List<Map<String, Object>> signDocs = Lists.newArrayList();
  135. for (Map<String, Object> doc : docs) {
  136. Map<String, Object> docNew = new HashMap<>(doc);
  137. docNew.remove("signListGroup");
  138. signDocs.add(docNew);
  139. }
  140. signMap.put("docs", signDocs);
  141. if (null != docs && docs.size() > 0) {
  142. //签署流程配置项
  143. Map<String, Object> signFlowConfig = this.addSignFlowConfig(context, signFlowTitle, jsonObject);
  144. signMap.put("signFlowConfig", signFlowConfig);
  145. //签署方信息
  146. List<Map<String, Object>> signers = this.addSigners(context, signs, docs, sourceId);
  147. signMap.put("signers", signers);
  148. //抄送方信息
  149. JSONArray copis = signInfo.getJSONArray("copiers");
  150. List<Map<String, Object>> copiers = this.addCopiers(context, copis);
  151. if (copiers.size() > 0) {
  152. signMap.put("copiers", copiers);
  153. }
  154. System.out.println("--------------------------------------------------------------------------------------");
  155. System.out.println("signMap:" + JSON.toJSONString(signMap));
  156. System.out.println("--------------------------------------------------------------------------------------");
  157. EsignHttpResponse response = EsignHttpUtil.create_by_file(context, personId, signFlowTitle, operatorId, sourceId, JSON.toJSONString(signMap));
  158. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  159. JSONObject body = JSON.parseObject(response.getBody());
  160. resul.putAll(body);
  161. } else {
  162. resul.put("code", 403);
  163. resul.put("message", "参数异常,没有模版id");
  164. resul.put("data", null);
  165. }
  166. }
  167. } else {
  168. //处理文件字段
  169. Map<String, List<Map<String, Object>>> pDocs = this.addPreviewDocs(context, jsonObject, templateInfo);
  170. List<Map<String, Object>> components = pDocs.get("components");
  171. //设置待签署文件信息
  172. List<Map<String, Object>> docs = pDocs.get("docs");
  173. List<Map<String, Object>> signDocs = Lists.newArrayList();
  174. for (Map<String, Object> doc : docs) {
  175. Map<String, Object> docNew = new HashMap<>(doc);
  176. docNew.remove("signListGroup");
  177. signDocs.add(docNew);
  178. }
  179. signMap.put("docs", signDocs);
  180. String eSignTemplateNum = jsonObject.getString("eSignTemplateNum");
  181. if (null != docs) {
  182. //签署流程配置项
  183. Map<String, Object> signFlowConfig = this.addSignFlowConfig(context, signFlowTitle, jsonObject);
  184. signMap.put("signFlowConfig", signFlowConfig);
  185. // //签署方信息
  186. // JSONObject signInfo = jsonObject.getJSONObject("signInfo");
  187. // JSONArray signs = signInfo.getJSONArray("signers");
  188. // List<Map<String, Object>> signers = this.addSigners(context, signs, docs, sourceId);
  189. // signMap.put("signers", signers);
  190. // //抄送方信息
  191. // JSONArray copis = signInfo.getJSONArray("copiers");
  192. // List<Map<String, Object>> copiers = this.addCopiers(context, copis);
  193. // if (copiers.size() > 0) {
  194. // signMap.put("copiers", copiers);
  195. // }
  196. //
  197. //必须要存在签署文件才能签署
  198. if (docs.size() > 0) {
  199. if (StringUtils.isNotBlank(eSignTemplateNum)) {
  200. String sealId = "";
  201. Map<String, List<Map<String, Object>>> pMap = this.addParticipants(context, signInfo, eSignTemplateNum, sealId, sourceId);
  202. signMap.put("signTemplateId", eSignTemplateNum);
  203. signMap.put("components", components);
  204. List<Map<String, Object>> participants = Lists.newArrayList();
  205. for (int x = 0; x < pMap.get("participants").size(); x++) {
  206. Map<String, Object> participant = Maps.newHashMap();
  207. participant.putAll(pMap.get("participants").get(x));
  208. Integer participantSetMode = (Integer) participant.get("participantSetMode");
  209. Map<String, Object> psnParticipant = (Map<String, Object>) participant.get("psnParticipant");
  210. if (null != psnParticipant) {
  211. psnParticipant.remove("psnId");
  212. }
  213. Map<String, Object> orgParticipant = (Map<String, Object>) participant.get("orgParticipant");
  214. if (null != orgParticipant) {
  215. orgParticipant.remove("orgId");
  216. Map<String, Object> transactor = (Map<String, Object>) orgParticipant.get("transactor");
  217. transactor.remove("transactorPsnId");
  218. }
  219. if (null != participantSetMode && (participantSetMode.compareTo(1) == 0 || participantSetMode.compareTo(4) == 0)) {
  220. participants.add(participant);
  221. }
  222. }
  223. signMap.putAll(pMap);
  224. signMap.put("participants", participants);
  225. ObjectMapper mapper = new ObjectMapper();
  226. // 从 pMap 中取出原始的 participants 列表
  227. List<Map<String, Object>> originalList = (List<Map<String, Object>>) pMap.get("participants");
  228. // 从 JSON 反序列化为新的列表(独立副本)
  229. // 使用 fastjson 深拷贝:序列化时禁用循环引用检测,确保 JSON 完全展开
  230. String jsonString = JSON.toJSONString(originalList, SerializerFeature.DisableCircularReferenceDetect);
  231. List<Map> copiedList = JSON.parseArray(jsonString, Map.class);
  232. signMap.put("stubborn_participants", copiedList);
  233. System.out.println("--------------------------------------------------------------------------------------");
  234. System.out.println("signMap:" + JSON.toJSONString(signMap));
  235. System.out.println("--------------------------------------------------------------------------------------");
  236. EsignHttpResponse response = EsignHttpUtil.createBySignTemplate(context, personId, signFlowTitle, operatorId, sourceId, JSON.toJSONString(signMap));
  237. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  238. JSONObject body = JSON.parseObject(response.getBody());
  239. resul.putAll(body);
  240. } else {
  241. resul.put("code", response.getStatus());
  242. resul.put("message", "网络异常");
  243. resul.put("data", null);
  244. }
  245. } else {
  246. // System.out.println("--------------------------------------------------------------------------------------");
  247. // System.out.println("signMap:" + JSON.toJSONString(signMap));
  248. // System.out.println("--------------------------------------------------------------------------------------");
  249. // EsignHttpResponse response = EsignHttpUtil.create_by_file(context, personId, signFlowTitle, operatorId, sourceId, JSON.toJSONString(signMap));
  250. // if (response.getStatus() >= 200 && response.getStatus() < 300) {
  251. // JSONObject body = JSON.parseObject(response.getBody());
  252. // resul.putAll(body);
  253. // } else {
  254. resul.put("code", 403);
  255. resul.put("message", "参数异常,没有模版id");
  256. resul.put("data", null);
  257. // }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. } catch (EsignException | URISyntaxException | SQLException e) {
  264. e.printStackTrace();
  265. resul.put("code", 400);
  266. resul.put("message", e.getMessage());
  267. resul.put("data", null);
  268. }
  269. return resul;
  270. }
  271. /**
  272. * 根据”填写模板生成文件“返回的文件id查询文件处理状态
  273. * 需要阻塞等待这个状态变为2或者5才能调用发起签署接口
  274. *
  275. * @param response
  276. * @param context
  277. * @return
  278. */
  279. public Map<String, Object> processFileResponse(EsignHttpResponse response, Context context) {
  280. Map<String, Object> result = Maps.newHashMap();
  281. JSONObject body = JSON.parseObject(response.getBody());
  282. if (!SUCCESS_CODE.equals(String.valueOf(body.get("code")))) {
  283. result.putAll(body);
  284. return result;
  285. }
  286. JSONObject jsonData = body.getJSONObject("data");
  287. String fileId = jsonData.getString("fileId");
  288. JSONObject pollData = pollFileDownloadUrl(context, fileId);
  289. if (null != pollData && StringUtils.isNotBlank(pollData.getString("fileDownloadUrl"))) {
  290. jsonData.putAll(pollData);
  291. }
  292. result.putAll(body);
  293. System.out.println("processFileResponse:" + result);
  294. return result;
  295. }
  296. /**
  297. * 阻塞循环调用获取文件信息接口
  298. *
  299. * @param context
  300. * @param fileId
  301. * @return
  302. */
  303. private JSONObject pollFileDownloadUrl(Context context, String fileId) {
  304. for (int i = MAX_RETRY_COUNT; i > 0; i--) {
  305. try {
  306. JSONObject data = tryGetFileDownloadUrl(context, fileId);
  307. if (null != data && StringUtils.isNotBlank(data.getString("fileDownloadUrl"))) {
  308. return data;
  309. }
  310. TimeUnit.SECONDS.sleep(RETRY_INTERVAL_SECONDS);
  311. } catch (InterruptedException e) {
  312. Thread.currentThread().interrupt();
  313. break;
  314. } catch (Exception e) {
  315. // 记录日志,继续重试
  316. e.printStackTrace();
  317. }
  318. }
  319. return null;
  320. }
  321. /**
  322. * 等待这个状态变为2或者5才返回
  323. *
  324. * @param context
  325. * @param fileId
  326. * @return
  327. */
  328. private JSONObject tryGetFileDownloadUrl(Context context, String fileId) {
  329. try {
  330. EsignHttpResponse response = EsignHttpUtil.getFileStatus(context, fileId, PREVIEW_OPERATION);
  331. if (response.getStatus() < 200 || response.getStatus() >= 300) {
  332. return null;
  333. }
  334. JSONObject responseBody = JSON.parseObject(response.getBody());
  335. if (!SUCCESS_CODE.equals(String.valueOf(responseBody.get("code")))) {
  336. return null;
  337. }
  338. JSONObject data = responseBody.getJSONObject("data");
  339. String fileStatus = data.getString("fileStatus");
  340. Set<String> COMPLETED_STATUS = Sets.newHashSet();
  341. COMPLETED_STATUS.add("2");
  342. COMPLETED_STATUS.add("5");
  343. if (COMPLETED_STATUS.contains(fileStatus)) {
  344. return data;
  345. }
  346. } catch (EsignException e) {
  347. e.printStackTrace();
  348. }
  349. return null;
  350. }
  351. /**
  352. * 设置待签署文件位置信息
  353. *
  354. * @param context
  355. * @param fileId
  356. * @param keyword
  357. * @param sourceId
  358. * @return
  359. * @throws EsignException
  360. */
  361. public JSONObject addPositions(Context context, String fileId, String keyword, String sourceId) throws EsignException {
  362. List<String> keywords = Lists.newArrayList();
  363. keywords.add(keyword);
  364. JSONObject coordinate = null;
  365. System.out.println("respPosition:fileId:" + fileId);
  366. System.out.println("respPosition:keywords:" + keywords);
  367. EsignHttpResponse respPosition = EsignHttpUtil.keyword_positions(context, fileId, keywords, sourceId);
  368. System.out.println("respPosition:" + respPosition.getBody());
  369. if (respPosition.getStatus() >= 200 && respPosition.getStatus() < 300) {
  370. JSONObject resp = JSON.parseObject(respPosition.getBody());
  371. if ("0".equals(String.valueOf(resp.get("code")))) {
  372. JSONObject keywordData = (JSONObject) resp.get("data");
  373. if (null != keywordData) {
  374. JSONArray keywordPositions = keywordData.getJSONArray("keywordPositions");
  375. if (null != keywordPositions && keywordPositions.size() > 0) {
  376. JSONObject keywordPs = keywordPositions.getJSONObject(keywordPositions.size() - 1);
  377. if (keywordPs.getBoolean("searchResult")) {
  378. if (null != keywordPs) {
  379. JSONArray positions = keywordPs.getJSONArray("positions");
  380. if (null != positions && positions.size() > 0) {
  381. JSONObject position = positions.getJSONObject(positions.size() - 1);
  382. if (null != position) {
  383. Object pageNum = position.get("pageNum");
  384. JSONArray coordinates = position.getJSONArray("coordinates");
  385. if (null != coordinates && coordinates.size() > 0) {
  386. coordinate = coordinates.getJSONObject(coordinates.size() - 1);
  387. if (null != coordinate) {
  388. coordinate.put("positionPage", pageNum);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
  398. System.out.println("coordinate:" + coordinate);
  399. }
  400. return coordinate;
  401. }
  402. /**
  403. * 获取签署人的签署区
  404. *
  405. * @param context
  406. * @param fields
  407. * @param fileId
  408. * @param sourceId
  409. * @return
  410. * @throws EsignException
  411. */
  412. public Map<String, Map<String, Map<String, Object>>> addsignAreaList(Context context, Map<String, Object> fields, String fileId, String sourceId, String docTemplateId) throws EsignException, BOSException, EASBizException, SQLException {
  413. //Map<String, List<Map<String, Object>>> signListGroup = Maps.newHashMap();
  414. Map<String, Map<String, Map<String, Object>>> signMapGroup = Maps.newHashMap();
  415. //获取文件模版字段
  416. IESignTemplateFileEntry ieSignTemplateFileEntry = ESignTemplateFileEntryFactory.getLocalInstance(context);
  417. SelectorItemCollection selColl = new SelectorItemCollection();
  418. selColl.add(new SelectorItemInfo("*"));
  419. selColl.add(new SelectorItemInfo("fields.*"));
  420. selColl.add(new SelectorItemInfo("fields.relatedControl.*"));
  421. String sel = "select fid from CT_ESI_ESignTemplateFileEntry where cfeSignTemplateId = ?";
  422. IRowSet rs = DbUtil.executeQuery(context, sel.toString(), new Object[]{docTemplateId});
  423. String Entryid = "";
  424. if (rs.next()) {
  425. Entryid = rs.getString("fid");
  426. }
  427. ESignTemplateFileEntryInfo eSignTemplateFileEntry = ieSignTemplateFileEntry.getESignTemplateFileEntryInfo(new ObjectUuidPK(Entryid), selColl);
  428. Map<String, ESignTemplateFileEntryFieldInfo> fieldsMap = Maps.newHashMap();
  429. ESignTemplateFileEntryFieldCollection entryFieldCollection = eSignTemplateFileEntry.getFields();
  430. for (int i = 0; i < entryFieldCollection.size(); i++) {
  431. ESignTemplateFileEntryFieldInfo fieldInfo = entryFieldCollection.get(i);
  432. //失效的控件跳过
  433. if (fieldInfo.isInvalid()) {
  434. continue;
  435. }
  436. String fieldId = fieldInfo.getTemplateFieldId().toString();
  437. JSONObject field = (JSONObject) fields.get(fieldId);
  438. if (ComponentTypeEnum.SIGN_AREA.equals(fieldInfo.getComponentType())) {
  439. Map<String, Object> signMap = Maps.newHashMap();
  440. if (StringUtils.isBlank(fieldInfo.getSigningParty())) {
  441. Map<String, Object> errMap = Maps.newHashMap();
  442. errMap.put("模版", eSignTemplateFileEntry.getName());
  443. errMap.put("控件", fieldInfo.getTemplateFieldName());
  444. errMap.put("内容", "签署方为空请维护!");
  445. throw new EsignException(JSON.toJSONString(errMap));
  446. }
  447. signMap.put("key", fieldInfo.getSigningParty());
  448. signMap.put("fileId", fileId);
  449. signMap.put("fieldId", fieldInfo.getTemplateFieldId());
  450. signMap.put("signFieldType", 0);
  451. String keyword = fieldInfo.getTemplateFieldName();
  452. if (keyword.indexOf("骑缝") >= 0) {
  453. signMap.put("signFieldStyle", 2);
  454. signMap.put("positionX", fieldInfo.getPositionX());
  455. signMap.put("positionY", fieldInfo.getPositionY());
  456. signMap.put("positionPage", fieldInfo.getPageNum());
  457. } else {
  458. signMap.put("signFieldStyle", 1);
  459. if (null == fieldInfo.getPositionX() && null == fieldInfo.getPositionY()) {
  460. Map<String, Object> errMap = Maps.newHashMap();
  461. errMap.put("模版", eSignTemplateFileEntry.getName());
  462. errMap.put("控件", fieldInfo.getTemplateFieldName());
  463. errMap.put("内容", "印章坐标为空请维护!");
  464. throw new EsignException(JSON.toJSONString(errMap));
  465. }
  466. signMap.put("positionX", fieldInfo.getPositionX());
  467. signMap.put("positionY", fieldInfo.getPositionY());
  468. signMap.put("positionPage", fieldInfo.getPageNum());
  469. }
  470. Map<String, Map<String, Object>> skMap = signMapGroup.get((String) signMap.get("key"));
  471. if (null == skMap) {
  472. skMap = Maps.newHashMap();
  473. signMapGroup.put((String) signMap.get("key"), skMap);
  474. }
  475. skMap.put(fieldInfo.getTemplateFieldId(), signMap);
  476. }
  477. //签署日期
  478. if (ComponentTypeEnum.DATESIGNATURE.equals(fieldInfo.getComponentType())) {
  479. fieldsMap.put(fieldId, fieldInfo);
  480. }
  481. }
  482. //签署日期
  483. for (Map.Entry<String, ESignTemplateFileEntryFieldInfo> fieldLinkEntry : fieldsMap.entrySet()) {
  484. //签署日期
  485. ESignTemplateFileEntryFieldInfo fieldLink = fieldLinkEntry.getValue();
  486. //签署方
  487. ESignTemplateFileEntryFieldInfo fieldInfo = fieldLink.getRelatedControl();
  488. if (null == fieldInfo) {
  489. Map<String, Object> errMap = Maps.newHashMap();
  490. errMap.put("模版", eSignTemplateFileEntry.getName());
  491. errMap.put("控件", fieldLink.getTemplateFieldName());
  492. errMap.put("内容", "签署方关联关系为空请维护!");
  493. throw new EsignException(JSON.toJSONString(errMap));
  494. }
  495. if (!ComponentTypeEnum.SIGN_AREA.equals(fieldInfo.getComponentType())) {
  496. Map<String, Object> errMap = Maps.newHashMap();
  497. errMap.put("模版", eSignTemplateFileEntry.getName());
  498. errMap.put("控件", fieldLink.getTemplateFieldName());
  499. errMap.put("内容", "关联关系不为签署区,请检查!");
  500. throw new EsignException(JSON.toJSONString(errMap));
  501. }
  502. Map<String, Object> signMap = Maps.newHashMap();
  503. //签署日期坐标
  504. Map<String, Object> signDateConfig = Maps.newHashMap();
  505. signDateConfig.put("showSignDate", 1);
  506. if (null != fieldLink.getPositionX() && null != fieldLink.getPositionY()) {
  507. signDateConfig.put("signDatePositionX", fieldLink.getPositionX());
  508. signDateConfig.put("signDatePositionY", fieldLink.getPositionY());
  509. }
  510. if (null != fieldLink.get("fontSize")) {
  511. signDateConfig.put("fontSize", fieldLink.get("fontSize"));
  512. }
  513. if (null != fieldLink.get("dateFormat")) {
  514. signDateConfig.put("dateFormat", fieldLink.get("dateFormat"));
  515. }
  516. signMap.put("signDateConfig", signDateConfig);
  517. signMap.put("key", fieldInfo.getSigningParty());
  518. signMap.put("fileId", fileId);
  519. signMap.put("signFieldType", 0);
  520. String keyword = fieldInfo.getTemplateFieldName();
  521. if (keyword.indexOf("骑缝") >= 0) {
  522. signMap.put("signFieldStyle", 2);
  523. signMap.put("positionX", fieldInfo.getPositionX());
  524. signMap.put("positionY", fieldInfo.getPositionY());
  525. signMap.put("positionPage", fieldInfo.getPageNum());
  526. } else {
  527. signMap.put("signFieldStyle", 1);
  528. if (null == fieldInfo.getPositionX() && null == fieldInfo.getPositionY()) {
  529. Map<String, Object> errMap = Maps.newHashMap();
  530. errMap.put("模版", eSignTemplateFileEntry.getName());
  531. errMap.put("控件", fieldInfo.getTemplateFieldName());
  532. errMap.put("内容", "印章坐标为空请维护!");
  533. throw new EsignException(JSON.toJSONString(errMap));
  534. }
  535. signMap.put("positionX", fieldInfo.getPositionX());
  536. signMap.put("positionY", fieldInfo.getPositionY());
  537. signMap.put("positionPage", fieldInfo.getPageNum());
  538. }
  539. Map<String, Map<String, Object>> skMap = signMapGroup.get((String) signMap.get("key"));
  540. if (null == skMap) {
  541. skMap = Maps.newHashMap();
  542. signMapGroup.put((String) signMap.get("key"), skMap);
  543. }
  544. skMap.remove(fieldInfo.getTemplateFieldId());
  545. skMap.put(fieldLink.getTemplateFieldId(), signMap);
  546. }
  547. System.out.println("signMapGroup:" + signMapGroup);
  548. return signMapGroup;
  549. }
  550. /**
  551. * 设置待签署文件信息
  552. *
  553. * @param context
  554. * @param jsonObject
  555. * @param templateInfo
  556. * @return
  557. * @throws EsignException
  558. */
  559. public List<Map<String, Object>> addDocs(Context context, JSONObject jsonObject, JSONObject templateInfo) throws EsignException, EASBizException, BOSException, SQLException {
  560. //设置待签署文件信息
  561. Map<String, Object> resul = Maps.newHashMap();
  562. List<Map<String, Object>> docs = Lists.newArrayList();
  563. for (Map.Entry<String, Object> fileEntry : templateInfo.entrySet()) {
  564. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  565. String docTemplateId = fieldObject.getString("id");
  566. String fileName = fieldObject.getString("name");
  567. String eSignTemplateNum = fieldObject.getString("eSignTemplateNum");
  568. if (StringUtils.isBlank(docTemplateId)) {
  569. docTemplateId = fieldObject.getString("tableId");
  570. }
  571. if (StringUtils.isBlank(fileName)) {
  572. fileName = fieldObject.getString("tableName");
  573. }
  574. if (StringUtils.isNotBlank(eSignTemplateNum)) {
  575. jsonObject.put("eSignTemplateNum", eSignTemplateNum);
  576. }
  577. Object order = fieldObject.get("order");
  578. String sourceId = jsonObject.getString("sourceId");
  579. this.initDocAndSignAreaList(context, resul, docs, fieldObject, docTemplateId, fileName, order, sourceId);
  580. }
  581. return docs;
  582. }
  583. /**
  584. * 设置待签署文件信息
  585. *
  586. * @param context
  587. * @param jsonObject
  588. * @param templateInfo
  589. * @return
  590. * @throws EsignException
  591. */
  592. public Map<String, List<Map<String, Object>>> addPreviewDocs(Context context, JSONObject jsonObject, JSONObject templateInfo) throws EsignException, EASBizException, BOSException, SQLException {
  593. //设置待签署文件信息
  594. Map<String, List<Map<String, Object>>> resul = Maps.newHashMap();
  595. List<Map<String, Object>> docs = Lists.newArrayList();
  596. List<Map<String, Object>> components = Lists.newArrayList();
  597. for (Map.Entry<String, Object> fileEntry : templateInfo.entrySet()) {
  598. JSONObject fieldObject = (JSONObject) fileEntry.getValue();
  599. String docTemplateId = fieldObject.getString("id");
  600. String fileName = fieldObject.getString("name");
  601. String eSignTemplateNum = fieldObject.getString("eSignTemplateNum");
  602. if (StringUtils.isBlank(docTemplateId)) {
  603. docTemplateId = fieldObject.getString("tableId");
  604. }
  605. if (StringUtils.isBlank(fileName)) {
  606. fileName = fieldObject.getString("tableName");
  607. }
  608. if (StringUtils.isNotBlank(eSignTemplateNum)) {
  609. jsonObject.put("eSignTemplateNum", eSignTemplateNum);
  610. }
  611. Object order = fieldObject.get("order");
  612. String sourceId = jsonObject.getString("sourceId");
  613. Map<String, Object> doc = Maps.newHashMap();
  614. doc.put("fileId", docTemplateId);
  615. doc.put("fileName", fileName);
  616. if (null != order) {
  617. doc.put("order", Integer.parseInt(String.valueOf(order)));
  618. }
  619. System.out.println("addDocs:" + fieldObject);
  620. JSONObject fields = fieldObject.getJSONObject("fields");
  621. for (Map.Entry<String, Object> field : fields.entrySet()) {
  622. Map<String, Object> component = Maps.newHashMap();
  623. component.put("componentId", field.getKey());
  624. component.put("fileId", docTemplateId);
  625. JSONObject fieldInfo = (JSONObject) field.getValue();
  626. String dataType = (String) fieldInfo.get("dataType");
  627. if (ComponentTypeEnum.SIGN_AREA.getAlias().equals(dataType)) {
  628. continue;
  629. }
  630. if (ComponentTypeEnum.DATESIGNATURE.getAlias().equals(dataType)) {
  631. continue;
  632. }
  633. if (null == fieldInfo.get("value")) {
  634. continue;
  635. }
  636. component.put("componentValue", fieldInfo.get("value"));
  637. components.add(component);
  638. }
  639. //取得这个文件的签署人的签署区
  640. //Map<String, Map<String, Map<String, Object>>> signAreaList = this.addsignAreaList(context, fields, (String) docTemplateId, sourceId, docTemplateId);
  641. //doc.put("signListGroup", signAreaList);
  642. docs.add(doc);
  643. }
  644. resul.put("docs", docs);
  645. resul.put("components", components);
  646. return resul;
  647. }
  648. private void initDocAndSignAreaList(Context context, Map<String, Object> resul, List<Map<String, Object>> docs, JSONObject fieldObject, String docTemplateId, String fileName, Object order, String sourceId) throws EsignException, BOSException, EASBizException, SQLException {
  649. EsignHttpResponse response = this.previewFile(context, fieldObject, sourceId);
  650. resul.put("id", docTemplateId);
  651. resul.put("name", fileName);
  652. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  653. Map<String, Object> body = this.processFileResponse(response, context);
  654. if ("0".equals(String.valueOf(body.get("code")))) {
  655. JSONObject file = (JSONObject) body.get("data");
  656. Map<String, Object> doc = Maps.newHashMap();
  657. doc.put("fileId", file.get("fileId"));
  658. doc.put("fileName", fileName);
  659. if (null != order) {
  660. doc.put("order", Integer.parseInt(String.valueOf(order)));
  661. }
  662. System.out.println("addDocs:" + fieldObject);
  663. JSONObject fields = fieldObject.getJSONObject("fields");
  664. //取得这个文件的签署人的签署区
  665. Map<String, Map<String, Map<String, Object>>> signAreaList = this.addsignAreaList(context, fields, (String) file.get("fileId"), sourceId, docTemplateId);
  666. doc.put("signListGroup", signAreaList);
  667. docs.add(doc);
  668. } else {
  669. //文件模板生成失败
  670. resul.putAll(body);
  671. throw new EsignException(JSON.toJSONString(resul));
  672. }
  673. } else {
  674. throw new EsignException(JSON.toJSONString(response));
  675. }
  676. }
  677. /**
  678. * 抄送方信息
  679. *
  680. * @param copis
  681. * @return
  682. */
  683. public List<Map<String, Object>> addCopiers(Context context, JSONArray copis) {
  684. List<Map<String, Object>> copiers = Lists.newArrayList();
  685. for (int i = 0; i < copis.size(); i++) {
  686. JSONObject cop = copis.getJSONObject(i);
  687. Map<String, Object> copierPsn = Maps.newHashMap();
  688. Map<String, Object> copierPsnInfo = Maps.newHashMap();
  689. copierPsnInfo.put("psnAccount", cop.getString("psnAccount"));
  690. copierPsn.put("copierPsnInfo", copierPsnInfo);
  691. copiers.add(copierPsn);
  692. }
  693. return copiers;
  694. }
  695. /**
  696. * 添加签署组织
  697. *
  698. * @param context
  699. * @param orgSignerInfo
  700. * @param docs
  701. * @param sourceId
  702. * @return
  703. * @throws EsignException
  704. * @throws URISyntaxException
  705. */
  706. public Map<String, Object> addOrgSignerInfo(Context context, JSONObject orgSignerInfo, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  707. List<Map<String, Object>> resulList = Lists.newArrayList();
  708. List<Map<String, Object>> resulErrList = Lists.newArrayList();
  709. Map<String, Object> resul = Maps.newHashMap();
  710. Map<String, Object> signMapInfo = Maps.newHashMap();
  711. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  712. String orgName = orgSignerInfo.getString("orgName");
  713. EsignHttpResponse response = EsignHttpUtil.getOrgIdentity_infoByOrgName(context, orgName);
  714. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  715. JSONObject body = JSON.parseObject(response.getBody());
  716. if ("0".equals(String.valueOf(body.get("code")))) {
  717. JSONObject orgPsn = body.getJSONObject("data");
  718. String orgId = orgPsn.getString("orgId");
  719. orgName = orgPsn.getString("orgName");
  720. //自动落章只能传orgId
  721. if (true == orgSignerInfo.getBoolean("isRadio")) {
  722. orgPsnSignMap.put("orgId", orgId);
  723. } else {
  724. orgPsnSignMap.put("orgName", orgName);
  725. Map<String, Object> transactorInfo = Maps.newHashMap();
  726. transactorInfo.put("psnAccount", orgSignerInfo.getString("psnAccount"));
  727. Map<String, Object> psnInfo = Maps.newHashMap();
  728. psnInfo.put("psnName", orgSignerInfo.getString("psnName"));
  729. transactorInfo.put("psnInfo", psnInfo);
  730. orgPsnSignMap.put("transactorInfo", transactorInfo);
  731. orgPsnSignMap.put("orgInfo", orgPsn.getJSONObject("orgInfo"));
  732. }
  733. } else {
  734. //失败
  735. resul.putAll(body);
  736. throw new EsignException(JSON.toJSONString(resul));
  737. }
  738. } else {
  739. throw new EsignException("网络异常:" + JSON.toJSONString(response));
  740. }
  741. Integer signOrder = orgSignerInfo.getInteger("signOrder");
  742. //设置签署方的签署顺序
  743. if (null != signOrder) {
  744. Map<String, Object> signConfig = Maps.newHashMap();
  745. signConfig.put("signOrder", signOrder);
  746. signMapInfo.put("signConfig", signConfig);
  747. }
  748. signMapInfo.put("orgSignerInfo", orgPsnSignMap);
  749. //设置签署方的通知方式
  750. String noticeTypes = orgSignerInfo.getString("noticeTypes");
  751. Map<String, String> noticeConfig = Maps.newHashMap();
  752. if (StringUtils.isNotBlank(noticeTypes)) {
  753. noticeConfig.put("noticeTypes", noticeTypes);
  754. } else {
  755. noticeConfig.put("noticeTypes", "1,2");
  756. }
  757. signMapInfo.put("noticeConfig", noticeConfig);
  758. //签署区信息
  759. String signName = orgSignerInfo.getString("signName");
  760. if (StringUtils.isBlank(signName)) {
  761. signName = "签署方" + signOrder;
  762. }
  763. List<Map<String, Object>> signFields = Lists.newArrayList();
  764. for (Map<String, Object> doc : docs) {
  765. Map<String, Map<String, Map<String, Object>>> signListGroup = (Map<String, Map<String, Map<String, Object>>>) doc.get("signListGroup");
  766. //判断是否存在签署区
  767. if (null != signListGroup && signListGroup.size() > 0) {
  768. Map<String, Map<String, Object>> fieldList = signListGroup.get(signName);
  769. if (null != fieldList) {
  770. for (Map<String, Object> fieldMap : fieldList.values()) {
  771. Map<String, Object> field = Maps.newHashMap();
  772. field.put("fileId", fieldMap.get("fileId"));
  773. field.put("customBizNum", doc.get("fileName"));
  774. if (0 == ((int) fieldMap.get("signFieldType"))) {
  775. field.put("signFieldType", fieldMap.get("signFieldType"));
  776. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  777. Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  778. if (true == orgSignerInfo.getBoolean("isRadio")) {
  779. //企业/机构自动落章(autoSign为true),请不要传该参数。
  780. orgPsnSignMap.remove("transactorInfo");
  781. //自动落章
  782. normalSignFieldConfig.put("freeMode", false);
  783. normalSignFieldConfig.put("autoSign", true);
  784. //印章id
  785. normalSignFieldConfig.put("assignedSealId", orgSignerInfo.getString("sealId"));
  786. //印章类型:1印章,2骑缝章
  787. normalSignFieldConfig.put("signFieldStyle", fieldMap.get("signFieldStyle"));
  788. Map<String, Object> signFieldPosition = Maps.newHashMap();
  789. if (1 == ((int) fieldMap.get("signFieldStyle"))) {
  790. //正常章
  791. if (null != fieldMap.get("positionPage") && null != fieldMap.get("positionX") && null != fieldMap.get("positionY")) {
  792. signFieldPosition.put("positionPage", fieldMap.get("positionPage"));
  793. signFieldPosition.put("positionX", fieldMap.get("positionX"));
  794. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  795. Map<String, Object> signDateConfig = (Map<String, Object>) fieldMap.get("signDateConfig");
  796. if (null == signDateConfig) {
  797. signDateConfig = Maps.newHashMap();
  798. //signDateConfig.put("showSignDate", 1);
  799. }
  800. field.put("signDateConfig", signDateConfig);
  801. } else {
  802. Map<String, Object> res = Maps.newHashMap();
  803. //失败
  804. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  805. resulErrList.add(res);
  806. }
  807. } else {
  808. //骑缝章
  809. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  810. }
  811. normalSignFieldConfig.put("signFieldPosition", signFieldPosition);
  812. } else {
  813. //手动签章
  814. normalSignFieldConfig.put("freeMode", false);
  815. normalSignFieldConfig.put("autoSign", false);
  816. //印章id
  817. normalSignFieldConfig.put("assignedSealId", orgSignerInfo.getString("sealId"));
  818. //印章类型:1印章,2骑缝章
  819. normalSignFieldConfig.put("signFieldStyle", fieldMap.get("signFieldStyle"));
  820. Map<String, Object> signFieldPosition = Maps.newHashMap();
  821. if (1 == ((int) fieldMap.get("signFieldStyle"))) {
  822. //正常章
  823. if (null != fieldMap.get("positionPage") && null != fieldMap.get("positionX") && null != fieldMap.get("positionY")) {
  824. signFieldPosition.put("positionPage", fieldMap.get("positionPage"));
  825. signFieldPosition.put("positionX", fieldMap.get("positionX"));
  826. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  827. Map<String, Object> signDateConfig = (Map<String, Object>) fieldMap.get("signDateConfig");
  828. if (null == signDateConfig) {
  829. signDateConfig = Maps.newHashMap();
  830. //signDateConfig.put("showSignDate", 1);
  831. }
  832. field.put("signDateConfig", signDateConfig);
  833. } else {
  834. Map<String, Object> res = Maps.newHashMap();
  835. //失败
  836. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  837. resulErrList.add(res);
  838. }
  839. } else {
  840. //骑缝章
  841. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  842. }
  843. normalSignFieldConfig.put("signFieldPosition", signFieldPosition);
  844. }
  845. field.put("normalSignFieldConfig", normalSignFieldConfig);
  846. } else {
  847. field.put("signFieldType", fieldMap.get("signFieldType"));
  848. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  849. Map<String, Object> remarkSignFieldConfig = Maps.newHashMap();
  850. remarkSignFieldConfig.put("freeMode", true);
  851. remarkSignFieldConfig.put("inputType", 2);
  852. field.put("remarkSignFieldConfig", remarkSignFieldConfig);
  853. }
  854. signFields.add(field);
  855. }
  856. } else {
  857. Map<String, Object> res = Maps.newHashMap();
  858. //失败
  859. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  860. resulList.add(res);
  861. }
  862. }
  863. }
  864. if (signFields.size() <= 0) {
  865. throw new EsignException(JSON.toJSONString(resulList));
  866. }
  867. if (resulErrList.size() > 0) {
  868. throw new EsignException(JSON.toJSONString(resulErrList));
  869. }
  870. signMapInfo.put("signFields", signFields);
  871. signMapInfo.put("signerType", 1);
  872. return signMapInfo;
  873. }
  874. /**
  875. * 添加签署人
  876. *
  877. * @param context
  878. * @param psnSignerInfo
  879. * @param docs
  880. * @param sourceId
  881. * @return
  882. * @throws EsignException
  883. * @throws URISyntaxException
  884. */
  885. public Map<String, Object> addPsnSignerInfo(Context context, JSONObject psnSignerInfo, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  886. List<Map<String, Object>> resulList = Lists.newArrayList();
  887. Map<String, Object> resul = Maps.newHashMap();
  888. Map<String, Object> signMapInfo = Maps.newHashMap();
  889. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  890. //个人签署方信息
  891. String psnAccount = psnSignerInfo.getString("psnAccount");
  892. String psnName = psnSignerInfo.getString("psnName");
  893. Integer signOrder = psnSignerInfo.getInteger("signOrder");
  894. //设置签署方的签署顺序
  895. if (null != signOrder) {
  896. Map<String, Object> signConfig = Maps.newHashMap();
  897. signConfig.put("signOrder", signOrder);
  898. signMapInfo.put("signConfig", signConfig);
  899. }
  900. orgPsnSignMap.put("psnAccount", psnAccount);
  901. Map<String, String> psnInfo = Maps.newHashMap();
  902. psnInfo.put("psnName", psnName);
  903. orgPsnSignMap.put("psnInfo", psnInfo);
  904. //个人签署方信息
  905. signMapInfo.put("psnSignerInfo", orgPsnSignMap);
  906. //设置签署方的通知方式
  907. String noticeTypes = psnSignerInfo.getString("noticeTypes");
  908. Map<String, String> noticeConfig = Maps.newHashMap();
  909. if (StringUtils.isNotBlank(noticeTypes)) {
  910. noticeConfig.put("noticeTypes", noticeTypes);
  911. } else {
  912. noticeConfig.put("noticeTypes", "1,2");
  913. }
  914. signMapInfo.put("noticeConfig", noticeConfig);
  915. //签署区信息
  916. List<Map<String, Object>> signFields = Lists.newArrayList();
  917. // for (Map<String, Object> doc : docs) {
  918. // Map<String, Object> field = Maps.newHashMap();
  919. // field.put("fileId", doc.get("fileId"));
  920. // field.put("customBizNum", sourceId);
  921. // field.put("signFieldType", 0);
  922. // //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  923. // Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  924. // normalSignFieldConfig.put("freeMode", true);
  925. // normalSignFieldConfig.put("movableSignField", false);
  926. //
  927. // field.put("normalSignFieldConfig", normalSignFieldConfig);
  928. // signFields.add(field);
  929. // }
  930. //签署区信息
  931. String signName = psnSignerInfo.getString("signName");
  932. if (StringUtils.isBlank(signName)) {
  933. signName = "签署方" + signOrder;
  934. }
  935. for (Map<String, Object> doc : docs) {
  936. Map<String, Map<String, Map<String, Object>>> signListGroup = (Map<String, Map<String, Map<String, Object>>>) doc.get("signListGroup");
  937. Map<String, Map<String, Object>> fieldList = signListGroup.get(signName);
  938. if (null != fieldList) {
  939. for (Map<String, Object> fieldMap : fieldList.values()) {
  940. Map<String, Object> field = Maps.newHashMap();
  941. field.put("fileId", fieldMap.get("fileId"));
  942. field.put("customBizNum", doc.get("fileName"));
  943. if (0 == ((int) fieldMap.get("signFieldType"))) {
  944. field.put("signFieldType", fieldMap.get("signFieldType"));
  945. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  946. Map<String, Object> normalSignFieldConfig = Maps.newHashMap();
  947. //手动签章
  948. normalSignFieldConfig.put("freeMode", false);
  949. normalSignFieldConfig.put("autoSign", false);
  950. //印章id
  951. //normalSignFieldConfig.put("assignedSealId", orgSignerInfo.getString("sealId"));
  952. //印章类型:1印章,2骑缝章
  953. normalSignFieldConfig.put("signFieldStyle", fieldMap.get("signFieldStyle"));
  954. Map<String, Object> signFieldPosition = Maps.newHashMap();
  955. if (1 == ((int) fieldMap.get("signFieldStyle"))) {
  956. //正常章
  957. if (null != fieldMap.get("positionPage") && null != fieldMap.get("positionX") && null != fieldMap.get("positionY")) {
  958. signFieldPosition.put("positionPage", fieldMap.get("positionPage"));
  959. signFieldPosition.put("positionX", fieldMap.get("positionX"));
  960. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  961. Map<String, Object> signDateConfig = (Map<String, Object>) fieldMap.get("signDateConfig");
  962. if (null == signDateConfig) {
  963. signDateConfig = Maps.newHashMap();
  964. }
  965. field.put("signDateConfig", signDateConfig);
  966. }
  967. } else {
  968. //骑缝章
  969. signFieldPosition.put("positionY", fieldMap.get("positionY"));
  970. }
  971. normalSignFieldConfig.put("signFieldPosition", signFieldPosition);
  972. field.put("normalSignFieldConfig", normalSignFieldConfig);
  973. } else {
  974. field.put("signFieldType", fieldMap.get("signFieldType"));
  975. //签章区配置项(指定signFieldType为 0 - 签章区时,该参数为必传项)
  976. Map<String, Object> remarkSignFieldConfig = Maps.newHashMap();
  977. remarkSignFieldConfig.put("freeMode", true);
  978. remarkSignFieldConfig.put("inputType", 2);
  979. field.put("remarkSignFieldConfig", remarkSignFieldConfig);
  980. }
  981. signFields.add(field);
  982. }
  983. } else {
  984. Map<String, Object> res = Maps.newHashMap();
  985. //失败
  986. res.put("文件模板:" + doc.get("fileName"), signName + ",未匹配到签署区,请检查模板的签署区命名规范。签署区命名规则:如(“甲方:(盖章)_签署方1_X50_Y50“)根据“_”分割字符串,第一个是要盖章的内容位置标识;第二个是签署方用来判断这个签署区是谁的;X50表示:印章位置向右移动50;Y50表示:印章位置向上移动50");
  987. resulList.add(res);
  988. }
  989. }
  990. if (signFields.size() <= 0) {
  991. throw new EsignException(JSON.toJSONString(resulList));
  992. }
  993. signMapInfo.put("signFields", signFields);
  994. signMapInfo.put("signerType", 0);
  995. return signMapInfo;
  996. }
  997. /**
  998. * 处理签署方信息
  999. *
  1000. * @param context
  1001. * @param signs
  1002. * @param docs
  1003. * @param sourceId
  1004. * @return
  1005. * @throws EsignException
  1006. * @throws URISyntaxException
  1007. */
  1008. public List<Map<String, Object>> addSigners(Context context, JSONArray signs, List<Map<String, Object>> docs, String sourceId) throws EsignException, URISyntaxException {
  1009. List<Map<String, Object>> signers = Lists.newArrayList();
  1010. for (int i = 0; i < signs.size(); i++) {
  1011. Map<String, Object> signMapInfo = Maps.newHashMap();
  1012. JSONObject sign = signs.getJSONObject(i);
  1013. Map<String, Object> orgPsnSignMap = Maps.newHashMap();
  1014. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  1015. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  1016. //企业/机构签署方信息
  1017. if (null != orgSignerInfo) {
  1018. signMapInfo = addOrgSignerInfo(context, orgSignerInfo, docs, sourceId);
  1019. } else {
  1020. //个人签署方信息
  1021. signMapInfo = addPsnSignerInfo(context, psnSignerInfo, docs, sourceId);
  1022. }
  1023. signers.add(signMapInfo);
  1024. }
  1025. return signers;
  1026. }
  1027. /**
  1028. * todo查询流程模板详情 https://open.esign.cn/doc/opendoc/file-and-template3/pfzut7ho9obc7c5r
  1029. * 处理签署方信息
  1030. *
  1031. * @param context
  1032. * @param signTemplateId
  1033. * @param sealOwnerId
  1034. * @param sourceId
  1035. * @return
  1036. * @throws EsignException
  1037. * @throws URISyntaxException
  1038. */
  1039. public Map<String, List<Map<String, Object>>> addParticipants(Context context, JSONObject signInfo, String signTemplateId, String sealOwnerId, String sourceId) throws EsignException, URISyntaxException {
  1040. Map<String, List<Map<String, Object>>> participants_addCopiers = Maps.newHashMap();
  1041. List<Map<String, Object>> participants = Lists.newArrayList();
  1042. List<Map<String, Object>> addCopiers = Lists.newArrayList();
  1043. String orgName = EsignConfig.getInstance().get("orgName");
  1044. EsignHttpResponse response = EsignHttpUtil.getOrgIdentity_infoByOrgName(context, orgName);
  1045. if (response.getStatus() >= 200 && response.getStatus() < 300) {
  1046. JSONObject body = JSON.parseObject(response.getBody());
  1047. if ("0".equals(String.valueOf(body.get("code")))) {
  1048. JSONObject orgPsn = body.getJSONObject("data");
  1049. String orgId = orgPsn.getString("orgId");
  1050. orgName = orgPsn.getString("orgName");
  1051. //自动落章只能传orgId
  1052. EsignHttpResponse std = EsignHttpUtil.getSign_templates_detail(context, signTemplateId, orgId, true, sourceId);
  1053. if (std.getStatus() >= 200 && std.getStatus() < 300) {
  1054. JSONObject bodyStd = JSON.parseObject(std.getBody());
  1055. if ("0".equals(String.valueOf(bodyStd.get("code")))) {
  1056. JSONObject orgPsnStd = bodyStd.getJSONObject("data");
  1057. JSONArray parts = orgPsnStd.getJSONArray("participants");
  1058. if (null != parts) {
  1059. for (int i = 0; i < parts.size(); i++) {
  1060. JSONObject p = parts.getJSONObject(i);
  1061. JSONObject orgParticipant = p.getJSONObject("orgParticipant");
  1062. JSONObject psnParticipant = p.getJSONObject("psnParticipant");
  1063. JSONArray components = p.getJSONArray("components");
  1064. String participantFlag = p.getString("participantFlag");
  1065. JSONArray signs = signInfo.getJSONArray("signers");
  1066. for (int x = 0; x < signs.size(); x++) {
  1067. JSONObject sign = signs.getJSONObject(x);
  1068. JSONObject orgSignerInfo = sign.getJSONObject("orgSignerInfo");
  1069. if (null != orgSignerInfo) {
  1070. String signName = orgSignerInfo.getString("signName");
  1071. if (participantFlag.equals(signName)) {
  1072. orgParticipant = new JSONObject();
  1073. p.put("orgParticipant", orgParticipant);
  1074. for (int k = 0; k < components.size(); k++) {
  1075. JSONObject component = components.getJSONObject(k);
  1076. JSONObject normalSignField = component.getJSONObject("normalSignField");
  1077. if (null != normalSignField) {
  1078. JSONArray designatedSealIds = normalSignField.getJSONArray("designatedSealIds");
  1079. //添加印章
  1080. sealOwnerId=orgSignerInfo.getString("sealId");
  1081. if (StringUtils.isNotBlank(sealOwnerId)) {
  1082. for (int j = 0; j < designatedSealIds.size(); j++) {
  1083. JSONObject designated = designatedSealIds.getJSONObject(j);
  1084. String sealId = designated.getString("sealId");
  1085. String dorgId = designated.getString("orgId");
  1086. if (sealId.equals(sealOwnerId)) {
  1087. orgParticipant.put("sealOwnerId", dorgId);
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. //orgParticipant.put("orgId",orgId);
  1094. orgParticipant.put("orgName", orgSignerInfo.getString("orgName"));
  1095. JSONObject transactor = new JSONObject();
  1096. transactor.put("transactorName", orgSignerInfo.getString("psnName"));
  1097. transactor.put("transactorPsnAccount", orgSignerInfo.getString("psnAccount"));
  1098. orgParticipant.put("transactor", transactor);
  1099. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1100. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1101. }
  1102. } else {
  1103. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  1104. if (null != psnSignerInfo) {
  1105. String signName = psnSignerInfo.getString("signName");
  1106. if (participantFlag.equals(signName)) {
  1107. psnParticipant = new JSONObject();
  1108. p.put("psnParticipant", psnParticipant);
  1109. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1110. psnParticipant.put("psnAccount", psnSignerInfo.get("psnAccount"));
  1111. psnParticipant.put("psnName", psnSignerInfo.get("psnName"));
  1112. //orgParticipant.put("orgName",orgName);
  1113. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1114. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. participants.add(p);
  1120. }
  1121. }
  1122. JSONArray copiers = orgPsnStd.getJSONArray("copiers");
  1123. if (null != copiers) {
  1124. for (int i = 0; i < copiers.size(); i++) {
  1125. addCopiers.add((Map<String, Object>) copiers.get(i));
  1126. }
  1127. }
  1128. }
  1129. }
  1130. }
  1131. }
  1132. for (int i = 0; i < participants.size(); i++) {
  1133. JSONObject p = (JSONObject) participants.get(i);
  1134. p.remove("components");
  1135. }
  1136. JSONArray copiers = signInfo.getJSONArray("copiers");
  1137. for (int i = 0; i < copiers.size(); i++) {
  1138. JSONObject copierPsnInfo = (JSONObject) copiers.get(i);
  1139. JSONObject p = new JSONObject();
  1140. p.put("copierPsnInfo",copierPsnInfo);
  1141. addCopiers.add(p);
  1142. }
  1143. for (int i = 0; i < addCopiers.size(); i++) {
  1144. JSONObject copier = (JSONObject) addCopiers.get(i);
  1145. JSONObject p = (JSONObject) copier.get("copierPsnInfo");
  1146. p.remove("psnId");
  1147. p.remove("f7Data");
  1148. p.remove("signName");
  1149. }
  1150. participants_addCopiers.put("participants", participants);
  1151. participants_addCopiers.put("addCopiers", addCopiers);
  1152. return participants_addCopiers;
  1153. }
  1154. /**
  1155. * 签署流程配置项
  1156. *
  1157. * @param context
  1158. * @param signFlowTitle
  1159. * @param jsonObject
  1160. * @return
  1161. */
  1162. public Map<String, Object> addSignFlowConfig(Context context, String signFlowTitle, JSONObject jsonObject) {
  1163. Map<String, Object> signFlowConfig = Maps.newHashMap();
  1164. signFlowConfig.put("signFlowTitle", signFlowTitle);
  1165. Boolean autoStart = jsonObject.getBoolean("autoStart");
  1166. if (null != autoStart) {
  1167. signFlowConfig.put("autoStart", autoStart);
  1168. } else {
  1169. signFlowConfig.put("autoStart", true);
  1170. }
  1171. Boolean autoFinish = jsonObject.getBoolean("autoFinish");
  1172. if (null != autoFinish) {
  1173. signFlowConfig.put("autoFinish", autoFinish);
  1174. } else {
  1175. signFlowConfig.put("autoFinish", true);
  1176. }
  1177. String notifyUrl = jsonObject.getString("notifyUrl");
  1178. if (null != notifyUrl) {
  1179. signFlowConfig.put("notifyUrl", notifyUrl);
  1180. } else {
  1181. notifyUrl = EsignConfig.getInstance().get("notifyUrl");
  1182. signFlowConfig.put("notifyUrl", notifyUrl);
  1183. }
  1184. Map<String, Object> signConfig = Maps.newHashMap();
  1185. signConfig.put("availableSignClientTypes", "1,2");
  1186. signConfig.put("autoFillAndSubmit", "false");
  1187. signConfig.put("editComponentValue", "true");
  1188. Map<String, Object> noticeConfig = Maps.newHashMap();
  1189. noticeConfig.put("noticeTypes", "1,2");
  1190. noticeConfig.put("examineNotice", "true");
  1191. signFlowConfig.put("signConfig", signConfig);
  1192. signFlowConfig.put("noticeConfig", noticeConfig);
  1193. return signFlowConfig;
  1194. }
  1195. /**
  1196. * 根据传入数据调用 ”填写模板生成文件“ 接口
  1197. *
  1198. * @param jsonObject
  1199. * @return
  1200. */
  1201. public EsignHttpResponse previewFile(Context context, JSONObject jsonObject, String sourceId) throws EsignException {
  1202. String docTemplateId = jsonObject.getString("id");
  1203. String fileName = jsonObject.getString("name");
  1204. if (StringUtils.isBlank(docTemplateId)) {
  1205. docTemplateId = jsonObject.getString("tableId");
  1206. }
  1207. if (StringUtils.isBlank(fileName)) {
  1208. fileName = jsonObject.getString("tableName");
  1209. }
  1210. Map<String, Object> map = Maps.newHashMap();
  1211. map.put("docTemplateId", docTemplateId);
  1212. map.put("fileName", fileName);
  1213. List<Map<String, Object>> components = Lists.newArrayList();
  1214. JSONObject fields = jsonObject.getJSONObject("fields");
  1215. for (Map.Entry<String, Object> field : fields.entrySet()) {
  1216. Map<String, Object> component = Maps.newHashMap();
  1217. component.put("componentId", field.getKey());
  1218. JSONObject fieldInfo = (JSONObject) field.getValue();
  1219. String dataType = (String) fieldInfo.get("dataType");
  1220. if (ComponentTypeEnum.SIGN_AREA.getAlias().equals(dataType)) {
  1221. continue;
  1222. }
  1223. if (ComponentTypeEnum.DATESIGNATURE.getAlias().equals(dataType)) {
  1224. continue;
  1225. }
  1226. if (null == fieldInfo.get("value")) {
  1227. continue;
  1228. }
  1229. component.put("componentValue", fieldInfo.get("value"));
  1230. components.add(component);
  1231. }
  1232. map.put("components", components);
  1233. EsignHttpResponse response = EsignHttpUtil.createByDocTemplate(context, JSON.toJSONString(map), sourceId);
  1234. return response;
  1235. }
  1236. }