Utils.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. package com.jtgh.yjpt.common;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.PrintWriter;
  12. import java.io.StringWriter;
  13. import java.lang.reflect.Field;
  14. import java.lang.reflect.Modifier;
  15. import java.math.BigDecimal;
  16. import java.math.RoundingMode;
  17. import java.security.MessageDigest;
  18. import java.security.NoSuchAlgorithmException;
  19. import java.text.SimpleDateFormat;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.Locale;
  27. import java.util.ResourceBundle;
  28. import javax.imageio.ImageIO;
  29. import javax.persistence.criteria.CriteriaBuilder;
  30. import javax.persistence.criteria.CriteriaQuery;
  31. import javax.persistence.criteria.Predicate;
  32. import javax.persistence.criteria.Root;
  33. import javax.persistence.criteria.Subquery;
  34. import javax.servlet.http.HttpServletRequest;
  35. import javax.servlet.http.HttpSession;
  36. import org.springframework.jdbc.core.JdbcTemplate;
  37. import org.springframework.jdbc.support.rowset.SqlRowSet;
  38. import org.springframework.stereotype.Component;
  39. import org.springframework.util.StringUtils;
  40. import org.springframework.web.context.request.RequestContextHolder;
  41. import org.springframework.web.context.request.ServletRequestAttributes;
  42. import sun.misc.BASE64Decoder;
  43. import sun.misc.BASE64Encoder;
  44. import com.jtgh.qlyg.entity.ApplyFormItem;
  45. import com.jtgh.yjpt.common.servlet.InitServlet;
  46. import com.jtgh.yjpt.entity.BaseEntity;
  47. import com.jtgh.yjpt.entity.auth.UserEntity;
  48. import com.jtgh.yjpt.entity.common.AccessoryEntity;
  49. import com.jtgh.yjpt.entity.common.CodeEntity;
  50. import com.jtgh.yjpt.entity.common.TaskInfoEntity;
  51. import com.jtgh.yjpt.entity.qlyg.ApplyEntity;
  52. import com.jtgh.yjpt.entity.qlyg.ApplyProcessEntity;
  53. import com.jtgh.yjpt.entity.zysqbp.ZysqbpEntity;
  54. import com.jtgh.yjpt.webService.common.AuthHandler;
  55. import com.swetake.util.Qrcode;
  56. import flex.messaging.FlexContext;
  57. /**
  58. * 系统静态工具方法类。
  59. */
  60. @Component
  61. public abstract class Utils {
  62. /**
  63. * 比较密码是否一致
  64. *
  65. * @param pwd
  66. * @param md5Pwd
  67. * @return
  68. */
  69. public static boolean pwdEquals(String pwd, String md5Pwd) {
  70. if (pwd == null || md5Pwd == null)
  71. return false;
  72. return md5Pwd.equals(encrypt(pwd));
  73. }
  74. /**
  75. * 返回MD5加密后的密码
  76. *
  77. * @param pwd
  78. * @return
  79. */
  80. public static String encrypt(String password) {
  81. String str = "";
  82. try {
  83. MessageDigest md = MessageDigest.getInstance("MD5");
  84. md.update(password.getBytes());
  85. byte b[] = md.digest();
  86. int i;
  87. StringBuffer buf = new StringBuffer("");
  88. for (int offset = 0; offset < b.length; offset++) {
  89. i = b[offset];
  90. if (i < 0)
  91. i += 256;
  92. if (i < 16)
  93. buf.append("0");
  94. buf.append(Integer.toHexString(i));
  95. }
  96. str = buf.toString();
  97. } catch (NoSuchAlgorithmException e) {
  98. e.printStackTrace();
  99. }
  100. return str;
  101. }
  102. /**
  103. * 二进制流解析为字符串
  104. *
  105. * @param content
  106. * @return
  107. */
  108. public static String encodeBase64(byte[] content) {
  109. BASE64Encoder encoder = new BASE64Encoder();
  110. return encoder.encode(content);
  111. }
  112. /**
  113. * 字符串解析为二进制流
  114. *
  115. * @param content
  116. * @return
  117. */
  118. public static byte[] decodeBase64(String content) {
  119. BASE64Decoder decoder = new BASE64Decoder();
  120. try {
  121. return decoder.decodeBuffer(content);
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }
  125. return null;
  126. }
  127. /**
  128. * 获取资源文件
  129. *
  130. * @param bundle
  131. * 包名
  132. * @param key
  133. * @return
  134. */
  135. public static String getResource(String bundle, String key) {
  136. ResourceBundle resource = ResourceBundle.getBundle(Locale.getDefault()
  137. .toString() + "/" + bundle, Locale.getDefault(),
  138. Utils.class.getClassLoader());
  139. if (resource.containsKey(key))
  140. return resource.getString(key);
  141. return key;
  142. }
  143. /**
  144. * 服务器端判断客户端浏览器类型(IE/FF/SF)
  145. *
  146. * @param request
  147. * @return
  148. */
  149. public static String getBrowser(HttpServletRequest request) {
  150. String UserAgent = request.getHeader("USER-AGENT").toLowerCase();
  151. if (UserAgent != null) {
  152. if (UserAgent.indexOf("msie") >= 0)
  153. return "IE";
  154. if (UserAgent.indexOf("firefox") >= 0)
  155. return "FF";
  156. if (UserAgent.indexOf("safari") >= 0)
  157. return "SF";
  158. }
  159. return null;
  160. }
  161. /**
  162. * 根据开始日期和时间段,得到到期日
  163. *
  164. * @param startDate
  165. * @param during
  166. * @param type
  167. * 承诺时限单位(0天/1工作日/2月/3年)
  168. * @return
  169. */
  170. public static Date getDueDate(Date startDate, Integer during, String type) {
  171. if (startDate == null)
  172. return null;
  173. JdbcTemplate jdbcTemplate = (JdbcTemplate) ApplicationContextHelper
  174. .getBean(JdbcTemplate.class);
  175. if (during == null)
  176. during = 0;
  177. Calendar c = Calendar.getInstance();
  178. c.setTime(startDate);
  179. if (Constants.QLYG_APPLY_PROMISE_TYPE_DAY.equals(type)) {
  180. // 按天计算
  181. c.add(Calendar.DATE, during);
  182. } else if (Constants.QLYG_APPLY_PROMISE_TYPE_WORKING_DAY.equals(type)) {
  183. // 按工作日计算
  184. String sql = "SELECT 1 FROM T_YJPT_WORKING_CALENDAR WHERE WORKING_DATE=? AND IS_WORKING=?";
  185. SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
  186. while (during > 0) {
  187. String date = sf.format(c.getTime());
  188. if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
  189. || c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
  190. SqlRowSet rs = jdbcTemplate.queryForRowSet(sql, date,
  191. Constants.YES);
  192. if (rs.next()) {
  193. during--;
  194. }
  195. c.add(Calendar.DATE, 1);
  196. } else {
  197. SqlRowSet rs = jdbcTemplate.queryForRowSet(sql, date,
  198. Constants.NO);
  199. if (!rs.next()) {
  200. during--;
  201. }
  202. c.add(Calendar.DATE, 1);
  203. }
  204. }
  205. } else if (Constants.QLYG_APPLY_PROMISE_TYPE_MONTH.equals(type)) {
  206. // 按月计算
  207. c.add(Calendar.MONTH, during);
  208. } else if (Constants.QLYG_APPLY_PROMISE_TYPE_YEAR.equals(type)) {
  209. // 按年计算
  210. c.add(Calendar.YEAR, during);
  211. }
  212. return c.getTime();
  213. }
  214. /**
  215. * 根据开始日期和时间段,得到预警日期4/5
  216. *
  217. * @param startDate
  218. * @param during
  219. * @param type
  220. * 承诺时限单位(0天/1工作日/2月/3年)
  221. * @return
  222. */
  223. public static Date getWarnDate(Date startDate, Integer during, String type) {
  224. if (startDate == null)
  225. return null;
  226. if (during == null)
  227. during = 0;
  228. during = BigDecimal.valueOf(during * 0.8)
  229. .setScale(0, RoundingMode.CEILING).intValue();
  230. return getDueDate(startDate, during, type);
  231. }
  232. /**
  233. * 根据配置文件设置办件信息
  234. *
  235. * @param entity
  236. * @param processKey
  237. */
  238. public static void setApplyProperties(ApplyEntity entity, String processKey) {
  239. HashMap<String, Object> applyMap = GlobalData.qlygApplyMap
  240. .get(processKey);
  241. if (applyMap == null)
  242. return;
  243. Iterator<String> it = applyMap.keySet().iterator();
  244. while (it.hasNext()) {
  245. String key = it.next();
  246. try {
  247. ApplyEntity.class.getMethod(
  248. "set" + key.toUpperCase().substring(0, 1)
  249. + key.substring(1),
  250. applyMap.get(key).getClass()).invoke(entity,
  251. applyMap.get(key));
  252. } catch (Exception e) {
  253. e.printStackTrace();
  254. }
  255. }
  256. String orgId = Utils.getCurrentUser().getSzd().getByzd4();
  257. // 部门编码
  258. entity.setOrgId(orgId + "JT");
  259. // 权力编码前面10位的部门编号是所在地的备用字段4
  260. if (!StringUtils.isEmpty(entity.getItemId())) {
  261. entity.setItemId(orgId + "JT-" + entity.getItemId());
  262. }
  263. // 办理处室使用所在地的备用字段5
  264. entity.setDepartment(getCurrentUser().getSzd().getByzd5());
  265. }
  266. /**
  267. * 根据配置文件设置办件过程信息
  268. *
  269. * @param entity
  270. * @param processKey
  271. */
  272. public static void setApplyItemProperties(ApplyProcessEntity entity,
  273. String processKey, String taskKey) {
  274. HashMap<String, HashMap<String, Object>> applyMap = GlobalData.qlygApplyItemMap
  275. .get(processKey);
  276. if (applyMap == null)
  277. return;
  278. HashMap<String, Object> applyItemMap = applyMap.get(taskKey);
  279. if (applyItemMap == null)
  280. return;
  281. Iterator<String> it = applyItemMap.keySet().iterator();
  282. while (it.hasNext()) {
  283. String key = it.next();
  284. try {
  285. ApplyProcessEntity.class.getMethod(
  286. "set" + key.toUpperCase().substring(0, 1)
  287. + key.substring(1),
  288. applyItemMap.get(key).getClass()).invoke(entity,
  289. applyItemMap.get(key));
  290. } catch (Exception e) {
  291. e.printStackTrace();
  292. }
  293. }
  294. }
  295. /**
  296. * 根据配置文件生成办件基本信息form
  297. *
  298. * @param entity
  299. * @param processKey
  300. * @return
  301. */
  302. public static String getApplyForm(BaseEntity<?> entity, String processKey) {
  303. StringBuffer sb = new StringBuffer();
  304. sb.append("<?XML VERSION=\"1.0\" ENCODING=\"GBK\"?><FORMDATA>");
  305. if (processKey != null
  306. && GlobalData.qlygApplyFormMap.get(processKey) != null) {
  307. List<ApplyFormItem> list = (List<ApplyFormItem>) GlobalData.qlygApplyFormMap
  308. .get(processKey);
  309. if (list != null && list.size() > 0) {
  310. for (ApplyFormItem formItem : list) {
  311. sb.append("<DATA>");
  312. sb.append("<KEY>");
  313. sb.append(formItem.getKey());
  314. sb.append("</KEY>");
  315. sb.append("<NAME>");
  316. sb.append(formItem.getName());
  317. sb.append("</NAME>");
  318. sb.append("<VALUE>");
  319. try {
  320. if (formItem.getId() != null) {
  321. Object parentObj = entity;
  322. String[] props = formItem.getId().split("\\.");
  323. for (String prop : props) {
  324. Object obj = parentObj
  325. .getClass()
  326. .getMethod(
  327. "get"
  328. + prop.toUpperCase()
  329. .substring(0, 1)
  330. + prop.substring(1))
  331. .invoke(parentObj);
  332. parentObj = obj;
  333. if (parentObj == null) {
  334. break;
  335. }
  336. }
  337. if (parentObj == null) {
  338. // 空值
  339. } else if ("date".equals(formItem.getType())) {
  340. Date d = (Date) parentObj;
  341. sb.append(new SimpleDateFormat(
  342. "yyyy-MM-dd HH:mm:ss").format(d));
  343. } else {
  344. if (!StringUtils.isEmpty(formItem.getLocale())) {
  345. sb.append(getResource(formItem.getLocale(),
  346. parentObj.toString()));
  347. } else {
  348. sb.append(parentObj.toString());
  349. }
  350. }
  351. }
  352. } catch (Exception e) {
  353. e.printStackTrace();
  354. }
  355. sb.append("</VALUE>");
  356. sb.append("</DATA>");
  357. }
  358. }
  359. }
  360. sb.append("</FORMDATA>");
  361. return sb.toString();
  362. }
  363. /**
  364. * 设置对象属性
  365. *
  366. * @param obj
  367. * @param params
  368. * @param name
  369. * @param clazz
  370. */
  371. public static void setField(Object obj, Object params, String name,
  372. Class<?> clazz) {
  373. try {
  374. Field fld = clazz.getDeclaredField(name);
  375. int mod = fld.getModifiers();
  376. if (null == obj && Modifier.isStatic(mod)) {
  377. if (!Modifier.isFinal(mod)) {
  378. if (!fld.isAccessible())
  379. fld.setAccessible(true);
  380. fld.set(null, params);
  381. }
  382. } else {
  383. if (!fld.isAccessible())
  384. fld.setAccessible(true);
  385. fld.set(obj, params);
  386. }
  387. } catch (SecurityException e) {
  388. e.printStackTrace();
  389. } catch (NoSuchFieldException e) {
  390. e.printStackTrace();
  391. } catch (IllegalArgumentException e) {
  392. e.printStackTrace();
  393. } catch (IllegalAccessException e) {
  394. e.printStackTrace();
  395. }
  396. }
  397. /**
  398. * 获取session
  399. *
  400. * @return
  401. */
  402. public static HttpSession getSession() {
  403. if (FlexContext.getHttpRequest() != null) {
  404. HttpSession session = FlexContext.getHttpRequest().getSession();
  405. // System.out.println("Flex session id:" + session.getId());
  406. // flex客户端取session
  407. return session;
  408. }
  409. if (RequestContextHolder.getRequestAttributes() != null) {
  410. HttpSession session = ((ServletRequestAttributes) RequestContextHolder
  411. .getRequestAttributes()).getRequest().getSession();
  412. // System.out.println("Http session id:" + session.getId());
  413. return session;
  414. }
  415. return null;
  416. }
  417. public static Long JunitUserId = 1l;
  418. /**
  419. * 获取当前登录人
  420. *
  421. * @return
  422. */
  423. public static UserEntity getCurrentUser() {
  424. if (GlobalData.isJunit) {
  425. UserEntity testUser = new UserEntity();
  426. testUser.setId(JunitUserId);
  427. testUser.setCode("user_junit");
  428. testUser.setName("junit用户");
  429. return testUser;
  430. }
  431. HttpSession session = getSession();
  432. if (session == null) {
  433. if (AuthHandler.getCurrentUser() != null) {
  434. return AuthHandler.getCurrentUser();
  435. }
  436. return null;
  437. }
  438. Object user = session.getAttribute(GlobalData.USER_SESSION_KEY);
  439. return (UserEntity) user;
  440. }
  441. /**
  442. * WebService登录用户
  443. *
  444. * @return
  445. */
  446. public static UserEntity getWebServiceUser() {
  447. if (AuthHandler.getCurrentUser() != null) {
  448. return AuthHandler.getCurrentUser();
  449. }
  450. return null;
  451. }
  452. /**
  453. * 获取当前登录人姓名
  454. *
  455. * @return
  456. */
  457. public static String getCurrentUserCode() {
  458. HttpSession session = getSession();
  459. if (session != null) {
  460. Object user = session.getAttribute(GlobalData.USER_SESSION_KEY);
  461. if (user != null)
  462. return ((UserEntity) user).getCode();
  463. }
  464. return "";
  465. }
  466. /**
  467. * 获取当前登录人姓名
  468. *
  469. * @return
  470. */
  471. public static String getCurrentUserName() {
  472. HttpSession session = getSession();
  473. if (session != null) {
  474. Object user = session.getAttribute(GlobalData.USER_SESSION_KEY);
  475. if (user != null)
  476. return ((UserEntity) user).getName();
  477. }
  478. return "";
  479. }
  480. /**
  481. * 根据所属区域代码获得webservice的ip
  482. *
  483. * @param ssqyId
  484. * @return
  485. */
  486. public static String getWsIpBySsqy(Long ssqyId) {
  487. if (ssqyId == null)
  488. return "";
  489. String wsIp = "";
  490. if (InitServlet.groupcodeMap.get(Constants.GROUP_CODE_SZD_WS_IP) != null) {
  491. List<CodeEntity> ips = InitServlet.groupcodeMap
  492. .get(Constants.GROUP_CODE_SZD_WS_IP);
  493. for (CodeEntity ip : ips) {
  494. if (ip.getParent() != null
  495. && ssqyId.toString().indexOf(
  496. ip.getParent().getId().toString()) == 0) {
  497. wsIp = ip.getCode();
  498. break;
  499. }
  500. }
  501. }
  502. return wsIp;
  503. }
  504. /**
  505. * 根据所属区域代码获得webservice的port
  506. *
  507. * @param ssqyId
  508. * @return
  509. */
  510. public static String getWsPortBySsqy(Long ssqyId) {
  511. if (ssqyId == null)
  512. return "";
  513. String wsPort = "";
  514. if (InitServlet.groupcodeMap.get(Constants.GROUP_CODE_SZD_WS_PORT) != null) {
  515. List<CodeEntity> ports = InitServlet.groupcodeMap
  516. .get(Constants.GROUP_CODE_SZD_WS_PORT);
  517. for (CodeEntity port : ports) {
  518. if (port.getParent() != null
  519. && ssqyId.toString().indexOf(
  520. port.getParent().getId().toString()) == 0) {
  521. wsPort = port.getCode();
  522. break;
  523. }
  524. }
  525. }
  526. return wsPort;
  527. }
  528. /**
  529. * 返回当前角色的上级角色
  530. *
  531. * @param currRole
  532. * @return
  533. */
  534. public static String getParentRole(String currRole) {
  535. if (StringUtils.isEmpty(currRole)
  536. || currRole.length() <= Constants.ROLE_LENGTH) {
  537. return "";
  538. }
  539. return currRole.substring(0, currRole.length() - Constants.ROLE_LENGTH);
  540. }
  541. public static List<Predicate> setWorkflowSpec(List<Predicate> list,
  542. CriteriaBuilder cb, CriteriaQuery<?> query,
  543. Root<? extends BaseEntity<?>> root, String lcStatus) {
  544. // 根据流程状态过滤
  545. if (null != lcStatus && !"".equals(lcStatus)) {
  546. if (String.valueOf(BaseEntity.RECORD_STATE_VALID).equals(lcStatus)
  547. || String.valueOf(BaseEntity.RECORD_STATE_COMPLETED)
  548. .equals(lcStatus)) {
  549. list.add(cb.equal(root.get("recordStatus").as(String.class),
  550. lcStatus));
  551. } else {
  552. Subquery<TaskInfoEntity> subquery = query
  553. .subquery(TaskInfoEntity.class);
  554. Root<TaskInfoEntity> taskRoot = subquery
  555. .from(TaskInfoEntity.class);
  556. subquery.select(taskRoot);
  557. List<Predicate> subQueryPredicates = new ArrayList<Predicate>();
  558. subQueryPredicates.add(cb.equal(
  559. root.get("id").as(String.class), taskRoot.get("busId")
  560. .as(String.class)));
  561. subQueryPredicates.add(cb.equal(
  562. taskRoot.get("currName").as(String.class), lcStatus));
  563. subQueryPredicates.add(cb.notEqual(
  564. taskRoot.get("state").as(Long.class),
  565. BaseEntity.RECORD_STATE_COMPLETED));
  566. // List<Predicate> subOrList = new
  567. // ArrayList<Predicate>();
  568. // subOrList.add(cb.equal(taskRoot
  569. // .get("roleCode").as(String.class),
  570. // getCurrentRoleCode(functionId)));
  571. // subOrList.add(cb.equal(taskRoot
  572. // .get("runner").as(UserEntity.class),
  573. // Utils.getCurrentUser()));
  574. // subQueryPredicates.add(cb.or(subOrList.toArray(new
  575. // Predicate[] {})));
  576. subquery.where(subQueryPredicates.toArray(new Predicate[] {}));
  577. list.add(cb.exists(subquery));
  578. }
  579. }
  580. return list;
  581. }
  582. public static List<Predicate> setAuditNameSpec(List<Predicate> list,
  583. CriteriaBuilder cb, CriteriaQuery<?> query,
  584. Root<? extends BaseEntity<?>> root, String lcStatus, String name) {
  585. if(null != name && !"".equals(name)) {
  586. Subquery<TaskInfoEntity> subquery = query
  587. .subquery(TaskInfoEntity.class);
  588. Root<TaskInfoEntity> taskRoot = subquery.from(TaskInfoEntity.class);
  589. subquery.select(taskRoot);
  590. List<Predicate> subQueryPredicates = new ArrayList<Predicate>();
  591. subQueryPredicates.add(cb.equal(root.get("id").as(String.class),
  592. taskRoot.get("busId").as(String.class)));
  593. subQueryPredicates.add(cb.or(new Predicate[] {
  594. cb.equal(taskRoot.get("busKey").as(String.class),
  595. ZysqbpEntity.AUTO_PROCESS_DEFINITION_KEY),
  596. cb.equal(taskRoot.get("busKey").as(String.class),
  597. ZysqbpEntity.PROCESS_DEFINITION_KEY) }));
  598. subQueryPredicates.add(cb.like(
  599. taskRoot.get("auditUsername").as(String.class), "%" + name
  600. + "%"));
  601. if (null != lcStatus && !"".equals(lcStatus)) {
  602. if (String.valueOf(BaseEntity.RECORD_STATE_VALID).equals(
  603. lcStatus)
  604. || String.valueOf(BaseEntity.RECORD_STATE_COMPLETED)
  605. .equals(lcStatus)) {
  606. list.add(cb
  607. .equal(root.get("recordStatus").as(String.class),
  608. lcStatus));
  609. } else {
  610. subQueryPredicates.add(cb.notEqual(
  611. taskRoot.get("state").as(Long.class),
  612. BaseEntity.RECORD_STATE_COMPLETED));
  613. }
  614. }
  615. subquery.where(subQueryPredicates.toArray(new Predicate[] {}));
  616. list.add(cb.exists(subquery));
  617. } else {
  618. // 根据流程状态过滤
  619. if (null != lcStatus && !"".equals(lcStatus)) {
  620. if (String.valueOf(BaseEntity.RECORD_STATE_VALID).equals(lcStatus)
  621. || String.valueOf(BaseEntity.RECORD_STATE_COMPLETED)
  622. .equals(lcStatus)) {
  623. list.add(cb.equal(root.get("recordStatus").as(String.class),
  624. lcStatus));
  625. } else {
  626. Subquery<TaskInfoEntity> subquery = query
  627. .subquery(TaskInfoEntity.class);
  628. Root<TaskInfoEntity> taskRoot = subquery
  629. .from(TaskInfoEntity.class);
  630. subquery.select(taskRoot);
  631. List<Predicate> subQueryPredicates = new ArrayList<Predicate>();
  632. subQueryPredicates.add(cb.equal(
  633. root.get("id").as(String.class), taskRoot.get("busId")
  634. .as(String.class)));
  635. subQueryPredicates.add(cb.equal(
  636. taskRoot.get("currName").as(String.class), lcStatus));
  637. subQueryPredicates.add(cb.notEqual(
  638. taskRoot.get("state").as(Long.class),
  639. BaseEntity.RECORD_STATE_COMPLETED));
  640. subquery.where(subQueryPredicates.toArray(new Predicate[] {}));
  641. list.add(cb.exists(subquery));
  642. }
  643. }
  644. }
  645. return list;
  646. }
  647. /**
  648. * 将传入的附件拼成固定格式的String返回
  649. */
  650. public static String getWord(List<AccessoryEntity> list) {
  651. StringBuffer sbf = new StringBuffer();
  652. String str = "<?XML VERSION=\"1.0\" ENCODING=\"GBK\"?><DOCUMENTDATA>";
  653. sbf.append(str);
  654. if (list != null && list.size() > 0) {
  655. for (AccessoryEntity accessoryEntity : list) {
  656. sbf.append("<DOCUMENT>");
  657. sbf.append("<DOCUMENT_ID>").append(accessoryEntity.getId())
  658. .append("</DOCUMENT_ID>");
  659. sbf.append("<DOCUMENT_NAME>")
  660. .append(getResource("fileType", accessoryEntity
  661. .getType().getName()))
  662. .append("</DOCUMENT_NAME>");
  663. sbf.append("<FILE_NAME>").append(accessoryEntity.getName())
  664. .append("</FILE_NAME>");
  665. sbf.append("<FILE_CONTENT>")
  666. .append(encodeBase64(accessoryEntity.getContent()
  667. .getValue())).append("</FILE_CONTENT>");
  668. sbf.append("</DOCUMENT>");
  669. }
  670. }
  671. sbf.append("</DOCUMENTDATA>");
  672. return sbf.toString();
  673. }
  674. /**
  675. * 该天最早的时候
  676. *
  677. * @param date
  678. * @return
  679. */
  680. public static Date getDateFirstTime(Date date) {
  681. if (null == date) {
  682. return null;
  683. }
  684. Calendar c = Calendar.getInstance();
  685. c.setTime(date);
  686. c.set(java.util.Calendar.HOUR_OF_DAY, 0);
  687. c.set(java.util.Calendar.MINUTE, 0);
  688. c.set(java.util.Calendar.SECOND, 0);
  689. return c.getTime();
  690. }
  691. /**
  692. * 该天最迟的时候
  693. *
  694. * @param date
  695. * @return
  696. */
  697. public static Date getDateLastTime(Date date) {
  698. if (null == date) {
  699. return null;
  700. }
  701. Calendar c = Calendar.getInstance();
  702. c.setTime(date);
  703. c.set(java.util.Calendar.HOUR_OF_DAY, 23);
  704. c.set(java.util.Calendar.MINUTE, 59);
  705. c.set(java.util.Calendar.SECOND, 59);
  706. return c.getTime();
  707. }
  708. public static String getExeMsg(Throwable t) {
  709. StringWriter sw = new StringWriter();
  710. t.printStackTrace(new PrintWriter(sw));
  711. return sw.toString();
  712. }
  713. public static byte[] getQrcodeImage(int length,int width, String content){
  714. BufferedImage img = new BufferedImage(length, width,
  715. BufferedImage.TYPE_INT_RGB);
  716. for (int i = 0; i < img.getWidth(); i++) {
  717. for (int j = 0; j < img.getHeight(); j++) {
  718. img.setRGB(i, j, 0xffffff);
  719. }
  720. }
  721. Graphics2D g = (Graphics2D) img.getGraphics();
  722. g.rotate(Math.PI / 2, length / 2, length / 2);
  723. g.setColor(new Color(0, 0, 0));
  724. g.setFont(new Font("宋体", Font.PLAIN, 14));
  725. // 二维码
  726. try {
  727. // 二维码版本
  728. int version = 7;
  729. // 图片尺寸
  730. int imgSize = 67 + 12 * (version - 1);
  731. BufferedImage qrImg = new BufferedImage(imgSize, imgSize,
  732. BufferedImage.TYPE_INT_RGB);
  733. Graphics2D gs = qrImg.createGraphics();
  734. gs.setBackground(Color.WHITE);
  735. gs.clearRect(0, 0, imgSize, imgSize);
  736. Qrcode qrcodeHandler = new Qrcode();
  737. // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
  738. qrcodeHandler.setQrcodeErrorCorrect('M');
  739. // N代表数字,A代表字符a-Z,B代表其他字符
  740. qrcodeHandler.setQrcodeEncodeMode('B');
  741. // 设置二维码版本,取值范围1-40,值越大尺寸越大,可存储的信息越大
  742. qrcodeHandler.setQrcodeVersion(version);
  743. byte[] contentBytes = content.getBytes("gb2312");
  744. gs.setColor(Color.BLACK);
  745. // 设置偏移量 不设置可能导致解析出错
  746. int pixoff = 2;
  747. // 输出内容> 二维码
  748. if (contentBytes.length > 0 && contentBytes.length < 120) {
  749. boolean[][] codeOut = qrcodeHandler
  750. .calQrcode(contentBytes);
  751. for (int i = 0; i < codeOut.length; i++) {
  752. for (int j = 0; j < codeOut.length; j++) {
  753. if (codeOut[j][i]) {
  754. gs.fillRect(j * 3 + pixoff, i * 3 + pixoff,
  755. 3, 3);
  756. }
  757. }
  758. }
  759. } else {
  760. System.err.println("QRCode content bytes length = "
  761. + contentBytes.length + " not in [ 0,120 ]. ");
  762. }
  763. // 中间logo
  764. InputStream imgIn = new FileInputStream(Utils.getSession()
  765. .getServletContext().getRealPath("/")
  766. + "resource/yjpt/login/login_logo.png");
  767. Image logo = ImageIO.read(imgIn);
  768. int widthLogo = logo.getWidth(null) > qrImg.getWidth() * 2 / 10 ? (qrImg
  769. .getWidth() * 2 / 10) : logo.getWidth(null);
  770. int heightLogo = logo.getHeight(null) > qrImg.getHeight() * 2 / 10 ? (qrImg
  771. .getHeight() * 2 / 10) : logo.getWidth(null);
  772. int x = (qrImg.getWidth() - widthLogo) / 2;
  773. int y = (qrImg.getHeight() - heightLogo) / 2;
  774. gs.drawImage(logo, x, y, widthLogo, heightLogo, null);
  775. gs.dispose();
  776. qrImg.flush();
  777. g.drawImage(qrImg.getScaledInstance(100, 100,
  778. Image.SCALE_DEFAULT), 6, 6, null);
  779. } catch (Exception e) {
  780. e.printStackTrace();
  781. }
  782. g.dispose();
  783. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  784. try {
  785. ImageIO.write(img, "PNG", bos);
  786. return bos.toByteArray();
  787. } catch(Exception e){
  788. e.printStackTrace();
  789. }
  790. return null;
  791. }
  792. }