Beispiel #1
0
 public IHttpActionResult GetAll(string _maxe)
 {
     try
     {
         using (var db = new DB())
         {
             List <Ghe> ghes = db.Ghes
                               .Where(x => x.maxe == _maxe).OrderByDescending(x => x.ngaycapnhat)
                               .ToList();
             List <GheHang> ghesAllTang1 = new List <GheHang>();
             List <GheHang> ghesAllTang2 = new List <GheHang>();
             for (int i = 0; i < 7; i++)
             {
                 GheHang gheHangTang1 = new GheHang()
                 {
                     Hang = (i + 1),
                     Tang = 1,
                     Ghes = new List <EGhe>()
                 };
                 GheHang gheHangTang2 = new GheHang()
                 {
                     Hang = (i + 1),
                     Tang = 2,
                     Ghes = new List <EGhe>()
                 };
                 for (int j = 0; j < 5; j++)
                 {
                     gheHangTang1.Ghes.Add(new EGhe()
                     {
                         active = false,
                         maghe  = "",
                         maxe   = _maxe,
                         tang   = 1,
                         vitriX = (j + 1),
                         vitriY = (i + 1)
                     });
                     gheHangTang2.Ghes.Add(new EGhe()
                     {
                         active = false,
                         maghe  = "",
                         maxe   = _maxe,
                         tang   = 2,
                         vitriX = (j + 1),
                         vitriY = (i + 1)
                     });
                 }
                 ghesAllTang1.Add(gheHangTang1);
                 ghesAllTang2.Add(gheHangTang2);
             }
             ghes.ForEach(x =>
             {
                 EGhe ghe = ghesAllTang1.FirstOrDefault(y => y.Tang == x.tang && y.Hang == x.vitriY)?.Ghes.FirstOrDefault(y => y.vitriX == x.vitriX);
                 if (ghe != null)
                 {
                     ghe.maghe  = x.maghe.Split('_')[1];
                     ghe.active = x.active;
                 }
                 else
                 {
                     ghe = ghesAllTang2.FirstOrDefault(y => y.Tang == x.tang && y.Hang == x.vitriY)
                           .Ghes
                           .FirstOrDefault(y => y.vitriX == x.vitriX);
                     if (ghe != null)
                     {
                         ghe.maghe  = x.maghe.Split('_')[1];
                         ghe.active = x.active;
                     }
                 }
             });
             return(Ok(new
             {
                 ghetang1 = ghesAllTang1.Select(x => new
                 {
                     x.Hang,
                     x.Tang,
                     Ghes = x.Ghes.Select(y => new
                     {
                         y.maghe,
                         y.maxe,
                         y.vitriX,
                         y.vitriY,
                         y.tenghe,
                         y.active,
                         y.tang
                     })
                 }),
                 ghetang2 = ghesAllTang2.Select(x => new
                 {
                     x.Hang,
                     x.Tang,
                     Ghes = x.Ghes.Select(y => new
                     {
                         y.maghe,
                         y.maxe,
                         y.vitriX,
                         y.vitriY,
                         y.tenghe,
                         y.active,
                         y.tang
                     })
                 })
             }));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }
 public IHttpActionResult GetGheXe(string machuyenxe, DateTime ngaydi)
 {
     try
     {
         using (var db = new DB())
         {
             ngaydi = ngaydi.Date;
             List <Database.TrangThaiGhe> trangThaiGhes = db.TrangThaiGhes
                                                          .Where(x => x.machuyenxe == machuyenxe && x.Ghe.active == true)
                                                          .ToList();
             List <GheHang> ghesAllTang1 = new List <GheHang>();
             List <GheHang> ghesAllTang2 = new List <GheHang>();
             for (int i = 0; i < 7; i++)
             {
                 GheHang gheHangTang1 = new GheHang()
                 {
                     Hang = (i + 1),
                     Tang = 1,
                     Ghes = new List <EGhe>()
                 };
                 GheHang gheHangTang2 = new GheHang()
                 {
                     Hang = (i + 1),
                     Tang = 2,
                     Ghes = new List <EGhe>()
                 };
                 for (int j = 0; j < 5; j++)
                 {
                     gheHangTang1.Ghes.Add(new EGhe()
                     {
                         active    = false,
                         maghe     = "",
                         tang      = 1,
                         vitriX    = (j + 1),
                         vitriY    = (i + 1),
                         trangthai = 0
                     });
                     gheHangTang2.Ghes.Add(new EGhe()
                     {
                         active    = false,
                         maghe     = "",
                         tang      = 2,
                         vitriX    = (j + 1),
                         vitriY    = (i + 1),
                         trangthai = 0
                     });
                 }
                 ghesAllTang1.Add(gheHangTang1);
                 ghesAllTang2.Add(gheHangTang2);
             }
             trangThaiGhes.ForEach(x =>
             {
                 EGhe ghe = ghesAllTang1.FirstOrDefault(y => y.Tang == x.Ghe.tang && y.Hang == x.Ghe.vitriY)?.Ghes.FirstOrDefault(y => y.vitriX == x.Ghe.vitriX);
                 if (ghe != null)
                 {
                     ghe.maghe     = x.maghe.Split('_')[1];
                     ghe.active    = x.Ghe.active;
                     ghe.trangthai = x.trangthai;
                 }
                 else
                 {
                     ghe = ghesAllTang2.FirstOrDefault(y => y.Tang == x.Ghe.tang && y.Hang == x.Ghe.vitriY)
                           .Ghes
                           .FirstOrDefault(y => y.vitriX == x.Ghe.vitriX);
                     if (ghe != null)
                     {
                         ghe.maghe     = x.maghe.Split('_')[1];
                         ghe.active    = x.Ghe.active;
                         ghe.trangthai = x.trangthai;
                     }
                 }
             });
             return(Ok(new
             {
                 ghetang1 = ghesAllTang1.Select(x => new
                 {
                     x.Hang,
                     x.Tang,
                     Ghes = x.Ghes.Select(y => new
                     {
                         y.maghe,
                         y.vitriX,
                         y.vitriY,
                         y.active,
                         y.tang,
                         y.trangthai
                     })
                 }),
                 ghetang2 = ghesAllTang2.Select(x => new
                 {
                     x.Hang,
                     x.Tang,
                     Ghes = x.Ghes.Select(y => new
                     {
                         y.maghe,
                         y.vitriX,
                         y.vitriY,
                         y.active,
                         y.tang,
                         y.trangthai
                     })
                 })
             }));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.Message));
     }
 }