DeparuserAddAction.java~78~
上传用户:dlqqsh
上传日期:2021-11-13
资源大小:7840k
文件大小:8k
源码类别:

OA系统

开发平台:

Java

  1. package officeol.mc.actions;
  2. import org.apache.struts.action.ActionMapping;
  3. import org.apache.struts.action.ActionForm;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts.action.ActionForward;
  7. import org.apache.struts.action.Action;
  8. import officeol.mc.tools.*;
  9. public class DeparuserAddAction extends Action {
  10.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  11.                                  HttpServletRequest request,
  12.                                  HttpServletResponse response) {
  13.         try {
  14.             request.setCharacterEncoding("GBK");
  15.             DBConn dbc = new DBConn();
  16.             String dep = request.getParameter("dep");
  17.             int jj = Integer.parseInt(request.getParameter("j"));
  18.             String[] uids = new String[jj];
  19.             for (int i = 0; i < uids.length; i++) {
  20.                 uids[i] = request.getParameter("un" + i);
  21.             }
  22.             String[] sql = new String[jj];
  23.             String checksql =
  24.                     "select userid from userdepar where deparid =" + dep;
  25.             String udsql1 = "update [user] set deparment = 1 where id = ";
  26.             String udsql0 = "update [user] set deparment = 0 where id = ";
  27.             String temping = "";
  28.             String[][] checkInfo = dbc.getArray(checksql);
  29.             if(checkInfo!=null){
  30.                 int type = 0; //修改类型0不变,1添加,2删除
  31.                 int len = 0;
  32.                 if (uids.length <= checkInfo.length) {
  33.                     len = checkInfo.length;
  34.                 } else {
  35.                     len = uids.length;
  36.                 }
  37.                 String[] same = new String[len];
  38.                 int j = 0;
  39.                 //得出相同的,不用修改的权限ID
  40.                 for (int x = 0; x < checkInfo.length; x++) {
  41.                     for (int i = 0; i < uids.length; i++) {
  42.                         String tempsql1 = "";
  43.                         if (uids[i] != null) {
  44.                             if (checkInfo[x][0].equals(uids[i])) {
  45.                                 same[j] = checkInfo[x][0];
  46.                                 j++;
  47.                                 //System.out.println(checkInfo[x][0]);
  48.                                 continue;
  49.                             }
  50.                         }
  51.                     }
  52.                 }
  53.                 int l = 0;
  54.                 int h = 0;
  55.                 for (int i = 0; i < uids.length; i++) {
  56.                     //得出应该向数据库添加的
  57.                     String tempsql1 = "";
  58.                     String temp = "a";
  59.                     for (int k = l; k < same.length; k++) {
  60.                         h = l;
  61.                         if (same[k] != null && uids[i] != null) {
  62.                             if (same[k].equals(uids[i])) {
  63.                                 l++;
  64.                                 break;
  65.                             } else {
  66.                                 temp = "b";
  67.                                 tempsql1 =
  68.                                         "insert into userdepar (userid,deparid) values (" +
  69.                                         uids[i] + ",'" + dep + "')";
  70.                                 //System.out.println(tempsql1);
  71.                                 dbc.executeUpdate(tempsql1);
  72.                                 temping = "";
  73.                                 temping = udsql1 + "'"+uids[i]+"'";
  74.                                 dbc.executeUpdate(temping);
  75.                                 //System.out.println("dd" + ids[i]);
  76.                                 break;
  77.                             }
  78.                         }
  79.                     }
  80.                     if (l <= i && l == h) {
  81.                         if (uids[i] != null) {
  82.                             if (temp.equals("a")) {
  83.                                 tempsql1 =
  84.                                         "insert into userdepar (userid,deparid) values (" +
  85.                                         uids[i] + ",'" + dep + "')";
  86.                                 //System.out.println(tempsql1);
  87.                                 dbc.executeUpdate(tempsql1);
  88.                                 temping = "";
  89.                                 temping = udsql1 + "'"+uids[i]+"'";
  90.                                 dbc.executeUpdate(temping);
  91.                                 //System.out.println("dd" + ids[i]);
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.                 l = 0;
  97.                 h = 0;
  98.                 for (int i = 0; i < checkInfo.length; i++) {
  99.                     //得出应该删除的
  100.                     String temp = "a";
  101.                     String tempsql2 = "";
  102.                     for (int k = l; k < same.length; k++) {
  103.                         h = l;
  104.                         if (same[k] != null && checkInfo[i][0] != null) {
  105.                             if (same[k].equals(checkInfo[i][0])) {
  106.                                 l++;
  107.                                 break;
  108.                             } else {
  109.                                 temp = "b";
  110.                                 tempsql2 =
  111.                                         "delete from userdepar where deparid = " +
  112.                                         dep + " and userid =" + checkInfo[i][0];
  113.                                 //System.out.println(tempsql2);
  114.                                 dbc.executeUpdate(tempsql2);
  115.                                 temping = "";
  116.                                 temping = udsql0 + "'"+uids[i]+"'";
  117.                                 dbc.executeUpdate(temping);
  118.                                 //System.out.println("ttt" + checkInfo[i][0]);
  119.                                 break;
  120.                             }
  121.                         }
  122.                     }
  123.                     if (l <= i && l == h) {
  124.                         if (checkInfo[i][0] != null)
  125.                             if (!temp.equals("b")) {
  126.                                 tempsql2 =
  127.                                         "delete from userdepar where deparid = " +
  128.                                         dep + " and userid =" + checkInfo[i][0];
  129.                                 //System.out.println(tempsql2);
  130.                                 temping = "";
  131.                                temping = udsql0 + "'"+uids[i]+"'";
  132.                                dbc.executeUpdate(temping);
  133.                                dbc.executeUpdate(tempsql2);
  134.                                 //System.out.println("ttt" + checkInfo[i][0]);
  135.                             }
  136.                     }
  137.                 }
  138.             }else{
  139.                 String[] un = new String[jj];
  140.                 String temp = "emp";
  141.                 for (int i = 0; i < jj; i++) {
  142.                     un[i] = request.getParameter("un" + i);
  143.                     if (un[i] != null) {
  144.                         temp = "notemp";
  145.                         String acheck =
  146.                                 "select * from userdepar where deparid=" +
  147.                                 dep + " and userid = " + un[i];
  148.                         //System.out.println(acheck);
  149.                         String[][] check = dbc.getArray(acheck);
  150.                         if (check == null) {
  151.                             String asql =
  152.                                     "insert into userdepar (deparid,userid) values (" +
  153.                                     dep + "," + un[i] + ")";
  154.                             //System.out.println(asql);
  155.                             temping = "";
  156.                             temping = udsql1 + "'" + uids[i] + "'";
  157.                             dbc.executeUpdate(temping);
  158.                             dbc.executeUpdate(asql);
  159.                         } else {
  160.                             request.setAttribute("msg", "用户已经添加,请不要重复添加!");
  161.                         }
  162.                     }
  163.                 }
  164.                 if (temp.equals("emp")) {
  165.                     request.setAttribute("msg", "用户名不能为空!");
  166.                     return mapping.findForward("succ");
  167.                 }
  168.             }
  169.         } catch (Exception ex) {
  170.             ex.printStackTrace();
  171.         }
  172.         request.setAttribute("msg", "修改成功!");
  173.         return mapping.findForward("succ");
  174.     }
  175. }