public static List <Dto.DiskFile.Info> SelectDescendantsByFolder(List <int> folderIds)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                List <Dto.DiskFile.Info> fileDescendants = new List <Dto.DiskFile.Info>();

                List <Dto.DiskFolder.Info> folderDescendants = DiskFolderController.SelectDescendants(folderIds);
                foreach (var folder in folderDescendants)
                {
                    var tb = (from t in db.Table <Disk.Entity.tbDiskFile>()
                              where t.tbDiskFolder.Id == folder.Id
                              select new Dto.DiskFile.Info
                    {
                        Id = t.Id,
                        FileTitle = t.FileTitle,
                        FileName = t.FileName,
                        FolderId = t.tbDiskFolder.Id,
                        FolderPath = folder.FolderPath
                    }).ToList();

                    fileDescendants = fileDescendants.Union(tb).ToList();
                }

                return(fileDescendants);
            }
        }
        public ActionResult DownloadFolder(int id)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var tb = (from t in db.Table <Disk.Entity.tbDiskFolder>()
                          where t.Id == id && t.tbSysUser.Id == Code.Common.UserId
                          select t).FirstOrDefault();

                if (tb != null)
                {
                    List <int> ids = new List <int>();
                    ids.Add(tb.Id);
                    List <Dto.DiskFolder.Info> folderDescendants = DiskFolderController.SelectDescendants(ids);

                    string strTempPathName = Path.GetTempPath() + Guid.NewGuid().ToString();
                    string strTempPath     = strTempPathName + "/";
                    string zipFileName     = strTempPathName + ".zip";

                    foreach (var folder in folderDescendants)
                    {
                        if (!Directory.Exists(strTempPath + folder.FolderPath))
                        {
                            Directory.CreateDirectory(strTempPath + folder.FolderPath);
                        }
                    }

                    List <Dto.DiskFile.Info> diskFileDescendants = DiskFileController.SelectDescendantsByFolder(ids);
                    string strFilesDiskPath = Server.MapPath("~/Files/Disk/");
                    foreach (var diskFile in diskFileDescendants)
                    {
                        FileInfo file = new FileInfo(strFilesDiskPath + diskFile.FileName);
                        file.CopyTo(strTempPath + diskFile.FolderPath + diskFile.FileTitle);
                    }

                    Code.ZipHelper.CreateZip(zipFileName, strTempPath + tb.DiskFolderName + "/");

                    Code.FileHelper.DeleteDirectory(strTempPath);

                    return(File(zipFileName, Code.Common.DownloadType, tb.DiskFolderName + ".zip"));
                }

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

                List <int> ids = new List <int>();
                ids.Add(vm.DiskFolderEdit.Id);
                List <Dto.DiskFolder.Info> folderDescendants = DiskFolderController.SelectDescendants(ids);

                if (folderDescendants.Select(t => t.Id).Contains(vm.DiskFolderEdit.ParentId))
                {
                    error.AddError("文件夹不能移动到自身及子文件夹中!");
                }

                if (error.Count() == 0)
                {
                    var tb = (from p in db.Table <Disk.Entity.tbDiskFolder>()
                              .Include(p => p.tbDiskFolderParent)
                              where p.Id == vm.DiskFolderEdit.Id && p.tbSysUser.Id == Code.Common.UserId
                              select p).FirstOrDefault();
                    if (tb != null)
                    {
                        if (vm.DiskFolderEdit.ParentId == 0)
                        {
                            tb.tbDiskFolderParent = null;
                        }
                        else
                        {
                            tb.tbDiskFolderParent = db.Set <Disk.Entity.tbDiskFolder>().Find(vm.DiskFolderEdit.ParentId);
                        }
                    }

                    db.SaveChanges();
                }

                return(Code.MvcHelper.Post(error, Url.Action("List", "DiskFile", new
                {
                    folderId = vm.FolderId
                }), "移动成功!"));
            }
        }
        public ActionResult DownloadChecked(string ids)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                List <int> fileIds   = new List <int>();
                List <int> folderIds = new List <int>();
                string[]   arrIds    = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                if (arrIds.Length == 1)
                {
                    string[] arrId = arrIds[0].Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (arrId[1] == "Folder")
                    {
                        int folderId = arrId[0].ConvertToInt();
                        var tb       = (from t in db.Table <Disk.Entity.tbDiskFolder>()
                                        where t.Id == folderId
                                        select t).FirstOrDefault();

                        if (tb != null)
                        {
                            folderIds.Add(folderId);
                            List <Dto.DiskFolder.Info> folderDescendants = DiskFolderController.SelectDescendants(folderIds);

                            string strTempPathName = Path.GetTempPath() + Guid.NewGuid().ToString();
                            string strTempPath     = strTempPathName + "/";
                            string zipFileName     = strTempPathName + ".zip";

                            foreach (var folder in folderDescendants)
                            {
                                if (!Directory.Exists(strTempPath + folder.FolderPath))
                                {
                                    Directory.CreateDirectory(strTempPath + folder.FolderPath);
                                }
                            }

                            List <Dto.DiskFile.Info> diskFileDescendants = DiskFileController.SelectDescendantsByFolder(folderIds);
                            string strFilesDiskPath = Server.MapPath("~/Files/Disk/");
                            foreach (var diskFile in diskFileDescendants)
                            {
                                FileInfo file = new FileInfo(strFilesDiskPath + diskFile.FileName);
                                file.CopyTo(strTempPath + diskFile.FolderPath + diskFile.FileTitle);
                            }

                            Code.ZipHelper.CreateZip(zipFileName, strTempPath + tb.DiskFolderName + "/");

                            Code.FileHelper.DeleteDirectory(strTempPath);

                            return(File(zipFileName, Code.Common.DownloadType, tb.DiskFolderName + ".zip"));
                        }
                    }
                    else
                    {
                        int fileId = arrId[0].ConvertToInt();
                        if (fileId != 0)
                        {
                            var tb = (from p in db.Table <Disk.Entity.tbDiskFile>()
                                      where p.Id == fileId
                                      select p).FirstOrDefault();
                            if (tb != null)
                            {
                                string filePath = Server.MapPath("~/Files/Disk/") + tb.FileName;

                                return(File(filePath, Code.Common.DownloadType, tb.FileTitle));
                            }
                        }
                    }
                }
                else
                {
                    foreach (string strIds in arrIds)
                    {
                        string[] arrId = strIds.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                        if (arrId[1] == "Folder")
                        {
                            folderIds.Add(arrId[0].ConvertToInt());
                        }
                        else
                        {
                            fileIds.Add(arrId[0].ConvertToInt());
                        }
                    }

                    List <Dto.DiskFolder.Info> folderDescendants = DiskFolderController.SelectDescendants(folderIds);
                    string strTempPathName = Path.GetTempPath() + Guid.NewGuid().ToString();
                    string strTempPath     = strTempPathName + "/";
                    string zipFileName     = strTempPathName + ".zip";

                    if (!Directory.Exists(strTempPath))
                    {
                        Directory.CreateDirectory(strTempPath);
                    }

                    foreach (var folder in folderDescendants)
                    {
                        if (!Directory.Exists(strTempPath + folder.FolderPath))
                        {
                            Directory.CreateDirectory(strTempPath + folder.FolderPath);
                        }
                    }

                    List <Dto.DiskFile.Info> diskFileDescendants = DiskFileController.SelectDescendantsByFolder(folderIds);
                    foreach (var fileId in fileIds)
                    {
                        var tb = (from t in db.Table <Disk.Entity.tbDiskFile>()
                                  where t.Id == fileId
                                  select new Dto.DiskFile.Info
                        {
                            Id = t.Id,
                            FileTitle = t.FileTitle,
                            FileName = t.FileName,
                            FolderId = t.tbDiskFolder == null ? 0 : t.tbDiskFolder.Id
                        }).FirstOrDefault();
                        if (tb != null)
                        {
                            diskFileDescendants.Add(tb);
                        }
                    }

                    string strFilesDiskPath = Server.MapPath("~/Files/Disk/");
                    foreach (var diskFile in diskFileDescendants)
                    {
                        FileInfo file = new FileInfo(strFilesDiskPath + diskFile.FileName);
                        file.CopyTo(strTempPath + diskFile.FolderPath + diskFile.FileTitle);
                    }

                    Code.ZipHelper.CreateZip(zipFileName, strTempPath + "/");

                    Code.FileHelper.DeleteDirectory(strTempPath);

                    return(File(zipFileName, Code.Common.DownloadType, DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".zip"));
                }

                return(Content("<script>alert('文件不存在');</script>"));
            }
        }
        public ActionResult MoveToChecked(Models.DiskFile.Edit vm, string ids)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();

                List <int> fileIds   = new List <int>();
                List <int> folderIds = new List <int>();
                string[]   arrIds    = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string id in arrIds)
                {
                    string[] arrId = id.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (arrId[1] == "Folder")
                    {
                        folderIds.Add(Convert.ToInt32(arrId[0]));
                    }
                    else
                    {
                        fileIds.Add(Convert.ToInt32(arrId[0]));
                    }
                }

                List <Dto.DiskFolder.Info> folderDescendants = DiskFolderController.SelectDescendants(folderIds);

                if (folderDescendants.Select(t => t.Id).Contains(vm.DiskFileEdit.FolderId))
                {
                    error.AddError("文件夹不能移动到自身及子文件夹中!");
                }

                if (error.Count() == 0)
                {
                    foreach (int folderId in folderIds)
                    {
                        var tb = (from p in db.Table <Disk.Entity.tbDiskFolder>()
                                  .Include(p => p.tbDiskFolderParent)
                                  where p.Id == folderId && p.tbSysUser.Id == Code.Common.UserId
                                  select p).FirstOrDefault();
                        if (tb != null)
                        {
                            if (vm.DiskFileEdit.FolderId == 0)
                            {
                                tb.tbDiskFolderParent = null;
                            }
                            else
                            {
                                tb.tbDiskFolderParent = db.Set <Disk.Entity.tbDiskFolder>().Find(vm.DiskFileEdit.FolderId);
                            }
                        }
                    }

                    foreach (int fileId in fileIds)
                    {
                        var tb = (from p in db.Table <Disk.Entity.tbDiskFile>()
                                  .Include(p => p.tbDiskFolder)
                                  where p.Id == fileId && p.tbSysUser.Id == Code.Common.UserId
                                  select p).FirstOrDefault();
                        if (tb != null)
                        {
                            if (vm.DiskFileEdit.FolderId == 0)
                            {
                                tb.tbDiskFolder = null;
                            }
                            else
                            {
                                tb.tbDiskFolder = db.Set <Disk.Entity.tbDiskFolder>().Find(vm.DiskFileEdit.FolderId);
                            }
                        }
                    }

                    db.SaveChanges();
                }

                return(Code.MvcHelper.Post(error, Url.Action("List", "DiskFile", new
                {
                    folderId = vm.FolderId
                }), "移动成功!"));
            }
        }
        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));
            }
        }
        public ActionResult Edit(int id = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.DiskFolder.Edit();

                var tbDiskType = DiskTypeController.SelectInfo(vm.DiskTypeId);
                if (tbDiskType != null && tbDiskType.DiskType == Code.EnumHelper.DiskType.Public)
                {
                    if (Code.Common.UserType != Code.EnumHelper.SysUserType.Administrator)
                    {
                        return(Content("只有管理员才可以新建公开文件夹!"));
                    }
                    else
                    {
                        // 管理员只能新建【学校共享文件夹】的子文件夹
                        var tbDiskFolder = (from t in db.Table <Disk.Entity.tbDiskFolder>()
                                            where t.DiskFolderName.Equals("学校共享文件夹") &&
                                            t.tbDiskType.DiskType == Code.EnumHelper.DiskType.Public
                                            select t).FirstOrDefault();
                        if (tbDiskFolder != null)
                        {
                            List <int> ids = new List <int>();
                            ids.Add(tbDiskFolder.Id);

                            if (!DiskFolderController.SelectDescendants(ids).Select(t => t.Id).Contains(vm.FolderId))
                            {
                                return(Content("只能在学校共享文件夹中新建子文件夹!"));
                            }
                            else
                            {
                                vm.DiskFolderEdit.DiskPermit = Code.EnumHelper.DiskPermit.Public;
                            }
                        }
                    }
                }

                vm.DiskTypeList = Disk.Controllers.DiskTypeController.SelectInfoList();

                if (id != 0)
                {
                    var tb = (from p in db.Table <Disk.Entity.tbDiskFolder>()
                              where p.Id == id
                              select new Dto.DiskFolder.Edit
                    {
                        Id = p.Id,
                        No = p.No,
                        DiskFolderName = p.DiskFolderName,
                        DiskTypeId = p.tbDiskType.Id,
                        DiskPermit = p.DiskPermit
                    }).FirstOrDefault();
                    if (tb != null)
                    {
                        vm.DiskFolderEdit = tb;
                    }
                }
                else
                {
                    vm.DiskFolderEdit.DiskTypeId = (tbDiskType == null || tbDiskType.DiskType == Code.EnumHelper.DiskType.Private)
                        ? vm.DiskTypeList.Where(t => t.DiskType == Code.EnumHelper.DiskType.Private).FirstOrDefault().Id
                        : vm.DiskTypeList.Where(t => t.DiskType == Code.EnumHelper.DiskType.Public).FirstOrDefault().Id;
                }

                return(View(vm));
            }
        }