public async Task Upsert(CreateRoomActRequest req, DateTime time) { var roomActLst = await Create(req, "จอง", time); var writeModels = roomActLst .OrderBy(it => it.RoomNo) .Select(it => new ReplaceOneModel <RoomActivated>(Builders <RoomActivated> .Filter.Eq(d => d._id, it._id), it) { IsUpsert = true }); await roomActivatedDac.Creates(writeModels); }
public async Task <Response> Create([FromBody] MasterDetail req) { var master = req.Master; var oldMaster = await masterDac.Get(x => x.MemberId == master.MemberId && x.GroupName == master.GroupName && x.Active == true); if (string.IsNullOrEmpty(master._id)) { master._id = oldMaster?._id ?? now.Ticks.ToString(); } var roomActLst = req.RoomActLst.Select(it => { var expenseListSetTime = roomActService.SetCreationDateTime(it.ExpenseList, now); var calc = roomActService.CalculateExpense(expenseListSetTime, now); return(new RoomActivated { _id = $"{master._id}{it.RoomNo}", GroupId = master._id, RoomNo = it.RoomNo, RoomType = it.RoomType, BedType = it.BedType, Rate = it.Rate, ArrivalDate = master.CheckInDate, Departure = master.CheckOutDate, ExpenseList = calc.ExpenseList, TotalCost = calc.TotalCost, Paid = calc.Paid, Remaining = calc.Remaining, Status = it.Status, Active = it.Active, CreationDateTime = now, LastUpdate = now, }); }); var writeModels = roomActLst .OrderBy(it => it.RoomNo) .Select(it => new ReplaceOneModel <RoomActivated>(Builders <RoomActivated> .Filter.Eq(d => d._id, it._id), it) { IsUpsert = true }); await roomActivatedDac.Creates(writeModels); master.TotalCost = roomActLst.Sum(room => room.TotalCost); master.Paid = roomActLst.Sum(room => room.Paid); master.Remaining = roomActLst.Sum(room => room.Remaining); if (master.HaveRoomDeposit) { master.Deposit = 200 * master.Rooms.Count(); // ห้องละ 200 ??? } if (oldMaster != null) { if (master.CheckInDate < oldMaster.CheckInDate) { oldMaster.CheckInDate = master.CheckInDate; } if (master.CheckOutDate > oldMaster.CheckOutDate) { oldMaster.CheckOutDate = master.CheckOutDate; } oldMaster.Rooms = oldMaster.Rooms.Concat(master.Rooms).OrderBy(it => it.RoomNo); oldMaster.HaveRoomDeposit = oldMaster.HaveRoomDeposit || master.HaveRoomDeposit; oldMaster.HaveTaxInvoice = oldMaster.HaveTaxInvoice || master.HaveTaxInvoice; oldMaster.Deposit += master.Deposit; oldMaster.TotalCost += master.TotalCost; oldMaster.Paid += master.Paid; oldMaster.Remaining += master.Remaining; oldMaster.LastUpdate = now; await masterDac.Update(x => x._id == oldMaster._id, oldMaster); } else { master.Active = true; master.CreationDateTime = now; master.LastUpdate = now; await masterDac.Create(master); } return(new Response { IsSuccess = true }); }