kingno
2025-06-09 305e2990bb2161177e87ab7711f9dfb205762bcd
src/Bro.M141.Process/M141Process_Mysql.cs
@@ -12,6 +12,7 @@
using System.Text;
using System.Threading.Tasks;
using Windows.Globalization.PhoneNumberFormatting;
using Windows.Media.Ocr;
using static Org.BouncyCastle.Math.EC.ECCurve;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
@@ -22,7 +23,6 @@
        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";
        #region 基础操作
@@ -145,33 +145,19 @@
        {
            try
            {
                if (pro.Details!=null)
                if (pro.Details != null)
                {
                    foreach (var item in pro.Details)
                    {
                        if (item.ResultList!=null)
                        if (item != 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和Name去重
                             .Select(g => g.First()) // 选择每个组的第一个元素
                             .ToList();
                                            x.DetectDetails = distinctItems;
                                        }
                                    });
                                }
                            });
                            var defects = item.ResultList.GetDefectDescList();
                            var ngSpecCodes = item.SpecList.Where(u => u.MeasureResult != true).Select(u => u.Code);
                            defects.AddRange(ngSpecCodes);
                            item.DefectList.AddRange(defects.Distinct());
                        }
                    }
                }
                JsonSerializerSettings settings = new JsonSerializerSettings
                {
@@ -183,7 +169,7 @@
                //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"{jsondata}");
                string mysqlstr = $"update forlocal set data='{jsondata}' where plcnum='{pro.SEQUENCE}'";
                string mysqlstr = $"update forlocal set data='{jsondata}',pid='{pro.PID}' where plcnum='{pro.SEQUENCE}'";
                if (Operatoremysql(mysqlstr, connStrLocal))
                {
@@ -206,29 +192,62 @@
            {
                try
                {
                    //保留数据库最新的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))
                    if (pro.Details != null)
                    {
                        string str2 = $"delete from forall where id < ({index}-1000)";
                        Operatoremysql(str2, connStrAll);
                        foreach (var item in pro.Details)
                        {
                            if (item != null)
                            {
                                var defects = item.ResultList.GetDefectDescList();
                                var ngSpecCodes = item.SpecList.Where(u => u.MeasureResult != true).Select(u => u.Code);
                                defects.AddRange(ngSpecCodes);
                                item.DefectList.AddRange(defects.Distinct());
                                item.DefectList = item.DefectList.Distinct().ToList();
                            }
                        }
                    }
                    string strtem = "select * from forall  where plcnum='" + pro.SEQUENCE + "' and device='" + devicestr + "' ";
                    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))
                    var temdb = selectdt(strtem, connStrAll);
                    if (temdb != null && temdb.Rows.Count > 0)
                    {
                        CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"数据库forall插入数据成功");
                        string mysqlstr = $"update forall set data='" + JsonConvert.SerializeObject(pro, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }) + "' where plcnum='" + pro.SEQUENCE + "' and device='" + devicestr + "' ";
                        if (Operatoremysql(mysqlstr, connStrAll))
                        {
                            CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Action, $"数据库forall更新数据成功");
                        }
                        else
                        {
                            CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"数据库forall更新数据异常  {mysqlstr}");
                        }
                    }
                    else
                    {
                        CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"数据库forall插入数据异失败  {mysqlstr}");
                        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, $"数据库forall插入数据成功");
                        }
                        else
                        {
                            CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Error, $"数据库forall插入数据异失败  {mysqlstr}");
                        }
                    }
                }
@@ -263,20 +282,26 @@
            return null;
        }
        public string Getbasketcode(string plcnum)
        public string Getbasketcode(string plcnum, out string sn)
        {
            sn = "Noread";
            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))
                string mysqlstr = $"select basketcode,sn from forall where plcnum='{plcnum}' && device='S2' order by id desc limit 1";
                var dt = selectdt(mysqlstr, connStrAll);
                if (dt == null || dt.Rows.Count == 0)
                {
                    return str;
                    return "Noread";
                }
                else
                {
                    sn = dt.Rows[0][1].ToString();
                    return dt.Rows[0][0].ToString();
                }
            }
            catch (Exception e)
            {
                //CommonLogger.LogAsync(DateTime.Now, EnumHelper.LogLevel.Exception, $"数据库forlocal获取异常 {e.ToString()}");
            }
            return "NoRead";
        }
@@ -286,7 +311,7 @@
        {
            try
            {
                string mysqlstr = $"select data from forall where plcnum='{plcnum}'";
                string mysqlstr = $"select data from forall where plcnum='{plcnum}' order by device";
                var dt = selectdt(mysqlstr, connStrAll);
                if (dt == null)
                {