Пример #1
0
        /// <summary>
        /// 插入音乐信息
        /// </summary>
        /// <returns></returns>
        private bool ImportMusicToDB()
        {
            try
            {
                //格式检查
                if (!FormatCheck())
                {
                    return false;
                }

                //导入列表
                MusicDataSet.ImportMusicListDataTable resultList = new MusicDataSet.ImportMusicListDataTable();

                #region resultList作成
                foreach (DataGridViewRow dr in MusicDataGridView.Rows)
                {
                    //------做成需要导入的曲目 开始------
                    MusicDataSet.ImportMusicListRow resRow = resultList.NewImportMusicListRow();

                    //曲号
                    resRow.TrackID = dr.Cells[TRACKIDCLN].Value.ToString();
                    //曲名
                    resRow.TrackTitleName = dr.Cells[TRACKNAMECLN].Value.ToString().Trim();
                    //曲目类型
                    resRow.TrackTypeId = Convert.ToInt32(dr.Cells[TRACKTYPEIDCLN].Value);
                    //专辑号
                    resRow.AlbumID = dr.Cells[ALBUMIDCLN].Value.ToString();
                    //专辑名
                    resRow.AlbumTitleName = dr.Cells[ALBUMNAMECLN].Value.ToString().Trim();
                    //专辑类型
                    resRow.AlbumTypeID = Convert.ToInt32(dr.Cells[ALBUMTYPEIDCLN].Value);
                    //艺术家编号
                    resRow.ArtistID = Convert.ToInt32(dr.Cells[ARTISTIDCLN].Value);
                    //碟号
                    resRow.DiscNo = dr.Cells[DISCNOCLN].Value.ToString().Trim();
                    //音轨
                    resRow.TrackNo = dr.Cells[TRACKNOCLN].Value.ToString().Trim();
                    //发售年份
                    if (dr.Cells[YEARCLN].Value != null)
                    {
                        resRow.SalesYear = dr.Cells[YEARCLN].Value.ToString().Trim();
                    }
                    //资源路径
                    resRow.FilePath = dr.Cells[RESOURCEPATHCLN].Value.ToString();
                    //描述
                    if (dr.Cells[DESCRIPTIONCLN].Value != null)
                    {
                        resRow.Description = dr.Cells[DESCRIPTIONCLN].Value.ToString();
                    }
                    //动画编号
                    if (dr.Cells[ANIMENOCLN].Value != null)
                    {
                        resRow.AnimeNo = dr.Cells[ANIMENOCLN].Value.ToString();
                    }
                    //专辑类型编号
                    resRow.AlbumTypeID = Convert.ToInt32(dr.Cells[ALBUMTYPEIDCLN].Value);
                    //曲目类型编号
                    resRow.TrackTypeId = Convert.ToInt32(dr.Cells[TRACKTYPEIDCLN].Value);
                    //比特率
                    resRow.BitRate = dr.Cells[BITRATECLN].Value.ToString();
                    //歌曲长度
                    resRow.TrackLength = dr.Cells[TRACKTIMELENGTHCLN].Value.ToString();
                    //艺术家姓名
                    resRow.ArtistName = dr.Cells[ARTISTNAMECLN].Value.ToString();

                    resultList.Rows.Add(resRow);
                }

                resultList.AcceptChanges();
                #endregion

                //所有需要插入数据物理表对应的List
                List<AlbumSeries> AlbumSeriesList = new List<AlbumSeries>();
                //所有需要复制的资源,考虑到以后的专辑封面图预留
                List<ResourceSeries> ResourceToCopy = new List<ResourceSeries>();

                #region 所有信息作成

                var targetAlbum = (from v in resultList
                                   select new
                                   {
                                       v.AlbumID,
                                       v.AlbumTitleName,
                                       v.AlbumTypeID,
                                       v.AnimeNo
                                   }).Distinct().ToList();

                foreach (var albumInfo in targetAlbum)
                {
                    //专辑信息作成
                    AlbumSeries album = new AlbumSeries();

                    album.ID = albumInfo.AlbumID;
                    album.AlbumTitleName = albumInfo.AlbumTitleName;
                    album.AlbumTypeId = albumInfo.AlbumTypeID;
                    album.AnimeNo = albumInfo.AnimeNo;
                    album.InAnimeNo = service.GetNextInAnimeAlbumNo(album.ID, album.AlbumTypeId);

                    //计算总碟数:maxdiscno
                    album.TotalDiscCount = GetTotalDisc(album.ID, resultList);

                    //计算总曲数:count
                    var counttrack = (from discc in resultList
                                      select discc.TrackID).Count();
                    album.TotalTrackCount = Convert.ToInt32(counttrack);

                    //曲目信息作成
                    var targetTrack = from v in resultList
                                      where v.AlbumID.Equals(album.ID)
                                      select v;
                    foreach (MusicDataSet.ImportMusicListRow ir in targetTrack)
                    {
                        //曲目基本信息
                        TrackSeries track = new TrackSeries();

                        track.ID = ir.TrackID;
                        track.PAlbumID = ir.AlbumID;
                        track.TrackTypeId = ir.TrackTypeId;
                        track.TrackTitleName = ir.TrackTitleName;
                        track.ArtistID = ir.ArtistID;
                        track.AnimeNo = ir.AnimeNo;
                        if (!string.IsNullOrEmpty(ir.SalesYear))
                        {
                            track.SalesYear = Convert.ToInt32(ir.SalesYear);
                        }
                        if (!string.IsNullOrEmpty(track.Description))
                        {
                            track.Description = ir.Description;
                        }

                        if (!string.IsNullOrEmpty(ir.DiscNo))
                        {
                            track.DiscNo = format.GetDiscNoOrTrackNo(ir.DiscNo);
                        }
                        else
                        {
                            track.DiscNo = 0;
                        }
                        if (!string.IsNullOrEmpty(ir.TrackNo))
                        {
                            track.TrackNo = format.GetDiscNoOrTrackNo(ir.TrackNo);
                        }
                        else
                        {
                            track.TrackNo = 0;
                        }

                        //既存音源文件
                        FileInfo MusicResource = new FileInfo(ir.FilePath);

                        if (!MusicResource.Exists)
                        {
                            MsgBox.Show(MSG_IMPORTMUSIC_014, MusicResource.FullName);
                        }

                        //曲目资源信息
                        //1.备份音源
                        ResourceSeries bakMusic = new ResourceSeries();

                        bakMusic.GetNewID();
                        bakMusic.TypeID = ResourceFile.Type.MUSIC_MP3_1;
                        bakMusic.StorageID = StorageID.Path.BAK_RESOURCE_BUCKET_101;
                        //sound.FilePath = null;
                        bakMusic.FileName = format.FileNameFormat(bakMusic.ID.ToString());
                        bakMusic.Suffix = ".mp3";
                        bakMusic.TrackBitRate = ir.BitRate;
                        bakMusic.TrackLength = format.GetSecondFromTime(ir.TrackLength);
                        bakMusic.objectFilePath = ir.FilePath;
                        track.AddResource(bakMusic);
                        ResourceToCopy.Add(bakMusic);

                        TrackResource bakMusicMap = new TrackResource();
                        bakMusicMap.ResourceID = bakMusic.ID;
                        bakMusicMap.TrackID = ir.TrackID;
                        track.AddMapping(bakMusicMap);

                        //2.主要音源
                        ResourceSeries mainMusic = new ResourceSeries();
                        mainMusic.GetNewID();
                        mainMusic.TypeID = ResourceFile.Type.MUSIC_MP3_1;
                        mainMusic.StorageID = StorageID.Path.MAIN_RESOURCE_BUCKET_201;
                        //..\@anime_no\@album_type_name\@album_title_name\@[email protected]
                        mainMusic.FilePath = format.FilePathFormat(ir.AnimeNo + "\\" + service.GetAlbumTypeNameByAlbumTypeID(ir.AlbumTypeID) + "\\" + format.FileNameFormat(ir.AlbumTitleName));
                        mainMusic.FileName = format.FileNameFormat(ir.ArtistName + "_" + ir.TrackTitleName);
                        mainMusic.Suffix = ".mp3";
                        mainMusic.TrackBitRate = ir.BitRate;
                        mainMusic.TrackLength = format.GetSecondFromTime(ir.TrackLength);
                        mainMusic.objectFilePath = ir.FilePath;
                        track.AddResource(mainMusic);
                        ResourceToCopy.Add(mainMusic);

                        TrackResource mainMusicMap = new TrackResource();
                        mainMusicMap.ResourceID = mainMusic.ID;
                        mainMusicMap.TrackID = ir.TrackID;
                        track.AddMapping(mainMusicMap);

                        //歌词文件存否判断
                        string Directory = MusicResource.DirectoryName;
                        string ResourceName = Path.GetFileNameWithoutExtension(MusicResource.Name);
                        string LrcFullPath = Directory + "\\" + ResourceName + ".lrc";
                        FileInfo LrcResource = new FileInfo(LrcFullPath);

                        if (LrcResource.Exists)
                        {
                            //3.备份歌词
                            ResourceSeries bakLrc = new ResourceSeries();

                            bakLrc.GetNewID();
                            bakLrc.TypeID = ResourceFile.Type.LYRIC_LRC_201;
                            bakLrc.StorageID = StorageID.Path.BAK_RESOURCE_BUCKET_101;
                            //sound.FilePath = null;
                            bakLrc.FileName = format.FileNameFormat(bakLrc.ID.ToString());
                            bakLrc.Suffix = ".lrc";
                            bakLrc.objectFilePath = ir.FilePath;
                            track.AddResource(bakLrc);
                            ResourceToCopy.Add(bakLrc);

                            TrackResource bakLrcMap = new TrackResource();
                            bakLrcMap.ResourceID = bakLrc.ID;
                            bakLrcMap.TrackID = LrcFullPath;
                            track.AddMapping(bakLrcMap);

                            //4.主要歌词
                            ResourceSeries mainLrc = new ResourceSeries();
                            mainLrc.GetNewID();
                            mainLrc.TypeID = ResourceFile.Type.LYRIC_LRC_201;
                            mainLrc.StorageID = StorageID.Path.MAIN_RESOURCE_BUCKET_201;
                            //..\@anime_no\@album_type_name\@album_title_name\@[email protected]
                            mainLrc.FilePath = format.FilePathFormat(ir.AnimeNo + "\\" + service.GetAlbumTypeNameByAlbumTypeID(ir.AlbumTypeID) + "\\" + format.FileNameFormat(ir.AlbumTitleName));
                            mainLrc.FileName = format.FileNameFormat(ir.ArtistName + "_" + ir.TrackTitleName);
                            mainLrc.objectFilePath = LrcFullPath;
                            track.AddResource(mainLrc);
                            ResourceToCopy.Add(mainLrc);

                            TrackResource mainLrcMap = new TrackResource();
                            mainLrcMap.ResourceID = mainLrc.ID;
                            mainLrcMap.TrackID = ir.TrackID;
                            track.AddMapping(mainLrcMap);
                        }

                        album.AddTracks(track);
                    }
                    AlbumSeriesList.Add(album);
                }
                #endregion

                #region DB操作

                foreach (AlbumSeries album in AlbumSeriesList)
                {
                    album.Insert();
                }

                #endregion

                #region 文件操作

                foreach (ResourceSeries resource in ResourceToCopy)
                {
                    string DirPath = service.GetResourceDirectoryPath(resource.StorageID, resource.FilePath);
                    if (!Directory.Exists(DirPath))
                    {
                        Directory.CreateDirectory(DirPath);
                    }

                    string targetPath = service.GetResourcePath(resource.StorageID, resource.FilePath, resource.FileName, resource.Suffix);
                    File.Copy(resource.objectFilePath, targetPath);
                }

                #endregion
                return true;
            }
            catch (Exception ex)
            {
                MsgBox.Show(MSG_COMMON_001, ex.ToString());
                return false;
            }
        }
Пример #2
0
        /// <summary>
        /// 展示导入的音乐
        /// </summary>
        /// <param name="musicPath">音乐路径数组</param>
        private void ShowMusicInfo(string[] musicPath)
        {
            //导入列表
            MusicDataSet.ImportMusicListDataTable importList = new MusicDataSet.ImportMusicListDataTable();

            for (int i = 0; i < musicPath.Length; i++)
            {
                if (!System.IO.File.Exists(musicPath[i]))
                {
                    MsgBox.Show(MSG_IMPORTMUSIC_001, musicPath[i]);
                    continue;
                }

                //------做成新曲目 开始------
                MusicDataSet.ImportMusicListRow importRow = importList.NewImportMusicListRow();

                //FilePath
                importRow.FilePath = musicPath[i];

                ID3V2Tag tag = new ID3V2Tag(importRow.FilePath);

                //TrackID 仮采番
                importRow.TrackID = service.GetNextTrackID();
                //AlbumID 仮采番
                importRow.AlbumID = service.GetAlbumIDFromAlbumTitleName(tag.AlbumName);
                //TrackTypeID
                //DiscNo
                importRow.DiscNo = tag.DiscNo;
                //TrackNo
                importRow.TrackNo = tag.TrackNo;
                //OldTrackNo

                //AlbumTitleName
                importRow.AlbumTitleName = tag.AlbumName.Replace('�', char.MinValue);
                //TrackTitleName
                importRow.TrackTitleName = tag.TrackTitleName.Replace('�', char.MinValue);
                //ArtistName
                importRow.ArtistName = tag.ArtistName.Replace('�', char.MinValue);
                //ArtistID
                //AnimeNo
                //SalesYear
                importRow.SalesYear = tag.SalesYear.Replace('�', char.MinValue);
                //TrackLength
                importRow.TrackLength = tag.TrackLength.Replace('�', char.MinValue);
                //BitRate
                importRow.BitRate = tag.BitRate.TrimEnd('\0');
                importList.AddImportMusicListRow(importRow);
            }

            importList.AcceptChanges();

            DataView dv = importList.DefaultView;
            dv.Sort = "AlbumID asc , DiscNo asc , TrackNo asc";
            MusicDataSet.ImportMusicListDataTable newImportList = new MusicDataSet.ImportMusicListDataTable();
            newImportList.Merge(dv.ToTable());

            foreach (MusicDataSet.ImportMusicListRow ir in newImportList.Rows)
            {
                DataGridViewRow dr = MusicDataGridView.Rows[MusicDataGridView.Rows.Add()];

                dr.Cells[TRACKIDCLN].Value = ir.TrackID;
              if (ir.TrackTitleName != null)
                {
                    dr.Cells[TRACKNAMECLN].Value = ir.TrackTitleName.Replace('�', char.MinValue).Trim('\0');
                }
                dr.Cells[ALBUMIDCLN].Value = ir.AlbumID;
                if (ir.AlbumTitleName != null)
                {
                    dr.Cells[ALBUMNAMECLN].Value = ir.AlbumTitleName.Replace('�', char.MinValue).Trim('\0');
                }
                if (ir.ArtistName != null)
                {
                    dr.Cells[ARTISTNAMECLN].Value = ir.ArtistName.Replace('�', char.MinValue).Trim('\0');
                }
                if (ir.DiscNo != null)
                {
                    dr.Cells[DISCNOCLN].Value = ir.DiscNo.Replace('�', char.MinValue).Trim('\0');
                }
                if (ir.TrackNo != null)
                {
                    dr.Cells[TRACKNOCLN].Value = ir.TrackNo.Replace('�', char.MinValue).Trim('\0');
                }
                if (!ir.IsSalesYearNull())
                {
                    dr.Cells[YEARCLN].Value = ir.SalesYear.Replace('�', char.MinValue).Trim('\0');
                }
                if (!ir.IsDescriptionNull())
                {
                    dr.Cells[DESCRIPTIONCLN].Value = ir.Description;
                }
                if (!ir.IsBitRateNull())
                {
                    dr.Cells[BITRATECLN].Value = ir.BitRate;
                }
                if (!ir.IsTrackLengthNull())
                {
                    dr.Cells[TRACKTIMELENGTHCLN].Value = ir.TrackLength;
                }
                dr.Cells[RESOURCEPATHCLN].Value = ir.FilePath;

                //艺术家处理
                //Todo:预留同名artist处理机制:复数artist
                int artistID = service.GetArtistIDFromArtistName(ir.ArtistName);
                if (artistID > 0)
                {
                    //不在dic,在DB中存在名称完全相同,加入dic
                    dr.Cells[ARTISTIDCLN].Value = artistID;
                }

                SetArtistCellStyle(dr.Cells[ARTISTNAMECLN], ir.ArtistName);

                //MusicDataGridView.Sort(MusicDataGridView.Columns[ALBUMIDCLN], ListSortDirection.Ascending);
            }

            dgvStyle.SetDataGridViewColumnWidch(MusicDataGridView, new int[] {
                120,    //OldTrackNo
                100,    //TrackNo
                200,    //TrackName
                80,    //TrackType
                100,    //AlbumNo
                200,    //AlbumName
                80,     //AlbumType
                200,    //ArtistName
                120,    //AnimeCHNName
                80,     //BitRate
                70,     //DiscNo
                70,     //TrackNo
                120,    //Year
                100,    //TrackLength
                300,    //ResourcePath
                200     //Description
            });

            string firstRowPath = null;

            if (MusicDataGridView.Rows.Count != 0)
            {
                firstRowPath = MusicDataGridView.Rows[0].Cells[RESOURCEPATHCLN].Value.ToString();

                ShowMP3TagInfo(firstRowPath);
            }
        }