yz_server/Server/Controllers/FunctionSetting/KeywordsController.cs

170 lines
5.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Common.DbExtends.Extends;
using Common.Models.Enums;
using Common.Models.UnqTables;
using Server.MyClass.Views;
using SqlSugar;
using System.Web.Http;
namespace Server.Controllers.FunctionSetting
{
public class KeywordsController : DefaultController
{
/// <summary>
/// 获取机器人信息
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult GetRobotInfo()
{
var PageIndex = GetInt("PageIndex");
var PageSize = GetInt("PageSize");
var tNumber = 0;
var Keyword = GetString("Keyword");
var exp = Expressionable.Create<Robot>();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(f => f.Nickname.Contains(Keyword));
}
var DataList = Db.Queryable<Robot>().Where(exp.ToExpression())
.Select(m => new RobotInfo { RobotId = m.Id, RobotName = m.Nickname })
.ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult<RobotInfo>(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
/// <summary>
/// 回复关键字查询
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult GetKeyword()
{
var RobotId = GetInt("RobotId", true);
var Keyword = GetString("Keyword");
var PageIndex = GetInt("PageIndex", true);
var PageSize = GetInt("PageSize", true);
if (PageSize > 100) PageSize = 100;
var tNumber = 0;
var exp = Expressionable.Create<Keyword>();
if (!string.IsNullOrEmpty(Keyword)) exp.And(f => f.KeyWord.Contains(Keyword));
if (RobotId >= 0) exp.And(f => f.RobotId == RobotId);
var DataList = Db.Queryable<Keyword>()
.LeftJoin<Robot>((f, r) => f.RobotId == r.Id)
.Where(exp.ToExpression())
.Select((f, r) => new KeywordShow
{
KeyWord = f.KeyWord,
RobotId = r.Id,
Content = f.Content,
Id = f.Id,
KeywordType = f.KeywordType,
RobotName = r.Nickname
})
.ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult<KeywordShow>(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
/// <summary>
/// 新增关键词
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult AddKeyword()
{
var Keyword = GetString("Keyword", true);
var KeywordType = GetEnum<KeywordType>("KeywordType", true);//2:模糊匹配 1:精准匹配
var RobotId = GetInt("RobotId");//支持的机器人Id:0为通用消息其他则为某个机器人
var Content = GetString("Content");
var keywords = Db.Queryable<Keyword>().Where(f => f.KeyWord == Keyword && f.RobotId == RobotId).First();
if (keywords != null)
{
return PutData("添加失败,关键词已存在");
}
if (RobotId != 0)
{
var robot = Db.Queryable<Robot>().Single(f => f.Id == RobotId);
if (robot == null) return PutData("没有此机器人ID");
}
else
{
keywords = new Keyword()
{
KeyWord = Keyword,
KeywordType = KeywordType,
RobotId = RobotId,
Content = Content
};
Client.SendClientMsg(RobotId, DeviceMessageType., new { RobotId = RobotId });
Db.Insertable(keywords).ExecuteCommand();
}
return PutSuccess;
}
/// <summary>
/// 编辑关键字
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult UpdKeyword()
{
var Id = GetInt("Id", true);
var Keyword = GetString("Keyword");
var KeywordType = GetEnum<KeywordType>("KeywordType");//2:模糊匹配 1:精准匹配
var RobotId = GetInt("RobotId");//支持的机器人Id:0为通用消息-1为所有机器人其他则为某个机器人
var Content = GetString("Content");
var Keywords = Db.Queryable<Keyword>().Single(f => f.Id == Id);
if (Keywords == null)
{
return PutData("编辑失败,没有此关键词");
}
var keyword = Db.Queryable<Keyword>().Where(f => f.KeyWord == Keyword && f.RobotId == RobotId).First();
if (keyword != null) return PutData("此关键词已存在,请重新输入");
Keywords.KeyWord = Keyword;
Keywords.KeywordType = KeywordType;
Keywords.RobotId = RobotId;
Keywords.Content = Content;
Db.Updateable(Keywords).ExecuteCommand();
return PutSuccess;
}
/// <summary>
/// 删除关键词
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult DelKeyowrd()
{
var Id = GetInt("Id", true);
var res = Db.Deleteable<Keyword>().Where(f => f.Id == Id).ExecuteCommand();
if (res > 0)
{
Db.OnLog("服务端", "删除关键词");
return PutSuccess;
}
else return PutData("删除失败,没有此关键词");
}
}
}