2cf85768786fd7f76bfb6076d29c0a18e64f6379.svn-base 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. package com.sinosoft.em.baobiao.rcbb.dao;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.text.ParseException;
  12. import java.text.SimpleDateFormat;
  13. import java.util.ArrayList;
  14. import java.util.Date;
  15. import java.util.HashMap;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Vector;
  20. import javax.ws.rs.POST;
  21. import javax.ws.rs.Path;
  22. import javax.ws.rs.ProduceMime;
  23. import org.apache.log4j.Logger;
  24. import org.apache.poi.hssf.usermodel.HSSFCell;
  25. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  26. import org.apache.poi.hssf.usermodel.HSSFRow;
  27. import org.apache.poi.hssf.usermodel.HSSFSheet;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  30. import org.apache.poi.ss.usermodel.Cell;
  31. import org.apache.poi.ss.usermodel.DateUtil;
  32. import org.apache.poi.xssf.usermodel.XSSFCell;
  33. import org.apache.poi.xssf.usermodel.XSSFRow;
  34. import org.apache.poi.xssf.usermodel.XSSFSheet;
  35. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  36. import com.formaction.Parameter;
  37. import com.formaction.Utils;
  38. import com.formaction.vo.Msg;
  39. import com.persistence.DbConnection;
  40. import com.persistence.service.PersistenceFactory;
  41. import com.persistence.service.SysPersistence;
  42. import com.persistence.service.assitant.generator.IdentityGenerator;
  43. import com.persistence.service.exception.PersistenceException;
  44. import com.sinosoft.common.excel.JsonPluginsUtil;
  45. import com.sinosoft.em.baobiao.xianlutingyun.vo.XianlutingyunBean;
  46. import com.sinosoft.lz.system.util.JsonUtil;
  47. import com.sysmodel.datamodel.xmlmodel.ModelFactory;
  48. import com.sysmodel.datamodel.xmlmodel.able.MdpClass;
  49. import com.sysmodel.datamodel.xmlmodel.able.SysModel;
  50. import com.sysmodel.datamodel.xmlmodel.impl.MdpAttributeImpl;
  51. /**
  52. * 读取Excel将数据返回到頁面
  53. *
  54. * @author 彭志超
  55. *
  56. */
  57. public class XianluTingyunExcel {
  58. // private String columns_info ="";
  59. // private String report_type;
  60. private static final Logger log = Logger.getLogger(XianluTingyunExcel.class);
  61. public String readExcelByName(String fileName,String report_type) {
  62. // this.report_type=report_type;
  63. XianluTingyunExcel tool = new XianluTingyunExcel();
  64. // String fileName=Utils.getParameter("fileName", params) == null ? "" : Utils.getParameter(
  65. // "fileName", params);
  66. ////
  67. // BaoBiaoDao bbd=new BaoBiaoDao();
  68. // String columns_info=bbd.getTabCol(report_type);
  69. // String fileName="E:\\javaDemo\\person.xls";
  70. int i=fileName.lastIndexOf(".");
  71. String type=fileName.substring(i+1);
  72. log.info("fileName===="+fileName);
  73. String result="";
  74. if("xls".equals(type)){
  75. result=tool.readExcel(fileName,report_type);
  76. }else if("xlsx".equals(type)){
  77. try {
  78. result= tool.readExcel2007(fileName,report_type);
  79. } catch (Exception e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. }
  83. }
  84. result="{\"rows\":"+result+"}";
  85. log.info(result);
  86. return result;
  87. }
  88. // public void readExcel2007(DBModel dbConn, String filePath, String tableName)
  89. @SuppressWarnings({ "rawtypes", "unused", "resource", "unchecked" })
  90. public String readExcel2007( String filePath, String report_type)
  91. {
  92. String result="";
  93. try {
  94. // InputStream inp = new FileInputStream(filePath);
  95. // Workbook wb = WorkbookFactory.create(inp);
  96. XSSFWorkbook wb = new XSSFWorkbook(filePath);
  97. int sheetSize = wb.getNumberOfSheets();
  98. // for (int i = 0; i < sheetSize; i++) {
  99. List list=new ArrayList();
  100. XSSFSheet sheet = wb.getSheetAt(0);
  101. // Sheet sheet = wb.getSheetAt(i);
  102. int lastRow = sheet.getLastRowNum();
  103. for (int i=1;i<=lastRow;i++) {
  104. // 迭代行
  105. // XSSFRow row = (XSSFRow) rit.next();
  106. XSSFRow row = sheet.getRow(i);
  107. // 迭代单元格
  108. Vector datas = new Vector();
  109. StringBuffer sb=new StringBuffer();
  110. Map<String, String>lineMap=new HashMap<String, String>();
  111. for (Iterator cit = row.cellIterator(); cit.hasNext();) {
  112. // 定义集合datas用于存Excel中一个行的数据
  113. XSSFCell cell = (XSSFCell) cit.next();
  114. String value = getValue2007(cell);
  115. sb.append(value+"|");
  116. // 注意行和列是基于0索引的
  117. // System.out.print(cell.getRowIndex() + ":" + cell.getColumnIndex() + " ");
  118. datas.add(value);
  119. // System.out.println();
  120. // 开始操作单元格
  121. }
  122. String[] arr=sb.toString().split("[|]");
  123. list.add(arr);
  124. // result = fillMap(list);;
  125. // 向表中插入数据
  126. // DBFactory.insertData(dbConn, tableName, datas);
  127. }
  128. result = fillMap(list,report_type);
  129. System.out.println(result);
  130. // }
  131. } catch (FileNotFoundException e) {
  132. e.printStackTrace();
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. // result = new String(result.getBytes("iso8859-1"),"UTF-8");
  137. return result;
  138. }
  139. /**
  140. * 根据ID获取单位名称
  141. * @param id
  142. * @return
  143. */
  144. public String getNameById(String id){
  145. SysModel sysmodel = ModelFactory.getSysmodel();
  146. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  147. String sql="select dept_name from sys_department where id='"+id+"'";
  148. try {
  149. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  150. if(tempList==null||tempList.size()==0){
  151. return "";
  152. }else{
  153. String name=tempList.get(0)[0];
  154. log.info("name===="+name);
  155. return name;
  156. }
  157. } catch (PersistenceException e) {
  158. // TODO Auto-generated catch block
  159. e.printStackTrace();
  160. return "";
  161. }
  162. }
  163. /**
  164. * 將数据转换成json
  165. * @param list
  166. * @return
  167. * @throws UnsupportedEncodingException
  168. */
  169. public String fillMap(List<String[]> list,String report_type) throws UnsupportedEncodingException{
  170. RcBbDao bbd=new RcBbDao();
  171. String columns_info=bbd.getTabCol(report_type);
  172. String columns=columns_info.split("[|]")[0];
  173. // String tabName=columns_info.split("[|]")[0];
  174. String []col_arr=columns.split(",");
  175. List<Map<String, String>>dataList=new ArrayList<Map<String,String>>();
  176. Map<String, String> noHuizongMap = getNoHuizongCol(report_type);
  177. Map<String,String>huiZongMap=new HashMap<String, String>();
  178. Map<String, String>hMap=new HashMap<String, String>();
  179. for(int i=0;i<list.size();i++){
  180. Map<String, String>tempMap=new HashMap<String, String>();
  181. String[]arr=list.get(i);
  182. tempMap.put("COMP_ID", getIdByName(arr[0]));
  183. tempMap.put("COMP_NAME", new String(java.net.URLEncoder.encode(arr[0], "utf-8").getBytes(),"ISO-8859-1"));
  184. tempMap.put("FD_OBJECTID", IdentityGenerator.getIdentityGenerator().gerenalIdentity(338));
  185. for(int j=0;j<col_arr.length;j++){
  186. tempMap.put(col_arr[j], arr[j+1]);
  187. if (noHuizongMap.containsKey(col_arr[j])) {
  188. huiZongMap.put(col_arr[j], "");
  189. } else {
  190. String ddd=arr[j + 1];
  191. String match="/^\\+?[1-9][0-9]*$/";
  192. if(arr[j + 1]!=null&&!arr[j + 1].matches(match)){
  193. return "";
  194. }
  195. huiZongMap.put(
  196. col_arr[j],
  197. (Float.parseFloat((huiZongMap.get(col_arr[j]) == null
  198. || "".equals(huiZongMap.get(col_arr[j]))
  199. || "null".equals(huiZongMap.get(col_arr[j]))
  200. || "--".equals(huiZongMap.get(col_arr[j])) || "未填报"
  201. .equals(huiZongMap.get(col_arr[j]))) ? "0" : huiZongMap
  202. .get(col_arr[j]))
  203. + Float.parseFloat((arr[j + 1] == null
  204. || "".equals(arr[j + 1]) || "null"
  205. .equals(arr[j + 1])) ? "0"
  206. : arr[j + 1]) + ""));
  207. }
  208. }
  209. for(int j=0;j<col_arr.length;j++){
  210. if (!noHuizongMap.containsKey(col_arr[j])) {
  211. String value=huiZongMap.get(col_arr[j]);
  212. String value1="未填报".equals(value)||value==null||"".equals(value)?"":value;
  213. value1=value.endsWith(".0")?value.split("[.]")[0]:value;
  214. hMap.put(col_arr[j], ("未填报".equals(value)||value==null||"".equals(value))?"":(value.endsWith(".0")?value.split("[.]")[0]:value));
  215. }else{
  216. hMap.put(col_arr[j], "");
  217. }
  218. }
  219. hMap.put("FD_OBJECTID", IdentityGenerator.getIdentityGenerator().gerenalIdentity(338));
  220. hMap.put("COMP_NAME", new String(java.net.URLEncoder.encode("总计", "utf-8").getBytes(),"ISO-8859-1"));
  221. dataList.add(tempMap);
  222. }
  223. huiZongMap.put("FD_OBJECTID", IdentityGenerator.getIdentityGenerator().gerenalIdentity(338));
  224. huiZongMap.put("COMP_NAME", new String(java.net.URLEncoder.encode("总计", "utf-8").getBytes(),"ISO-8859-1"));
  225. dataList.add(hMap);
  226. String result=JsonUtil.list2json(dataList);
  227. return result;
  228. }
  229. public Map<String, String> getNoHuizongCol(String report_type) {
  230. SysModel sysmodel = ModelFactory.getSysmodel();
  231. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  232. Map<String, String> map = new HashMap<String, String>();
  233. String sql = "select TABLE_COLUMN from EMC_AM_REPORT_DICTIONARY where is_del='0' and (is_count='1' or is_count='2') and is_used='0' and report_type='"
  234. + report_type + "'";
  235. try {
  236. List<String[]> tempList = persistence.getSearchResult(99, sql.toString());
  237. if (tempList != null && tempList.size() > 0) {
  238. for (int i = 0; i < tempList.size(); i++) {
  239. map.put(tempList.get(i)[0], tempList.get(i)[0]);
  240. }
  241. }
  242. } catch (PersistenceException e) {
  243. // TODO Auto-generated catch block
  244. e.printStackTrace();
  245. }
  246. return map;
  247. }
  248. public String getIdByName(String name){
  249. SysModel sysmodel = ModelFactory.getSysmodel();
  250. SysPersistence persistence = PersistenceFactory.getInstance(sysmodel);
  251. String result="";
  252. String sql="select comp_id from REPORT_COMP_DICTIONARY where comp_name='"+name+"'";
  253. try {
  254. List<String[]>tempList= persistence.getSearchResult(99, sql.toString());
  255. if(tempList==null||tempList.size()==0){
  256. }else{
  257. result=tempList.get(0)[0];
  258. log.info("comp_id===="+result);
  259. }
  260. } catch (PersistenceException e) {
  261. // TODO Auto-generated catch block
  262. e.printStackTrace();
  263. return result;
  264. }
  265. return result;
  266. }
  267. /**
  268. * 将Excel数据导入到表中
  269. *
  270. * @param filePath
  271. * Excel路径
  272. * @param tableName
  273. * 表名
  274. */
  275. // public void readExcel(DBModel dbConn, String filePath, String tableName)
  276. @SuppressWarnings({ "unchecked", "rawtypes", "deprecation", "resource" })
  277. public String readExcel( String filePath, String report_type){
  278. String result="";
  279. try {
  280. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( filePath));
  281. // 创建工作簿
  282. HSSFWorkbook workBook = new HSSFWorkbook(fs);
  283. log.info(workBook.getNumberOfSheets());
  284. // System.out.println("工作表个数 :" + workBook.getNumberOfSheets());
  285. for (int i = 0; i < workBook.getNumberOfSheets(); i++) {
  286. // 创建工作表
  287. HSSFSheet sheet = workBook.getSheetAt(i);
  288. int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
  289. int startRow=0;
  290. if("7".equals(report_type)){
  291. startRow=1;
  292. }else{
  293. startRow=2;
  294. }
  295. if (rows > 1) {
  296. sheet.getMargin(HSSFSheet.TopMargin);
  297. List list=new ArrayList();
  298. for (int r = 1; r < rows; r++) { // 行循环
  299. HSSFRow row = sheet.getRow(r);
  300. // if (row != null && r != startRow && r != startRow+1) {// 不取第一行、第二行
  301. if (row != null && r >startRow) {// 不取第一行、第二行
  302. int cells = row.getLastCellNum();// 获得列数
  303. // 定义集合datas用于存Excel中一个行的数据
  304. Vector datas = new Vector();
  305. String[]arr=new String[cells];
  306. List<String>paramList=new ArrayList<String>();
  307. for (short c = 0; c < cells; c++) { // 列循环
  308. HSSFCell cell = row.getCell(c);
  309. if (cell != null) {
  310. String value = getValue(cell);
  311. // System.out.println("第" + r + "行 " + "第" + c + "列:" + value);
  312. datas.add(value);
  313. paramList.add(value);
  314. arr[c]=value;
  315. }
  316. }
  317. list.add(arr);
  318. result = fillMap(list,report_type);
  319. // dataIntoDb(tableName, paramList);
  320. // 向表中插入数据
  321. // DBFactory.insertData(dbConn, tableName, datas);
  322. }
  323. }
  324. } else {
  325. }
  326. }
  327. } catch (Exception ex) {
  328. ex.printStackTrace();
  329. log.info(ex);
  330. return "";
  331. //throw ex;
  332. }
  333. // result = new String(result.getBytes("iso8859-1"),"UTF-8");
  334. return result;
  335. }
  336. public String getValue2007(XSSFCell cell) {
  337. String value = "";
  338. switch (cell.getCellType()) {
  339. case Cell.CELL_TYPE_STRING:
  340. // System.out.println(cell.getRichStringCellValue().getString());
  341. value=cell.getRichStringCellValue().getString();
  342. break;
  343. case Cell.CELL_TYPE_NUMERIC:
  344. if (DateUtil.isCellDateFormatted(cell)) {
  345. // System.out.println(cell.getDateCellValue());
  346. java.util.Date date = cell.getDateCellValue();
  347. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  348. value = format.format(date);
  349. } else {
  350. // System.out.println(cell.getNumericCellValue());
  351. value = String.valueOf((cell.getNumericCellValue()+"").split("[.]")[0]);
  352. }
  353. break;
  354. case Cell.CELL_TYPE_BOOLEAN:
  355. // System.out.println(cell.getBooleanCellValue());
  356. value = " " + cell.getBooleanCellValue();
  357. break;
  358. case Cell.CELL_TYPE_FORMULA:
  359. // System.out.println(cell.getCellFormula());
  360. value = cell.getCellFormula();
  361. break;
  362. default:
  363. System.out.println();
  364. }
  365. log.info(value);
  366. return value;
  367. }
  368. /**
  369. * 获取Excel中某个单元格的值
  370. *
  371. * @param cell
  372. * @return
  373. * @throws ParseException
  374. */
  375. @SuppressWarnings("deprecation")
  376. public String getValue(HSSFCell cell) {
  377. String value = "";
  378. switch (cell.getCellType()) {
  379. case HSSFCell.CELL_TYPE_NUMERIC: // 数值型
  380. System.out.println("数字类型");
  381. if (HSSFDateUtil.isCellDateFormatted(cell)) {
  382. // 如果是date类型则 ,获取该cell的date值
  383. value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue())
  384. .toString();
  385. java.util.Date date1 = new Date(value);
  386. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  387. value = format.format(date1);
  388. } else {// 纯数字
  389. value = String.valueOf(cell.getNumericCellValue());
  390. if(value!=null||!"".equals(value)){
  391. log.info("value==="+value);
  392. value=value.endsWith(".0")?(value.split("[.]")[0]):value;
  393. }
  394. }
  395. break;
  396. /* 此行表示单元格的内容为string类型 */
  397. case HSSFCell.CELL_TYPE_STRING: // 字符串型
  398. value = cell.getStringCellValue();
  399. break;
  400. case HSSFCell.CELL_TYPE_FORMULA:// 公式型
  401. // 读公式计算值
  402. value = String.valueOf(cell.getNumericCellValue());
  403. if (value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串
  404. value = cell.getStringCellValue().toString();
  405. }
  406. cell.getCellFormula();
  407. break;
  408. case HSSFCell.CELL_TYPE_BOOLEAN:// 布尔
  409. value = " " + cell.getBooleanCellValue();
  410. break;
  411. /* 此行表示该单元格值为空 */
  412. case HSSFCell.CELL_TYPE_BLANK: // 空值
  413. value = "";
  414. break;
  415. case HSSFCell.CELL_TYPE_ERROR: // 故障
  416. value = "";
  417. break;
  418. default:
  419. value = cell.getStringCellValue().toString();
  420. }
  421. return value;
  422. }
  423. // public boolean dataIntoDb(String tabName,List<String>list) throws ClassNotFoundException, SQLException{
  424. // Class.forName("dm.jdbc.driver.DmDriver");
  425. // Connection conn = DriverManager.getConnection("jdbc:dm://192.168.1.16:5236","NWYJ","NWYJ123456");
  426. // String sql="INSERT INTO SYS_MSG_MODEL(FD_OBJECTID,MSG_MODEL_TYPE,MSG_MODEL_CONTENT,MSG_MODEL_RANGE,MSG_MODEL_COMP,MSG_MODEL_USER,MSG_MODEL_TIME,IS_DEL)VALUES(?,?,?,?,?,?,?,?)";
  427. // PreparedStatement ps=null;
  428. // conn.setAutoCommit(false);
  429. // ps = conn.prepareStatement(sql);
  430. // ps.setString(1, (long) ((Math.random()+1)*1000000000)+"");
  431. // ps.setString(2, list.get(0));
  432. // ps.setString(3,list.get(1));
  433. // ps.setString(4, list.get(2));
  434. // ps.setString(5, list.get(3));
  435. // ps.setString(6, list.get(4));
  436. // ps.setString(7, list.get(5));
  437. // ps.setString(8,list.get(6));
  438. // int k=ps.executeUpdate();
  439. // conn.commit();
  440. //// System.out.println("k====="+k);
  441. // return false;
  442. // }
  443. public Msg xianluToDb(String fd_id,String json,String report_id,String classId) throws ClassNotFoundException {
  444. Msg m=new Msg();
  445. Connection conn = null;
  446. PreparedStatement st = null;
  447. DbConnection db = new DbConnection();
  448. RcBbDao bbd=new RcBbDao();
  449. // String columns_info=bbd.getTabCol(report_type);
  450. // String tabName=columns_info.split("[|]")[1];
  451. // String columns=columns_info.split("[|]")[0];
  452. // String[]arr=columns.split(",");
  453. SysModel sysmodel = ModelFactory.getSysmodel();
  454. MdpClass mdpClass = sysmodel.getMdpClassByClassID(Integer.parseInt(classId));
  455. List<MdpAttributeImpl> attribute = mdpClass.getAllMdpAttributes();
  456. String queryField = "";
  457. String tableName = mdpClass.getName();
  458. Map<String,String> expMap = new HashMap<String,String>();
  459. for(MdpAttributeImpl attr : attribute){
  460. if(!"UPDATEDATE".equals(attr.getName()) && !"IS_DEL".equals(attr.getName())&& !"USER_LIMITS".equals(attr.getName())&&!"FD_OBJECTID".equals(attr.getName())){
  461. queryField += attr.getName() + ",";
  462. }
  463. }
  464. String[] colArr=queryField.split(",");
  465. int colNum=colArr.length;
  466. StringBuffer sb=new StringBuffer();
  467. StringBuffer sb1=new StringBuffer();
  468. for(int i=0;i<colNum+3;i++){
  469. sb.append("?,");
  470. }
  471. for(int j=0;j<colNum;j++){
  472. sb1.append(colArr[j]+",");
  473. }
  474. if (sb1.lastIndexOf(",") > -1) {
  475. sb1.deleteCharAt(sb1.lastIndexOf(","));
  476. }
  477. if (sb.lastIndexOf(",") > -1) {
  478. sb.deleteCharAt(sb.lastIndexOf(","));
  479. }
  480. List<Map<String, String>> list = JsonPluginsUtil.jsonToMapList(json);
  481. String sql="INSERT INTO "+tableName+" (FD_OBJECTID,COMP_ID,"+sb1.toString()+
  482. // "TRIP_OUT_ONE,NO_RECOVER_ONE,"
  483. // + "TRIP_OUT_TOTAL_ONE,REMARK_ONE,TRIP_OUT_TWO,NO_RECOVER_TWO,TRIP_OUT_TOTAL_TWO,REMARK_TWO,TRIP_OUT_THREE,NO_RECOVER_THREE,TRIP_OUT_TOTAL_THREE,REMARK_THREE,TRIP_OUT_FOUR,NO_RECOVER_FOUR,TRIP_OUT_FIVE,NO_RECOVER_FIVE,LOSS_LOAD,LOSS_PERCENT,"
  484. ",REPORT_ID) VALUES("+sb.toString()
  485. // + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?"
  486. + ")";
  487. try {
  488. conn = db.getConnection();
  489. int num=0;
  490. conn.setAutoCommit(false);
  491. st = conn.prepareStatement(sql);
  492. for(int i=0;i<list.size();i++){
  493. Map<String, String>map=list.get(i);
  494. String fd_objectid=map.get("FD_OBJECTID");
  495. st.setString(1, fd_objectid);
  496. st.setString(2, map.get("COMP_ID"));
  497. for(int k=0;k<colNum;k++){
  498. st.setString(k+3, "null".equals(map.get(colArr[k]))?"":map.get(colArr[k]));
  499. }
  500. st.setString(colNum+3, fd_id);
  501. st.executeUpdate();
  502. num++;
  503. }
  504. if(num==list.size()){
  505. log.info("保存成功");
  506. conn.commit();
  507. m.setSucsess(new Boolean(true).toString());
  508. m.setInfo(Parameter.DELETE_SUCSESS);
  509. return m;
  510. }else{
  511. m.setSucsess(new Boolean(false).toString());
  512. m.setInfo(Parameter.DELETE_FAILURE);
  513. // st.executeBatch();
  514. conn.rollback();
  515. log.info("保存失败");
  516. return m;
  517. }
  518. } catch (SQLException e) {
  519. this.log.error(e.getMessage(), e);
  520. throw new ClassNotFoundException("DAO Layou: 消息保存", e);
  521. } finally {
  522. db.close(st);
  523. db.close(conn);
  524. }
  525. }
  526. public String formatDate(Date d) {
  527. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  528. String date = sd.format(d);
  529. return date;
  530. }
  531. public String formatTime(Date d) {
  532. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  533. String date = sd.format(d);
  534. return date;
  535. }
  536. @SuppressWarnings({ "unchecked", "rawtypes" })
  537. public List<String> getMsgId() throws ClassNotFoundException {
  538. String sql = "SELECT FD_OBJECTID FROM ROUTE_OFLINE_RESTORE";
  539. Connection conn = null;
  540. Statement stat = null;
  541. ResultSet rs = null;
  542. DbConnection db = new DbConnection();
  543. try {
  544. conn = db.getConnection();
  545. stat = conn.createStatement();
  546. rs = stat.executeQuery(sql);
  547. List<String> list = new ArrayList();
  548. while (rs.next()) {
  549. list.add(rs.getString("FD_OBJECTID"));
  550. }
  551. return list;
  552. } catch (SQLException e) {
  553. // this.log.error(e.getMessage(), e);
  554. throw new ClassNotFoundException("DAO Layou: 获得数据库消息ID集合"
  555. + sql, e);
  556. } finally {
  557. db.close(rs);
  558. db.close(stat);
  559. db.close(conn);
  560. }
  561. }
  562. public String createMsgId(List<String> list) throws ClassNotFoundException{
  563. long l=(long) ((Math.random()+1)*1000000000);
  564. String msgId=l+"";
  565. if(list.contains(msgId)){
  566. return createMsgId(list);
  567. }
  568. else{
  569. // log.info("msgid==="+msgId);
  570. return msgId;
  571. }
  572. }
  573. }