public ActionResult DoAddTempUser(FormCollection collection) { /* * 参数获取 */ // 姓名 var name = collection["name"]; // 身份证号 var credNo = collection["credNo"]; /* * 参数校验 */ // 姓名 if (string.IsNullOrEmpty(name)) { return(ResponseUtil.Error(400, "姓名不能为空")); } /* * 查询重复 */ using (var db = new YGSDbContext()) { // 身份证号 if (string.IsNullOrEmpty(credNo)) { // 身份证号为空,说明只填写了姓名,直接建立新用户,返回新用户ID给前端 var user = new YGS_User(); user.Name = name; user.CreateTime = DateTime.Now; db.User.Add(user); db.SaveChanges(); return(new JsonNetResult(new { code = 200, data = new { id = user.ID } })); } else { // 校验身份证格式 if ((!Regex.IsMatch(credNo, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase))) { return(ResponseUtil.Error(400, "身份证格式不正确")); } // 输入了身份证号,则进行校验,身份信息是否正确。 // 如果身份证与姓名一致,则返回用户ID,如果不正确,则该用户已存在,输入身份证重复 var user = db.User.Where(n => n.CredNo == credNo).FirstOrDefault(); if (user == null) { user = new YGS_User(); user.Name = name; user.CredNo = credNo; user.CreateTime = DateTime.Now; db.User.Add(user); db.SaveChanges(); return(new JsonNetResult(new { code = 200, data = new { id = user.ID } })); } else { if (user.Name == name) { return(new JsonNetResult(new { code = 200, data = new { id = user.ID } })); } else { // 不同人,同身份证 return(ResponseUtil.Error(400, "已存在身份证相同的其他用户")); } } } } }
public ActionResult DoAdd(FormCollection collection) { /* * 变量定义 */ // 性别 int sex = 0; // 出生日期 DateTime birthday = new DateTime(); /* * 参数获取 */ // 姓名 var name = collection["name"]; // 性别 var sexString = collection[@"sex"]; // 出生地 var location = collection["location"]; // 出生日期 var birthDay = collection["birthDay"]; // 身份证号 var credNo = collection["credNo"]; // 工作单位 var unit = collection["unit"]; // 工作部门 var depart = collection["depart"]; // 级别 var level = collection["level"]; // 职务 var duty = collection["duty"]; /* * 参数校验 */ // 姓名 if (string.IsNullOrEmpty(name)) { return(ResponseUtil.Error(400, "姓名不能为空")); } // 性别 if (!string.IsNullOrEmpty(sexString)) { if (int.TryParse(sexString, out sex)) { if (sex != 0 && sex != 1) { return(ResponseUtil.Error(400, "性别错误")); } } else { return(ResponseUtil.Error(400, "性别错误")); } } // 出生地 //if (string.IsNullOrEmpty(location)) //{ // return ResponseUtil.Error(400, "出生地不能为空"); //} // 出生日期 if (!string.IsNullOrEmpty(birthDay)) { if (!DateTime.TryParse(birthDay, out birthday)) { return(ResponseUtil.Error(400, "出生日期无效")); } } // 身份证号 if (!string.IsNullOrEmpty(credNo)) { // 校验身份证格式 if ((!Regex.IsMatch(credNo, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$", RegexOptions.IgnoreCase))) { return(ResponseUtil.Error(400, "身份证格式不正确")); } } // 工作单位 //if (string.IsNullOrEmpty(unit)) //{ // return ResponseUtil.Error(400, "工作单位不能为空"); //} // 工作部门 //if (string.IsNullOrEmpty(depart)) //{ // return ResponseUtil.Error(400, "工作部门不能为空"); //} // 级别 //if (string.IsNullOrEmpty(level)) //{ // return ResponseUtil.Error(400, "级别不能为空"); //} // 职务 //if (string.IsNullOrEmpty(duty)) //{ // return ResponseUtil.Error(400, "职务不能为空"); //} /* * 添加用户 */ using (var db = new YGSDbContext()) { var user = db.User.Where(n => n.CredNo == credNo && !string.IsNullOrEmpty(credNo)).FirstOrDefault(); if (user == null) { user = new YGS_User(); user.Name = name; if (!string.IsNullOrEmpty(sexString)) { user.Sex = sex; } if (!string.IsNullOrEmpty(location)) { user.Location = location; } if (!string.IsNullOrEmpty(birthDay)) { user.BirthDay = birthday; } if (!string.IsNullOrEmpty(credNo)) { user.CredNo = credNo; } if (!string.IsNullOrEmpty(unit)) { user.Unit = unit; } if (!string.IsNullOrEmpty(depart)) { user.Depart = depart; } if (!string.IsNullOrEmpty(level)) { user.Level = level; } if (!string.IsNullOrEmpty(duty)) { user.Duty = duty; } user.CreateTime = DateTime.Now; user.UpdateTime = DateTime.Now; db.User.Add(user); db.SaveChanges(); return(ResponseUtil.OK(200, "创建成功!")); } else { return(ResponseUtil.Error(400, "用户已存在")); } } }