212 lines
7.0 KiB
C#
212 lines
7.0 KiB
C#
|
using Common.Models.JsonModels;
|
|||
|
using Common.Models.UnqTables;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Web.Http;
|
|||
|
using SqlSugar;
|
|||
|
using Common.DbExtends.Extends;
|
|||
|
using System.Reflection;
|
|||
|
using static Common.DbExtends.Extends.ReplyExtend;
|
|||
|
using Server.MyClass.Views;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using Common.Utils;
|
|||
|
using Newtonsoft.Json.Linq;
|
|||
|
using Common.Models.Enums;
|
|||
|
|
|||
|
namespace Server.Controllers.FunctionSetting
|
|||
|
{
|
|||
|
public class ReplyController : DefaultController
|
|||
|
{
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 查询变量
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult GetReply()
|
|||
|
{
|
|||
|
var Id = GetInt("ConfigId", true);
|
|||
|
var Config = Db.GetReplyConfigById(Id);
|
|||
|
var List = TranslationByObj(Config);
|
|||
|
return PutData(List);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 新增基础配置模板
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult AddConfig()
|
|||
|
{
|
|||
|
string Name = GetString("Name", true);
|
|||
|
var json = JObject.Parse(GetString("Cfg", true));
|
|||
|
var config = Db.Queryable<ConfigData>().Where(f => f.Name == Name && f.Type == Common.Models.Enums.ConfigType.文案).First();
|
|||
|
if (config != null) return PutData("新增失败,此模板名称已存在");
|
|||
|
ReplyConfig wenan_config = new ReplyConfig();
|
|||
|
Util.CopyToObj(json, wenan_config);
|
|||
|
var Cfg = JsonConvert.SerializeObject(wenan_config);
|
|||
|
|
|||
|
config = new ConfigData();
|
|||
|
config.Type = ConfigType.文案;
|
|||
|
config.JsonContent = Cfg;
|
|||
|
config.Name = Name;
|
|||
|
Db.Save(config);
|
|||
|
|
|||
|
return PutSuccess;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 编辑配置
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
[HttpPost, ErrorFilter]
|
|||
|
public WebResult UpdConfig()
|
|||
|
{
|
|||
|
var Id = GetInt("Id", true);
|
|||
|
var jsondata = GetString("Cfg", true);
|
|||
|
|
|||
|
var config = Db.GetConfigData(Id);
|
|||
|
if (config == null) return PutData("编辑失败,没有此模板");
|
|||
|
if (config.Type != Common.Models.Enums.ConfigType.文案)
|
|||
|
{
|
|||
|
return PutData("编辑失败,此模板类型不匹配");
|
|||
|
}
|
|||
|
|
|||
|
ReplyConfig wenan_config = TranslationByJson(jsondata);
|
|||
|
|
|||
|
var Cfg = JsonConvert.SerializeObject(wenan_config);
|
|||
|
config.JsonContent = Cfg;
|
|||
|
Db.Save(config);
|
|||
|
|
|||
|
|
|||
|
var Robots = Db.Queryable<Robot>().Where(f => f.ConfigReplyId == Id).Select(f => f.Id).ToList();
|
|||
|
if (Robots.Count != 0)
|
|||
|
{
|
|||
|
foreach (var RobotId in Robots)
|
|||
|
Client.SendClientMsg(RobotId, DeviceMessageType.返利配置修改, new { ConfigId = Id });
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
return PutSuccess;
|
|||
|
}
|
|||
|
|
|||
|
private ReplyConfig TranslationByJson(string json)
|
|||
|
{
|
|||
|
var jArray = JsonConvert.DeserializeObject<JArray>(json);
|
|||
|
var replyConfig = new ReplyConfig();
|
|||
|
|
|||
|
|
|||
|
foreach (var item in jArray)
|
|||
|
{
|
|||
|
var pname = item["Name"].Value<string>();
|
|||
|
var property = replyConfig.GetType().GetProperty(pname);
|
|||
|
if (property != null)
|
|||
|
{
|
|||
|
var pobj = Activator.CreateInstance(property.PropertyType);
|
|||
|
property.SetValue(replyConfig, pobj);
|
|||
|
TranslationByJson(item, pobj);
|
|||
|
}
|
|||
|
}
|
|||
|
return replyConfig;
|
|||
|
}
|
|||
|
|
|||
|
private void TranslationByJson(JToken token, object obj)
|
|||
|
{
|
|||
|
var menuTokens = token["Menus"].ToArray();
|
|||
|
if (!menuTokens.Any())
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
foreach (var item in menuTokens)
|
|||
|
{
|
|||
|
var pname = item["Name"].Value<string>();
|
|||
|
var context = item["Content"].Value<string>();
|
|||
|
var property = obj.GetType().GetProperty(pname);
|
|||
|
if (property == null)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
if (property.PropertyType == typeof(string))
|
|||
|
{
|
|||
|
property.SetValue(obj, context);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var pobj = Activator.CreateInstance(property.PropertyType);
|
|||
|
property.SetValue(obj, pobj);
|
|||
|
TranslationByJson(item, pobj);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
//private ReplyConfig TranslationByJson(ReplyShowClass obj, ReplyConfig config)
|
|||
|
//{
|
|||
|
// PropertyInfo[] propertys = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
|
|||
|
// foreach (PropertyInfo item in propertys)
|
|||
|
// {
|
|||
|
// var value = item.GetValue(obj) as ReplyShowClass;
|
|||
|
// if (value == null) continue;
|
|||
|
|
|||
|
// //有子菜单
|
|||
|
// if (value.Menus != null && value.Menus.Count > 0)
|
|||
|
// {
|
|||
|
// TranslationByJson(obj, config);
|
|||
|
// }
|
|||
|
// //没有子菜单,到了跟类
|
|||
|
// else
|
|||
|
// {
|
|||
|
|
|||
|
// }
|
|||
|
// }
|
|||
|
//}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private List<ReplyShowClass> TranslationByObj(object obj)
|
|||
|
{
|
|||
|
if (obj == null)
|
|||
|
{
|
|||
|
return new List<ReplyShowClass>();
|
|||
|
}
|
|||
|
var replys = new List<ReplyShowClass>();
|
|||
|
var stringType = typeof(string);
|
|||
|
var atrType = typeof(ReplyAttribute);
|
|||
|
PropertyInfo[] propertys = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
|
|||
|
foreach (var item in propertys)
|
|||
|
{
|
|||
|
if (item.IsDefined(atrType, true))
|
|||
|
{
|
|||
|
var curAttribute = item.GetCustomAttribute<ReplyAttribute>();
|
|||
|
if (curAttribute == null)//防止异常
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
var respose = new ReplyShowClass()
|
|||
|
{
|
|||
|
Name = item.Name,
|
|||
|
Description = curAttribute.Describe
|
|||
|
};
|
|||
|
var propertyValue = item.GetValue(obj);
|
|||
|
if (item.PropertyType == stringType)
|
|||
|
{
|
|||
|
respose.PrivateVariables = curAttribute.PrivateVariables;
|
|||
|
respose.Content = propertyValue?.ToString();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
respose.Menus = new List<ReplyShowClass>();
|
|||
|
respose.Menus.AddRange(TranslationByObj(propertyValue));
|
|||
|
}
|
|||
|
replys.Add(respose);
|
|||
|
}
|
|||
|
}
|
|||
|
return replys;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|