From a97ac998301461e6284595b1cf2c7b40ce5b2459 Mon Sep 17 00:00:00 2001 From: kingno <30263@KINGNO> Date: 星期四, 03 四月 2025 14:09:36 +0800 Subject: [PATCH] 优化数据库字段的json传输 新增打印机 --- src/Bro.M141.Process/M141Process_Mysql.cs | 199 +++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 173 insertions(+), 26 deletions(-) diff --git a/src/Bro.M141.Process/M141Process_Mysql.cs b/src/Bro.M141.Process/M141Process_Mysql.cs index d6b7296..632a3be 100644 --- a/src/Bro.M141.Process/M141Process_Mysql.cs +++ b/src/Bro.M141.Process/M141Process_Mysql.cs @@ -1,4 +1,6 @@ 锘縰sing Bro.Common.Helper; +using Bro.Common.Interface; +using Bro.Common.Model; using Bro.M135.Common; using MySql.Data.MySqlClient; using Newtonsoft.Json; @@ -10,6 +12,7 @@ using System.Text; using System.Threading.Tasks; using Windows.Globalization.PhoneNumberFormatting; +using static Org.BouncyCastle.Math.EC.ECCurve; using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView; namespace Bro.M141.Process @@ -17,8 +20,8 @@ public class M141Process_Mysql { - string connStrAll = "server=127.0.0.1;port=3306;user id=root;password=123456;database=BroDB;charset=utf8"; - string connStrLocal = "server=127.0.0.1;port=3306;user id=root;password=123456;database=BroDB;charset=utf8"; + string connStrAll = "server=localhost;port=3306;user id=root;password=123456;database=BroDB;charset=utf8"; + string connStrLocal = "server=localhost;port=3306;user id=root;password=123456;database=BroDB;charset=utf8"; @@ -28,7 +31,7 @@ connStrAll = $"server={IP};port=3306;user id=root;password=123456;database=BroDB;charset=utf8"; } - public void Operatoremysql(string sql, string connStr)//鎿嶄綔 + public bool Operatoremysql(string sql, string connStr)//鎿嶄綔 { MySqlConnection con = new MySqlConnection(connStr); try @@ -40,12 +43,16 @@ con.Open(); MySqlCommand coon = new MySqlCommand(sql, con); coon.ExecuteNonQuery(); + con.Close(); } catch (Exception e) { - CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴撴搷浣滃紓甯� {e.ToString()}"); + con.Close(); + return false; + //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴撴搷浣滃紓甯� {sql} {e.ToString()}"); } - con.Close(); + return true; + } public DataTable selectdt(string str, string connStr)//鏌ヨ琛ㄦ牸 @@ -61,12 +68,14 @@ con.Open(); MySqlDataAdapter da = new MySqlDataAdapter(str, con); da.Fill(dt); + con.Close(); } catch (Exception e) { - CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴撴煡璇㈠紓甯� {e.ToString()}"); + //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴撴煡璇㈠紓甯� {str} {e.ToString()}"); + con.Close(); + return null; } - con.Close(); return dt; } @@ -88,9 +97,8 @@ } catch (Exception e) { - CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴撴煡璇㈡暟鎹紓甯� {e.ToString()}"); + //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴撴煡璇㈡暟鎹紓甯� {str} {e.ToString()}"); } - con.Close(); return strr; } @@ -103,9 +111,29 @@ { try { - string mysqlstr = $"insert into forlocal (pid,plcnum,sn,data,time) values('{pro.PID}','{pro.SEQUENCE}','{pro.SN}','{JsonConvert.SerializeObject(pro)}','{DateTime.Now.ToString("yyyyMMddHHmmss")}')"; + //淇濈暀鏁版嵁搴撴渶鏂扮殑1000鏉℃暟鎹� + //string destr = "delete from forlocal where id not in (select id from (select id from forlocal order BY id desc limit 1000) as subquery)"; + //Operatoremysql(destr, connStrAll); - Operatoremysql(mysqlstr, connStrLocal); + string str1 = "select id from forlocal order by id desc limit 1"; + string index = selectmysql(str1, connStrLocal); + + if (!string.IsNullOrEmpty(index)) + { + string str2 = $"delete from forlocal where id < ({index}-1000)"; + Operatoremysql(str2, connStrLocal); + } + + string mysqlstr = $"insert into forlocal (basketcode,pid,plcnum,sn,data,time) values('{pro.BasketCode}','{pro.PID}','{pro.SEQUENCE}','{pro.SN}','{JsonConvert.SerializeObject(pro, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All })}','{DateTime.Now.ToString("yyyyMMddHHmmss")}')"; + + if (Operatoremysql(mysqlstr, connStrLocal)) + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"鏁版嵁搴揻orlocal鎻掑叆鏁版嵁鎴愬姛"); + } + else + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"鏁版嵁搴揻orlocal鎻掑叆鏁版嵁寮傚け璐� {mysqlstr}"); + } } catch (Exception e) { @@ -117,8 +145,54 @@ { try { - string mysqlstr = $"update forlocal set data='{JsonConvert.SerializeObject(pro)}' where plcnum='{pro.SEQUENCE}'"; - Operatoremysql(mysqlstr, connStrLocal); + if (pro.Details!=null) + { + foreach (var item in pro.Details) + { + if (item.ResultList!=null) + { + item.ResultList.ForEach(u => + { + if (u.NetResults != null) + { + u.NetResults.ForEach(x => + { + if (x.DetectDetails != null) + { + var distinctItems = x.DetectDetails.GroupBy(i => new { i.ClassName, i.FinalResult }) // 鏍规嵁Id鍜孨ame鍘婚噸 + .Select(g => g.First()) // 閫夋嫨姣忎釜缁勭殑绗竴涓厓绱� + .ToList(); + x.DetectDetails = distinctItems; + } + }); + } + }); + } + } + } + + + + JsonSerializerSettings settings = new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }; + + string jsondata = JsonConvert.SerializeObject(pro, settings); + + + //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{jsondata}"); + + string mysqlstr = $"update forlocal set data='{jsondata}' where plcnum='{pro.SEQUENCE}'"; + + if (Operatoremysql(mysqlstr, connStrLocal)) + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"鏁版嵁搴揻orlocal鏇存柊鏁版嵁鎴愬姛"); + } + else + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"鏁版嵁搴揻orlocal鏇存柊鏁版嵁澶辫触 {mysqlstr}"); + } } catch (Exception e) { @@ -128,42 +202,115 @@ public void NewForAll(ProductModel pro, string devicestr) { - try + Task.Run(() => { - string mysqlstr = $"insert into forall (device,plcnum,sn,data,time) values('{devicestr}','{pro.SEQUENCE}','{pro.SN}','{JsonConvert.SerializeObject(pro)}','{DateTime.Now.ToString("yyyyMMddHHmmss")}')"; + try + { - Operatoremysql(mysqlstr, connStrAll); - } - catch (Exception e) - { - CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴揻orall鎻掑叆鏁版嵁寮傚父 {e.ToString()}"); - } + //淇濈暀鏁版嵁搴撴渶鏂扮殑1000鏉℃暟鎹� + //string destr = "delete from forall where id not in (select id from (select id from forall order BY id desc limit 1000) as subquery)"; + //Operatoremysql(destr, connStrAll); + + string str1 = "select id from forall order by id desc limit 1"; + string index = selectmysql(str1, connStrAll); + if (!string.IsNullOrEmpty(index)) + { + string str2 = $"delete from forall where id < ({index}-1000)"; + Operatoremysql(str2, connStrAll); + } + + + string mysqlstr = $"insert into forall (basketcode,device,plcnum,sn,data,time) values('{pro.BasketCode}','{devicestr}','{pro.SEQUENCE}','{pro.SN}','{JsonConvert.SerializeObject(pro, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All })}','{DateTime.Now.ToString("yyyyMMddHHmmss")}')"; + + if (Operatoremysql(mysqlstr, connStrAll)) + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"鏁版嵁搴揻orall鎻掑叆鏁版嵁鎴愬姛"); + } + else + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"鏁版嵁搴揻orall鎻掑叆鏁版嵁寮傚け璐� {mysqlstr}"); + } + + } + catch (Exception e) + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴揻orall鎻掑叆鏁版嵁寮傚父 {e.ToString()}"); + } + }); } - public ProductModel GetProduct(string plcnum) { try { - string mysqlstr = $"select data from forlocal where plcnum='{plcnum}'"; - string str= selectmysql(mysqlstr, connStrLocal); + string mysqlstr = $"select data from forlocal where plcnum='{plcnum}' order by id desc limit 1"; + string str = selectmysql(mysqlstr, connStrLocal); if (string.IsNullOrEmpty(str)) { return null; } else { - ProductModel p = JsonConvert.DeserializeObject<ProductModel>(str); + ProductModel p = null; + p = JsonConvert.DeserializeObject<ProductModel>(str, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); return p; } } catch (Exception e) { - CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴揻orlocal鏇存柊鏁版嵁寮傚父 {e.ToString()}"); + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴揻orlocal鑾峰彇瀵硅薄寮傚父 {e.ToString()}"); } return null; } + public string Getbasketcode(string plcnum) + { + try + { + string mysqlstr = $"select basketcode from forall where plcnum='{plcnum}' && device='S2' order by id desc limit 1"; + string str = selectmysql(mysqlstr, connStrAll); + if (!string.IsNullOrEmpty(str)) + { + return str; + } + } + catch (Exception e) + { + //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴揻orlocal鑾峰彇寮傚父 {e.ToString()}"); + } + return "NoRead"; + } + + + public List<ProductModel> GetProductList(string plcnum) + { + try + { + string mysqlstr = $"select data from forall where plcnum='{plcnum}'"; + var dt = selectdt(mysqlstr, connStrAll); + if (dt == null) + { + return null; + } + else + { + List<ProductModel> plist = new List<ProductModel>(); + for (int i = 0; i < dt.Rows.Count; i++) + { + ProductModel p = JsonConvert.DeserializeObject<ProductModel>(dt.Rows[i][0].ToString(), new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); + + //JsonConvert.DeserializeObject<IProcessConfig>(_configBackupStr, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }); + plist.Add(p); + } + return plist; + } + } + catch (Exception e) + { + CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"鏁版嵁搴揻orall鏁版嵁寮傚父 {e.ToString()}"); + } + return null; + } } -- Gitblit v1.8.0