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 公共接口 /// /// OK加1 /// public void AddOK() { var data = GetProductionData(); data.Total++; data.OK++; data.LastUpdate = DateTime.Now; SaveProductionData(data); } /// /// NG加1 /// public void AddNG() { var data = GetProductionData(); data.Total++; data.NG++; data.LastUpdate = DateTime.Now; SaveProductionData(data); } /// /// 获取总产能 /// public int GetTotal() { return GetProductionData().Total; } /// /// 获取OK产能 /// public int GetOK() { return GetProductionData().OK; } /// /// 获取NG产能 /// public int GetNG() { return GetProductionData().NG; } /// /// 获取合格率 /// public double GetOKRate() { return GetProductionData().OKRate; } /// /// 获取不合格率 /// public double GetNGRate() { return GetProductionData().NGRate; } /// /// 获取最后更新时间 /// public DateTime GetLastUpdate() { return GetProductionData().LastUpdate; } /// /// 清除所有产能数据 /// public void Clear() { var data = new ProductionData { Total = 0, OK = 0, NG = 0, LastUpdate = DateTime.Now }; SaveProductionData(data); } /// /// 获取所有产能信息 /// public Dictionary GetAllInfo() { var data = GetProductionData(); return new Dictionary { { "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(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 _channelRecorders; // public MultiChannelProductionRecorder(string baseConfigPath = "production") // { // _channelRecorders = new Dictionary(); // // 初始化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 GetChannelInfo(int channel) // { // return _channelRecorders.ContainsKey(channel) // ? _channelRecorders[channel].GetAllInfo() // : new Dictionary(); // } // 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 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 // { // { "Total", total }, // { "OK", ok }, // { "NG", ng }, // { "OKRate", okRate }, // { "NGRate", ngRate } // }; // } //} }