e7dc1a0eeb473fd227c75796945b3879d78779fc.svn-base 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. package com.sinosoft.lz.system.logon.dao;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import org.apache.log4j.Logger;
  9. import com.persistence.DbConnection;
  10. import com.sinosoft.lz.system.logon.vo.LoginInfo;
  11. public class LoginDao{
  12. private final Logger log = Logger.getLogger(getClass());
  13. public LoginInfo checkLoginPw(String userName, String password) {
  14. // 构造查询用户的sql语句
  15. StringBuffer sb = new StringBuffer();
  16. // 登录认证开始
  17. sb.append("select * from sys_user_info where login_name = ? and password = ? ");
  18. DbConnection dbConn = new DbConnection();
  19. Connection conn = null;
  20. PreparedStatement pstm = null;
  21. ResultSet rs = null;
  22. LoginInfo loginInfo = new LoginInfo();
  23. try {
  24. conn = dbConn.getConnection();
  25. pstm = conn.prepareStatement(sb.toString());
  26. //转化为MD5
  27. String md5psw= password.trim();
  28. pstm.setString(1, userName);
  29. pstm.setString(2, md5psw);
  30. rs = pstm.executeQuery();
  31. boolean flag = rs.next();
  32. if(flag){
  33. //还没超出限制,登录成功 设置st=0
  34. loginInfo.setSt("0");
  35. loginInfo.setFd_objectid(rs.getString("FD_OBJECTID"));
  36. loginInfo.setUser_id(rs.getString("USER_ID"));
  37. loginInfo.setLogin_name(rs.getString("LOGIN_NAME"));
  38. loginInfo.setPassword(rs.getString("PASSWORD"));
  39. loginInfo.setEmploy_no(rs.getString("EMPLOY_NO"));
  40. loginInfo.setReal_name(rs.getString("REAL_NAME"));
  41. loginInfo.setContingency_type(rs.getString("CONTINGENCY_TYPE"));
  42. loginInfo.setDept_id(rs.getString("DEPT_ID"));
  43. loginInfo.setOffice_phone(rs.getString("OFFICE_PHONE"));
  44. loginInfo.setMobile(rs.getString("MOBILE"));
  45. loginInfo.setEmail(rs.getString("EMAIL"));
  46. loginInfo.setCompany_id(rs.getString("COMPANY_ID"));
  47. loginInfo.setOrg_id(rs.getString("ORG_ID"));
  48. loginInfo.setPosition(rs.getString("POSITION"));
  49. loginInfo.setLogin_date(rs.getString("LOGIN_DATE"));
  50. loginInfo.setLogin_time(rs.getString("LOGIN_TIMES"));
  51. loginInfo.setSaphruserid(rs.getString("SAPHRUSERID"));
  52. log.info(loginInfo.toString());
  53. return loginInfo;
  54. }else{
  55. //找不到用户,设置3 密码错误
  56. loginInfo.setSt("1");
  57. return loginInfo;
  58. }
  59. } catch (Exception e) {
  60. log.error("根据用户密码查询用户信息错误:" + e.getMessage());
  61. } finally {
  62. try {
  63. rs.close();
  64. pstm.close();
  65. conn.close();
  66. } catch (SQLException e) {
  67. // TODO Auto-generated catch block
  68. log.error("关闭数据库连接错误:" + e.getMessage());
  69. }
  70. }
  71. return loginInfo;
  72. }
  73. public LoginInfo checkLogin(String userName) {
  74. // 构造查询用户的sql语句
  75. StringBuffer sb = new StringBuffer();
  76. // 登录认证开始
  77. sb.append("select * from sys_user_info where login_name = ? ");
  78. DbConnection dbConn = new DbConnection();
  79. Connection conn = null;
  80. PreparedStatement pstm = null;
  81. ResultSet rs = null;
  82. LoginInfo loginInfo = new LoginInfo();
  83. try {
  84. conn = dbConn.getConnection();
  85. pstm = conn.prepareStatement(sb.toString());
  86. //转化为MD5
  87. // String md5psw= password.trim();
  88. pstm.setString(1, userName);
  89. // pstm.setString(2, md5psw);
  90. rs = pstm.executeQuery();
  91. boolean flag = rs.next();
  92. if(flag){
  93. //还没超出限制,登录成功 设置st=0
  94. loginInfo.setSt("0");
  95. loginInfo.setFd_objectid(rs.getString("FD_OBJECTID"));
  96. loginInfo.setUser_id(rs.getString("USER_ID"));
  97. loginInfo.setLogin_name(rs.getString("LOGIN_NAME"));
  98. loginInfo.setPassword(rs.getString("PASSWORD"));
  99. loginInfo.setEmploy_no(rs.getString("EMPLOY_NO"));
  100. loginInfo.setReal_name(rs.getString("REAL_NAME"));
  101. loginInfo.setContingency_type(rs.getString("CONTINGENCY_TYPE"));
  102. loginInfo.setDept_id(rs.getString("DEPT_ID"));
  103. loginInfo.setOffice_phone(rs.getString("OFFICE_PHONE"));
  104. loginInfo.setMobile(rs.getString("MOBILE"));
  105. loginInfo.setEmail(rs.getString("EMAIL"));
  106. loginInfo.setCompany_id(rs.getString("COMPANY_ID"));
  107. loginInfo.setOrg_id(rs.getString("ORG_ID"));
  108. loginInfo.setPosition(rs.getString("POSITION"));
  109. loginInfo.setLogin_date(rs.getString("LOGIN_DATE"));
  110. loginInfo.setLogin_time(rs.getString("LOGIN_TIMES"));
  111. loginInfo.setSaphruserid(rs.getString("SAPHRUSERID"));
  112. log.info(loginInfo.toString());
  113. return loginInfo;
  114. }else{
  115. //找不到用户,设置3 密码错误
  116. loginInfo.setSt("3");
  117. return loginInfo;
  118. }
  119. } catch (Exception e) {
  120. log.error("根据用户密码查询用户信息错误:" + e.getMessage());
  121. } finally {
  122. try {
  123. rs.close();
  124. pstm.close();
  125. conn.close();
  126. } catch (SQLException e) {
  127. // TODO Auto-generated catch block
  128. log.error("关闭数据库连接错误:" + e.getMessage());
  129. }
  130. }
  131. return loginInfo;
  132. }
  133. public void setLoginDate(String login_name ){
  134. //String sql = "update sys_user_info set LOGIN_DATE = ? , LOGIN_TIMES = ? where login_name = ? ";
  135. String sql = "update sys_user_info set LOGIN_DATE = ? where login_name = ? ";
  136. DbConnection dbConn = new DbConnection();
  137. Connection conn = null;
  138. PreparedStatement pstm = null;
  139. try {
  140. conn = dbConn.getConnection();
  141. pstm = conn.prepareStatement(sql);
  142. //获取时间日期
  143. /*Date date = new Date();
  144. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  145. String[] dateStr = sdf.format(date).split(" ");
  146. String dateStr1 = dateStr[0];
  147. String dateStr2 = dateStr[1];*/
  148. Date date = new Date();
  149. SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  150. //SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
  151. String dateStr1 = sdf1.format(date);
  152. //String dateStr2 = sdf2.format(date);
  153. pstm.setString(1, dateStr1);
  154. //pstm.setString(2, dateStr2);
  155. pstm.setString(2, login_name);
  156. int result = pstm.executeUpdate();
  157. if(result == 1){
  158. log.info("保存时间成功");
  159. }
  160. } catch (ClassNotFoundException e) {
  161. // TODO Auto-generated catch block
  162. e.printStackTrace();
  163. } catch (SQLException e) {
  164. // TODO Auto-generated catch block
  165. e.printStackTrace();
  166. }finally{
  167. try {
  168. pstm.close();
  169. conn.close();
  170. } catch (SQLException e) {
  171. // TODO Auto-generated catch block
  172. log.error("关闭数据库连接错误:"+e.getMessage());
  173. }
  174. }
  175. }
  176. /*public LoginInfo checkLogin(String userName, String password) {
  177. // 构造查询用户的sql语句
  178. StringBuffer sb = new StringBuffer();
  179. // 登录认证开始
  180. sb.append("select decode(a.login_times-4,'1','1','0') as TIMESLIMIT,");
  181. sb.append("decode(sign(getDate()-(to_date(login_date)+NUMTODSINTERVAL(0.5, 'hour'))),1,'0','1') as TIMELIMIT,");
  182. sb.append("DECODE(A.PASSWORD,?,'1','0') AS PWD,");
  183. sb.append("FD_OBJECTID,USER_ID,LOGIN_NAME,PASSWORD,EMPLOY_NO,REAL_NAME,CONTINGENCY_TYPE,DEPT_ID,OFFICE_PHONE,MOBILE,EMAIL,COMPANY_ID,ORG_ID,POSITION,IS_DEL,MOD_DATE,LOGIN_DATE,"
  184. + "30-ROUND((getDate()-to_Date(LOGIN_DATE))*24*60) AS LASTLOGINDATE ,NVL(LOGIN_TIMES,'0') LOGIN_TIMES "
  185. + " from sys_user_info a where login_name = ? and is_del ='0'");
  186. //sb.append("and ((login_times <5) or (getDate()>to_date(login_date)+NUMTODSINTERVAL(0.5,'hour') and login_times>5))");
  187. //登陆后处理登录次数和登录时间
  188. StringBuffer sb2 = new StringBuffer();
  189. //sb2.append("update sys_user_info set login_times = ? , login_date=getDate() where login_name = ?");
  190. DbConnection dbConn = new DbConnection();
  191. Connection conn = null;
  192. PreparedStatement pstm = null;
  193. PreparedStatement pstm2 = null;
  194. ResultSet rs = null;
  195. LoginInfo loginInfo = new LoginInfo();
  196. try {
  197. conn = dbConn.getConnection();
  198. pstm = conn.prepareStatement(sb.toString());
  199. //转化为MD5
  200. String md5psw= password.trim();
  201. pstm.setString(2, userName);
  202. pstm.setString(1, md5psw);
  203. rs = pstm.executeQuery();
  204. *//**
  205. * rs存在那么表示用户名密码没问题
  206. *
  207. *//*
  208. boolean flag = rs.next();
  209. if(flag){
  210. //用户名密码正确
  211. boolean is_times_limit = "1".equals(rs.getString("TIMESLIMIT"));
  212. boolean is_time_limit = "1".equals(rs.getString("TIMELIMIT"));
  213. boolean is_try_many = is_times_limit && is_time_limit;
  214. boolean error_pwd = "0".equals(rs.getString("PWD"));
  215. //最后一次登录的时间
  216. String loginDate = rs.getString("LASTLOGINDATE");
  217. //剩下尝试次数
  218. String loginTimesLimit=(4-Integer.parseInt(rs.getString("LOGIN_TIMES")))+"";
  219. if(is_try_many){
  220. //设置st=1 表示半小时后再来尝试
  221. loginInfo.setSt("1");
  222. loginInfo.setMod_date(loginDate);
  223. return loginInfo;
  224. };
  225. if(error_pwd){
  226. //密码错误(如果记录登录时间大于30分钟 则清零)
  227. sb2.append("update sys_user_info set login_times = decode(sign(getDate()-(to_date(login_date)+NUMTODSINTERVAL(0.5, 'hour'))),1,'0',login_times+1) , login_date=getDate() where login_name = ?");
  228. pstm2 = conn.prepareStatement(sb2.toString());
  229. //记录登录次数
  230. pstm2.setString(1, userName);
  231. pstm2.executeUpdate();
  232. loginInfo.setSt("2");
  233. loginInfo.setMod_date(loginTimesLimit);
  234. return loginInfo;
  235. }else{
  236. //还没超出限制,登录成功 设置st=0
  237. loginInfo.setSt("0");
  238. loginInfo.setFd_objectid(rs.getString("FD_OBJECTID"));
  239. loginInfo.setUser_id(rs.getString("USER_ID"));
  240. loginInfo.setLogin_name(rs.getString("LOGIN_NAME"));
  241. loginInfo.setPassword(rs.getString("PASSWORD"));
  242. loginInfo.setEmploy_no(rs.getString("EMPLOY_NO"));
  243. loginInfo.setReal_name(rs.getString("REAL_NAME"));
  244. loginInfo.setContingency_type(rs.getString("CONTINGENCY_TYPE"));
  245. loginInfo.setDept_id(rs.getString("DEPT_ID"));
  246. loginInfo.setOffice_phone(rs.getString("OFFICE_PHONE"));
  247. loginInfo.setMobile(rs.getString("MOBILE"));
  248. loginInfo.setEmail(rs.getString("EMAIL"));
  249. loginInfo.setCompany_id(rs.getString("COMPANY_ID"));
  250. loginInfo.setOrg_id(rs.getString("ORG_ID"));
  251. loginInfo.setPosition(rs.getString("POSITION"));
  252. log.info(loginInfo.toString());
  253. sb2.append("update sys_user_info set login_times = '0' , login_date=getDate() where login_name = ?");
  254. pstm2 = conn.prepareStatement(sb2.toString());
  255. //清空登录次数
  256. pstm2.setString(1, userName);
  257. pstm2.executeUpdate();
  258. return loginInfo;
  259. }
  260. }else{
  261. //找不到用户,设置3 密码错误
  262. loginInfo.setSt("3");
  263. return loginInfo;
  264. }
  265. } catch (Exception e) {
  266. log.error("根据用户密码查询用户信息错误:" + e.getMessage());
  267. } finally {
  268. try {
  269. rs.close();
  270. pstm.close();
  271. if(pstm2!=null){
  272. pstm2.close();
  273. }
  274. conn.close();
  275. } catch (SQLException e) {
  276. // TODO Auto-generated catch block
  277. log.error("关闭数据库连接错误:" + e.getMessage());
  278. }
  279. }
  280. return loginInfo;
  281. }*/
  282. public String[] getRoleIdsByUserId(String userId) {
  283. StringBuffer sb = new StringBuffer();
  284. sb.append("select distinct r.role_id,r.role_name from sys_role r, sys_user_ROLE_REL ur where ur.role_id = r.role_id and ur.user_id = ? and ur.is_del='0'");
  285. DbConnection dbConn = new DbConnection();
  286. Connection conn = null;
  287. PreparedStatement pstm = null;
  288. ResultSet rs = null;
  289. String roleids = "";
  290. String rolenames = "";
  291. try {
  292. conn = dbConn.getConnection();
  293. log.info(sb.toString());
  294. pstm = conn.prepareStatement(sb.toString());
  295. pstm.setString(1, userId);
  296. rs = pstm.executeQuery();
  297. while (rs.next()) {
  298. roleids += rs.getString("role_id") + ",";
  299. rolenames += rs.getString("role_name") + ",";
  300. log.info(roleids);
  301. }
  302. if(roleids!=null && !"".equals(roleids)){
  303. roleids = roleids.substring(0,roleids.lastIndexOf(","));
  304. }
  305. if(rolenames!=null && !"".equals(rolenames)){
  306. rolenames = rolenames.substring(0,rolenames.lastIndexOf(","));
  307. }
  308. } catch (Exception e) {
  309. log.error("获取roleids错误:" + e.getMessage());
  310. e.printStackTrace();
  311. } finally {
  312. try {
  313. rs.close();
  314. pstm.close();
  315. conn.close();
  316. } catch (SQLException e) {
  317. // TODO Auto-generated catch block
  318. log.error("关闭数据库连接错误:" + e.getMessage());
  319. }
  320. }
  321. return new String[]{roleids,rolenames};
  322. }
  323. public String[] getDeptsByUserId(String userId) {
  324. StringBuffer sb = new StringBuffer();
  325. // sb.append("select d.dept_id,d.dept_name ,d.parent_id ,d.dept_type,d.corp_name,d.corp_id from SYS_DEPARTMENT d ,SYS_DEPT_USER du where du.dept_id = d.dept_id and du.user_id = ? order by d.dept_id ");
  326. sb.append(" select distinct ");
  327. sb.append(" d.dept_id , ");
  328. sb.append(" d.dept_name, ");
  329. sb.append(" d.parent_id, ");
  330. sb.append(" d.dept_type, ");
  331. sb.append(" d.corp_name, ");
  332. sb.append(" d.corp_id, ");
  333. sb.append(" pd.corp_id as pcorp_id, ");
  334. sb.append(" pd.corp_name as pcorp_name ");
  335. sb.append(" from ");
  336. sb.append(" SYS_USER_INFO du ");
  337. sb.append(" left join SYS_DEPARTMENT d on du.dept_id = d.dept_id ");
  338. sb.append(" left join SYS_DEPARTMENT cd on cd.dept_id = d.corp_id ");
  339. sb.append(" left join SYS_DEPARTMENT pd on pd.dept_id = cd.parent_id ");
  340. sb.append(" where ");
  341. sb.append(" du.user_id = ? ");
  342. sb.append(" order by ");
  343. sb.append(" d.dept_id ");
  344. DbConnection dbConn = new DbConnection();
  345. Connection conn = null;
  346. PreparedStatement pstm = null;
  347. ResultSet rs = null;
  348. String deptids = "";
  349. String deptnames = "";
  350. String parent_ids = "";
  351. String dept_types = "";
  352. String corp_ids = "";
  353. String corp_names = "";
  354. String pcorp_ids = "";
  355. String pcorp_names = "";
  356. try {
  357. conn = dbConn.getConnection();
  358. pstm = conn.prepareStatement(sb.toString());
  359. pstm.setString(1, userId);
  360. rs = pstm.executeQuery();
  361. while (rs.next()) {
  362. if(!"".equals(rs.getString("dept_id"))&&rs.getString("dept_id")!=null&&!"null".equals(rs.getString("dept_id"))){
  363. deptids += rs.getString("dept_id") + ",";
  364. deptnames += rs.getString("dept_name") + ",";
  365. parent_ids += rs.getString("parent_id") + ",";
  366. dept_types += rs.getString("dept_type") + ",";
  367. corp_ids += rs.getString("corp_id") + ",";
  368. corp_names += rs.getString("corp_name") + ",";
  369. pcorp_ids += rs.getString("pcorp_id") + ",";
  370. pcorp_names += rs.getString("pcorp_name") + ",";
  371. log.info(deptids);
  372. }
  373. }
  374. if(deptids!=null&&!"".equals(deptids)){
  375. deptids = deptids.substring(0, deptids.lastIndexOf(","));
  376. }
  377. if(deptnames!=null&&!"".equals(deptnames)){
  378. deptnames = deptnames.substring(0, deptnames.lastIndexOf(","));
  379. }
  380. if(parent_ids!=null&&!"".equals(parent_ids)){
  381. parent_ids = parent_ids.substring(0,parent_ids.lastIndexOf(","));
  382. }
  383. if(dept_types!=null&&!"".equals(dept_types)){
  384. dept_types = dept_types.substring(0,dept_types.lastIndexOf(","));
  385. }
  386. if(corp_ids!=null&&!"".equals(corp_ids)){
  387. corp_ids = corp_ids.substring(0,corp_ids.lastIndexOf(","));
  388. }
  389. if(corp_names!=null&&!"".equals(corp_names)){
  390. corp_names = corp_names.substring(0,corp_names.lastIndexOf(","));
  391. }
  392. if(pcorp_ids!=null&&!"".equals(pcorp_ids)){
  393. pcorp_ids = pcorp_ids.substring(0,pcorp_ids.lastIndexOf(","));
  394. }
  395. if(pcorp_names!=null&&!"".equals(pcorp_names)){
  396. pcorp_names = pcorp_names.substring(0,pcorp_names.lastIndexOf(","));
  397. }
  398. } catch (Exception e) {
  399. log.error("获取deptids错误:" + e.getMessage());
  400. e.printStackTrace();
  401. } finally {
  402. try {
  403. rs.close();
  404. pstm.close();
  405. conn.close();
  406. } catch (SQLException e) {
  407. // TODO Auto-generated catch block
  408. log.error("关闭数据库连接错误:" + e.getMessage());
  409. }
  410. }
  411. return new String[] { deptids, deptnames ,parent_ids,dept_types,corp_ids,corp_names,pcorp_ids,pcorp_names};
  412. }
  413. }