C#:复杂条件判断类型(练习)

 

/// <summary>
    /// 文件类型
    /// </summary>
    public enum FileType
    { 
        Courseware,         //"课件"
        Document,           //"文档"
        Picture,            //"图片"
        Audio,              //"音频"
        Video,              //"视频"
        Cartoon,            //"动画"
        Other               //"其他"
    }

    /// <summary>
    ///  资源类型
    /// </summary>
    public enum ContentType
    { 
        MaterialInterpretation,     //"教材解读",
        Writing,                    //"写作", 
        ExpandData,                 //"拓展资料",
        CoursewareIllustrated,      //"课件配图",
        ReadText,                   //"课文朗读",
        LearnCase,                  //"学案",
        AudioVideo,                 //"音视频",
        TeachingPlan,               //"教案",
        Courseware,                 //"课件",
        TestQuestions,              //"试题",
        Other,                      //"其他"
    }

    /// <summary>
    /// 判断资源类型 (shenc 2016-7-11)
    /// </summary>
    public class JudgeContentType
    {
        LogService _logger = new LogService();

        #region 文件格式类型

        public List<string> _fileTypeName = new List<string>{"课件","文档","图片","音频","视频","动画","其他"};
        public List<string> _coursewareFormatType = new List<string> { "ppt", "pptx"};
        public List<string> _documentFormatType = new List<string> { "doc", "docx", "xls", "xlsx", "pdf", "vsd", "rtf", "txt", "xml", "xpt", "pps", "ppsx", "pdfx" };
        public List<string> _pictureFormatType = new List<string> { "jpg", "jpeg", "gif", "bmp", "png", "tif" };
        public List<string> _audioFormatType = new List<string> { "mp3", "mid", "wma", "wav", "flac", "m4a", "voc" };
        public List<string> _videoFormatType = new List<string> { "flv", "avi", "mkv", "wmv", "mp4", "f4v", "mpg", "asf", "webm", "mov", "rmvb", "rm" };
        public List<string> _cartoonFormatType = new List<string> { "swf" };
        public List<string> _otherFormatType = new List<string> { "exe", "dbb" };
        public Dictionary<FileType, List<string>> _dicFileType_Formats = new Dictionary<FileType, List<string>>();
        //public Dictionary<string, List<string>> _dicFileTypeName_Formats = new Dictionary<string, List<string>>();
        public Dictionary<FileType, string> _dicFileType_Name = new Dictionary<FileType, string>();
        #endregion

        #region 资源格式

        public List<string> _contentTypeName = new List<string> { "教材解读", "写作", "拓展资料", "课件配图", "课文朗读", "学案", "音视频", "教案", "课件", "试题", "其他" };
        public Dictionary<ContentType, string> _dicContentType_Name = new Dictionary<ContentType, string>();
        public List<string> _materialInterpretationFlags = new List<string> { "教材解读" };
        public List<string> _writingFlags = new List<string> { "写作" };
        public List<string> _expandDataFlags = new List<string> { "拓展资料", "专题资源"};
        public List<string> _coursewareIllustratedFlags = new List<string> {  };
        public List<string> _readTextFlags = new List<string> { "课文朗读" };
        public List<string> _learnCaseFlags = new List<string> { "学案" };
        public List<string> _audioVideoFlags = new List<string> {  };
        public List<string> _teachingPlanFlags = new List<string> { "教案", "教学设计", "教学案例", "说课", "教学反思", "教学案", "课程设计", "教学方案" };  //教案、教学设计、教学案例、说课、教学反思,教学案,课程设计,教学方案
        public List<string> _coursewareFlags = new List<string> {  };
        public List<string> _testQuestionsFlags = new List<string> { "试题", "习题", "练习" }; //试题、习题、练习
        public List<string> _otherFlags = new List<string> { };    //以上不包含的文件,资源格式均列入“其他”

        #endregion

        public JudgeContentType()
        {
            InitFileType_Name();
            InitFileTypeFormats();
            //InitFileTypeNameFormats();
            InitContentType_Name();
        }

        private void InitFileType_Name()
        {
            _dicFileType_Name.Clear();
            int i = 0;
            FileType type = FileType.Other;
            foreach (string typeName in _contentTypeName)
            {
                type = (FileType)i++;
                _dicFileType_Name.Add(type, typeName);
            }
        }
        /*
        private void InitFileTypeNameFormats()
        {
            _dicFileTypeName_Formats.Add("课件", _coursewareFormatType);
            _dicFileTypeName_Formats.Add("文档", _documentFormatType);
            _dicFileTypeName_Formats.Add("图片", _pictureFormatType);
            _dicFileTypeName_Formats.Add("音频", _audioFormatType);
            _dicFileTypeName_Formats.Add("视频", _videoFormatType);
            _dicFileTypeName_Formats.Add("动画", _cartoonFormatType);
            _dicFileTypeName_Formats.Add("其他", _otherFormatType);
        }
        */
        private void InitFileTypeFormats()
        {
            _dicFileType_Formats.Clear();
            _dicFileType_Formats.Add(FileType.Courseware, _coursewareFormatType);
            _dicFileType_Formats.Add(FileType.Document, _documentFormatType);
            _dicFileType_Formats.Add(FileType.Picture, _pictureFormatType);
            _dicFileType_Formats.Add(FileType.Audio, _audioFormatType);
            _dicFileType_Formats.Add(FileType.Video, _videoFormatType);
            _dicFileType_Formats.Add(FileType.Cartoon, _cartoonFormatType);
            _dicFileType_Formats.Add(FileType.Other, _otherFormatType);
        }
        private void InitContentType_Name()
        {
            _dicContentType_Name.Clear();
            int i = 0;
            ContentType contentType = ContentType.Other;
            foreach (string typeName in _contentTypeName)
            {
                contentType = (ContentType) i++;
                _dicContentType_Name.Add(contentType,typeName);
            }
        }


        #region 获取内容类型、名称

        public string GetContentTypeName(string fileName)
        {
            string contentName = string.Empty;
            ContentType contentType = GetContentType(fileName);
            contentName = _dicContentType_Name[contentType];
            return contentName;
        }

        public ContentType GetContentType(string fileName)
        {
            ContentType contentType = ContentType.Other;
            try
            {
                if (IsMaterialInterpretationType(fileName))
                {
                    contentType = ContentType.MaterialInterpretation;
                }
                else if (IsWritingType(fileName))
                { 
                    contentType = ContentType.Writing;
                }
                else if (IsExpandDataType(fileName))
                { 
                    contentType = ContentType.ExpandData;
                }
                else if (IsCoursewareIllustratedType(fileName))
                { 
                    contentType = ContentType.CoursewareIllustrated;
                }
                else if (IsReadTextType(fileName))
                { 
                    contentType = ContentType.ReadText;
                }
                else if (IsLearnCaseType(fileName))
                { 
                    contentType = ContentType.LearnCase;
                }
                else if (IsAudioVideoType(fileName))
                { 
                    contentType = ContentType.AudioVideo;
                }
                else if (IsTeachingPlanType(fileName))
                { 
                    contentType = ContentType.TeachingPlan;
                }
                else if (IsCoursewareType(fileName))
                { 
                    contentType = ContentType.Courseware;
                }
                else if (IsTestQuestionsType(fileName))
                {
                    contentType = ContentType.TestQuestions;
                }
                else
                {
                    contentType = ContentType.Other;
                }
            }
            catch(Exception ex)
            {
                contentType = ContentType.Other;
                _logger.Error("获取教材解读内容类型出错:" + ex.Message);
            }
            return contentType;
        }

        public bool IsMaterialInterpretationType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _materialInterpretationFlags) && _documentFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsWritingType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName,_writingFlags) && _documentFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取写作内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsExpandDataType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _expandDataFlags) && _documentFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取拓展资料内容类型出错:" + ex.Message);
            }
            return isRight;         
        }
        public bool IsCoursewareIllustratedType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _coursewareIllustratedFlags) && _pictureFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取课件配图内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsReadTextType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _readTextFlags) && _audioFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取课文朗读内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsLearnCaseType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _learnCaseFlags) && _documentFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取学案内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsAudioVideoType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (!IsReadTextType(fileName) && isFileNameContainsFlags(fileName, _audioVideoFlags) && (_audioFormatType.Contains(ext) || _videoFormatType.Contains(ext) || _cartoonFormatType.Contains(ext)))
                {
                    isRight = true;
                }
            }
            catch(Exception ex)
            {
                isRight = false;
                _logger.Error("获取音视频内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsTeachingPlanType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _teachingPlanFlags) && _documentFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch(Exception ex)
            {
                isRight = false;
                _logger.Error("获取教案内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsCoursewareType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _coursewareFlags) && _coursewareFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch(Exception ex)
            {
                isRight = false;
                _logger.Error("获取课件内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsTestQuestionsType(string fileName)
        {
            bool isRight = false;
            try
            {
                string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                if (isFileNameContainsFlags(fileName, _teachingPlanFlags) && _documentFormatType.Contains(ext))
                {
                    isRight = true;
                }
            }
            catch(Exception ex)
            {
                isRight = false;
                _logger.Error("获取试题内容类型出错:" + ex.Message);
            }
            return isRight;
        }
        public bool IsOtherType(string fileName)
        {
            bool isRight = false;
            try
            {
                //string ext = Path.GetExtension(fileName.Trim()).Replace(".",string.Empty);
                //if (isFileNameContainsFlags(fileName, _otherFlags) && _otherFormatType.Contains(ext))
                //{
                //    isRight = true;
                //}
                //else 
                if (!IsAudioVideoType(fileName) && !IsCoursewareIllustratedType(fileName) && !IsCoursewareType(fileName) && !IsExpandDataType(fileName) && IsLearnCaseType(fileName) && !IsMaterialInterpretationType(fileName) && !IsReadTextType(fileName) && !IsTeachingPlanType(fileName) && !IsTestQuestionsType(fileName) && !IsWritingType(fileName))
                {
                    isRight = true;
                }
            }
            catch (Exception ex)
            {
                isRight = false;
                _logger.Error("获取其他内容类型出错:" + ex.Message);
            }
            return isRight;
        }

        public bool isFileNameContainsFlags(string fileName,List<string> contentFlags)
        {
            bool isContain = false;
            if (contentFlags == null || contentFlags.Count == 0)
            {
                isContain = true;
            }
            else
            {
                foreach (string flag in contentFlags)
                {
                    if (fileName.Contains(flag.Trim()))
                    {
                        isContain = true;
                        break;
                    }
                }
            }
            return isContain;
        }
        #endregion
    }
View Code

 

posted @ 2016-07-13 15:26  慧由心生  阅读(1678)  评论(0编辑  收藏  举报