Create_by_fileOSFService.java 66 KB

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