public ActionResult Add(HouseAddModel model) { long?userId = AdminHelper.GetUserId(HttpContext); long?cityId = userSerivce.GetById(userId.Value).CityId; if (cityId == null) { return(View("Error", (object)"总部不能进行房源管理")); } HouseAddnewDTO dto = new HouseAddnewDTO(); dto.Address = model.address; dto.Area = model.area; dto.AttachmentIds = model.attachmentIds; dto.CheckInDateTime = model.checkInDateTime; dto.CommunityId = model.CommunityId; dto.DecorateStatusId = model.DecorateStatusId; dto.Description = model.description; dto.Direction = model.direction; dto.FloorIndex = model.floorIndex; dto.LookableDateTime = model.lookableDateTime; dto.MonthRent = model.monthRent; dto.OwnerName = model.ownerName; dto.OwnerPhoneNum = model.ownerPhoneNum; dto.RoomTypeId = model.RoomTypeId; dto.StatusId = model.StatusId; dto.TotalFloorCount = model.totalFloor; dto.TypeId = model.TypeId; long houseId = houseService.AddNew(dto); //生成房源查看的html文件 CreateStaticPage(houseId); //把房源信息写入ElasticSearch //创建与SQLLite的连接 ElasticConnection client = new ElasticConnection("localhost", 9200); var serializer = new JsonNetSerializer(); //写入数据 //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键” IndexCommand indexcmd = new IndexCommand("ZSZHouse", "House", houseId.ToString()); //不用手动创建数据库,es会自动分配空间用zsz命名 //Put()第二个参数是要插入的数据 OperationResult result = client.Put(indexcmd, serializer.Serialize(dto));//把对象序列化成json放入Elastic中返回结果 return(Json(new AjaxResult { Status = "ok" })); }
/* * public long AddNew(HouseDTO house) * { * HouseEntity houseEntity = new HouseEntity(); * houseEntity.Address = house.Address; * houseEntity.Area = house.Area; * * using (ZSZDbContext ctx = new ZSZDbContext()) * { * BaseService<AttachmentEntity> attBS * = new BaseService<AttachmentEntity>(ctx); * var atts = attBS.GetAll().Where(a => house.AttachmentIds.Contains(a.Id)); * * foreach (var att in atts) * { * houseEntity.Attachments.Add(att); * } * houseEntity.CheckInDateTime = house.CheckInDateTime; * houseEntity.CommunityId = house.CommunityId; * houseEntity.CreateDateTime = house.CreateDateTime; * houseEntity.DecorateStatusId = house.DecorateStatusId; * houseEntity.Description = house.Description; * houseEntity.Direction = house.Direction; * houseEntity.FloorIndex = house.FloorIndex; * //houseEntity.HousePics 新增后再单独添加 * houseEntity.LookableDateTime = house.LookableDateTime; * houseEntity.MonthRent = house.MonthRent; * houseEntity.OwnerName = house.OwnerName; * houseEntity.OwnerPhoneNum = house.OwnerPhoneNum; * houseEntity.RoomTypeId = house.RoomTypeId; * houseEntity.StatusId = house.StatusId; * houseEntity.TotalFloorCount = house.TotalFloorCount; * houseEntity.TypeId = house.TypeId; * ctx.Houses.Add(houseEntity); * ctx.SaveChanges(); * return houseEntity.Id; * } * } */ #endregion //优化的AddNew public long AddNew(HouseAddnewDTO house) { HouseEntity houseEntity = new HouseEntity(); houseEntity.Address = house.Address; houseEntity.Area = house.Area; using (MyDbContext ctx = new MyDbContext()) { BaseService <AttachmentEntity> attBS = new BaseService <AttachmentEntity>(ctx); //拿到house.AttachmentIds为主键的房屋配套设施 var atts = attBS.GetAll().Where(a => house.AttachmentIds.Contains(a.Id)); //houseEntity.Attachments = new List<AttachmentEntity>(); foreach (var att in atts) { houseEntity.Attachments.Add(att); } houseEntity.CheckinDateTime = house.CheckInDateTime; houseEntity.CommunityId = house.CommunityId; houseEntity.DecorateStatusId = house.DecorateStatusId; houseEntity.Description = house.Description; houseEntity.Direction = house.Direction; houseEntity.FloorIndex = house.FloorIndex; //houseEntity.HousePics 新增后再单独添加 houseEntity.LookableDateTime = house.LookableDateTime; houseEntity.MonthRent = house.MonthRent; houseEntity.OwnerName = house.OwnerName; houseEntity.OwnerPhoneNum = house.OwnerPhoneNum; houseEntity.RoomTypeId = house.RoomTypeId; houseEntity.StatusId = house.StatusId; houseEntity.TotalFloorCount = house.TotalFloorCount; houseEntity.TypeId = house.TypeId; ctx.Houses.Add(houseEntity); ctx.SaveChanges(); return(houseEntity.Id); } }