处理歌曲标签的问题

问题:

突然出现一个问题,修改歌曲后标签序号没有了?

 

查找原因:

经过排查,发现是在歌曲保存后会清掉歌曲的Tag,然后重新创建Tag,这样给歌曲打上的Tag自然就有问题了,当初是为了方便这样写代码。

 

解决思路:

 

 在修改歌曲的情况下:

1:标签没有发生变化

2:标签发生了变化

 

也就是说需要知道删除了哪些标签,新增加了哪些标签, 重合的标签不需要处理!

 

1:怎么得到删除的标签?

从原来歌曲本身的标签中做循环,判断在复选框中是否勾选?如果没有勾选,那么表示是要删除的!

 

2:怎么得到新增的标签? 

从已勾选的复选框中做循环, 判断在歌曲本身有没有这个标签,如果没有,表示是要添加的标签!

 

代码实现:

View Code 
  1         //tmpmuscitagdata是当前歌曲的标签数据 DataTable
  2 
  3         private void Add()
  4         {
  5             musicitem.Visible = true;
  6             musicitem.Admin = Global.CurrentUser.ID;
  7             if (Proxies.MusicServices.Add(ref musicitem))
  8             {
  9                 id = musicitem.ID;
 10                 addtag(GetTagList(true));
 11             }
 12             else
 13             {
 14                 MessageUtil.ShowTips(musicitem.OperateMessage);
 15             }
 16         }
 17 
 18         private void Edit()
 19         {
 20             if (Proxies.MusicServices.Update(ref musicitem))
 21             {
 22                 if (!DeleteOldTag())
 23                 {
 24                     return;
 25                 }
 26                 addtag(GetNewTag());
 27             }
 28             else
 29             {
 30                 MessageUtil.ShowTips(musicitem.OperateMessage);
 31             }
 32         }
 33 
 34         private bool DeleteOldTag()
 35         {
 36             if (tmpmuscitagdata == null || tmpmuscitagdata.Rows.Count == 0)//如果歌曲没有标签,那么肯定也不需要进行删除操作
 37             {
 38                 return true;
 39             }
 40 
 41             List<int> checkedlist = GetTagList(true);//选中的标签
 42             string deleteids = string.Empty;
 43 
 44             for (int i = 0; i < tmpmuscitagdata.Rows.Count; i++)//循环歌曲本身的标签
 45             {
 46                 string tmp = tmpmuscitagdata.Rows[i]["TagCategoryID"].ToString();
 47                 if (!checkedlist.Contains(int.Parse(tmp)))//如果选中的标签列表中不存在当前歌曲的标签,则表示删除
 48                 {
 49                     deleteids += (tmp + ",");
 50                 }
 51             }
 52 
 53             if (deleteids.Length == 0)//没有取消的标签,则不需要进行删除处理
 54             {
 55                 return true;
 56             }
 57 
 58             TagDataListEntity tag = new TagDataListEntity();
 59             tag.Where = string.Format("SourceID={0}  and TagCategoryID in ({1}) ",id, deleteids.TrimEnd(','));
 60             return Proxies.TagContentServices.Delete(ref tag);
 61         }
 62 
 63         private List<int> GetNewTag()
 64         {
 65             List<int> newids = new List<int>();
 66             var taglist = GetTagList(true);//得到选中的标签
 67             for (int i = 0; i < taglist.Count; i++)
 68             {
 69                 int tmp = taglist[i];
 70                 if (tmpmuscitagdata.Select(string.Format("TagCategoryID={0}", tmp.ToString())).Length == 0)//如果在歌曲本身标签中没有找到,则表示新标签
 71                 {
 72                     newids.Add(tmp);
 73                 }
 74             }
 75             return newids;
 76         }
 77 
 78         private List<int> GetTagList(bool ischecked)
 79         {
 80             List<int> tmplist = new List<int>();
 81             for (int i = 0; i < tabControl1.TabPages.Count; i++)
 82             {
 83                 var tmptab = tabControl1.TabPages[i];
 84                 int tabid = Convert.ToInt32(tmptab.Name.Substring(tmptab.Name.IndexOf("_") + 1));
 85 
 86                 var tmptabckboxlist = tabControl1.TabPages[i].Controls["checkedList_" + tabid.ToString()] as CheckedListBox;
 87 
 88                 for (int j = 0; j < tmptabckboxlist.Items.Count; j++)
 89                 {
 90                     if (tmptabckboxlist.GetItemChecked(j) == ischecked)
 91                     {
 92                         ListItem o = tmptabckboxlist.Items[j] as ListItem;
 93                         tmplist.Add(int.Parse(o.Value));
 94                     }
 95                 }
 96             }
 97             return tmplist;
 98         }
 99 
100         private void addtag(List<int> tagdatalist)
101         {
102             if (tagdatalist.Count > 0)
103             {
104                 StringBuilder sb = new StringBuilder();
105 
106                 TagDataListEntity data = new TagDataListEntity();
107                 data.SourceID = musicitem.ID;
108                 data.AddTime = DateTime.Now;
109                 data.AddIP = musicitem.AddIP;
110 
111                 for (int j = 0; j < tagdatalist.Count; j++)
112                 {
113                     data.TagCategoryID = tagdatalist[j];
114                     if (Proxies.TagContentServices.Add(ref data))
115                     {
116                         data.ID = 0;
117                     }
118                     else
119                     {
120                         sb.Append(data.OperateMessage);
121                     }
122                 }
123 
124                 string message = sb.ToString();
125                 if (message.Length > 0)
126                 {
127                     MessageUtil.ShowTips(message);
128                     return;
129                 }
130             }
131 
132             Finish();
133

posted on 2012-08-10 12:13  star-star  阅读(198)  评论(0编辑  收藏  举报

导航