Beispiel #1
0
        /// <summary>
        /// 在删除文件或者关键字的同时将其对应的搜索结果删除
        /// </summary>
        /// <param name="targetType">
        ///     当此参数为1时,则第二个参数表示TraficFilesId
        ///     当此参数为2时,则第二个参数表示KeywordsId
        /// </param>
        /// <param name="targetId">文件Id或者关键字Id</param>
        private void DeleteSearchResult(int targetType, int targetId)
        {
            string condition;

            switch (targetType)
            {
            case 1:
                condition = $"TraficFileId={targetId}";
                break;

            case 2:
                condition = $"KeywordsId={targetId}";
                break;

            default:
                return;
            }

            var resultBll = new TraficSearchResultBll();
            var list      = resultBll.QueryList(condition, new[] { "Id" }).ToList();

            if (list.Any())
            {
                var idList    = list.Select(item => item.Id);
                var tableName = typeof(TraficSearchResult).Name;

                resultBll.ExecuteTransation(
                    () => resultBll.Delete(condition),
                    () =>
                {
                    DataUpdateLog.BulkUpdate(tableName, idList);
                    return(true);
                });
            }
        }
Beispiel #2
0
        /// <summary>
        /// 处理由重复的关键字引起的重复搜索结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDistinct2_Click(object sender, EventArgs e)
        {
            var bll         = new TraficKeywordsBll();
            var resBll      = new TraficSearchResultBll();
            var dbUpdateBll = new DbUpdateLogBll();

            var list          = bll.QueryAll();
            var searchResults = resBll.QueryList("", new string[] { "Id", "KeywordsId" });
            var groups        = list.GroupBy(k => k.Keywords);

            foreach (var group in groups)
            {
                if (group.Count() > 1)
                {
                    var ids     = group.Select(k => k.Id).ToList();
                    var firstId = ids.First();
                    ids.RemoveAt(0);

                    // 删除重复项
                    var idsStr                = string.Join(",", ids);
                    var deleteKeywodsSql      = $"DELETE FROM TraficKeywords WHERE Id IN({idsStr})";
                    var deleteKeywordsUpdates = $"DELETE FROM DbUpdateLog WHERE TargetId IN({idsStr}) AND TableName='TraficKeywords'";

                    var results             = searchResults.Where(r => ids.Contains(r.KeywordsId));
                    var resIds              = results.Select(r => r.Id);
                    var resIdsStr           = string.Join(",", resIds);
                    var deleteResultSql     = $"DELETE FROM TraficSearchResult WHERE KeywordsId IN({idsStr})";
                    var deleteResultUpdates = $"DELETE FROM DbUpdateLog WHERE TargetId IN({(resIdsStr == "" ? "0" : resIdsStr)}) AND TableName='TraficSearchResult'";

                    bll.ExecuteTransation(
                        () => bll.ExecuteSql(deleteKeywodsSql) >= 0,
                        () => bll.ExecuteSql(deleteKeywordsUpdates) >= 0,
                        () => bll.ExecuteSql(deleteResultSql) >= 0,
                        () => bll.ExecuteSql(deleteResultUpdates) >= 0
                        );
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 删除目录,将在事务环境中执行以下操作
        ///  1. 删除此目录及所有子目录
        ///  2. 删除此目录及所有子目录下的文件
        ///  3. 删除此与此目录下所有文件关联的关键字搜索结果数据
        ///  4. 将操作同步到DbUpdateLog表中
        ///  5. 将操作日志记录到OperateLog表中
        /// </summary>
        /// <param name="id">待删除的目录Id</param>
        /// <returns></returns>
        private bool DeleteDirectoryAndChildren(int id)
        {
            var dirBll    = new TraficFileTypeBll();
            var dirList   = dirBll.QueryList("IsDelete=0", new[] { nameof(TraficFileType.Id), nameof(TraficFileType.ParentId), nameof(TraficFileType.TypeName) }).ToList();
            var deleteDir = dirList.Find(dir => dir.Id == id);

            if (deleteDir == null)
            {
                return(false);
            }

            var fileBll  = new TraficFilesBll();
            var fileList = fileBll.QueryList("IsDelete=0", new[] { nameof(TraficFiles.Id), nameof(TraficFiles.TypeId) }).ToList();

            // 所有待删除的目录Id
            var dirDeleteIdList = new List <int> {
                id
            };

            FindChildDirIds(dirList, id, dirDeleteIdList);

            // 所有待删除的文件Id
            var fileDeleteIdList = fileList.Where(file => dirDeleteIdList.Contains(file.TypeId)).Select(file => file.Id).ToList();

            // 所有待删除的搜索结果Id
            var searchBll    = new TraficSearchResultBll();
            var searchSql    = $"TraficFileId IN({string.Join(",", fileDeleteIdList.Count == 0 ? new List<int> { 0 } : fileDeleteIdList)})"; // 不能让sql语句中IN()中的字符串为空
            var searchIdList = searchBll.QueryList(searchSql, new[] { nameof(TraficSearchResult.Id) })
                               .Select(item => item.Id).ToList();

            // 操作日志
            var log = $"删除了目录[{deleteDir.TypeName}]及其所有的子目录和子文件";

            // 准备sql语句
            var dirDeleteSql    = $"UPDATE TraficFileType SET IsDelete=1 WHERE Id IN({string.Join(",", dirDeleteIdList)})";
            var fileDeleteSql   = $"UPDATE TraficFiles SET IsDelete=1 WHERE Id IN({string.Join(",", fileDeleteIdList)})";
            var searchDeleteSql = $"DELETE FROM TraficSearchResult WHERE TraficFileId IN({string.Join(",", fileDeleteIdList)})";

            // 准备执行委托
            var         logBll                = new OperateLogBll();
            var         loginUser             = LoginStatus.GetLoginId();
            Func <bool> deleteDirs            = () => dirBll.ExecuteSql(dirDeleteSql) == dirDeleteIdList.Count;
            Func <bool> deleteFiles           = () => fileDeleteIdList.Count == 0 ? true : fileBll.ExecuteSql(fileDeleteSql) == fileDeleteIdList.Count;
            Func <bool> deleleSearch          = () => searchIdList.Count == 0 ? true : searchBll.ExecuteSql(searchDeleteSql) == searchIdList.Count;
            Func <bool> insertDirUpdateLog    = () => { DataUpdateLog.BulkUpdate(nameof(TraficFileType), dirDeleteIdList, DataUpdateType.Delete); return(true); };
            Func <bool> insertFileUpdateLog   = () => { DataUpdateLog.BulkUpdate(nameof(TraficFiles), fileDeleteIdList, DataUpdateType.Delete); return(true); };
            Func <bool> insertSearchUpdateLog = () => { DataUpdateLog.BulkUpdate(nameof(TraficSearchResult), searchIdList, DataUpdateType.Delete); return(true); };
            Func <bool> insertOperateLog      = () => logBll.Add(nameof(TraficFileType), id, DataUpdateType.Delete, loginUser, log);

            // 执行事务
            var success = dirBll.ExecuteTransation(
                deleteDirs,
                deleteFiles,
                deleleSearch,
                insertDirUpdateLog,
                insertFileUpdateLog,
                insertSearchUpdateLog,
                insertOperateLog
                );

            return(success);
        }