PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/website/control/StudentControl.ashx.cs

https://github.com/panmingzhi815/SchoolManagementSystem
C# | 265 lines | 236 code | 26 blank | 3 comment | 37 complexity | 58db2e562a941e5fe3ece28f58c271da MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Services;
  7. using System.Web.Services.Protocols;
  8. using System.Xml.Linq;
  9. using System.Reflection;
  10. using Domain.Entities;
  11. using DataService.service.dao;
  12. using DataService.util;
  13. using Newtonsoft.Json;
  14. using System.Web.Script.Serialization;
  15. using System.Collections.Generic;
  16. using System.Web.SessionState;
  17. namespace Domain.control
  18. {
  19. /// <summary>
  20. /// Summary description for $codebehindclassname$
  21. /// </summary>
  22. [WebService(Namespace = "http://tempuri.org/")]
  23. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  24. public class StudentControl : IHttpHandler, IRequiresSessionState
  25. {
  26. public HttpContext context;
  27. public void ProcessRequest(HttpContext context)
  28. {
  29. this.context = context;
  30. context.Response.ContentType = "text/plain";
  31. String method = context.Request.Form.Get("method");
  32. if (method == null)
  33. {
  34. method = context.Request.QueryString["method"];
  35. }
  36. switch (method)
  37. {
  38. case "addStudent":
  39. addStudent();
  40. break;
  41. case "delStudent":
  42. delStudent();
  43. break;
  44. case "updateStudent":
  45. updateStudent();
  46. break;
  47. case "getStudents":
  48. getStudents();
  49. break;
  50. case "getStudent":
  51. getStudent();
  52. break;
  53. case "updatePassword":
  54. updatePassword();
  55. break;
  56. default:
  57. context.Response.Write("-1");
  58. break;
  59. }
  60. }
  61. public bool IsReusable
  62. {
  63. get
  64. {
  65. return false;
  66. }
  67. }
  68. public void addStudent()
  69. {
  70. try
  71. {
  72. Student student = new Student();
  73. setValue(student, context);
  74. HttpPostedFile hpf = context.Request.Files["headImgFile"];
  75. if (hpf != null)
  76. {
  77. string serverPath = "/uploadFile/headImg/" + System.DateTime.Now.Ticks + "." + hpf.FileName.Split('.')[1];
  78. string savePath = context.Server.MapPath(serverPath);//路径,相对于服务器当前的路径
  79. hpf.SaveAs(savePath);//保存
  80. student.HeadImage = serverPath;
  81. }
  82. string FacultyID = context.Request.Form.Get("Faculty");
  83. DepartmentService ds = new DepartmentService();
  84. string professionID = context.Request.Form.Get("Profession");
  85. if (!string.IsNullOrEmpty(professionID))
  86. {
  87. Profession profession = ds.getProfessionByID(professionID);
  88. if (profession != null)
  89. student.Profession = profession;
  90. }
  91. string ClassGradeID = context.Request.Form.Get("ClassGrade");
  92. if (!string.IsNullOrEmpty(ClassGradeID))
  93. {
  94. ClassGrade classGrade = ds.getClassGradeByID(ClassGradeID);
  95. if (classGrade != null)
  96. student.ClassGrade = classGrade;
  97. }
  98. StudentService s = new StudentService();
  99. student.Password = student.Sn;
  100. s.save(student);
  101. context.Response.Write("1");
  102. }
  103. catch (Exception e)
  104. {
  105. context.Response.Write("0");
  106. }
  107. }
  108. public void delStudent()
  109. {
  110. try
  111. {
  112. string Id = context.Request.QueryString["Id"];
  113. StudentService service = new StudentService();
  114. service.del(service.get(typeof(Student), Id));
  115. context.Response.Write("1");
  116. }
  117. catch (Exception e)
  118. {
  119. context.Response.Write("0");
  120. }
  121. }
  122. public void updateStudent()
  123. {
  124. try
  125. {
  126. Student student = new Student();
  127. setValue(student, context);
  128. HttpPostedFile hpf = context.Request.Files["headImgFile"];
  129. if (hpf != null) {
  130. string savepath = context.Server.MapPath("/uploadFile/headImg/" + student.Id + "." + hpf.GetType());//路径,相对于服务器当前的路径
  131. hpf.SaveAs(savepath);//保存
  132. student.HeadImage = savepath;
  133. }
  134. DepartmentService ds = new DepartmentService();
  135. string professionID = context.Request.Form.Get("Profession");
  136. if (!string.IsNullOrEmpty(professionID)) {
  137. Profession profession = ds.getProfessionByID(professionID);
  138. if (profession != null)
  139. student.Profession = profession;
  140. }
  141. string ClassGradeID = context.Request.Form.Get("ClassGrade");
  142. if (!string.IsNullOrEmpty(ClassGradeID)) {
  143. ClassGrade classGrade = ds.getClassGradeByID(ClassGradeID);
  144. if (classGrade != null)
  145. student.ClassGrade = classGrade;
  146. }
  147. StudentService s = new StudentService();
  148. s.save(student);
  149. context.Response.Write("1");
  150. }
  151. catch (Exception e)
  152. {
  153. context.Response.Write("0");
  154. }
  155. }
  156. public void getStudent() {
  157. string Id = context.Request.QueryString["Id"];
  158. StudentService service = new StudentService();
  159. Student student = (Student)service.get(typeof(Student), Id);
  160. String json = JsonConvert.SerializeObject(student);
  161. context.Response.Write(json);
  162. }
  163. public void getStudents()
  164. {
  165. try
  166. {
  167. StudentService service = new StudentService();
  168. Student student = new Student();
  169. setValue(student, context);
  170. string ProfessionID = context.Request.Form.Get("FacultyID");
  171. string FacultyID = context.Request.Form.Get("ProfessionID");
  172. IList<Profession> professionList = new List<Profession>();
  173. DepartmentService ds = new DepartmentService();
  174. if (!string.IsNullOrEmpty(ProfessionID)) {
  175. Profession profession = ds.getProfessionByID(ProfessionID);
  176. if (profession != null) professionList.Add(profession);
  177. }
  178. else if (!string.IsNullOrEmpty(FacultyID)) {
  179. Faculty faculty = ds.getFacultyByID(FacultyID);
  180. if (faculty != null && faculty.professionList != null)
  181. foreach (Profession p in faculty.professionList)
  182. professionList.Add(p);
  183. }
  184. student.ProfessionList = professionList;
  185. int rows = Convert.ToInt32(context.Request.Form["rows"]);
  186. int page = Convert.ToInt32(context.Request.Form["page"]);
  187. object[] data = service.getStudentList(student, rows, page);
  188. Hashtable ht = new Hashtable();
  189. ht.Add("total", data[0]);
  190. ht.Add("rows", data[1]);
  191. String json = JsonConvert.SerializeObject(ht);
  192. context.Response.Write(json);
  193. }
  194. catch (Exception e)
  195. {
  196. }
  197. }
  198. public void updatePassword() {
  199. try {
  200. string Sn = context.Request.Form.Get("Sn");
  201. string Password = context.Request.Form.Get("Password");
  202. StudentService ss = new StudentService();
  203. ss.updatePassword(Sn, Password);
  204. context.Response.Write("1");
  205. }
  206. catch (Exception e) {
  207. context.Response.Write("0");
  208. }
  209. }
  210. public Object setValue(Object o, HttpContext context)
  211. {
  212. string[] keys = context.Request.Form.AllKeys;
  213. foreach (string s in keys)
  214. {
  215. try
  216. {
  217. PropertyInfo property = o.GetType().GetProperty(s);
  218. if (property == null)
  219. {
  220. continue;
  221. }
  222. if (property.PropertyType == typeof(DateTime))
  223. {
  224. property.SetValue(o, Convert.ToDateTime(context.Request.Form.Get(s)), null);
  225. }
  226. else if (property.PropertyType == typeof(string) || property.PropertyType == typeof(int))
  227. {
  228. property.SetValue(o, context.Request.Form.Get(s), null);
  229. }
  230. }
  231. catch (Exception e)
  232. {
  233. Console.Write(e.Message);
  234. }
  235. }
  236. return o;
  237. }
  238. }
  239. }