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
{
///
/// 获取机器人信息
///
///
[HttpPost, ErrorFilter]
public WebResult GetRobotInfo()
{
var PageIndex = GetInt("PageIndex");
var PageSize = GetInt("PageSize");
var tNumber = 0;
var Keyword = GetString("Keyword");
var exp = Expressionable.Create();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(f => f.Nickname.Contains(Keyword));
}
var DataList = Db.Queryable().Where(exp.ToExpression())
.Select(m => new RobotInfo { RobotId = m.Id, RobotName = m.Nickname })
.ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
///
/// 回复关键字查询
///
///
[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();
if (!string.IsNullOrEmpty(Keyword)) exp.And(f => f.KeyWord.Contains(Keyword));
if (RobotId >= 0) exp.And(f => f.RobotId == RobotId);
var DataList = Db.Queryable()
.LeftJoin((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(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
///
/// 新增关键词
///
///
[HttpPost, ErrorFilter]
public WebResult AddKeyword()
{
var Keyword = GetString("Keyword", true);
var KeywordType = GetEnum("KeywordType", true);//2:模糊匹配 1:精准匹配
var RobotId = GetInt("RobotId");//支持的机器人Id:0为通用消息,其他则为某个机器人
var Content = GetString("Content");
var keywords = Db.Queryable().Where(f => f.KeyWord == Keyword && f.RobotId == RobotId).First();
if (keywords != null)
{
return PutData("添加失败,关键词已存在");
}
if (RobotId != 0)
{
var robot = Db.Queryable().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;
}
///
/// 编辑关键字
///
///
[HttpPost, ErrorFilter]
public WebResult UpdKeyword()
{
var Id = GetInt("Id", true);
var Keyword = GetString("Keyword");
var KeywordType = GetEnum("KeywordType");//2:模糊匹配 1:精准匹配
var RobotId = GetInt("RobotId");//支持的机器人Id:0为通用消息,-1为所有机器人,其他则为某个机器人
var Content = GetString("Content");
var Keywords = Db.Queryable().Single(f => f.Id == Id);
if (Keywords == null)
{
return PutData("编辑失败,没有此关键词");
}
var keyword = Db.Queryable().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;
}
///
/// 删除关键词
///
///
[HttpPost, ErrorFilter]
public WebResult DelKeyowrd()
{
var Id = GetInt("Id", true);
var res = Db.Deleteable().Where(f => f.Id == Id).ExecuteCommand();
if (res > 0)
{
Db.OnLog("服务端", "删除关键词");
return PutSuccess;
}
else return PutData("删除失败,没有此关键词");
}
}
}