29e2c56358143b5b5e23840e2ba0498b2bffcc2c.svn-base 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. package com.sinosoft.am.org.jdbcUtil;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.ResultSetMetaData;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.apache.log4j.Logger;
  12. import com.sinosoft.cm.DBUtils;
  13. import com.sinosoft.cm.ex.SQLStringException;
  14. public class ORGTemplate {
  15. private static final Logger log = Logger.getLogger(ORGTemplate.class);
  16. public ORGTemplate() {
  17. super();
  18. }
  19. /**
  20. *
  21. * @param sql PreparedStatement sql语句
  22. * @param args 占位符参数
  23. * @return 1 更新成功 0 跟新失败
  24. * @throws SQLException
  25. */
  26. public int update(String sql,Object...args) throws SQLException{
  27. Connection conn = null;
  28. PreparedStatement ps = null;
  29. try {
  30. conn = DBUtils.getConnection();
  31. ps = conn.prepareStatement(sql);
  32. if(args!= null){
  33. for(int i =0; i<args.length;i++){
  34. ps.setObject(i+1, args[i]);
  35. }
  36. }
  37. return ps.executeUpdate();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. throw new SQLStringException(this.getClass()+":update(String sql,Object...args);sql语句异常");
  41. }finally{
  42. DBUtils.close(null, ps, conn);
  43. }
  44. }
  45. public ArrayList<Map<String,String>> query(String sql,Object...args) throws SQLException{
  46. ArrayList<Map<String, String>> al = new ArrayList<Map<String, String>>();
  47. String[] fields = null;
  48. Connection conn = null;
  49. PreparedStatement ps = null;
  50. ResultSet rs=null;
  51. try {
  52. conn = DBUtils.getConnection();
  53. ps = conn.prepareStatement(sql);
  54. if(args!=null){
  55. for(int i = 0;i<args.length;i++){
  56. ps.setObject(i+1, args[i]);
  57. }
  58. rs = ps.executeQuery();
  59. ResultSetMetaData rsmd = rs.getMetaData();
  60. int colCount = rsmd.getColumnCount();
  61. fields = new String[colCount];
  62. for(int col =1;col<=fields.length;col++){
  63. fields[col - 1] = rsmd.getColumnName(col);
  64. }
  65. while(rs.next()){
  66. Map<String,String> dataMap = new HashMap<String,String>();
  67. for (int i = 0; i < fields.length; i++) {
  68. dataMap.put(fields[i], rs.getString(fields[i]));
  69. }
  70. al.add(dataMap);
  71. }
  72. }
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. throw new SQLStringException(this.getClass()+":query(String sql,Object...args);sql语句异常");
  76. }finally{
  77. DBUtils.close(rs, ps, conn);
  78. }
  79. return al;
  80. }
  81. public ArrayList<Map<String,String>> getSerachResult(String sql,String...args) throws SQLException{
  82. ArrayList<Map<String, String>> al = new ArrayList<Map<String, String>>();
  83. String[] fields = null;
  84. Connection conn = null;
  85. PreparedStatement ps = null;
  86. ResultSet rs=null;
  87. ArrayList<String> parList = new ArrayList<String>();
  88. try {
  89. conn = DBUtils.getConnection();
  90. ps = conn.prepareStatement(sql);
  91. if(args!=null){
  92. for(int i = 0;i<args.length;i++){
  93. if(args[i].endsWith("_needUsein")){
  94. args[i] = args[i].substring(0,args[i].indexOf("_needUsein"));
  95. String[] parArr = args[i].split(",");
  96. for (int j = 0; j < parArr.length; j++) {
  97. parList.add(parArr[j]);
  98. }
  99. }else{
  100. parList.add(args[i]);
  101. }
  102. }
  103. for(int m=0;m<parList.size();m++){
  104. ps.setObject(m+1, parList.get(m));
  105. }
  106. rs = ps.executeQuery();
  107. ResultSetMetaData rsmd = rs.getMetaData();
  108. int colCount = rsmd.getColumnCount();
  109. fields = new String[colCount];
  110. for(int col =1;col<=fields.length;col++){
  111. fields[col - 1] = rsmd.getColumnName(col);
  112. }
  113. while(rs.next()){
  114. Map<String,String> dataMap = new HashMap<String,String>();
  115. for (int i = 0; i < fields.length; i++) {
  116. dataMap.put(fields[i], rs.getString(fields[i]));
  117. }
  118. al.add(dataMap);
  119. }
  120. }
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. throw new SQLStringException(this.getClass()+":query(String sql,Object...args);sql语句异常");
  124. }finally{
  125. DBUtils.close(rs, ps, conn);
  126. }
  127. return al;
  128. }
  129. public List<String> getResultList(String sql,String...args) throws SQLStringException{
  130. String[] fields = null;
  131. Connection conn = null;
  132. PreparedStatement ps = null;
  133. ResultSet rs=null;
  134. ArrayList<String> list = new ArrayList<String>();
  135. ArrayList<String> parList = new ArrayList<String>();
  136. try {
  137. conn = DBUtils.getConnection();
  138. ps = conn.prepareStatement(sql);
  139. if(args!=null&&args.length>0){
  140. for(int i=0;i<args.length;i++){
  141. if(args[i].endsWith("_needUsein")){
  142. args[i] = args[i].substring(0,args[i].indexOf("_needUsein"));
  143. String[] parArr = args[i].split(",");
  144. for (int j = 0; j < parArr.length; j++) {
  145. parList.add(parArr[j]);
  146. }
  147. }else{
  148. parList.add(args[i]);
  149. }
  150. }
  151. for(int m=0;m<parList.size();m++){
  152. ps.setObject(m+1, parList.get(m));
  153. }
  154. }
  155. rs = ps.executeQuery();
  156. ResultSetMetaData rsmd = rs.getMetaData();
  157. int count = rsmd.getColumnCount();
  158. fields = new String[count];
  159. for(int i=1;i<=fields.length;i++){
  160. fields[i-1] = rsmd.getColumnName(i);
  161. }
  162. while(rs.next()){
  163. String val = "";
  164. for(int k=0;k<fields.length;k++){
  165. val = rs.getString(fields[k]);
  166. }
  167. list.add(val);
  168. }
  169. } catch (SQLException e) {
  170. e.printStackTrace();
  171. throw new SQLStringException(this.getClass()+":query(String sql,Object...args);sql语句异常");
  172. }finally{
  173. DBUtils.close(rs, ps, conn);
  174. }
  175. return list;
  176. }
  177. public List<String[]> getSearchResult(String sql,String...args) throws SQLException{
  178. String[] fields = null;
  179. Connection conn = null;
  180. PreparedStatement ps = null;
  181. ResultSet rs=null;
  182. ArrayList<String[]> list = new ArrayList<String[]>();
  183. ArrayList<String> parList = new ArrayList<String>();
  184. try {
  185. conn = DBUtils.getConnection();
  186. ps = conn.prepareStatement(sql);
  187. if(args!=null&&args.length>0){
  188. for(int i=0;i<args.length;i++){
  189. if(args[i].endsWith("_needUsein")){
  190. args[i] = args[i].substring(0,args[i].indexOf("_needUsein"));
  191. String[] parArr = args[i].split(",");
  192. for (int j = 0; j < parArr.length; j++) {
  193. parList.add(parArr[j]);
  194. }
  195. }else{
  196. parList.add(args[i]);
  197. }
  198. }
  199. for(int m=0;m<parList.size();m++){
  200. ps.setObject(m+1, parList.get(m));
  201. }
  202. }
  203. rs = ps.executeQuery();
  204. ResultSetMetaData rsmd = rs.getMetaData();
  205. int count = rsmd.getColumnCount();
  206. fields = new String[count];
  207. for(int i=1;i<=fields.length;i++){
  208. fields[i-1] = rsmd.getColumnName(i);
  209. }
  210. while(rs.next()){
  211. String[] val = new String[count];
  212. for(int k=0;k<val.length;k++){
  213. val[k] = rs.getString(fields[k]);
  214. }
  215. list.add(val);
  216. }
  217. } catch (SQLException e) {
  218. e.printStackTrace();
  219. throw new SQLStringException(this.getClass()+":query(String sql,Object...args);sql语句异常");
  220. }finally{
  221. DBUtils.close(rs, ps, conn);
  222. }
  223. return list;
  224. }
  225. public String listToString(List<Map<String,String>> list){
  226. StringBuffer sb = new StringBuffer();
  227. for(Map<String,String> map :list){
  228. sb.append("{");
  229. for (Map.Entry<String, String> entry : map.entrySet()) {
  230. sb.append("\""+entry.getKey()+"\":\""+entry.getValue()+"\",");
  231. }
  232. if(sb.lastIndexOf(",")>-1){
  233. sb.deleteCharAt(sb.lastIndexOf(","));
  234. }
  235. sb.append("},");
  236. }
  237. if(sb.lastIndexOf(",")>-1){
  238. sb.deleteCharAt(sb.lastIndexOf(","));
  239. }
  240. return sb.toString();
  241. }
  242. public String mapToJson(Map<?,?> map){
  243. StringBuffer sb = new StringBuffer();
  244. for(Map.Entry<?, ?> m:map.entrySet()){
  245. sb.append("{");
  246. sb.append("\""+m.getKey()+"\":\""+m.getValue()+"\",");
  247. if(sb.lastIndexOf(",")>-1){
  248. sb.deleteCharAt(sb.lastIndexOf(","));
  249. }
  250. sb.append("},");
  251. }
  252. if(sb.lastIndexOf(",")>-1){
  253. sb.deleteCharAt(sb.lastIndexOf(","));
  254. }
  255. return sb.toString();
  256. }
  257. /**
  258. *
  259. * 给list中的map 添加属性 key value 根据的map中的key 对应的value值
  260. * @return
  261. *//*
  262. public List<Map<String,String>> addExtParamToList(List<Map<String,String>> list,String key,String newKey){
  263. for(Map<String,String> map : list){
  264. String keyValue = map.get(key);
  265. String pinyin = Pinyin4jUtil.getPinYin(src);
  266. map.put(newKey, keyValue);
  267. }
  268. return null;
  269. }*/
  270. public static void main(String[] args){
  271. StringBuffer sql=new StringBuffer();
  272. String id = "8F6F2D6B24494CB6B1F1E0931E7AF87D";
  273. ORGTemplate org = new ORGTemplate();
  274. List<Map<String,String>> list = new ArrayList<Map<String,String>>();
  275. try {
  276. /*sql.append("--查询给定id及其一级子机构的信息 \n");
  277. sql.append("SELECT A.ORG_ID,A.ORG_NAME,A.PARENT_ID,A.ORG_LEVEL,A.IS_VIRTUAL, \n");
  278. sql.append("DECODE((SELECT COUNT(1) FROM EMC_DEPARTMENT_IN B WHERE B.PARENT_ID = A.ORG_ID),'0','0','1') AS 是否有子机构 \n");
  279. sql.append("FROM EMC_DEPARTMENT_IN A WHERE PARENT_ID = ? OR ORG_ID= ? AND IS_DEL='0'\n");*/
  280. /*sql.append("--查询给定id及其一级子机构的信息 \n");
  281. sql.append("WITH P AS(SELECT U.USER_ID \"id\",U.REAL_NAME \"name\",U.ORG_ID \"parentId\",'/nwyj/scripts/qui/libs/icons/home.png' \"icon\",U.POSITION \"orgLevel\", U.MOBILE \"perPhone\",U.GENDER \"isParent\",'per' \"leafType\" \n");
  282. sql.append("FROM SYS_USER_INFO U ,SYS_DEPARTMENT D,SYS_DEPT_USER DU \n");
  283. sql.append("WHERE U.USER_ID = DU.USER_ID AND D.DEPT_ID = DU.DEPT_ID AND D.DEPT_ID=? \n");
  284. sql.append("AND U.USER_ID NOT IN (SELECT INLINK_INDEX FROM EMC_AM_ORG_INLINK X \n");
  285. sql.append("WHERE X.VIRTUAL_ORG_ID =? AND X.IS_DEL ='0') ) , \n");
  286. sql.append("O AS(SELECT A.DEPT_ID ,A.DEPT_NAME ,A.PARENT_ID ,'/nwyj/scripts/qui/libs/icons/user-black.png' icon,A.DEPT_LEVEL,A.CORP_ID e, \n");
  287. sql.append("DECODE(DECODE((SELECT COUNT(1) FROM SYS_DEPARTMENT B WHERE B.PARENT_ID = A.DEPT_ID),0,0,1)+ \n");
  288. sql.append("DECODE((SELECT COUNT(1) FROM SYS_USER_INFO U ,SYS_DEPARTMENT D,SYS_DEPT_USER DU \n");
  289. sql.append("WHERE U.USER_ID = DU.USER_ID AND D.DEPT_ID = DU.DEPT_ID AND D.DEPT_ID=A.DEPT_ID \n");
  290. sql.append("AND U.USER_ID NOT IN (SELECT INLINK_INDEX FROM EMC_AM_ORG_INLINK X \n");
  291. sql.append("WHERE X.VIRTUAL_ORG_ID =? AND X.IS_DEL ='0') \n");
  292. sql.append("),0,0,1),0,'false','true') AS isParent,'org' leafType \n");
  293. sql.append("FROM SYS_DEPARTMENT A WHERE (A.PARENT_ID = ? OR A.DEPT_ID=?) AND A.IS_DEL='0') \n");
  294. sql.append("SELECT * FROM P \n");
  295. sql.append("UNION ALL \n");
  296. sql.append("SELECT * FROM O \n");*/
  297. sql.append("--查询给定id一级子机构的信息(不包括他本身) \n");
  298. sql.append("WITH T AS (SELECT * FROM SYS_DEPARTMENT WHERE INSTR(DEPT_PATH,'_'||?||'_')>0) \n");
  299. sql.append("SELECT A.DEPT_ID \"id\",A.DEPT_NAME \"name\",A.PARENT_ID \"parentId\",A.DEPT_LEVEL \"orgLevel\", \n");
  300. sql.append("DECODE((SELECT COUNT(1) FROM T B WHERE B.PARENT_ID = A.DEPT_ID),0,'false','true') AS \"isParent\" \n");
  301. sql.append("FROM T A WHERE A.PARENT_ID = ? OR A.DEPT_ID =? \n ");
  302. list = org.query(sql.toString(),id,id,id);
  303. System.out.println(org.listToString(list));
  304. } catch (SQLException e) {
  305. e.printStackTrace();
  306. }
  307. Map<String,String> map = new HashMap<String,String>();
  308. map.put("1", "2");
  309. map.put("2", "3");
  310. map.put("3", "4");
  311. String str = new ORGTemplate().mapToJson(map);
  312. System.out.println(str);
  313. }
  314. }