예제 #1
0
 public string addSingleProfessor(string name, string number, string title, string url, int needstudent, string passwd = "12345")
 {
     try
     {
         ProfessorDao professorDao = new ProfessorDao();
         string       res          = "";
         Professor    professor    = new Professor();
         professor.name     = name;
         professor.id       = number;
         professor.title    = title;
         professor.infoURL  = url;
         professor.password = CryptoUtil.Md5Hash(passwd);
         professor.quota    = needstudent;
         Exception e = new Exception("教师id重复");
         if (professorDao.getProfessorById(number) != null)
         {
             throw (e);
         }
         else
         {
             res = "success";
             professorDao.addProfessor(professor);
         }
         return(res);
     }
     catch (Exception e)
     {
         LogUtil.writeLogToFile(e, Request);
         return("平台出现异常,请联系管理员:XXX");
     }
 }
        public void TestMethod_AddProfessor()
        {
            ProfessorDao professorDao = new ProfessorDao();
            Professor    professor_1  = new Professor();

            professor_1.id       = "zzw16211094";
            professor_1.name     = "zzw";
            professor_1.password = "******";
            professor_1.remark   = "test";
            professor_1.quota    = 0;
            professor_1.infoURL  = "oldinfo";
            professor_1.title    = "software";
            int result2 = professorDao.addProfessor(professor_1);

            Assert.AreEqual(1, result2);

            Professor professor_2 = new Professor();

            professor_2.id       = "jyf16211084";
            professor_2.name     = "jyf";
            professor_2.password = "******";
            professor_2.remark   = "test";
            professor_2.infoURL  = "oldinfo";
            professor_2.title    = "software";
            professor_2.quota    = 0;
            int result3 = professorDao.addProfessor(professor_2);

            Professor professor_3 = new Professor();

            professor_3.id       = "xzy16211083";
            professor_3.name     = "xzy";
            professor_3.password = "******";
            professor_3.remark   = "test";
            professor_3.infoURL  = "oldinfo";
            professor_3.title    = "software";
            professor_3.quota    = 0;
            int result4 = professorDao.addProfessor(professor_3);
        }
예제 #3
0
        public string batchCreateTeachers(HttpPostedFileBase file)
        {
            var severPath = this.Server.MapPath("/ExcelFiles/");

            if (!Directory.Exists(severPath))
            {
                Directory.CreateDirectory(severPath);
            }
            var              savePath  = Path.Combine(severPath, file.FileName);
            Professor        professor = null;
            string           result    = "{}";
            bool             flag      = false;
            List <Professor> proList   = new List <Professor>();
            Workbook         workbook  = new Workbook();
            Worksheet        sheet     = null;
            int              error     = 0;

            Response.ContentType = "application/json";
            Response.Charset     = "utf-8";

            try
            {
                if (string.Empty.Equals(file.FileName) || (".xls" != Path.GetExtension(file.FileName) && ".xlsx" != Path.GetExtension(file.FileName)))
                {
                    throw new Exception("文件格式不正确");
                }

                file.SaveAs(savePath);
                workbook.LoadFromFile(savePath);
                sheet = workbook.Worksheets[0];
                int          row = sheet.Rows.Length;    //获取不为空的行数
                int          col = sheet.Columns.Length; //获取不为空的列数
                string       tempId;
                string       tempName;
                string       tempTitle;
                string       tempUrl;
                string       tempQuota;
                string       tempPass;
                int          idcol        = -11;
                int          namecol      = -11;
                int          titlecol     = -11;
                int          idrow        = -11;
                int          urlcol       = -11;
                int          quotacol     = -11;
                int          passwordcol  = -11;
                ProfessorDao professorDao = new ProfessorDao();
                CellRange[]  cellrange    = sheet.Cells;
                int          rangelength  = cellrange.Length;
                for (int i = 0; i < row; i++)
                {
                    for (int j = 0; j < col; j++)
                    {
                        tempId = cellrange[i * col + j].Value;
                        if (tempId.Equals("工号"))
                        {
                            idcol = j;
                            idrow = i + 1;
                        }
                        if (tempId.Equals("姓名"))
                        {
                            namecol = j;
                        }
                        if (tempId.Equals("职称"))
                        {
                            titlecol = j;
                        }
                        if (tempId.Equals("介绍页面url"))
                        {
                            urlcol = j;
                        }
                        if (tempId.Equals("最大招收学生数"))
                        {
                            quotacol = j;
                        }
                        if (tempId.Equals("密码"))
                        {
                            passwordcol = j;
                        }
                    }
                    if (idcol >= 0 && namecol >= 0)
                    {
                        break;
                    }
                }

                if (idcol < 0 || namecol < 0)
                {
                    throw new Exception("表格格式不正确");
                }
                for (int i = idrow; i < row; i++)
                {
                    tempId   = cellrange[i * col + idcol].Value;
                    tempName = cellrange[i * col + namecol].Value;

                    tempTitle = cellrange[i * col + titlecol].Value;
                    tempUrl   = cellrange[i * col + urlcol].Value;
                    tempQuota = cellrange[i * col + quotacol].Value;
                    tempPass  = cellrange[i * col + passwordcol].Value;
                    if (professorDao.getProfessorById(tempId) != null)
                    {
                        flag    = true;
                        result += "已存在教师:id:" + tempId + " 姓名:" + tempName + " 专业:" + tempId + "\n";
                        continue;
                    }
                    if (tempName != "")
                    {
                        professor          = new Professor();
                        professor.id       = tempId;
                        professor.name     = tempName;
                        professor.title    = tempTitle;
                        professor.infoURL  = tempUrl;
                        professor.quota    = int.Parse(tempQuota);
                        professor.password = CryptoUtil.Md5Hash(tempPass);
                        error = professorDao.addProfessor(professor);
                        if (error < 1)
                        {
                            throw new Exception("数据库更新出错");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogUtil.writeLogToFile(e, Request);
                return("{\"error\":\"" + e.Message + "\"}");
            }
            finally
            {
                workbook.Dispose();
                sheet    = null;
                workbook = null;
            }
            if (flag)
            {
                return("{\"error\":\"" + result + "\"}");
            }
            return("{" +
                   "\"initialPreview\":" +
                   "[\"<div style=\\\"text-align:center;padding:50px 25px;color:#00a65a\\\"><i class=\\\"fa fa-check-square-o\\\" style=\\\"font-size:60px;opacity:0.6\\\"></i><p style=\\\"padding-top:10px;font-size:18px\\\">添加成功</p></div>\"]" +
                   "}");
        }