public ActionResult Update(MinibarModel model, string values)
 {
     try
     {
         if (string.IsNullOrEmpty(values) || string.IsNullOrEmpty(model.Name) || model.RoomId == 0)
         {
             return(Json(new { result = -1 }, JsonRequestBehavior.AllowGet));
         }
         var lstPro = JsonConvert.DeserializeObject <List <Item> >(values);
         model.Item = lstPro.Where(x => x.ProductId != 0).ToList();
         var rs = _service.InsertOrUpdateStore(model);
         return(Json(new { result = rs }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception)
     {
         return(Json(new { result = 0 }, JsonRequestBehavior.AllowGet));
     }
 }
        public PartialViewResult ShowModal(int id, string action)
        {
            var model = new MinibarModel
            {
                ListRooms = _service.GetRoomByHotel(),
                Action    = action
            };

            if (id > 0 && action != "Add")
            {
                var store = _service.GetItem(id);
                model.ListProduct = _service.GetProductAllByStore(id);
                model.RoomId      = store.roomid ?? 0;
                model.Name        = store.title;
                model.Id          = store.Id;
                return(PartialView("_ptvDetail", model));
            }
            model.ListProductsAdd = _service.GetProductByStore();
            return(PartialView("_ptvAdd", model));
        }
        public int InsertOrUpdateStore(MinibarModel model)
        {
            try
            {
                using (var db = _connectionData.OpenDbConnection())
                {
                    using (var tran = db.OpenTransaction())
                    {
                        try
                        {
                            if (model.Id > 0)
                            {
                                var query = db.From <tbl_Store>().Where(e => e.Id == model.Id);
                                var up    = db.Select(query).SingleOrDefault();
                                if (up != null)//update store
                                {
                                    up.title  = model.Name;
                                    up.roomid = model.RoomId;
                                    db.Update(up);
                                }
                                var strDel = db.From <tbl_StoreProduct_Config>().Where(e => e.storeid == model.Id);
                                var del    = db.Select(strDel);
                                if (del.Any())
                                {
                                    db.DeleteAll(del);
                                }
                                if (model.Item.Any())//update store product config
                                {
                                    var list = model.Item.Select(item => new tbl_StoreProduct_Config
                                    {
                                        productid   = item.ProductId,
                                        minimize    = item.Limit,
                                        SysHotelID  = comm.GetHotelId(),
                                        datecreated = DateTime.Now,
                                        storeid     = model.Id
                                    });
                                    db.InsertAll(list);
                                }
                                tran.Commit();
                                return(1);
                            }
                            //Insert-------------------------
                            var check  = db.From <tbl_Store>().Where(e => e.roomid == model.RoomId);
                            var isExit = db.Select(check).FirstOrDefault();
                            if (isExit == null) //Nếu roomID tồn tại rồi thì k cho insert nữa
                            {
                                var store = new tbl_Store
                                {
                                    title       = model.Name,
                                    roomid      = model.RoomId,
                                    creatorid   = comm.GetUserId(),
                                    SysHotelID  = comm.GetHotelId(),
                                    datecreated = DateTime.Now,
                                    typeStore   = 2
                                };
                                var idStore = db.Insert(store, true); //insert product
                                if (model.Item.Any())                 //insert store product
                                {
                                    var list = model.Item.Select(item => new tbl_StoreProduct_Config
                                    {
                                        productid   = item.ProductId,
                                        minimize    = item.Limit,
                                        SysHotelID  = comm.GetHotelId(),
                                        datecreated = DateTime.Now,
                                        storeid     = int.Parse(idStore.ToString())
                                    });
                                    db.InsertAll(list);

                                    /*
                                     * var list = model.Item.Select(item => new tbl_StoreProduct
                                     * {
                                     *  productid = item.ProductId,
                                     *  quantity = item.Limit,
                                     *  SysHotelID = comm.GetHotelId(),
                                     *  datecreated = DateTime.Now,
                                     *  storeid = int.Parse(idStore.ToString())
                                     * });
                                     * db.InsertAll(list);
                                     */
                                }
                                tran.Commit();
                                return(1);
                            }
                            return(-100);
                        }
                        catch (Exception)
                        {
                            tran.Rollback();
                            return(-1);
                        }
                    }
                }
            }
            catch (Exception)
            {
                return(-1);
            }
        }