//查找地址码 public ActionResult Add(WorkAndWorkTypeDetails wawtd) { bool retVal = false; Work work = wawtd.Work; //主工单 WorkSub workSub = wawtd.WorkSub; //子工单 if (workSub == null) { workSub = new WorkSub(); } IList <WorkTypeDetail> workTypeDetails = wawtd.WorkTypeDetails; //子工单维修类型 IList <WorkPic> workPics = wawtd.WorkPics; //子工单图片 ISession s = null; ITransaction trans = null; try { s = HibernateOper.GetCurrentSession(); trans = s.BeginTransaction(); //如果主工单的ID不存在则新建主工单,有可能是在原来的主工单基础上追加的子工单就会把主工单的ID传过来 if (work.Id == Guid.Empty) { //生成工单编号 work.Code = DateTime.Now.ToString("yyyyMMddHHmmssfffff"); //查找客户ID string openId = work.Cstmr.OpenId; work.Cstmr = PojoUtil.GetCust(s, openId); if (work.Cstmr == null) { Customer cstmr = new Customer(); cstmr.OpenId = openId; s.Save(cstmr); work.Cstmr = cstmr; } //保存工单 s.Save(work); } else { //根据工单ID找到工单 work = s.Get <Work>(work.Id); if (work == null) { //工单不存在 throw new Exceptions.WorkIsNotFoundException(); } } //保存子工单 //如果子工单的下单员工存在 if (workSub.OrderEmp != null) { //如果子工单的下单员ID存在 if (workSub.OrderEmp.Id != Guid.Empty) { workSub.OrderEmp = s.Get <Employee>(workSub.OrderEmp.Id); } //如果子工单的下单员工号存在 if (workSub.OrderEmp.Code != null && !workSub.OrderEmp.Code.ToString().Equals("")) { workSub.OrderEmp = PojoUtil.GetEmployee(s, workSub.OrderEmp.Code); } if (workSub.OrderEmp == null) { //该下单员不存在 throw new Exceptions.EmployeeIsNotFoundException(); } } workSub.Work = work; //根据工单地址自动分配任务到分店 workSub.Dept = PojoUtil.GetMinDistDept(s, work); s.Save(workSub); //保存维修类型 if (workTypeDetails != null) { foreach (WorkTypeDetail wtd in workTypeDetails) { wtd.WorkSub = workSub; s.Save(wtd); } } else { //维修类型明细不存在 throw new Exceptions.WorkTypeDetailIsNotFoundException(); } //保存图片 if (workPics != null) { foreach (WorkPic wp in workPics) { wp.WorkSub = workSub; s.Save(wp); } } trans.Commit(); retVal = true; } catch (Exception e) { if (trans != null) { trans.Rollback(); } throw e; } return(Json(retVal)); }