Create_by_fileOSFService.java 65 KB

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