/// <summary> /// 删除敏感词 /// </summary> /// <param name="keyGroup"></param> /// <param name="keywords"></param> /// <returns></returns> public RPCResult DelKeywords(string keyGroup, List <string> keywords) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult(false, "关键词组名不能为空!")); } if (keywords.Count == 0) { return(new RPCResult(false, "关键词不能为空")); } if (WordfilteDB.Del(keyGroup, keywords)) { string message = "DEL" + (char)5; foreach (var key in keywords) { message += key + (char)5; } KeywordsSend.KeyGroup = keyGroup; KeywordsSend.Instance.Send(keyGroup, message.Substring(0, message.Length - 1)); return(new RPCResult(true, "")); } LogHelper.LogWarn("SMSService", "SMSService.DelKeywords", "关键字删除数据库失败"); return(new RPCResult(false, "删除关键字失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.DelKeywords", ex.ToString()); return(new RPCResult(false, "删除关键字出现错误")); } }
/// <summary> /// 添加敏感词类别 /// </summary> /// <param name="type"></param> /// <param name="remark"></param> /// <returns></returns> public RPCResult AddKeywordsType(string type, string remark) { try { if (type == "-1") { return(new RPCResult(false, "已存在此敏感词类型")); } if (string.IsNullOrEmpty(type)) { return(new RPCResult(false, "敏感词类型不能为空")); } if (WordfilteDB.ExistType(type)) { return(new RPCResult(false, "已存在此敏感词类型")); } bool ok = WordfilteDB.AddKeyType(type, remark); if (ok) { return(new RPCResult(true, "")); } return(new RPCResult(false, "添加敏感词类型失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.AddKeywordsType", ex.ToString()); return(new RPCResult(false, "添加敏感词类型失败")); } }
/// <summary> /// 添加词组与类别绑定 /// </summary> /// <param name="keyGroup"></param> /// <param name="keyTypes"></param> /// <returns></returns> public RPCResult AddKeywordsGroupTypeBind(string keyGroup, List <string> keyTypes) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult(false, "敏感词组不能为空")); } if (keyTypes == null || keyTypes.Count == 0) { return(new RPCResult(false, "敏感词类型不能为空")); } if (WordfilteDB.DelKeyTypesByGroup(keyGroup)) { bool ok = WordfilteDB.AddGroupTypeBind(keyGroup, keyTypes); if (ok) { return(new RPCResult(true, "")); } } return(new RPCResult(false, "添加敏感词组与类别绑定失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.AddKeywordsGroupTypeBind", ex.ToString()); return(new RPCResult(false, "添加敏感词组与类别绑定失败")); } }
/// <summary> /// 添加敏感词组 /// </summary> /// <param name="keyGroup"></param> /// <param name="remark"></param> /// <returns></returns> public RPCResult AddKeywordsGroup(string keyGroup, string remark) { try { if (keyGroup == "-1") { return(new RPCResult(false, "已存在此敏感词组")); } if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult(false, "敏感词组不能为空")); } if (WordfilteDB.ExistGroup(keyGroup)) { return(new RPCResult(false, "已存在此敏感词组")); } bool ok = WordfilteDB.AddKeyGroup(keyGroup, remark); if (ok) { return(new RPCResult(true, "")); } return(new RPCResult(false, "添加敏感词组失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.AddKeywordsGroup", ex.ToString()); return(new RPCResult(false, "添加敏感词组失败")); } }
/// <summary> /// 删除敏感词组 /// </summary> /// <param name="keyGroup"></param> /// <returns></returns> public RPCResult DelKeywordGroup(string keyGroup) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult(false, "敏感词组名不能为空!")); } if (KeywordsGatewayBindDB.GetGateways(keyGroup).Count > 0) { return(new RPCResult(false, "敏感词组正在使用,不允许删除")); } if (WordfilteDB.GetCountKeywords(keyGroup) > 0) { return(new RPCResult(false, "敏感词组内还包含敏感词,不允许删除")); } if (WordfilteDB.DelKeyTypesByGroup(keyGroup)) { if (WordfilteDB.DelKeyGroup(keyGroup)) { return(new RPCResult(true, "")); } } return(new RPCResult(false, "删除词组失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.DelKeywordGroup", ex.ToString()); return(new RPCResult(false, "删除词组失败出现错误")); } }
/// <summary> /// 获取敏感词类别 /// </summary> /// <returns></returns> public RPCResult <Dictionary <string, string> > GetKeywordsTypes() { try { Dictionary <string, string> list = WordfilteDB.GetKeyTypes(); return(new RPCResult <Dictionary <string, string> >(true, list, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetKeywordsTypes", ex.ToString()); return(new RPCResult <Dictionary <string, string> >(false, null, "获取敏感词类别失败")); } }
/// <summary> /// 获取敏感词组 /// </summary> /// <returns></returns> public RPCResult <Dictionary <string, string> > GetKeyGroups() { try { Dictionary <string, string> dic = WordfilteDB.GetKeyGroups(); return(new RPCResult <Dictionary <string, string> >(true, dic, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetKeyGroups", ex.ToString()); return(new RPCResult <Dictionary <string, string> >(false, null, "获取失败")); } }
/// <summary> /// 根据类型获取敏感词 /// </summary> /// <param name="type"></param> /// <returns></returns> public RPCResult <List <Keywords> > GetKeywordsByType(string type) { try { List <Keywords> list = new List <Keywords>(); list = WordfilteDB.GetKeywordsByType(type); return(new RPCResult <List <Keywords> >(true, list, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetKeywordsByType", ex.ToString()); return(new RPCResult <List <Keywords> >(false, null, "获取失败")); } }
/// <summary> /// 根据词获取敏感词(模糊查询) /// </summary> /// <param name="keyword"></param> /// <returns></returns> public RPCResult <List <Keywords> > GetKeywordsByKeyword(string keyword) { try { if (string.IsNullOrEmpty(keyword)) { return(new RPCResult <List <Keywords> >(false, null, "查询的敏感词是空的")); } List <Keywords> list = WordfilteDB.GetKeywordsByKeyword(keyword); return(new RPCResult <List <Keywords> >(true, list, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetKeywordsByKeyword", ex.ToString()); return(new RPCResult <List <Keywords> >(false, null, "获取敏感词失败")); } }
/// <summary> /// 根据词组获取敏感词类别 /// </summary> /// <param name="keyGroup"></param> /// <returns></returns> public RPCResult <List <string> > GetKeywordsTypesByGroup(string keyGroup) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult <List <string> >(false, null, "敏感词组不能为空")); } List <string> list = WordfilteDB.GetKeyTypesByGroup(keyGroup); return(new RPCResult <List <string> >(true, list, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetKeywordsTypesByGroup", ex.ToString()); return(new RPCResult <List <string> >(false, null, "获取敏感词组类别失败")); } }
/// <summary> /// 根据敏感词类型获取敏感词组 /// </summary> /// <param name="keyType"></param> /// <returns></returns> public RPCResult <List <string> > GetKeywordsGroupByType(string keyType) { try { if (string.IsNullOrEmpty(keyType)) { return(new RPCResult <List <string> >(false, null, "敏感词类型是空的")); } List <string> list = WordfilteDB.GetKeywordsGroupByType(keyType); return(new RPCResult <List <string> >(true, list, "")); } catch (Exception ex) { LogHelper.LogError("Pretreatment", "PretreatmentService.GetKeywordsGroupByType", ex.ToString()); return(new RPCResult <List <string> >(false, null, "获取敏感词类型失败")); } }
/// <summary> /// 更新敏感词(更新类型和替换成其他词语) /// </summary> /// <param name="keyword"></param> /// <returns></returns> public RPCResult UpdateKeywords(Keywords keyword) { try { bool ok = WordfilteDB.Update(keyword); if (ok) { return(new RPCResult(true, "")); } return(new RPCResult(false, "更新失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.UpdateKeywords", ex.ToString()); return(new RPCResult(false, "更新失败")); } }
/// <summary> /// 根据词组获取敏感词 /// </summary> /// <param name="keyGroup"></param> /// <returns></returns> public RPCResult <List <Keywords> > GetKeywords(string keyGroup) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult <List <Keywords> >(false, null, "关键词组名不能为空!")); } List <Keywords> list = new List <Keywords>(); list = WordfilteDB.Gets(keyGroup); return(new RPCResult <List <Keywords> >(true, list, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetKeywords", ex.ToString()); return(new RPCResult <List <Keywords> >(false, null, "获取失败")); } }
/// <summary> /// 敏感词状态启用与否 /// </summary> /// <param name="keyGroup"></param> /// <param name="keywords"></param> /// <param name="enabled"></param> /// <returns></returns> public RPCResult KeywordsEnabled(string keyGroup, string keywords, bool enabled) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult(false, "关键词组名不能为空!")); } if (string.IsNullOrEmpty(keywords)) { return(new RPCResult(false, "关键词不能为空")); } bool ok = WordfilteDB.KeywordsEnabled(keyGroup, keywords, enabled); if (ok) { if (enabled) { //敏感词启用 string message = "ADD" + (char)5 + keywords + (char)5; KeywordsSend.KeyGroup = keyGroup; KeywordsSend.Instance.Send(keyGroup, message.Substring(0, message.Length - 1)); } else { //敏感词禁用 string message = "DEL" + (char)5 + keywords + (char)5; KeywordsSend.KeyGroup = keyGroup; KeywordsSend.Instance.Send(keyGroup, message.Substring(0, message.Length - 1)); } return(new RPCResult(true, "")); } return(new RPCResult(false, "操作失败")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.KeywordsEnabled", ex.ToString()); return(new RPCResult(false, "操作失败")); } }
/// <summary> /// 获取所有敏感词 /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <returns></returns> public RPCResult <Dictionary <int, List <Keywords> > > GetAllKeywords(int pageIndex, int pageSize) { try { if (pageIndex < 0) { pageIndex = 0; } if (pageSize <= 0) { new RPCResult <List <Keywords> >(false, null, "每页的显示的条数应大于0"); } List <Keywords> list = WordfilteDB.GetAllKeywords(pageIndex, pageSize); int count = WordfilteDB.GetAllKeywordCount(); Dictionary <int, List <Keywords> > dic = new Dictionary <int, List <Keywords> >(); dic.Add(count, list); return(new RPCResult <Dictionary <int, List <Keywords> > >(true, dic, "")); } catch (Exception ex) { LogHelper.LogError("SMSService", "SMSService.GetAllKeywords", ex.ToString()); return(new RPCResult <Dictionary <int, List <Keywords> > >(false, null, "获取敏感词失败")); } }
/// <summary> /// 敏感词添加 /// </summary> /// <param name="keyGroup"></param> /// <param name="keywords"></param> /// <returns></returns> public RPCResult AddKeywords(string keyGroup, List <Keywords> keywords) { try { if (string.IsNullOrEmpty(keyGroup)) { return(new RPCResult(false, "关键词组名不能为空!")); } if (keywords.Count == 0) { return(new RPCResult(false, "关键词不能为空")); } if (WordfilteDB.Add(keyGroup, keywords)) { //WordfilteDB.Del(keyGroup, new List<string> { ""}); string message = "ADD" + (char)5; foreach (var key in keywords) { if (key.Enable) { message += key.Words + (char)5; } } KeywordsSend.KeyGroup = keyGroup; KeywordsSend.Instance.Send(keyGroup, message.Substring(0, message.Length - 1)); return(new RPCResult(true, "")); } LogHelper.LogWarn("Pretreatment", "PretreatmentService.AddKeywords", "关键字添加数据库失败"); return(new RPCResult(false, "添加关键字失败")); } catch (Exception ex) { LogHelper.LogError("Pretreatment", "PretreatmentService.AddKeywords", ex.ToString()); return(new RPCResult(false, "添加关键字出现错误")); } }
public void Test() { var keyGroups = WordfilteDB.GetKeyGroups(); Assert.IsNotNull(keyGroups); Assert.IsTrue(keyGroups.Count > 0); var keyTypes = WordfilteDB.GetKeyTypes(); Assert.IsNotNull(keyTypes); Assert.IsTrue(keyTypes.Count > 0); var keyGroup = keyGroups.First().Key; var b = WordfilteDB.ExistGroup(keyGroup); Assert.IsTrue(b); var keyType = keyTypes.First().Key; b = WordfilteDB.ExistType(keyType); Assert.IsTrue(b); var addKeyGroup = "AddKeyGroup" + UnitTestDBAccess.Util.GenRandStr(10); b = WordfilteDB.AddKeyGroup(addKeyGroup, "添加keyGroup 测试"); Assert.IsTrue(b); b = WordfilteDB.DelKeyGroup(addKeyGroup); Assert.IsTrue(b); var addKeyType = "AddKeyType" + UnitTestDBAccess.Util.GenRandStr(10); b = WordfilteDB.AddKeyType(addKeyType, "添加keyType 测试"); Assert.IsTrue(b); b = WordfilteDB.AddGroupTypeBind(addKeyGroup, new List <string>() { addKeyType, UnitTestDBAccess.Util.GenRandStr(15) }); Assert.IsTrue(b); var kgt = WordfilteDB.GetKeyTypesByGroup(addKeyGroup); Assert.IsNotNull(kgt); Assert.IsTrue(kgt.Count > 0); var ktg = WordfilteDB.GetKeywordsGroupByType(addKeyType); Assert.IsNotNull(ktg); Assert.IsTrue(ktg.Count > 0); b = WordfilteDB.DelKeyTypesByGroup(addKeyGroup); Assert.IsTrue(b); Keywords keywords1 = new Keywords() { KeyGroup = addKeyGroup, KeywordsType = addKeyType, Enable = true, ReplaceKeywords = "", Words = "words" + UnitTestDBAccess.Util.GenRandStr(15) }; Keywords keywords2 = new Keywords() { KeyGroup = addKeyGroup, KeywordsType = addKeyType, Enable = true, ReplaceKeywords = "", Words = "words" + UnitTestDBAccess.Util.GenRandStr(15) }; Keywords keywords3 = new Keywords() { KeyGroup = addKeyGroup, KeywordsType = addKeyType, Enable = true, ReplaceKeywords = "", Words = "words" + UnitTestDBAccess.Util.GenRandStr(15) }; List <Keywords> list = new List <Keywords>(); list.Add(keywords1); list.Add(keywords2); list.Add(keywords3); b = WordfilteDB.Add(addKeyGroup, list); Assert.IsTrue(b); var list2 = WordfilteDB.Gets(addKeyGroup); Assert.IsNotNull(list2); Assert.IsTrue(list2.Count > 2); var list3 = WordfilteDB.GetKeywordsByType(addKeyType); Assert.IsNotNull(list3); Assert.IsTrue(list3.Count > 2); b = WordfilteDB.KeywordsEnabled(addKeyGroup, keywords1.Words, false); Assert.IsTrue(b); keywords1.ReplaceKeywords = "replace"; b = WordfilteDB.Update(keywords1); Assert.IsTrue(b); var i = WordfilteDB.GetCountKeywords(addKeyGroup); Assert.IsTrue(i > 0); var arr = WordfilteDB.Get(addKeyGroup); Assert.IsNotNull(arr); Assert.IsTrue(arr.Length > 0); var klist = WordfilteDB.GetKeywordsByKeyword("words"); Assert.IsNotNull(klist); Assert.IsTrue(klist.Count > 0); i = WordfilteDB.GetAllKeywordCount(); Assert.IsTrue(i > 0); var list4 = WordfilteDB.GetAllKeywords(1, 2); Assert.IsTrue(list4.Count == 2); var gateway = GatewayConfigDB.GetConfigs().FirstOrDefault(); Assert.IsNotNull(gateway); b = KeywordsGatewayBindDB.Add(addKeyGroup, gateway.Gateway); Assert.IsTrue(b); var gts = KeywordsGatewayBindDB.GetGateways(addKeyGroup); Assert.IsTrue(gts.Count > 0); var kg = KeywordsGatewayBindDB.GetkeyGroup(gateway.Gateway); Assert.IsNotNull(kg); gts = KeywordsGatewayBindDB.GetGateways(); Assert.IsTrue(gts.Count > 0); b = KeywordsGatewayBindDB.Del(gateway.Gateway); Assert.IsTrue(b); List <string> keywords = (from k in list select k.Words).ToList(); b = WordfilteDB.Del(addKeyGroup, keywords); Assert.IsTrue(b); }