public ActionResult CreateUpdate(tw_UserGroup data, List <AccessDetail> AccessDetail)
        {
            try
            {
                using (var dbConn = Helpers.OrmliteConnection.openConn())
                {
                    if (data.id > 0)
                    {
                        if (data.id != 1 && data.name != "guest")
                        {
                            if (accessDetail != null && (accessDetail.access["all"] || accessDetail.access["update"]))
                            {
                                var existGroup = dbConn.SingleOrDefault <tw_UserGroup>("name={0} and id <> {1}", data.name, data.id);
                                if (existGroup != null)
                                {
                                    return(Json(new { success = false, error = "Group name already existed" }));
                                }

                                var exist = dbConn.SingleOrDefault <tw_UserGroup>("id={0}", data.id);
                                data.updatedAt = DateTime.Now;
                                data.updatedBy = currentUser.name;
                                dbConn.UpdateOnly(data,
                                                  onlyFields: p =>
                                                  new
                                {
                                    p.name,
                                    p.listAccess,
                                    p.listUser,
                                    p.description,
                                    p.active,
                                    p.updatedAt,
                                    p.updatedBy
                                },
                                                  where : p => p.id == data.id);
                            }
                            else
                            {
                                return(Json(new { success = false, error = "Don't have permission to update" }));
                            }
                        }
                    }
                    else
                    {
                        if (accessDetail != null && (accessDetail.access["all"] || accessDetail.access["create"]))
                        {
                            var existGroup = dbConn.SingleOrDefault <tw_UserGroup>("name={0}", data.name);
                            if (existGroup != null)
                            {
                                return(Json(new { success = false, error = "Group name already existed" }));
                            }

                            data.createdAt = DateTime.Now;
                            data.createdBy = currentUser.name;
                            dbConn.Insert(data);
                            int Id = (int)dbConn.GetLastInsertId();
                            data.id = Id;
                        }
                        else
                        {
                            return(Json(new { success = false, error = "Don't have permission to create" }));
                        }
                    }

                    if (accessDetail != null && (accessDetail.access["all"] || accessDetail.access["grant access"]))
                    {
                        if (data.id != 1)
                        {
                            if (AccessDetail != null && AccessDetail.Count > 0)
                            {
                                foreach (var item in AccessDetail)
                                {
                                    var existAccess = dbConn.SingleOrDefault <AccessRight>("controllerName={0}", item.controllerName);
                                    if (existAccess != null)
                                    {
                                        var newAccess = new Dictionary <string, bool>();
                                        foreach (var a in existAccess.access)
                                        {
                                            newAccess.Add(a, item.access != null && item.access.ContainsKey(a) ? true : false);
                                        }

                                        var exist = dbConn.SingleOrDefault <AccessDetail>("groupId={0} AND controllerName={1}", item.groupId, item.controllerName);
                                        if (exist != null)
                                        {
                                            exist.access    = newAccess;
                                            exist.updatedAt = DateTime.Now;
                                            exist.updatedBy = currentUser.name;

                                            dbConn.UpdateOnly(exist,
                                                              onlyFields: p =>
                                                              new
                                            {
                                                p.access,
                                                p.updatedAt,
                                                p.updatedBy
                                            },
                                                              where : p => p.id == exist.id);
                                        }
                                        else
                                        {
                                            item.access    = newAccess;
                                            item.createdAt = DateTime.Now;
                                            item.createdBy = currentUser.name;
                                            dbConn.Insert(item);
                                        }
                                    }
                                }
                            }
                        }


                        if (data.users != null && data.users.Count > 0)
                        {
                            foreach (var item in data.users)
                            {
                                var exist = dbConn.SingleOrDefault <tw_UserInGroup>("userId={0} AND groupId={1}", item, data.id);
                                if (exist == null)
                                {
                                    var userInGroup = new tw_UserInGroup();
                                    userInGroup.userId    = item;
                                    userInGroup.groupId   = data.id;
                                    userInGroup.createdAt = DateTime.Now;
                                    userInGroup.createdBy = currentUser.name;
                                    dbConn.Insert(userInGroup);
                                }
                            }

                            dbConn.Delete <tw_UserInGroup>("groupId={0} AND userId NOT IN (" + String.Join(",", data.users.Select(s => s)) + ")", data.id);
                        }
                    }
                }
                return(Json(new { success = true, data = data }));
            }
            catch (Exception e)
            {
                return(Json(new { success = false, error = e.Message }));
            }
        }
        public ActionResult Delete(tw_UserGroup data, List <AccessDetail> AccessDetail)
        {
            using (var dbConn = Helpers.OrmliteConnection.openConn())
            {
                if (accessDetail != null && (accessDetail.access["all"] || accessDetail.access["grant access"]))
                {
                    if (data.id != 1)
                    {
                        if (AccessDetail != null && AccessDetail.Count > 0)
                        {
                            foreach (var item in AccessDetail)
                            {
                                var existAccess = dbConn.SingleOrDefault <AccessRight>("controllerName={0}", item.controllerName);
                                if (existAccess != null)
                                {
                                    var newAccess = new Dictionary <string, bool>();
                                    foreach (var a in existAccess.access)
                                    {
                                        newAccess.Add(a, item.access != null && item.access.ContainsKey(a) ? true : false);
                                    }

                                    var exist = dbConn.SingleOrDefault <AccessDetail>("groupId={0} AND controllerName={1}", item.groupId, item.controllerName);
                                    if (exist != null)
                                    {
                                        exist.access    = newAccess;
                                        exist.updatedAt = DateTime.Now;
                                        exist.updatedBy = currentUser.name;

                                        dbConn.UpdateOnly(exist,
                                                          onlyFields: p =>
                                                          new
                                        {
                                            p.access,
                                            p.updatedAt,
                                            p.updatedBy
                                        },
                                                          where : p => p.id == exist.id);
                                    }
                                    else
                                    {
                                        item.access    = newAccess;
                                        item.createdAt = DateTime.Now;
                                        item.createdBy = currentUser.name;
                                        dbConn.Insert(item);
                                    }
                                }
                            }
                        }
                    }

                    if (data.users != null && data.users.Count > 0)
                    {
                        foreach (var item in data.users)
                        {
                            var exist = dbConn.SingleOrDefault <tw_UserInGroup>("userId={0} AND groupId={1}", item, data.id);
                            if (exist == null)
                            {
                                var userInGroup = new tw_UserInGroup();
                                userInGroup.userId    = item;
                                userInGroup.groupId   = data.id;
                                userInGroup.createdAt = DateTime.Now;
                                userInGroup.createdBy = currentUser.name;
                                dbConn.Insert(userInGroup);
                            }
                        }

                        dbConn.Delete <tw_UserInGroup>("groupId={0} AND userId NOT IN (" + String.Join(",", data.users.Select(s => s)) + ")", data.id);
                    }
                }
                return(Json(new { success = true, message = "Thành công!" }));
            }
        }