using Common.DbExtends.Extends;
using Common.Models.Enums;
using Common.Models.SubTables;
using Common.Models.UnqTables;
using Common.Utils;
using Server.MyClass.Views;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
namespace Server.Controllers.MemberManagement
{
///
/// 进粉记录
///
public class FansController : DefaultController
{
///
/// 获取机器人信息
///
///
[HttpPost, ErrorFilter]
public WebResult GetRobotInfo()
{
var Keyword = GetString("Keyword");
var exp = Expressionable.Create();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(f => f.Nickname.Contains(Keyword));
}
var robotInfos = Db.Queryable().Where(exp.ToExpression())
.Select(m => new RobotInfo { RobotId = m.Id, RobotName = m.Nickname }).Take(20).ToList();
return PutData(robotInfos);
}
///
/// 查询进粉记录
///
///
[HttpPost, ErrorFilter]
public WebResult GetFansRecord()
{
var RobotId = GetInt("RobotId");
var MinTime = GetTime("MinTime");
var MaxTime = GetTime("MaxTime");
var PageIndex = GetInt("PageIndex", true);
var PageSize = GetInt("PageSize", true);
if (PageSize > 100) PageSize = 100;
var tNumber = 0;
var exp = Expressionable.Create();
if (RobotId > 0)
exp.And(f => f.RobotId == RobotId);
if (MinTime != DateTime.MinValue)
exp.And(f => f.CreateTime > MinTime);
if (MaxTime != DateTime.MinValue)
exp.And(f => f.CreateTime < MaxTime);
var list = Db.Queryable()
.SplitTable(MinTime, MaxTime)
.LeftJoin((f, b) => f.RobotId == b.Id)
.Where(exp.ToExpression())
.Select((f, b) => new FansRecordShow
{
Id = f.Id,
ApplyRemark = f.ApplyRemark,
CreateTime = f.CreateTime,
Nickname = f.Nickname,
Username = f.Username,
SourceNickname = f.SourceNickname,
SourceUsername = f.SourceUsername,
Type = f.Type,
RobotId = f.RobotId,
RobotName = b.Nickname,
Gender = f.Gender,
Operation = f.Operation,
}).ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult(list, tNumber, PageSize, PageIndex);
return PutData(res);
}
///
/// 设置进粉状态
///
///
[HttpPost, ErrorFilter]
public WebResult AddMoniData()
{
FansRecord f = new FansRecord()
{
ApplyRemark = "aa",
CreateTime = DateTime.Now,
Gender = SexType.未知,
Nickname = GetString("Nickname", true),
Operation = OperationStatus.待审核,
RobotId = 1,
SourceNickname = "",
SourceUsername = "",
Type = UserType.微信用户,
Username = Guid.NewGuid().ToString("N"),
};
f.Id = Util.CreateID(f.CreateTime);//自己写的一个算法ID,可以根据时间计算出表名
Db.Insertable(f).SplitTable().ExecuteCommand();
return PutData(f);
}
///
/// 设置进粉状态
///
///
[HttpPost, ErrorFilter]
public WebResult SetRecordStatus()
{
//需要读long,否则越界
var Id = GetLong("Id", true);
//获取表名
var TableName = Db.GetTableNameById(Id);
var record = Db.Queryable().Where(f => f.Id == Id).SplitTable(tab => tab.InTableNames(TableName)).First();
if (record == null) return PutData("操作失败,当前数据不存在!");
//var MinTime = GetTime("MinTime", true);
//var MaxTime = GetTime("MaxTime", true);
//var record = Db.Queryable().SplitTable(MinTime, MaxTime).Single(f => f.Id == Id);
//if (record == null)
//{
// return PutError;
//}
var op = GetEnum("State", true);
record.Operation = op;
Db.Updateable(record).SplitTable(tab => tab.InTableNames(TableName)).ExecuteCommand();
//if (op == record.Operation)
//{
// return PutError;
//}
////TODO 向机器人发送同步好友状态消息
//record.Operation = op;
////TODO 同步分表数据库
////Db.Updateable(record).SplitTable(MinTime, MaxTime).ExecuteCommand();
return PutSuccess;
}
}
}