public ActionResult Delete(int id = 0, int folderId = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                string strMessage = String.Empty;
                if (id != 0)
                {
                    var tb = (from p in db.Table <Disk.Entity.tbDiskFile>()
                              .Include(p => p.tbDiskFolder)
                              .Include(p => p.tbSysUser)
                              where p.Id == id && p.tbSysUser.Id == Code.Common.UserId
                              select p).FirstOrDefault();
                    if (tb != null)
                    {
                        if (tb.tbSysUser.Id == Code.Common.UserId ||
                            DiskPowerController.CheckAdmin(tb.tbDiskFolder.Id, Code.Common.UserId))
                        {
                            tb.IsDeleted = true;
                            db.SaveChanges();

                            strMessage = "删除成功!";
                        }
                        else
                        {
                            strMessage = "您不具该文件删除权限!";
                        }
                    }
                }

                return(Code.MvcHelper.Post(null, Url.Action("List", new
                {
                    folderId = folderId
                }), strMessage));
            }
        }
        public ActionResult Edit()
        {
            var vm = new Models.DiskFile.Edit();

            #region 校验上传文件夹权限
            if (vm.FolderId != 0)
            {
                var diskFolder = DiskFolderController.SelectInfo(vm.FolderId);
                if (diskFolder != null &&
                    !diskFolder.UserId.Equals(Code.Common.UserId) &&
                    !DiskFolderController.CheckDickTypePublic(vm.FolderId) &&
                    !DiskPowerController.CheckInput(vm.FolderId, Code.Common.UserId) &&
                    Code.Common.UserType != Code.EnumHelper.SysUserType.Administrator)
                {
                    return(Content(String.Format("您不具备当前文件夹:【{0}】的上传权限!", diskFolder.DiskFolderName)));
                }
            }
            #endregion

            return(View(vm));
        }
        public ActionResult Report()
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.DiskFolder.Report();

                if (vm.ParentId == 0)
                {
                    var tb = (from t in db.Table <Disk.Entity.tbDiskFolder>()
                              where t.DiskFolderName == "学校共享文件夹" &&
                              t.tbDiskType.DiskType == Code.EnumHelper.DiskType.Public
                              select t).FirstOrDefault();
                    if (tb != null)
                    {
                        vm.ParentId = tb.Id;
                    }
                }

                var expr = from t in db.Table <Disk.Entity.tbDiskFolder>()
                           where t.tbDiskType.DiskType == Code.EnumHelper.DiskType.Public &&
                           t.DiskPermit != Code.EnumHelper.DiskPermit.Private &&
                           t.tbDiskFolderParent.Id == vm.ParentId
                           select new Dto.DiskFolder.Report
                {
                    Id             = t.Id,
                    DiskFolderName = t.DiskFolderName,
                    DiskPermit     = t.DiskPermit
                };
                if (!String.IsNullOrWhiteSpace(vm.SearchText))
                {
                    expr = expr.Where(t => t.DiskFolderName.Contains(vm.SearchText));
                }

                vm.FolderList        = expr.ToList();
                vm.DiskPowerUserList = DiskPowerController.SelectReportUserList(vm.ParentId);
                vm.DiskFileUserList  = DiskFileController.SelectReportUserList(vm.ParentId);

                return(View(vm));
            }
        }
        public ActionResult List()
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.DiskFile.List();

                var tbDiskType = DiskTypeController.SelectInfo(vm.DiskTypeId);

                #region 文件夹路径
                if (vm.UserId == 0)
                {
                    vm.FolderPath = DiskFolderController.SelectFolderPath(vm.FolderId);
                }
                else
                {
                    if (tbDiskType != null && tbDiskType.DiskType == Code.EnumHelper.DiskType.Public)
                    {
                        var tbSysUser    = db.Set <Sys.Entity.tbSysUser>().Find(vm.UserId);
                        var tbDiskFolder = (from t in db.Table <Disk.Entity.tbDiskFolder>()
                                            where t.DiskFolderName == "教师共享文件夹"
                                            select t).FirstOrDefault();
                        vm.FolderPath = DiskFolderController.SelectFolderPath(tbDiskFolder.Id);
                        vm.FolderPath.Add(new Dto.DiskFolder.Info
                        {
                            DiskFolderName = tbSysUser.UserName,
                            DiskTypeId     = vm.DiskTypeId,
                            UserId         = tbSysUser.Id,
                            ParentId       = 0
                        });
                    }

                    if (vm.FolderId != 0)
                    {
                        vm.FolderPath = vm.FolderPath.Union(DiskFolderController.SelectFolderPath(vm.FolderId, vm.DiskTypeId, vm.UserId)).ToList();
                    }
                }
                #endregion

                #region 文件夹
                if (vm.FolderId == 0)
                {
                    if (vm.DiskTypeId == 0)
                    {
                        // /Disk/DiskFile/List
                        vm.DiskFileList = DiskFolderController.SelectPublicRootFolder();
                        vm.DiskFileList = vm.DiskFileList.Union(DiskFolderController.SelectByParentId(vm.FolderId, vm.SearchText, 0, Code.Common.UserId)).ToList();
                    }

                    if (vm.UserId != 0)
                    {
                        vm.DiskFileList = DiskFolderController.SelectByDiskPermitPublic(vm.UserId, vm.DiskTypeId);
                        vm.DiskFileList = vm.DiskFileList.Union(DiskFolderController.SelectByDiskPermitAuthorize(vm.UserId, vm.DiskTypeId)).ToList();
                    }
                }
                else
                {
                    if (tbDiskType != null && tbDiskType.DiskType == Code.EnumHelper.DiskType.Public)
                    {
                        // 管理员查看全部
                        if (Code.Common.UserType == Code.EnumHelper.SysUserType.Administrator)
                        {
                            vm.DiskFileList = DiskFolderController.SelectPublicFolderByParentId(vm.FolderId);
                        }
                        else
                        {
                            // 学校共享文件夹-共有
                            vm.DiskFileList = DiskFolderController.SelectPublicFolderByParentId(vm.FolderId, Code.EnumHelper.DiskPermit.Public);

                            // 学校共享文件夹-授权
                            var tbDiskFolderAuthorize = DiskFolderController.SelectPublicFolderAuthorizeByParentId(vm.FolderId, Code.EnumHelper.DiskPermit.Public);
                            foreach (Dto.DiskFile.List diskFile in tbDiskFolderAuthorize)
                            {
                                if (!vm.DiskFileList.Any(t => t.FileTitle.Equals(diskFile.FileTitle)))
                                {
                                    vm.DiskFileList.Add(diskFile);
                                }
                            }
                        }

                        var folder = (from t in db.Table <Disk.Entity.tbDiskFolder>()
                                      where t.Id == vm.FolderId
                                      select t).FirstOrDefault();
                        if (folder != null)
                        {
                            if (folder.DiskFolderName.Equals("教师共享文件夹"))
                            {
                                vm.DiskFileList = vm.DiskFileList.Union(DiskFolderController.SelectByPublic(Code.EnumHelper.DiskType.Private, vm.DiskTypeId)).ToList();

                                foreach (Dto.DiskFile.List diskFile in DiskFolderController.SelectByAuthorize(Code.EnumHelper.DiskType.Private, vm.DiskTypeId))
                                {
                                    if (!vm.DiskFileList.Any(t => t.FileTitle.Equals(diskFile.FileTitle)))
                                    {
                                        vm.DiskFileList.Add(diskFile);
                                    }
                                }
                            }
                            else if (!folder.DiskFolderName.Equals("公开文件夹") && !(folder.DiskFolderName.Equals("学校共享文件夹")))
                            {
                                vm.DiskFileList = DiskFolderController.SelectByParentId(vm.FolderId, vm.SearchText, vm.DiskTypeId, vm.UserId);
                            }
                        }
                    }
                    else
                    {
                        vm.DiskFileList = DiskFolderController.SelectByParentId(vm.FolderId, vm.SearchText);
                    }
                }
                #endregion

                #region 文件
                var tbDiskFile = from t in db.Table <Disk.Entity.tbDiskFile>()
                                 select t;
                if (vm.FolderId != 0)
                {
                    tbDiskFile = tbDiskFile.Where(t => t.tbDiskFolder.Id == vm.FolderId);
                }
                else
                {
                    tbDiskFile = tbDiskFile.Where(t => t.tbDiskFolder == null);

                    //if (vm.UserId != 0)
                    //{
                    //    tbDiskFile = tbDiskFile.Where(t => t.tbSysUser.Id == vm.UserId);
                    //}
                    //else
                    //{
                    tbDiskFile = tbDiskFile.Where(t => t.tbSysUser.Id == Code.Common.UserId);
                    //}
                }
                if (!String.IsNullOrWhiteSpace(vm.SearchText))
                {
                    tbDiskFile = tbDiskFile.Where(t => t.FileTitle.Contains(vm.SearchText));
                }

                int diskTypeId = DiskTypeController.SelectByEnumDiskType(Code.EnumHelper.DiskType.Private).Id;

                var tb = (from t in tbDiskFile
                          orderby t.Id descending
                          select new Dto.DiskFile.List
                {
                    Id = t.Id,
                    DiskTypeId = t.tbDiskFolder == null ? diskTypeId : t.tbDiskFolder.tbDiskType.Id,
                    FileType = "File",
                    FileTitle = t.FileTitle,
                    FileLength = t.FileLength,
                    InputDate = t.InputDate,
                    UserId = t.tbSysUser.Id,
                    UserName = t.tbSysUser.UserName
                }).ToList();

                vm.DiskFileList = vm.DiskFileList.Union(tb).ToList();
                #endregion

                #region 文件夹文件权限

                // 管理权限
                vm.AdminFolderIds = DiskFolderController.SelectOwnFolderIdsByUserId(Code.Common.UserId);
                if (Code.Common.UserType == Code.EnumHelper.SysUserType.Administrator)
                {
                    vm.AdminFolderIds = vm.AdminFolderIds.Union(DiskFolderController.SelectDiskPermitPublicFolderIdsByUserId(Code.EnumHelper.DiskType.Public, Code.Common.UserId)).ToList();
                }
                vm.AdminFolderIds = vm.AdminFolderIds.Union(DiskFolderController.SelectDiskPermitPublicFolderIdsByUserId(Code.EnumHelper.DiskType.Private, Code.Common.UserId)).ToList();
                vm.AdminFolderIds = vm.AdminFolderIds.Union(DiskPowerController.SelectAdminFolderIds(Code.Common.UserId)).ToList();

                // 查看权限
                vm.ViewFolderIds = DiskFolderController.SelectOwnFolderIdsByUserId(Code.Common.UserId);
                vm.ViewFolderIds = vm.ViewFolderIds.Union(DiskFolderController.SelectDiskPermitPublicFolderIdsByUserId(Code.EnumHelper.DiskType.Public, Code.Common.UserId)).ToList();
                vm.ViewFolderIds = vm.ViewFolderIds.Union(DiskFolderController.SelectDiskPermitPublicFolderIdsByUserId(Code.EnumHelper.DiskType.Private, Code.Common.UserId)).ToList();
                vm.ViewFolderIds = vm.ViewFolderIds.Union(DiskPowerController.SelectViewFolderIds(Code.Common.UserId)).ToList();

                #endregion

                #region 文件权限
                vm.AdminFileIds = DiskFileController.SelectDescendantsByFolder(vm.AdminFolderIds).Select(t => t.Id).ToList();
                vm.ViewFileIds  = DiskFileController.SelectDescendantsByFolder(vm.ViewFolderIds).Select(t => t.Id).ToList();

                var MyRootFileIds = (from t in db.Table <Disk.Entity.tbDiskFile>()
                                     where t.tbDiskFolder == null &&
                                     t.tbSysUser.Id == Code.Common.UserId
                                     select t.Id).ToList();
                vm.AdminFileIds = vm.AdminFileIds.Union(MyRootFileIds).ToList();
                vm.ViewFileIds  = vm.ViewFileIds.Union(MyRootFileIds).ToList();

                #endregion

                return(View(vm));
            }
        }
        public ActionResult Edit(Models.DiskFolder.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();

                #region 新建公共文件夹校验

                var diskType = db.Set <Disk.Entity.tbDiskType>().Find(vm.DiskFolderEdit.DiskTypeId);
                if (diskType.DiskType == Code.EnumHelper.DiskType.Public && Code.Common.UserType != Code.EnumHelper.SysUserType.Administrator)
                {
                    error.AddError("需要管理员身份新建公开文件夹!");
                }

                #endregion

                if (error.Count == decimal.Zero)
                {
                    if (db.Table <Disk.Entity.tbDiskFolder>().Where(d => d.DiskFolderName == vm.DiskFolderEdit.DiskFolderName &&
                                                                    d.tbSysUser.Id == Code.Common.UserId &&
                                                                    d.tbDiskFolderParent.Id == vm.DiskFolderEdit.ParentId &&
                                                                    d.Id != vm.DiskFolderEdit.Id).Any())
                    {
                        error.AddError("该文件夹名称已存在!");
                    }
                    else
                    {
                        if (vm.DiskFolderEdit.Id == 0)
                        {
                            var tb = new Disk.Entity.tbDiskFolder();
                            tb.No = vm.DiskFolderEdit.No == null
                                ? db.Table <Disk.Entity.tbDiskFolder>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1
                                : (int)vm.DiskFolderEdit.No;

                            tb.DiskFolderName = vm.DiskFolderEdit.DiskFolderName;
                            tb.tbDiskType     = diskType;
                            tb.DiskPermit     = vm.DiskFolderEdit.DiskPermit;
                            tb.tbSysUser      = db.Set <Sys.Entity.tbSysUser>().Find(Code.Common.UserId);

                            if (vm.FolderId != 0)
                            {
                                if (diskType.DiskType == Code.EnumHelper.DiskType.Public)
                                {
                                    tb.tbDiskFolderParent = db.Table <Disk.Entity.tbDiskFolder>()
                                                            .Where(t => t.Id == vm.FolderId)
                                                            .FirstOrDefault();
                                }
                                else
                                {
                                    tb.tbDiskFolderParent = db.Table <Disk.Entity.tbDiskFolder>()
                                                            .Where(t => t.Id == vm.FolderId && t.tbSysUser.Id == Code.Common.UserId)
                                                            .FirstOrDefault();
                                }
                            }

                            db.Set <Disk.Entity.tbDiskFolder>().Add(tb);
                        }
                        else
                        {
                            var tb = (from p in db.Table <Disk.Entity.tbDiskFolder>()
                                      where p.Id == vm.DiskFolderEdit.Id
                                      select p).FirstOrDefault();
                            if (tb != null)
                            {
                                tb.No = vm.DiskFolderEdit.No == null?db.Table <Disk.Entity.tbDiskFolder>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.DiskFolderEdit.No;

                                tb.DiskFolderName = vm.DiskFolderEdit.DiskFolderName;
                                tb.DiskPermit     = vm.DiskFolderEdit.DiskPermit;

                                // 同时设置子文件夹权限
                                var tbDiskFolderDescendants = DiskFolderController.SelectDescendants(new List <int> {
                                    tb.Id
                                });
                                foreach (var folder in tbDiskFolderDescendants)
                                {
                                    if (folder.Id != vm.DiskFolderEdit.Id)
                                    {
                                        var tbDiskFolder = db.Set <Disk.Entity.tbDiskFolder>().Find(folder.Id);
                                        if (tbDiskFolder != null)
                                        {
                                            tbDiskFolder.DiskPermit = vm.DiskFolderEdit.DiskPermit;
                                        }
                                    }

                                    // 删除文件夹授权
                                    if (vm.DiskFolderEdit.DiskPermit != Code.EnumHelper.DiskPermit.Authorize)
                                    {
                                        DiskPowerController.DeleteByFolderId(folder.Id);
                                    }
                                }
                            }
                            else
                            {
                                error.AddError(Resources.LocalizedText.MsgNotFound);
                            }
                        }
                    }
                }

                db.SaveChanges();

                return(Code.MvcHelper.Post(error));
            }
        }