BaseDao.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. package com.uas.eis.dao;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.SQLException;
  5. import java.sql.Timestamp;
  6. import java.util.*;
  7. import com.uas.eis.entity.Configs;
  8. import com.uas.eis.entity.Employee;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.dao.DataAccessException;
  11. import org.springframework.dao.EmptyResultDataAccessException;
  12. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  13. import org.springframework.jdbc.core.CallableStatementCallback;
  14. import org.springframework.jdbc.core.CallableStatementCreator;
  15. import org.springframework.jdbc.core.JdbcTemplate;
  16. import org.springframework.stereotype.Repository;
  17. import com.uas.eis.utils.BaseUtil;
  18. import com.uas.eis.utils.Constant;
  19. import com.uas.eis.utils.DateUtil;
  20. import net.sf.json.JSONObject;
  21. @Repository
  22. public class BaseDao{
  23. static final String CREATE_SEQ = "CREATE SEQUENCE ?" + // 创建序列
  24. " MINVALUE 1 MAXVALUE 99999999999 INCREMENT BY 1 START WITH 3000 CACHE 20 NOORDER NOCYCLE ";
  25. @Autowired
  26. private JdbcTemplate jdbcTemplate;
  27. public JdbcTemplate getJdbcTemplate() {
  28. return jdbcTemplate;
  29. }
  30. public List<Map<String, Object>> queryForList(String sql) {
  31. try {
  32. return jdbcTemplate.queryForList(sql);
  33. } catch (EmptyResultDataAccessException e) {
  34. return null;
  35. }
  36. }
  37. public <T> List<T> queryForList(String sql, Class<T> elementType) {
  38. try {
  39. return jdbcTemplate.queryForList(sql, elementType);
  40. } catch (EmptyResultDataAccessException e) {
  41. return null;
  42. }
  43. }
  44. public List<Map<String, Object>> queryForList(String sql, Object... args) {
  45. try {
  46. return jdbcTemplate.queryForList(sql, args);
  47. } catch (EmptyResultDataAccessException e) {
  48. return null;
  49. }
  50. }
  51. public <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType) {
  52. try {
  53. return jdbcTemplate.queryForList(sql, args, elementType);
  54. } catch (EmptyResultDataAccessException e) {
  55. return null;
  56. }
  57. }
  58. public <T> List<T> queryForList(String sql, Class<T> elementType, Object... args) {
  59. try {
  60. return jdbcTemplate.queryForList(sql, elementType, args);
  61. } catch (EmptyResultDataAccessException e) {
  62. return null;
  63. }
  64. }
  65. public <T> List<T> query(String sql, Object[] args, Class<T> elementType) {
  66. try {
  67. return jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<T>(elementType));
  68. } catch (EmptyResultDataAccessException e) {
  69. return null;
  70. }
  71. }
  72. public <T> List<T> query(String sql, Class<T> elementType) {
  73. try {
  74. return jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(elementType));
  75. } catch (EmptyResultDataAccessException e) {
  76. return null;
  77. }
  78. }
  79. public <T> List<T> query(String sql, Class<T> elementType, Object... args) {
  80. try {
  81. return jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(elementType), args);
  82. } catch (EmptyResultDataAccessException e) {
  83. return null;
  84. }
  85. }
  86. public <T> T queryForObject(String sql, Class<T> elementType, Object... args) {
  87. try {
  88. return jdbcTemplate.queryForObject(sql, elementType, args);
  89. } catch (EmptyResultDataAccessException e) {
  90. return null;
  91. }
  92. }
  93. public void execute(String sql) {
  94. jdbcTemplate.execute(sql);
  95. }
  96. public synchronized boolean execute(String sql, Object... objs) {
  97. try {
  98. jdbcTemplate.update(sql, objs);
  99. return true;
  100. } catch (Exception e) {
  101. return false;
  102. }
  103. }
  104. public void execute(List<String> sqls) {
  105. if (sqls.size() > 0) {
  106. StringBuffer sb = new StringBuffer("begin ");
  107. for (String sql : sqls) {
  108. sb.append("execute immediate '").append(sql.replace("'", "''")).append("';");
  109. }
  110. sb.append("end;");
  111. jdbcTemplate.execute(sb.toString());
  112. }
  113. }
  114. /**
  115. * 批量执行Sql
  116. *
  117. * @param sqls
  118. * @param callbackSqls
  119. */
  120. public void batchExecute(List<SqlMap> sqls, List<String> callbackSqls) {
  121. if (sqls.size() > 0) {
  122. StringBuffer sb = new StringBuffer("begin ");
  123. for (SqlMap sql : sqls) {
  124. sb.append("execute immediate '").append(sql.getSql(false).replace("'", "''")).append("';");
  125. }
  126. for (String sql : callbackSqls) {
  127. sb.append("execute immediate '").append(sql.replace("'", "''")).append("';");
  128. }
  129. sb.append("end;");
  130. jdbcTemplate.execute(sb.toString());
  131. }
  132. }
  133. public void deleteById(String tablename, String keyField, long id) {
  134. deleteByCondition(tablename, keyField + "=" + id);
  135. }
  136. public void deleteByCondition(String tablename, String condition, Object... params) {
  137. StringBuffer sb = new StringBuffer();
  138. sb.append("DELETE FROM ");
  139. sb.append(tablename);
  140. sb.append(" WHERE ");
  141. sb.append(condition);
  142. execute(sb.toString(), params);
  143. }
  144. /**
  145. * 一个字段,一条结果
  146. *
  147. * @param tableName
  148. * 对应要查询的表
  149. * @param field
  150. * 要查询的字段
  151. * @param condition
  152. * 查询条件
  153. * @return field对应的数据
  154. */
  155. public Object getFieldDataByCondition(String tableName, String field, String condition) {
  156. StringBuffer sql = new StringBuffer("SELECT ");
  157. sql.append(field);
  158. sql.append(" FROM ");
  159. sql.append(tableName);
  160. sql.append(" WHERE ");
  161. sql.append(condition);
  162. SqlRowList srs = queryForRowSet(sql.toString());
  163. if (srs.next()) {
  164. return srs.getObject(1);
  165. } else {
  166. return null;
  167. }
  168. }
  169. /**
  170. * 一个字段,多条结果
  171. *
  172. * @param tableName
  173. * 对应要查询的表
  174. * @param field
  175. * 要查询的字段
  176. * @param condition
  177. * 查询条件
  178. * @return field对应的数据
  179. */
  180. public List<Object> getFieldDatasByCondition(String tableName, String field, String condition) {
  181. StringBuffer sb = new StringBuffer("SELECT ");
  182. sb.append(field);
  183. sb.append(" FROM ");
  184. sb.append(tableName);
  185. sb.append(((condition == null || "".equals(condition)) ? "" : (" WHERE " + condition)));
  186. SqlRowList srs = queryForRowSet(sb.toString());
  187. List<Object> list = new ArrayList<Object>();
  188. while (srs.next()) {
  189. list.add(srs.getObject(1));
  190. }
  191. return list;
  192. }
  193. /**
  194. * 多个字段,<=1条结果
  195. *
  196. * @param tableName
  197. * 对应要查询的表
  198. * @param fields
  199. * 要查询的字段,用逗号隔开
  200. * @param condition
  201. * 查询条件
  202. * @return fields对应的数据
  203. */
  204. public Object[] getFieldsDataByCondition(String tableName, String fields, String condition) {
  205. StringBuffer sql = new StringBuffer("SELECT ");
  206. sql.append(fields);
  207. sql.append(" FROM ");
  208. sql.append(tableName);
  209. sql.append(" WHERE ");
  210. sql.append(condition);
  211. String[] strs = fields.split(",");
  212. int length = strs.length;
  213. List<Map<String, Object>> list = jdbcTemplate.queryForList(sql.toString());
  214. Iterator<Map<String, Object>> iter = list.iterator();
  215. Object[] results = new Object[length];
  216. Object value = null;
  217. if (iter.hasNext()) {
  218. Map<String, Object> m = iter.next();
  219. for (int i = 0; i < length; i++) {
  220. value = m.get(strs[i].toUpperCase());
  221. if (value != null && value.getClass().getSimpleName().toUpperCase().equals("TIMESTAMP")) {
  222. Timestamp time = (Timestamp) value;
  223. try {
  224. value = DateUtil.parseDateToString(new Date(time.getTime()), Constant.YMD_HMS);
  225. } catch (Exception e) {
  226. e.printStackTrace();
  227. }
  228. }
  229. results[i] = value;
  230. }
  231. return results;
  232. }
  233. return null;
  234. }
  235. /**
  236. * 多个字段,<=1条结果
  237. *
  238. * @param tableName
  239. * 对应要查询的表
  240. * @param fields
  241. * 要查询的字段集合
  242. * @param condition
  243. * 查询条件
  244. * @return fields对应的数据
  245. */
  246. public Object[] getFieldsDataByCondition(String tableName, String[] fields, String condition) {
  247. StringBuffer sql = new StringBuffer("SELECT ");
  248. sql.append(BaseUtil.parseArray2Str(fields, ","));
  249. sql.append(" FROM ");
  250. sql.append(tableName);
  251. sql.append(" WHERE ");
  252. sql.append(condition);
  253. List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
  254. Iterator<Map<String, Object>> iter = list.iterator();
  255. int length = fields.length;
  256. Object[] results = new Object[length];
  257. Object value = null;
  258. if (iter.hasNext()) {
  259. Map<String, Object> m = iter.next();
  260. for (int i = 0; i < length; i++) {
  261. String upperField = fields[i].toUpperCase();
  262. if (upperField.indexOf(" AS ") > 0) {
  263. upperField = upperField.split(" AS ")[1].trim();
  264. }
  265. value = m.get(upperField);
  266. if (value != null && value.getClass().getSimpleName().toUpperCase().equals("TIMESTAMP")) {
  267. Timestamp time = (Timestamp) value;
  268. try {
  269. value = DateUtil.parseDateToString(new Date(time.getTime()), Constant.YMD_HMS);
  270. } catch (Exception e) {
  271. e.printStackTrace();
  272. }
  273. }
  274. results[i] = value;
  275. }
  276. return results;
  277. }
  278. return null;
  279. }
  280. /**
  281. * 查询结果集
  282. *
  283. * @param sql
  284. * 查询语句
  285. * @param arg
  286. * 参数
  287. */
  288. public SqlRowList queryForRowSet(String sql, Object arg) {
  289. SqlRowList rs = new SqlRowList();
  290. rs.setResultList(getJdbcTemplate().queryForList(sql, arg));
  291. return rs;
  292. }
  293. /**
  294. * 多个字段,多条结果
  295. *
  296. * @param tableName
  297. * 对应要查询的表
  298. * @param fields
  299. * 要查询的字段集合
  300. * @param condition
  301. * 查询条件
  302. * @return fields对应的数据
  303. */
  304. public List<Object[]> getFieldsDatasByCondition(String tableName, String[] fields, String condition) {
  305. StringBuffer sql = new StringBuffer("SELECT ");
  306. sql.append(BaseUtil.parseArray2Str(fields, ","));
  307. sql.append(" FROM ");
  308. sql.append(tableName);
  309. sql.append(" WHERE ");
  310. sql.append(condition);
  311. List<Map<String, Object>> list = getJdbcTemplate().queryForList(sql.toString());
  312. Iterator<Map<String, Object>> iter = list.iterator();
  313. List<Object[]> datas = new ArrayList<Object[]>();
  314. Object value = null;
  315. Map<String, Object> m = null;
  316. Object[] results = null;
  317. int length = fields.length;
  318. while (iter.hasNext()) {
  319. results = new Object[length];
  320. m = iter.next();
  321. for (int i = 0; i < length; i++) {
  322. value = m.get(fields[i].toUpperCase());
  323. if (value != null && value.getClass().getSimpleName().toUpperCase().equals("TIMESTAMP")) {
  324. Timestamp time = (Timestamp) value;
  325. try {
  326. value = DateUtil.parseDateToString(new Date(time.getTime()), Constant.YMD_HMS);
  327. } catch (Exception e) {
  328. e.printStackTrace();
  329. }
  330. }
  331. results[i] = value;
  332. }
  333. datas.add(results);
  334. }
  335. return datas;
  336. }
  337. /**
  338. * if resultSet is null return true
  339. */
  340. public boolean checkByCondition(String caller, String condition) {
  341. int count = getCountByCondition(caller, condition);
  342. if (count == 0) {
  343. return true;
  344. }
  345. return false;
  346. }
  347. /**
  348. * if resultSet not null return true
  349. */
  350. public boolean checkIf(String table, String condition) {
  351. int count = getCountByCondition(table, condition);
  352. if (count > 0) {
  353. return true;
  354. }
  355. return false;
  356. }
  357. /**
  358. * 修改操作
  359. *
  360. * @param tableName
  361. * 表
  362. * @param update
  363. * 修改内容
  364. * @param condition
  365. * 条件语句
  366. */
  367. public void updateByCondition(String tableName, String update, String condition) {
  368. StringBuffer sb = new StringBuffer("UPDATE ");
  369. sb.append(tableName);
  370. sb.append(" SET ");
  371. sb.append(update);
  372. sb.append(" WHERE ");
  373. sb.append(condition);
  374. execute(sb.toString());
  375. }
  376. /**
  377. * 调用存储过程 无返回值
  378. *
  379. * @param procedureName
  380. * 存储过程名称
  381. * @param args
  382. * 参数
  383. */
  384. public void procedure(String procedureName, Object[] args) {
  385. StringBuffer sql = new StringBuffer("{call ").append(procedureName).append("(");
  386. for (int i = 0; i < args.length; i++) {
  387. if (i > 0) {
  388. sql.append(",");
  389. }
  390. sql.append("?");
  391. }
  392. sql.append(")}");
  393. jdbcTemplate.update(sql.toString(), args);
  394. }
  395. /**
  396. * 调用存储过程
  397. *
  398. * @param procedureName
  399. * 存储过程名称
  400. * @param args
  401. * 参数
  402. * @return varchar类型结果
  403. */
  404. public List<String> callProcedureWithOut(final String procedureName, final Object[] args, final Integer[] inIndex,
  405. final Integer[] outIndex) {
  406. StringBuffer sql = new StringBuffer("{call " + procedureName + "(");
  407. for (int i = 0; i < inIndex.length + outIndex.length; i++) {
  408. if (sql.toString().contains("?")) {
  409. sql.append(",?");
  410. } else {
  411. sql.append("?");
  412. }
  413. }
  414. sql.append(")}");
  415. List<String> listR = jdbcTemplate.execute(sql.toString(), new CallableStatementCallback<List<String>>() {
  416. @Override
  417. public List<String> doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
  418. for (int i = 0; i < inIndex.length; i++) {
  419. cs.setObject(inIndex[i], args[i]);
  420. }
  421. for (int i = 0; i < outIndex.length; i++) {
  422. cs.registerOutParameter(outIndex[i], java.sql.Types.VARCHAR);
  423. }
  424. cs.execute();
  425. List<String> list = new ArrayList<String>();
  426. for (int i = 0; i < outIndex.length; i++) {
  427. list.add(cs.getString(outIndex[i]));
  428. }
  429. return list;
  430. }
  431. });
  432. return listR;
  433. }
  434. /**
  435. * 调用存储过程
  436. *
  437. * @param procedureName
  438. * 存储过程名称
  439. * @param args
  440. * 参数
  441. * @return varchar类型结果
  442. */
  443. public String callProcedure(final String procedureName, final Object... args) {
  444. try {
  445. return jdbcTemplate.execute(new CallableStatementCreator() {
  446. @Override
  447. public CallableStatement createCallableStatement(Connection conn) throws SQLException {
  448. StringBuffer storedProcName = new StringBuffer("{call ");
  449. int i = 0;
  450. storedProcName.append(procedureName + "(");
  451. for (i = 0; i < args.length; i++) {
  452. if (storedProcName.toString().contains("?")) {
  453. storedProcName.append(",");
  454. }
  455. storedProcName.append("?");
  456. }
  457. if (storedProcName.toString().contains("?")) {
  458. storedProcName.append(",");
  459. }
  460. storedProcName.append("?");
  461. storedProcName.append(")}");
  462. CallableStatement cs = conn.prepareCall(storedProcName.toString());
  463. for (i = 0; i < args.length; i++) {
  464. cs.setObject(i + 1, args[i]);
  465. }
  466. cs.registerOutParameter(args.length + 1, java.sql.Types.VARCHAR);
  467. return cs;
  468. }
  469. }, new CallableStatementCallback<String>() {
  470. @Override
  471. public String doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
  472. cs.execute();
  473. return cs.getString(args.length + 1);
  474. }
  475. });
  476. } catch (Exception e) {
  477. BaseUtil.showError(e.getMessage());
  478. }
  479. return null;
  480. }
  481. /**
  482. * 调用存储过程
  483. *
  484. * @param procedureName
  485. * 存储过程名称
  486. * @param cls
  487. * 返回结果java类型
  488. * @param sqlType
  489. * 返回结果的sql类型
  490. * @param args
  491. * 参数
  492. * @return varchar类型结果
  493. */
  494. public <T> T callbackProcedure(final String procedureName, final Class<T> cls, final int sqlType, final Object... args) {
  495. try {
  496. return jdbcTemplate.execute(new CallableStatementCreator() {
  497. @Override
  498. public CallableStatement createCallableStatement(Connection conn) throws SQLException {
  499. StringBuffer storedProcName = new StringBuffer("{call ");
  500. int i = 0;
  501. storedProcName.append(procedureName + "(");
  502. for (i = 0; i < args.length; i++) {
  503. if (storedProcName.toString().contains("?")) {
  504. storedProcName.append(",");
  505. }
  506. storedProcName.append("?");
  507. }
  508. if (storedProcName.toString().contains("?")) {
  509. storedProcName.append(",");
  510. }
  511. storedProcName.append("?");
  512. storedProcName.append(")}");
  513. CallableStatement cs = conn.prepareCall(storedProcName.toString());
  514. for (i = 0; i < args.length; i++) {
  515. cs.setObject(i + 1, args[i]);
  516. }
  517. cs.registerOutParameter(args.length + 1, sqlType);
  518. return cs;
  519. }
  520. }, new CallableStatementCallback<T>() {
  521. @SuppressWarnings("unchecked")
  522. @Override
  523. public T doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
  524. cs.execute();
  525. // do not use method: getObject(paramInt, paramClass)
  526. return (T) cs.getObject(args.length + 1);
  527. }
  528. });
  529. } catch (Exception e) {
  530. e.printStackTrace();
  531. BaseUtil.showError(e.getMessage());
  532. }
  533. return null;
  534. }
  535. /**
  536. * 查询结果集
  537. *
  538. * @param sql
  539. * 查询语句
  540. */
  541. public SqlRowList queryForRowSet(String sql) {
  542. SqlRowList rs = new SqlRowList();
  543. rs.setResultList(jdbcTemplate.queryForList(sql));
  544. return rs;
  545. }
  546. /**
  547. * @param tableName
  548. * 对应要查询的表
  549. * @param condition
  550. * 查询条件
  551. * @return Count
  552. */
  553. public int getCountByCondition(String tableName, String condition) {
  554. StringBuffer sql = new StringBuffer("SELECT count(1) FROM ");
  555. sql.append(tableName);
  556. sql.append(" WHERE ");
  557. sql.append(condition);
  558. SqlRowList srs = queryForRowSet(sql.toString());
  559. if (srs.next()) {
  560. return srs.getInt(1);
  561. } else {
  562. try {
  563. throw new Exception("Condition:" + condition + " is wrong!");
  564. } catch (Exception e) {
  565. return -1;
  566. }
  567. }
  568. }
  569. public List<JSONObject> getFieldsJSONDatasByCondition(String tableName, String[] fields, String condition) {
  570. StringBuffer sql = new StringBuffer("SELECT ");
  571. sql.append(BaseUtil.parseArray2Str(fields, ","));
  572. sql.append(" FROM ");
  573. sql.append(tableName);
  574. sql.append(" WHERE ");
  575. sql.append(condition);
  576. List<JSONObject> datas = new ArrayList<JSONObject>();
  577. JSONObject obj = null;
  578. Object value = null;
  579. SqlRowList sl = queryForRowSet(sql.toString());
  580. while (sl.next()) {
  581. obj = new JSONObject();
  582. for (int i = 0; i < fields.length; i++) {
  583. value = sl.getObject(i + 1);
  584. if (value != null && "TIMESTAMP".equals(value.getClass().getSimpleName().toUpperCase())) {
  585. Timestamp time = (Timestamp) value;
  586. value = DateUtil.parseDateToString(new Date(time.getTime()), "yyyy-MM-dd HH:mm:ss");
  587. }
  588. obj.put(fields[i], value);
  589. }
  590. datas.add(obj);
  591. }
  592. return datas;
  593. }
  594. /**
  595. * 获取序列号
  596. *
  597. * @param seq
  598. * 指定的序列名
  599. */
  600. public int getSeqId(String seq) {
  601. try {
  602. String sql = "select " + seq + ".nextval from dual";
  603. SqlRowList rs = queryForRowSet(sql);
  604. if (rs.next()) {
  605. return rs.getInt(1);
  606. } else {// 如果不存在就创建序列
  607. int count = getCountByCondition("user_sequences", "Sequence_Name='" + seq.toUpperCase() + "'");
  608. if (count == 0)
  609. getJdbcTemplate().execute(CREATE_SEQ.replace("?", seq));
  610. return getSeqId(seq);
  611. }
  612. } catch (Exception e) {
  613. int count = getCountByCondition("user_sequences", "Sequence_Name='" + seq.toUpperCase() + "'");
  614. if (count == 0)
  615. getJdbcTemplate().execute(CREATE_SEQ.replace("?", seq));
  616. return getSeqId(seq);
  617. }
  618. }
  619. /**
  620. * 获取编号序列
  621. *
  622. * @param myTable
  623. * Caller
  624. * @param thisType
  625. * 2
  626. */
  627. public synchronized String sGetMaxNumber(String myTable, int thisType) {
  628. return callProcedure("Sp_GetMaxNumber", new Object[] { myTable, thisType });
  629. }
  630. /**
  631. * 一个字段,一条结果
  632. *
  633. * @param tableName
  634. * 对应要查询的表
  635. * @param field
  636. * 要查询的字段
  637. * @param condition
  638. * 查询条件
  639. * @return field对应的数据
  640. */
  641. public <T> T getFieldValue(String tableName, String field, String condition, Class<T> requiredType) {
  642. StringBuffer sql = new StringBuffer("SELECT ");
  643. sql.append(field);
  644. sql.append(" FROM ");
  645. sql.append(tableName);
  646. sql.append(" WHERE ");
  647. sql.append(condition);
  648. SqlRowList srs = queryForRowSet(sql.toString());
  649. if (srs.next()) {
  650. RowConvert<T> convert = new RowConvert<T>(requiredType);
  651. return convert.convert(srs.getObject(1));
  652. } else {
  653. return null;
  654. }
  655. }
  656. /**
  657. * 判断指定参数是否配置为“是”
  658. *
  659. * @param caller
  660. * @param code
  661. * 参数编号
  662. */
  663. public boolean isDBSetting(String caller, String code) {
  664. int count = getCount("select count(1) from configs where caller='" + caller + "' and code='" + code + "'");
  665. if (count==0){
  666. return false;
  667. }
  668. Configs configs = jdbcTemplate.queryForObject("select * from configs where caller=? and code=?",
  669. new BeanPropertyRowMapper<Configs>(Configs.class), caller, code);
  670. if (configs != null) {
  671. String data = configs.getData();
  672. if ("YN".equals(configs.getData_type())) {
  673. return String.valueOf(Constant.YES).equals(data);
  674. }
  675. return data == null ? false : true;
  676. }
  677. return false;
  678. }
  679. public int getCount(String sql) {
  680. SqlRowList rs = queryForRowSet(sql);
  681. if (rs.next()) {
  682. return rs.getInt(1);
  683. }
  684. return 0;
  685. }
  686. public String getBatchCode(String caller, String field) {
  687. SqlRowList rs = queryForRowSet("select ds_inorout from DOCUMENTSETUP where ds_table=?", caller);
  688. String finalCode = null;
  689. if (rs.next()) {
  690. String Code = rs.getObject("ds_inorout").toString();
  691. if (Code.equals("IN") || Code.equals("-OUT")) {
  692. finalCode = sGetMaxNumber("ProdIOut", 2);
  693. }
  694. }
  695. return finalCode;
  696. }
  697. public boolean isProdIn(String caller){
  698. boolean isProdIn=false;
  699. SqlRowList rs = queryForRowSet("select ds_inorout from DOCUMENTSETUP where ds_table=?", caller);
  700. if (rs.next()) {
  701. isProdIn= "IN".equals(rs.getString(1)) || "-OUT".equals(rs.getString(1));
  702. }
  703. return isProdIn;
  704. }
  705. /**
  706. * 获取入库单批号
  707. */
  708. public String getBatchcode(String caller) {
  709. if (isProdIn(caller)) {
  710. /**
  711. * procedure utl_rseq
  712. *
  713. * @param seq_name
  714. * 序列名,支持自动创建,格式{@code seq_name}_rseq
  715. * @param seq_rule
  716. * {prefix}/{time format}/{length of pad and nextval}
  717. * @return seq_val
  718. */
  719. return callProcedure("utl_rseq", "BatchCode", "/yyMMdd/5");
  720. }
  721. return null;
  722. }
  723. /**
  724. * 查询结果集
  725. *
  726. * @param sql
  727. * 查询语句
  728. * @param args
  729. * 参数
  730. */
  731. public SqlRowList queryForRowSet(String sql, Object... args) {
  732. SqlRowList rs = new SqlRowList();
  733. rs.setResultList(getJdbcTemplate().queryForList(sql, args));
  734. return rs;
  735. }
  736. /**
  737. * 取指定系统参数设置
  738. *
  739. * @param code
  740. */
  741. public String getDBSetting(String caller,String code) {
  742. int count = getCount("select count(1) from configs where caller='" + caller + "' and code='" + code + "'");
  743. if (count>0) {
  744. Configs config = getJdbcTemplate().queryForObject("select * from configs where caller=? and code=?",
  745. new BeanPropertyRowMapper<Configs>(Configs.class), caller, code);
  746. if (config != null) {
  747. return config.getData();
  748. }
  749. }
  750. return null;
  751. }
  752. public boolean isIn(String caller) {
  753. SqlRowList rs = queryForRowSet("select ds_inorout from DOCUMENTSETUP where ds_table=?", caller);
  754. if (rs.next()) {
  755. return "IN".equals(rs.getString(1)) || "-OUT".equals(rs.getString(1));
  756. }
  757. return false;
  758. }
  759. public boolean isOut(String caller) {
  760. SqlRowList rs = queryForRowSet("select ds_inorout from DOCUMENTSETUP where ds_table=?", caller);
  761. if (rs.next()) {
  762. return "-IN".equals(rs.getString(1)) || "OUT".equals(rs.getString(1));
  763. }
  764. return false;
  765. }
  766. public Double getSummaryByField(String tablename, String summaryField, String condition) {
  767. SqlRowList rs = queryForRowSet("SELECT sum(nvl(" + summaryField + ",0)) FROM " + tablename + " WHERE " + condition);
  768. if (rs.next()) {
  769. return rs.getGeneralDouble(1, 6);
  770. }
  771. return 0.0;
  772. }
  773. public Map<String, Object> changeKeyToLowerCase(Map<String, Object> map) {
  774. Map<String, Object> map1 = new HashMap<String, Object>();
  775. Iterator<String> it = map.keySet().iterator();
  776. while (it.hasNext()) {
  777. String key = it.next();
  778. map1.put(key.toLowerCase(), map.get(key));
  779. }
  780. return map1;
  781. }
  782. public List<Map<String, Object>> changeKeyToLowerCase(List<Map<String, Object>> list) {
  783. Map<String, Object> map = new HashMap<String, Object>();
  784. List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
  785. Iterator<Map<String, Object>> iter = list.iterator();
  786. map = null;
  787. while (iter.hasNext()) {
  788. Map<String, Object> map1 = new HashMap<String, Object>();
  789. map = iter.next();
  790. Iterator<String> it = map.keySet().iterator();
  791. while (it.hasNext()) {
  792. String key = it.next();
  793. map1.put(key.toLowerCase(), map.get(key));
  794. }
  795. datas.add(map1);
  796. }
  797. return datas;
  798. }
  799. /**
  800. * 根据物料中的有效期天数更新有效期止
  801. */
  802. public void getEndDate(String caller, Object id) {
  803. SqlRowList rs = queryForRowSet("select ds_inorout from DOCUMENTSETUP where ds_table=?", caller);
  804. if (rs.next()) {
  805. String Code = rs.getObject("ds_inorout").toString();
  806. if (Code.equals("IN") || Code.equals("-OUT")) {
  807. execute("update prodiodetail set pd_replydate=pd_prodmadedate + (select nvl(pr_validdays,0) from product where pd_prodcode=pr_code) where pd_piid=? and pd_prodmadedate is not null",
  808. id);
  809. }
  810. }
  811. }
  812. /**
  813. * 修改单据为已审核(包括审核人+审核日期)
  814. *
  815. * @param tableName
  816. * @param condition
  817. * @param statusField
  818. * @param statusCodeField
  819. * @param auditdateField
  820. * @param auditorField
  821. */
  822. public void audit(String tableName, String condition, String statusField, String statusCodeField, String auditdateField,
  823. String auditorField,Employee employee) {
  824. updateByCondition(tableName, statusCodeField + "='AUDITED'," + statusField + "='已审核',"
  825. + auditdateField + "=" + DateUtil.parseDateToOracleString(Constant.YMD_HMS, new Date()) + "," + auditorField + "='"+employee.getEm_name()+"'", condition);
  826. }
  827. }