Files
LL-28/SLC1-N/ProductionData.cs
moxiliang 7065cad6d7 251024
2025-10-24 14:08:41 +08:00

236 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
namespace SLC1_N
{
public class ProductionRecord // 记录产能
{
private readonly JsonConfig config;
private readonly string configKey;
public ProductionRecord(string configFilePath = "productiondata.json")
{
config = new JsonConfig(configFilePath);
configKey = "ProductionData";
}
// 产能数据
private class ProductionData
{
public int Total { get; set; }
public int OK { get; set; }
public int NG { get; set; }
public DateTime LastUpdate { get; set; }
public double OKRate => Total > 0 ? Math.Round(OK * 100.0 / Total, 2) : 0;
public double NGRate => Total > 0 ? Math.Round(NG * 100.0 / Total, 2) : 0;
}
#region
/// <summary>
/// OK加1
/// </summary>
public void AddOK()
{
var data = GetProductionData();
data.Total++;
data.OK++;
data.LastUpdate = DateTime.Now;
SaveProductionData(data);
}
/// <summary>
/// NG加1
/// </summary>
public void AddNG()
{
var data = GetProductionData();
data.Total++;
data.NG++;
data.LastUpdate = DateTime.Now;
SaveProductionData(data);
}
/// <summary>
/// 获取总产能
/// </summary>
public int GetTotal()
{
return GetProductionData().Total;
}
/// <summary>
/// 获取OK产能
/// </summary>
public int GetOK()
{
return GetProductionData().OK;
}
/// <summary>
/// 获取NG产能
/// </summary>
public int GetNG()
{
return GetProductionData().NG;
}
/// <summary>
/// 获取合格率
/// </summary>
public double GetOKRate()
{
return GetProductionData().OKRate;
}
/// <summary>
/// 获取不合格率
/// </summary>
public double GetNGRate()
{
return GetProductionData().NGRate;
}
/// <summary>
/// 获取最后更新时间
/// </summary>
public DateTime GetLastUpdate()
{
return GetProductionData().LastUpdate;
}
/// <summary>
/// 清除所有产能数据
/// </summary>
public void Clear()
{
var data = new ProductionData
{
Total = 0,
OK = 0,
NG = 0,
LastUpdate = DateTime.Now
};
SaveProductionData(data);
}
/// <summary>
/// 获取所有产能信息
/// </summary>
public Dictionary<string, object> GetAllInfo()
{
var data = GetProductionData();
return new Dictionary<string, object>
{
{ "Total", data.Total },
{ "OK", data.OK },
{ "NG", data.NG },
{ "OKRate", data.OKRate },
{ "NGRate", data.NGRate },
{ "LastUpdate", data.LastUpdate }
};
}
#endregion
#region
private ProductionData GetProductionData()
{
// 从配置中获取数据,如果不存在则创建新的
if (config.ContainsKey(configKey))
{
var data = config.GetValue<ProductionData>(configKey);
if (data != null)
return data;
}
return new ProductionData();
}
private void SaveProductionData(ProductionData data)
{
config.SetValue(configKey, data);
}
#endregion
}
//public class MultiChannelProductionRecorder
//{
// private readonly Dictionary<int, ProductionRecorder> _channelRecorders;
// public MultiChannelProductionRecorder(string baseConfigPath = "production")
// {
// _channelRecorders = new Dictionary<int, ProductionRecorder>();
// // 初始化4个通道
// for (int i = 1; i <= 4; i++)
// {
// string configPath = $"{baseConfigPath}_channel{i}.json";
// _channelRecorders[i] = new ProductionRecorder(configPath);
// }
// }
// public void AddOK(int channel)
// {
// if (_channelRecorders.ContainsKey(channel))
// _channelRecorders[channel].AddOK();
// }
// public void AddNG(int channel)
// {
// if (_channelRecorders.ContainsKey(channel))
// _channelRecorders[channel].AddNG();
// }
// public Dictionary<string, object> GetChannelInfo(int channel)
// {
// return _channelRecorders.ContainsKey(channel)
// ? _channelRecorders[channel].GetAllInfo()
// : new Dictionary<string, object>();
// }
// public void ClearChannel(int channel)
// {
// if (_channelRecorders.ContainsKey(channel))
// _channelRecorders[channel].Clear();
// }
// public void ClearAll()
// {
// foreach (var recorder in _channelRecorders.Values)
// {
// recorder.Clear();
// }
// }
// // 获取总计信息
// public Dictionary<string, object> GetSummaryInfo()
// {
// int total = 0, ok = 0, ng = 0;
// foreach (var recorder in _channelRecorders.Values)
// {
// var info = recorder.GetAllInfo();
// total += (int)info["Total"];
// ok += (int)info["OK"];
// ng += (int)info["NG"];
// }
// double okRate = total > 0 ? Math.Round(ok * 100.0 / total, 2) : 0;
// double ngRate = total > 0 ? Math.Round(ng * 100.0 / total, 2) : 0;
// return new Dictionary<string, object>
// {
// { "Total", total },
// { "OK", ok },
// { "NG", ng },
// { "OKRate", okRate },
// { "NGRate", ngRate }
// };
// }
//}
}