Create_by_fileOSFService.java 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. if (StringUtils.isNotBlank(sealOwnerId)) {
  1081. for (int j = 0; j < designatedSealIds.size(); j++) {
  1082. JSONObject designated = designatedSealIds.getJSONObject(j);
  1083. String sealId = designated.getString("sealId");
  1084. String dorgId = designated.getString("orgId");
  1085. if (sealId.equals(sealOwnerId)) {
  1086. orgParticipant.put("sealOwnerId", dorgId);
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. //orgParticipant.put("orgId",orgId);
  1093. orgParticipant.put("orgName", orgSignerInfo.getString("orgName"));
  1094. JSONObject transactor = new JSONObject();
  1095. transactor.put("transactorName", orgSignerInfo.getString("psnName"));
  1096. transactor.put("transactorPsnAccount", orgSignerInfo.getString("psnAccount"));
  1097. orgParticipant.put("transactor", transactor);
  1098. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1099. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1100. }
  1101. } else {
  1102. JSONObject psnSignerInfo = sign.getJSONObject("psnSignerInfo");
  1103. if (null != psnSignerInfo) {
  1104. String signName = psnSignerInfo.getString("signName");
  1105. if (participantFlag.equals(signName)) {
  1106. psnParticipant = new JSONObject();
  1107. p.put("psnParticipant", psnParticipant);
  1108. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1109. psnParticipant.put("psnAccount", psnSignerInfo.get("psnAccount"));
  1110. psnParticipant.put("psnName", psnSignerInfo.get("psnName"));
  1111. //orgParticipant.put("orgName",orgName);
  1112. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1113. //orgParticipant.put("sealOwnerId",sealOwnerId);
  1114. }
  1115. }
  1116. }
  1117. }
  1118. participants.add(p);
  1119. }
  1120. }
  1121. JSONArray copiers = orgPsnStd.getJSONArray("copiers");
  1122. if (null != copiers) {
  1123. for (int i = 0; i < copiers.size(); i++) {
  1124. addCopiers.add((Map<String, Object>) copiers.get(i));
  1125. }
  1126. }
  1127. }
  1128. }
  1129. }
  1130. }
  1131. for (int i = 0; i < participants.size(); i++) {
  1132. JSONObject p = (JSONObject) participants.get(i);
  1133. p.remove("components");
  1134. }
  1135. for (int i = 0; i < addCopiers.size(); i++) {
  1136. JSONObject copier = (JSONObject) addCopiers.get(i);
  1137. JSONObject p = (JSONObject) copier.get("copierPsnInfo");
  1138. p.remove("psnId");
  1139. }
  1140. participants_addCopiers.put("participants", participants);
  1141. participants_addCopiers.put("addCopiers", addCopiers);
  1142. return participants_addCopiers;
  1143. }
  1144. /**
  1145. * 签署流程配置项
  1146. *
  1147. * @param context
  1148. * @param signFlowTitle
  1149. * @param jsonObject
  1150. * @return
  1151. */
  1152. public Map<String, Object> addSignFlowConfig(Context context, String signFlowTitle, JSONObject jsonObject) {
  1153. Map<String, Object> signFlowConfig = Maps.newHashMap();
  1154. signFlowConfig.put("signFlowTitle", signFlowTitle);
  1155. Boolean autoStart = jsonObject.getBoolean("autoStart");
  1156. if (null != autoStart) {
  1157. signFlowConfig.put("autoStart", autoStart);
  1158. } else {
  1159. signFlowConfig.put("autoStart", true);
  1160. }
  1161. Boolean autoFinish = jsonObject.getBoolean("autoFinish");
  1162. if (null != autoFinish) {
  1163. signFlowConfig.put("autoFinish", autoFinish);
  1164. } else {
  1165. signFlowConfig.put("autoFinish", true);
  1166. }
  1167. String notifyUrl = jsonObject.getString("notifyUrl");
  1168. if (null != notifyUrl) {
  1169. signFlowConfig.put("notifyUrl", notifyUrl);
  1170. } else {
  1171. notifyUrl = EsignConfig.getInstance().get("notifyUrl");
  1172. signFlowConfig.put("notifyUrl", notifyUrl);
  1173. }
  1174. Map<String, Object> signConfig = Maps.newHashMap();
  1175. signConfig.put("availableSignClientTypes", "1,2");
  1176. signConfig.put("autoFillAndSubmit", "false");
  1177. signConfig.put("editComponentValue", "true");
  1178. Map<String, Object> noticeConfig = Maps.newHashMap();
  1179. noticeConfig.put("noticeTypes", "1,2");
  1180. noticeConfig.put("examineNotice", "true");
  1181. signFlowConfig.put("signConfig", signConfig);
  1182. signFlowConfig.put("noticeConfig", noticeConfig);
  1183. return signFlowConfig;
  1184. }
  1185. /**
  1186. * 根据传入数据调用 ”填写模板生成文件“ 接口
  1187. *
  1188. * @param jsonObject
  1189. * @return
  1190. */
  1191. public EsignHttpResponse previewFile(Context context, JSONObject jsonObject, String sourceId) throws EsignException {
  1192. String docTemplateId = jsonObject.getString("id");
  1193. String fileName = jsonObject.getString("name");
  1194. if (StringUtils.isBlank(docTemplateId)) {
  1195. docTemplateId = jsonObject.getString("tableId");
  1196. }
  1197. if (StringUtils.isBlank(fileName)) {
  1198. fileName = jsonObject.getString("tableName");
  1199. }
  1200. Map<String, Object> map = Maps.newHashMap();
  1201. map.put("docTemplateId", docTemplateId);
  1202. map.put("fileName", fileName);
  1203. List<Map<String, Object>> components = Lists.newArrayList();
  1204. JSONObject fields = jsonObject.getJSONObject("fields");
  1205. for (Map.Entry<String, Object> field : fields.entrySet()) {
  1206. Map<String, Object> component = Maps.newHashMap();
  1207. component.put("componentId", field.getKey());
  1208. JSONObject fieldInfo = (JSONObject) field.getValue();
  1209. String dataType = (String) fieldInfo.get("dataType");
  1210. if (ComponentTypeEnum.SIGN_AREA.getAlias().equals(dataType)) {
  1211. continue;
  1212. }
  1213. if (ComponentTypeEnum.DATESIGNATURE.getAlias().equals(dataType)) {
  1214. continue;
  1215. }
  1216. if (null == fieldInfo.get("value")) {
  1217. continue;
  1218. }
  1219. component.put("componentValue", fieldInfo.get("value"));
  1220. components.add(component);
  1221. }
  1222. map.put("components", components);
  1223. EsignHttpResponse response = EsignHttpUtil.createByDocTemplate(context, JSON.toJSONString(map), sourceId);
  1224. return response;
  1225. }
  1226. }