Utils.java 25 KB

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