bbc39faef5010d2c1a142391c7a7a7f39ca18c94.svn-base 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /**
  2. *
  3. */
  4. package com.sinosoft.am.org.dept.outdept.dao;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import com.persistence.DbConnection;
  12. import com.sinosoft.am.org.dept.indept.dao.OrgInDeptDao;
  13. import com.sinosoft.am.org.dept.outdept.vo.OrgOutDeptVo;
  14. import org.apache.log4j.Logger;
  15. /**
  16. *
  17. * @author 蒋云涛
  18. *
  19. */
  20. public class OrgOutDeptDao{
  21. /* log信息 */
  22. private Logger log = Logger.getLogger(OrgInDeptDao.class);
  23. /**
  24. * 根据给定节点的id,获得下面的子机构;
  25. *
  26. * @param parentId
  27. * @return
  28. */
  29. public ArrayList<OrgOutDeptVo> getOrgOutDeptList(String parentId) {
  30. Connection conn = null;
  31. Statement st = null;
  32. ResultSet rs = null;
  33. DbConnection db = new DbConnection();
  34. StringBuffer sql = new StringBuffer();
  35. ArrayList<OrgOutDeptVo> list = new ArrayList<OrgOutDeptVo>();
  36. sql.append("--查询外部组织机构 \n");
  37. sql.append("SELECT * FROM EMC_AM_OUT_DEPARTMENT WHERE IS_DEL='0' \n");
  38. sql.append("AND PARENT_ID ='" + parentId + "'");
  39. log.info(sql);
  40. try {
  41. conn = db.getConnection();
  42. st = conn.createStatement();
  43. rs = st.executeQuery(sql.toString());
  44. while (rs.next()) {
  45. list.add(getOrgOutDeptVo(rs));
  46. }
  47. return list;
  48. } catch (Exception e) {
  49. log.error("查询内部组织机构根节点错误", e);
  50. } finally {
  51. db.close(conn);
  52. db.close(st);
  53. db.close(rs);
  54. }
  55. return null;
  56. }
  57. /**
  58. * 新增外部机构只是简单通过objectId parentId deptId新增
  59. *
  60. * @param deptId
  61. * @param parentId
  62. * @param deptName
  63. * @return 是否新增成功
  64. */
  65. public boolean addOrgOutDept(String deptId, String parentId, String deptName) {
  66. Connection conn = null;
  67. PreparedStatement ps = null;
  68. DbConnection db = new DbConnection();
  69. StringBuffer sql = new StringBuffer();
  70. /*String objectId = UUID.randomUUID().toString();*/
  71. String objectId = deptId;
  72. sql.append("--新增外部组织机构 \n");
  73. sql.append("INSERT INTO EMC_AM_OUT_DEPARTMENT(FD_OBJECTID,DEPT_ID,PARENT_ID,ORG_DEPT_NAME,IS_DEL)VALUES(?,?,?,?,?) \n");
  74. log.info(sql + "-------------------sql");
  75. try {
  76. conn = db.getConnection();
  77. ps = conn.prepareStatement(sql.toString());
  78. ps.setString(1, objectId);
  79. ps.setString(2, deptId);
  80. ps.setString(3, parentId);
  81. ps.setString(4, deptName);
  82. ps.setString(5, "0");
  83. return ps.executeUpdate() == 1;
  84. } catch (Exception e) {
  85. log.error("新增内部组织机构根节点错误", e);
  86. } finally {
  87. db.close(conn);
  88. db.close(ps);
  89. }
  90. return false;
  91. }
  92. /**
  93. * 修改外部机构名称
  94. *
  95. * @param deptId
  96. * @param parentId
  97. * @param deptName
  98. * @return
  99. */
  100. public boolean editOrgOutDeptName(String deptId, String deptName) {
  101. Connection conn = null;
  102. PreparedStatement ps = null;
  103. DbConnection db = new DbConnection();
  104. StringBuffer sql = new StringBuffer();
  105. sql.append("--修改外部组织机构名称 \n");
  106. sql.append("UPDATE EMC_AM_OUT_DEPARTMENT SET ORG_DEPT_NAME=? WHERE DEPT_ID=? \n");
  107. log.info(sql + "-------------------sql");
  108. try {
  109. conn = db.getConnection();
  110. ps = conn.prepareStatement(sql.toString());
  111. ps.setString(1, deptName);
  112. ps.setString(2, deptId);
  113. return ps.executeUpdate() == 1;
  114. } catch (Exception e) {
  115. log.error("修改内部组织机构根节点错误", e);
  116. } finally {
  117. db.close(conn);
  118. db.close(ps);
  119. }
  120. return false;
  121. }
  122. public boolean updateOrgOutDeptName(String deptId, String deptName) {
  123. Connection conn = null;
  124. PreparedStatement ps = null;
  125. PreparedStatement ps2 = null;
  126. DbConnection db = new DbConnection();
  127. //修改机构名称sql语句
  128. StringBuffer sql1 = new StringBuffer();
  129. //修改外部人员表的 部门 和部门名称
  130. StringBuffer sql2 = new StringBuffer();
  131. sql1.append("--修改外部组织机构名称 \n");
  132. sql1.append("UPDATE EMC_AM_OUT_DEPARTMENT SET ORG_DEPT_NAME=? WHERE DEPT_ID=? AND IS_DEL=? \n");
  133. log.info(sql1 + "-------------------sql1");
  134. sql2.append("--修改外部人员表的机构名称 \n");
  135. sql2.append("UPDATE EMC_AM_ORG_OUTLINK SET ORG_NAME=? WHERE ORG_DEPT_ID=? \n");
  136. log.info(sql2 + "-------------------sql2");
  137. try {
  138. //原子性还没做
  139. conn = db.getConnection();
  140. // conn.setAutoCommit(false);
  141. ps = conn.prepareStatement(sql1.toString());
  142. ps.setString(1, deptName);
  143. ps.setString(2, deptId);
  144. ps.setString(3,"0");
  145. boolean updateOrgName = ps.executeUpdate()>=1;
  146. boolean updateOutlinkName=true;
  147. if(hasStaff(deptId)){
  148. ps2 = conn.prepareStatement(sql2.toString());
  149. ps2.setString(1, deptName);
  150. ps2.setString(2, deptId);
  151. updateOutlinkName = ps2.executeUpdate()>=1;
  152. }
  153. boolean isUpdate = updateOrgName && updateOutlinkName;
  154. // conn.commit();
  155. return isUpdate;
  156. } catch (Exception e) {
  157. log.error("修改外部组织机构的名称出错", e);
  158. } finally {
  159. db.close(conn);
  160. db.close(ps);
  161. db.close(ps2);
  162. }
  163. return false;
  164. }
  165. /**
  166. * 通过部门Id删除外部机构
  167. *
  168. * @param deptId
  169. * @return 是否成功删除外部机构
  170. */
  171. public boolean deleteOrgOutDept(String deptId) {
  172. Connection conn = null;
  173. PreparedStatement ps = null;
  174. DbConnection db = new DbConnection();
  175. StringBuffer sql = new StringBuffer();
  176. sql.append("--删除外部组织机构名称 \n");
  177. sql.append("DELETE FROM EMC_AM_OUT_DEPARTMENT WHERE DEPT_ID=? \n");
  178. log.info(sql + "-------------------sql");
  179. try {
  180. conn = db.getConnection();
  181. ps = conn.prepareStatement(sql.toString());
  182. ps.setString(1, deptId);
  183. return ps.executeUpdate() == 1;
  184. } catch (Exception e) {
  185. log.error("删除内部组织机构根节点错误", e);
  186. } finally {
  187. db.close(conn);
  188. db.close(ps);
  189. }
  190. return false;
  191. }
  192. /**
  193. * 删除外部机构--修改删除状态为"1"
  194. * @param deptId
  195. * @return true为删除成功;false为删除失败;
  196. */
  197. public boolean shamDeleteOrgOutDept(String deptId){
  198. //先进行判断能否删除(判断机构下是否还有子机构存在)
  199. if(hasChildNode(deptId)){
  200. return false;
  201. }
  202. //判断机构下是否还有员工存在
  203. if(hasStaff(deptId)){
  204. return false;
  205. }
  206. Connection conn = null;
  207. PreparedStatement ps = null;
  208. DbConnection db = new DbConnection();
  209. StringBuffer sql = new StringBuffer();
  210. sql.append("--删除外部组织机构名称 \n");
  211. sql.append("UPDATE EMC_AM_OUT_DEPARTMENT SET IS_DEL=? WHERE FD_OBJECTID=? \n");
  212. log.info(sql + "-------------------sql");
  213. try {
  214. conn = db.getConnection();
  215. ps = conn.prepareStatement(sql.toString());
  216. ps.setString(1, "1");
  217. ps.setString(2, deptId);
  218. return ps.executeUpdate() == 1;
  219. } catch (Exception e) {
  220. log.error("删除外部组织机构节点错误", e);
  221. } finally {
  222. db.close(conn);
  223. db.close(ps);
  224. }
  225. return false;
  226. }
  227. /**
  228. * @param deptId 部门id
  229. * @return 是否存在员工
  230. */
  231. private boolean hasStaff(String deptId) {
  232. Connection conn = null;
  233. PreparedStatement ps = null;
  234. ResultSet rs = null;
  235. DbConnection db = new DbConnection();
  236. StringBuffer sql = new StringBuffer();
  237. sql.append("--查询该机构是否有没有删除的员工 \n");
  238. sql.append("SELECT COUNT(1) c FROM EMC_AM_ORG_OUTLINK WHERE ORG_DEPT_ID=? AND IS_DEL=? \n");
  239. try {
  240. conn = db.getConnection();
  241. ps = conn.prepareStatement(sql.toString());
  242. ps.setString(1, deptId);
  243. ps.setString(2, "0");
  244. log.info("查询内部机构是否有未删除的员工sql="+sql);
  245. rs =ps.executeQuery();
  246. rs.next();
  247. //log.info("内部机构是否有员工"+rs.getBoolean(1));
  248. return rs.getBoolean(1);
  249. } catch (Exception e) {
  250. log.error("查询内部机构是否存在员工", e);
  251. } finally {
  252. db.close(conn);
  253. db.close(ps);
  254. db.close(rs);
  255. }
  256. return false;
  257. }
  258. /**
  259. *
  260. * @param deptId
  261. * @return 是否存在未删除的员工
  262. */
  263. private boolean hasChildNode(String deptId) {
  264. Connection conn = null;
  265. PreparedStatement ps = null;
  266. ResultSet rs = null;
  267. DbConnection db = new DbConnection();
  268. StringBuffer sql = new StringBuffer();
  269. sql.append("--查询该外部机构是否有子机构 \n");
  270. sql.append("SELECT COUNT(1) c FROM EMC_AM_OUT_DEPARTMENT WHERE PARENT_ID=? AND IS_DEL=? \n");
  271. try {
  272. conn = db.getConnection();
  273. ps = conn.prepareStatement(sql.toString());
  274. ps.setString(1, deptId);
  275. ps.setString(2, "0");
  276. log.info("查询该外部机构是否有子机构 sql="+sql);
  277. rs =ps.executeQuery();
  278. rs.next();
  279. //log.info("是否有子机构"+rs.getBoolean(1));
  280. return rs.getBoolean(1);
  281. } catch (Exception e) {
  282. log.error("查询是否存在未删除的外部机构", e);
  283. } finally {
  284. db.close(conn);
  285. db.close(ps);
  286. db.close(rs);
  287. }
  288. return false;
  289. }
  290. /**
  291. * 将result结果集封装到实体类里面
  292. *
  293. * @param rs
  294. * @return
  295. * @throws SQLException
  296. */
  297. private OrgOutDeptVo getOrgOutDeptVo(ResultSet rs) throws SQLException {
  298. OrgOutDeptVo ov = new OrgOutDeptVo();
  299. ov.setCode(rs.getString("CODE"));
  300. ov.setDeptId(rs.getString("DEPT_ID"));
  301. ov.setFdObjectid(rs.getString("FD_OBJECTID"));
  302. ov.setIdDel(rs.getString("IS_DEL"));
  303. ov.setLinkDept(rs.getString("LINK_DEPT"));
  304. ov.setLinkMan(rs.getString("LINK_MAN"));
  305. ov.setLocation(rs.getString("LOCATION"));
  306. ov.setOrgDeptName(rs.getString("ORG_DEPT_NAME"));
  307. ov.setOrgDeptType(rs.getString("ORG_DEPT_TYPE"));
  308. ov.setParentId(rs.getString("PARENT_ID"));
  309. ov.setSite(rs.getString("SITE"));
  310. ov.setUpdateDate(rs.getString("UPDATEDATE"));
  311. ov.setWorkAddress(rs.getString("WORK_ADDRESS"));
  312. return ov;
  313. }
  314. }