ai_office_plugin/AIProofread/core/CommonSenseDetection.cs

101 lines
3.2 KiB
C#

using AIProofread.Controls;
using AIProofread.Model;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace AIProofread.core
{
/// <summary>
/// 常识性检测数据
/// </summary>
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class CommonSenseDetection
{
/// <summary>
/// 检测历史
/// </summary>
//public static readonly Dictionary<int, List<CommonsenseDetectionItem>> histories = new Dictionary<int, List<CommonsenseDetectionItem>>();
// 暂时改成全局
private List<CommonsenseDetectionItem> history = new List<CommonsenseDetectionItem>();
// 生成单例
private static readonly CommonSenseDetection instance = new CommonSenseDetection();
private FormCommonsenseDetection formCommonsenseDetection;
private CommonSenseDetection() { }
public static CommonSenseDetection Instance(FormCommonsenseDetection handler)
{
instance.formCommonsenseDetection = handler;
return instance;
}
public void WebInitializationCompleted()
{
if (formCommonsenseDetection != null && !formCommonsenseDetection.IsDisposed)
{
formCommonsenseDetection.InitializationCompleted();
}
}
// 检测状态
public bool isChecking = false;
public string checkingSummary;
public string checkingKey;
public string GetCheckStatus()
{
return JSONObject.Create()
.AddField("isChecking", isChecking)
.AddField("checkingKey", checkingKey)
.AddField("checkingSummary", checkingSummary)
.ToString();
}
public void SetCheckingData(bool isChecking, string checkingKey, string checkingSummary)
{
if (isChecking)
{
this.isChecking = isChecking;
this.checkingKey = checkingKey;
this.checkingSummary = checkingSummary;
Globals.ThisAddIn.ribbon.SetDetectionBtnStatus(false);
}
else
{
this.isChecking = false;
this.checkingKey = null;
this.checkingSummary = null;
Globals.ThisAddIn.ribbon.SetDetectionBtnStatus(true);
}
}
public string GetHistory()
{
//int id = Globals.ThisAddIn.ActiveDocument.Id;
return JsonConvert.SerializeObject(history);// histories.ContainsKey(id) ? JsonConvert.SerializeObject(histories[id]) : "[]";
}
public void SetHistory(string json)
{
var item = JsonConvert.DeserializeObject<CommonsenseDetectionItem>(json);
history.Add(item);
//int id = Globals.ThisAddIn.ActiveDocument.Id;
//if (histories.ContainsKey(id))
//{
// histories.Add(id, new List<CommonsenseDetectionItem>() { item });
//}
//else
//{
// histories[id].Add(item);
//}
}
public void Close()
{
Globals.ThisAddIn.HideDetection();
}
}
}