1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369 |
- using System;
- using System.Collections.Generic;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Data;
- using System.IO;
- using HslCommunication.LogNet;
- using Org.BouncyCastle.Ocsp;
- using System.Windows.Forms;
- using SqlSugar;
- using MainForm.ClassFile.ProjectClass;
- using NPOI.SS.Formula.Functions;
- namespace MainForm
- {
- public class SQLHelper
- {
- public static string DBDir = GlobalContext.DBDir;
- //用于数据库日志记录
- private static ILogNet logNet = new LogNetDateTime(GlobalContext.SQLLogDir, GenerateMode.ByEveryDay);
- /// <summary>
- /// 使用锁防止多线程同时操作数据库表
- /// </summary>
- private static readonly object sqlLock = new object();
- public class ResponseMessage
- {
- public string text { get; set; }
- public bool result { get; set; }
- }
- /// <summary>
- /// SQL连接
- /// </summary>
- private static SqlConnection connection = null;
- public static string connString = "";
- public static void DBInitWork()
- {
- GlobalContext.User = "sa";
- GlobalContext.PassWord = "Bb123456";
- //以月为单位存
- string subDir = DateTime.Now.ToString("yyyyMM");
- string dataBaseName = "DB" + subDir;
- connString =
- @"server= " + GlobalContext.Server +
- ";database= " + dataBaseName +
- ";uid=" + GlobalContext.User +
- ";pwd=" + GlobalContext.PassWord;
- //判断路径是否存在,不存在则创建路径
- if (!Directory.Exists(DBDir + subDir)) Directory.CreateDirectory(DBDir + subDir);
- //创建数据库和表
- SQLHelper.CreateDataBase(DBDir, subDir, dataBaseName);
- //SQLHelper.CreateDataBase();
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="SQLString"></param>
- /// <param name="cmdParms"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static DataSet Query(string SQLString, List<SqlParameter> cmdParms, string connectionString)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- SqlCommand cmd = new SqlCommand();
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- using (SqlDataAdapter da = new SqlDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- try
- {
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- }
- catch (System.Data.SqlClient.SqlException ex)
- {
- throw new Exception(ex.Message);
- }
- return ds;
- }
- }
- }
- /// <summary>
- /// 执行SQL语句,返回影响的记录数
- /// </summary>
- /// <param name="SQLString">SQL语句</param>
- /// <returns>影响的记录数</returns>
- public static int ExecuteSQL(string SQLString, List<SqlParameter> cmdParms, string connectionString)
- {
- using (SqlConnection connection = new SqlConnection(connectionString))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- try
- {
- PrepareCommand(cmd, connection, null, SQLString, cmdParms);
- int rows = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return rows;
- }
- catch (System.Data.SqlClient.SqlException e)
- {
- throw e;
- }
- }
- }
- }
- private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, List<SqlParameter> cmdParms)
- {
- if (conn.State != ConnectionState.Open)
- conn.Open();
- cmd.Connection = conn;
- cmd.CommandText = cmdText;
- if (trans != null)
- cmd.Transaction = trans;
- cmd.CommandType = CommandType.Text;//cmdType;
- if (cmdParms != null)
- {
- foreach (SqlParameter parameter in cmdParms)
- {
- if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
- (parameter.Value == null))
- {
- parameter.Value = DBNull.Value;
- }
- cmd.Parameters.Add(parameter);
- }
- }
- }
- /// <summary>
- /// 创建SQL连接属性
- /// </summary>
- public static SqlConnection Connection
- {
- get
- {
- //DBInitWork();
- try
- {
- if (connection == null)//如果没有创建连接,则先创建
- {
- //从配置文件中获取SQL连接字段
- //string connStr = ConfigurationManager.ConnectionStrings["ConnetcionNmae"].ToString();
- connection = new SqlConnection(connString);//创建连接
- connection.Open();//打开连接
- }
- else if (connection.State == ConnectionState.Broken)//如果连接中断,则重现打开
- {
- connection.Close();
- connection.Open();
- }
- else if (connection.State == ConnectionState.Closed)//如果关闭,则打开
- {
- connection.Open();
- }
- return connection;
- }
- catch (Exception ex)
- {
- if (connection != null)
- {
- connection.Close();
- connection.Dispose();
- }
- logNet.WriteError(ex.Message.ToString());
- return null;
- }
- }
- }
- /// <summary>
- /// 重置连接
- /// </summary>
- public static void ResetConnection()
- {
- if (connection != null)
- {
- connection.Close();
- connection.Dispose();
- connection = null;
- }
- }
- /// <summary>
- /// 获取数据集
- /// </summary>
- /// <param name="str">执行字符串</param>
- /// <returns></returns>
- public static DataSet GetDataSet(string str)
- {
- lock (sqlLock)
- {
- try
- {
- SqlDataAdapter sda = new SqlDataAdapter(str, Connection);
- DataSet ds = new DataSet();
- sda.Fill(ds);
- return ds;
- }
- catch (Exception ex)
- {
- ResetConnection();
- logNet.WriteError(ex.Message.ToString());
- return null;
- }
- }
- }
- /// <summary>
- /// 获取表格
- /// </summary>
- /// <param name="str">执行字符串</param>
- /// <returns></returns>
- public static DataTable GetDataTable(string str)
- {
- return GetDataSet(str).Tables[0] ?? null;
- }
- /// <summary>
- /// 执行SQL语句
- /// </summary>
- /// <param name="str"></param>
- public static string ExecuteNonQuery(string str)
- {
- string ret = string.Empty;
- try
- {
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = Connection;
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = str;
- cmd.ExecuteNonQuery();
- ret = "成功";
- }
- catch (Exception ex)
- {
- ret = ex.Message.ToString();
- logNet.WriteError(ex.Message.ToString());
- }
- return ret;
- }
- /////////////////////////////////////////////////////////////////////////
- ///创建数据库和表
- /////////////////////////////////////////////////////////////////////////
- /// <summary>
- /// 判断数据库是否存在
- /// </summary>
- /// <param name="db">数据库名称</param>
- /// <returns></returns>
- public static bool IsDBExist(string db)
- {
- string createDbStr = " select * from master.dbo.sysdatabases where name " + "= '" + db + "'";
- DataTable dt = GetDataTable(createDbStr);
- if (dt?.Rows.Count > 0)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 判断数据库中指定表格是否存在
- /// </summary>
- /// <param name="db"></param>
- /// <param name="tb"></param>
- /// <returns></returns>
- public static bool IsTableExist(string db, string tb)
- {
- string createTbStr = "USE " + db + " select 1 from sysobjects where id =object_id('" + tb + "') and type = 'U'";
- DataTable dt = GetDataTable(createTbStr);
- if (dt?.Rows.Count > 0)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 判断数据库中指定表格是否存在
- /// </summary>
- /// <param name="db"></param>
- /// <param name="tb"></param>
- /// <returns></returns>
- public static bool IsTableExist(string db, string tb, string connString)
- {
- string createTbStr = "USE " + db + " select 1 from sysobjects where id =object_id('" + tb + "') and type = 'U'";
- DataSet ds = Query(createTbStr, null, connString);
- if (ds?.Tables["ds"]?.Rows.Count > 0)
- {
- return true;
- }
- return false;
- }
- /// <summary>
- /// 创建数据库表
- /// </summary>
- /// <param name="db">数据库名</param>
- /// <param name="tb">表名</param>
- public static void CreateDataTable(string db, string tb, string content)
- {
- if (IsDBExist(db) == false)
- {
- throw new Exception("数据库不存在!");
- }
- if (IsTableExist(db, tb))
- {
- throw new Exception("数据库表已经存在!");
- }
- else
- {
- string createTableStr = "USE " + db + " Create table " + tb + "(" + content + ")";
- ExecuteNonQuery(createTableStr);
- }
- }
- public static void CreateDataBase(string dbDir, string subDir, string dataBaseName)
- {
- string fileMDF = dbDir + subDir + @"\" + dataBaseName + @".mdf";
- string fileLDF = dbDir + subDir + @"\" + dataBaseName + @".ldf";
- if ((!File.Exists(fileMDF)) && (!File.Exists(fileLDF)))
- {
- SqlConnection myConn = new SqlConnection("Server=" + GlobalContext.Server
- + ";Integrated security=SSPI;database=master");
- String strSQL;
- strSQL = @"CREATE DATABASE " + dataBaseName +
- " ON PRIMARY " +
- "(NAME = " + dataBaseName + "_Data, " +
- "FILENAME = '" + fileMDF + "', " +
- "SIZE = 64MB, " +
- "MAXSIZE = UNLIMITED," +
- "FILEGROWTH = 64MB)" +
- "LOG ON (NAME = " + dataBaseName + "_Log, " +
- "FILENAME = '" + fileLDF + "', " +
- "SIZE = 64MB, " +
- "MAXSIZE = UNLIMITED, " +
- "FILEGROWTH = 64MB)";
- SqlCommand myCommand = new SqlCommand(strSQL, myConn);
- try
- {
- myConn.Open();
- myCommand.ExecuteNonQuery();
- logNet.WriteInfo("DataBase is Created Successfully");
- //创建StationIn表
- string content = @"[GUID] [nvarchar](36) NOT NULL,
- [Workorder_code] [nvarchar](50) NOT NULL,
- [Mtltmrk] [nvarchar](50) NOT NULL,
- [Sn] [nvarchar](64) NOT NULL,
- [StationIn_body] [nvarchar](MAX) NOT NULL,
- [Parameter_values] [nvarchar](MAX) NULL,
- [Write_user] [nvarchar](20) NOT NULL,
- [Test_time] [varchar](23) NOT NULL,
- [Upload] [nvarchar](10) NOT NULL";
- CreateDataTable(dataBaseName, "StationIn", content);
- //创建ProcessData表
- content = @"[ID] [nvarchar](50) NOT NULL,
- [Equipment_code] [nvarchar](50) NOT NULL,
- [Workorder_code] [nvarchar](50) NOT NULL,
- [Batch_number] [nvarchar](32) NULL,
- [Sn] [nvarchar](64) NULL,
- [Testitem] [varchar](50) NULL,
- [Parameter_values] [nvarchar](MAX) NOT NULL,
- [Write_user] [nvarchar](20) NOT NULL,
- [Test_time] [varchar](23) NOT NULL,
- [Upload] [nvarchar](10) NOT NULL";
- CreateDataTable(dataBaseName, "ProcessData", content);
- //创建OneCheckData表
- content = @" [ID] [nvarchar](50) NOT NULL,
- [Line_code] [nvarchar](50) NOT NULL,
- [Line_name] [nvarchar](50) NOT NULL,
- [Equipment_code] [nvarchar](50) NOT NULL,
- [Equipment_name] [nvarchar](50) NOT NULL,
- [Workorder_code] [nvarchar](50) NOT NULL,
- [Procedure_code] [nvarchar](32) NOT NULL,
- [Procedure_name] [nvarchar](32) NOT NULL,
- [Oneckeck_values] [nvarchar](MAX) NOT NULL,
- [Onecheck_empcode] [nvarchar](32) NULL,
- [Onecheck_empname] [nvarchar](32) NULL,
- [Onecheck_time] [varchar](20) NOT NULL,
- [Upload] [nvarchar](10) NOT NULL";
- CreateDataTable(dataBaseName, "OneCheckData", content);
- //创建WorkingData表-单机
- content = @"
- [Date] [nvarchar](10) NOT NULL,
- [Equipment_code] [nvarchar](32) NOT NULL,
- [BootTime] [varchar](20) NOT NULL,
- [Bootmoment] [nvarchar](20) NOT NULL,
- [RunTime] [varchar](20) NOT NULL,
- [WoringTime] [varchar](20) NOT NULL,
- [RepairTime] [varchar](20) NOT NULL,
- [StopTime] [varchar](20) NOT NULL,
- [WoringNum] [nvarchar](20) NOT NULL,
- [NeedTime] [varchar](20) NOT NULL,
- [PlanStopMoment] [nvarchar](20) NOT NULL,
- [Equipment_status] [nvarchar](20) NOT NULL,
- [WorkingQty] [nchvarcharar](20) NOT NULL,
- [QualifiedQty] [nvarchar](20) NOT NULL,
- [TotalQty] [nvarchar](20) NOT NULL,
- [NGset] [nvarchar](20) NOT NULL,
- [QualifiedRateSet] [nvarchar](20) NOT NULL,
- [RhySet] [nvarchar](20) NOT NULL,
- [Update_time] [varchar](20) NOT NULL";
- CreateDataTable(dataBaseName, "WorkingData", content);
- //创建LineWorkingData表-整线
- content = @" [GUID] [nvarchar](36) NOT NULL,
- [LineName] [nvarchar](36) NOT NULL,
- [BootTimeLong] [float] NULL,
- [NormalTimeLong] [float] NULL,
- [StandbyTimeLong] [float] NULL,
- [FaultTimeLong] [float] NULL,
- [MaterialShortageTimeLong] [float] NULL,
- [MaintenanceTimeLong] [float] NULL,
- [FaultNumber] [int] NULL,
- [OutputNumber] [int] NULL,
- [QualifiedNumber] [int] NULL,
- [QualifiedRate] [float] NULL,
- [DesignRhythm] [float] NULL,
- [RealityRhythm] [float] NULL,
- [CreateTime] [datetime] NULL";
- CreateDataTable(dataBaseName, "LineWorkingData", content);
- //创建AlarmData表
- content = @" [GUID] [nvarchar](36) NOT NULL,
- [LineName] [nvarchar](36) NOT NULL,
- [AlarmType] [nvarchar](36) NOT NULL,
- [AlarmDesc] [nvarchar](64) NOT NULL,
- [StartTime] [datetime] NOT NULL,
- [EndTime] [datetime] NULL,
- [PersistTime] [int] NULL";
- CreateDataTable(dataBaseName, "AlarmData", content);
- }
- catch (System.Exception ex)
- {
- logNet.WriteError(ex.Message.ToString());
- }
- finally
- {
- if (myConn.State == ConnectionState.Open)
- {
- myConn.Close();
- }
- }
- }
- }
- #region
- /// <summary>
- /// 检验并创建工单信息表
- /// </summary>
- public static void CreateDataBase_DBMain()
- {
- try
- {
- // 数据库链接
- string connString =
- "server=" + GlobalContext.Server +
- ";database=DBMain" +
- ";uid=" + GlobalContext.User +
- ";pwd=" + GlobalContext.PassWord;
- // 创建OrderTable表
- if (!IsTableExist("DBMain", "OrderTable", connString))
- {
- string content =
- @"
- CREATE TABLE [dbo].[OrderTable](
- [WorkOrderNum] [nvarchar](150) NOT NULL,
- [WorkOrderStatus] [nvarchar](32) NOT NULL,
- [BatchNumber] [nvarchar](36) NULL,
- [ProductMtltmrk] [nvarchar](32) NULL,
- [ProductNo] [nvarchar](32) NULL,
- [ProductName] [nvarchar](32) NULL,
- [SupplierCode] [nvarchar](32) NULL,
- [PlannedQuantity] [nvarchar](32) NULL,
- [CompletedQuantity] [int] NULL,
- [Plnsign] [nvarchar](32) NULL,
- [Soreqdat] [nvarchar](32) NULL,
- [Process] [nvarchar](32) NULL,
- [CreatedTime] [datetime] NOT NULL,
- CONSTRAINT [PK__OrderTab__33E32040C02E055A] PRIMARY KEY CLUSTERED
- (
- [WorkOrderNum] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'车间订单号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'WorkOrderNum'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'订单状态' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'WorkOrderStatus'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'批次号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'BatchNumber'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'产品型号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'ProductMtltmrk'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'产品代号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'ProductNo'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'产品名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'ProductName'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'供应商代码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'SupplierCode'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计划数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'PlannedQuantity'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'已完成数量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'CompletedQuantity'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'计划标记' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'Plnsign'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'需求日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'Soreqdat'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'所属流程' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'Process'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'订单创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable', @level2type=N'COLUMN',@level2name=N'CreatedTime'
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'订单信息表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'OrderTable'";
- ExecuteSQL(content, null, connString);
- }
- }
- catch (System.Exception ex)
- {
- logNet.WriteError(ex.Message.ToString());
- }
- }
- #endregion
- #region 进出站相关数据记录
- //连接本地数据库
- public static SqlSugarClient db = new SqlSugarClient(new ConnectionConfig
- {
- ConnectionString = connString, // 连接到 SqlServer(不指定数据库)
- DbType = SqlSugar.DbType.SqlServer, // 数据库类型:SqlServer
- IsAutoCloseConnection = true, // 自动关闭连接
- InitKeyType = InitKeyType.Attribute // 使用实体类属性来初始化表结构
- });
- //连网数据库
- public static SqlSugarClient dbmain = new SqlSugarClient(new ConnectionConfig
- {
- ConnectionString = "server=" + GlobalContext.Server +
- ";database=DBMain" +
- ";uid=" + GlobalContext.User +
- ";pwd=" + GlobalContext.PassWord, // 连接到 SqlServer(不指定数据库)
- DbType = SqlSugar.DbType.SqlServer, // 数据库类型:SqlServer
- IsAutoCloseConnection = true, // 自动关闭连接
- InitKeyType = InitKeyType.Attribute // 使用实体类属性来初始化表结构
- });
- // 判断表格是否存在
- public static bool CheckTableExists(SqlSugarClient db, string tableName)
- {
- //判断表是否存在
- var result = db.Ado.SqlQuery<int>($@"
- SELECT COUNT(*)
- FROM INFORMATION_SCHEMA.TABLES
- WHERE TABLE_NAME = @tableName", new { tableName }).FirstOrDefault() > 0;
- return result;
- }
- //创建 载具绑定物料码表、物料码绑定部件码表、PLC返回MES数据记录表
- public static void CreateDataBase()
- {
- try
- {
- // 3. 判断并创建表格
- if (!CheckTableExists(dbmain, "carrierbind"))
- {
- CreateTables<CarrierBind>(dbmain);
- }
- if (!CheckTableExists(db, "productbind"))
- {
- CreateTables<ProductBind>(db);
- }
- if (!CheckTableExists(db, "testdata"))
- {
- CreateTables<TestData>(db);
- }
- logNet.WriteInfo("表格已创建或已存在。");
- }
- catch (Exception ex)
- {
- logNet.WriteError("表格创建失败,错误原因:" + ex.Message);
- MessageBox.Show("表格创建失败,错误码:" + ex.Message);
- }
- }
- public static void CreateTables<T>(SqlSugarClient db) where T : class, new()
- {
- db.CodeFirst.InitTables<T>(); // 根据传入的实体类类型来创建表格
- logNet.WriteInfo($"表格 {typeof(T).Name} 已创建。");
- }
- //获取载具绑定的产品码
- public static string GetProductBarcodeByCarrierCode(string CarrierCode)
- {
- string ProductBarcode = db.Queryable<CarrierBind>()
- .Where(x => x.CarrierCode == CarrierCode)
- .OrderByDescending(x => x.ID)
- .Select(x => x.ProductBarcode)
- .First();
- return ProductBarcode;
- }
- //载具码和产品码绑定关系
- public static ResponseMessage InsertCarrierBind(string carrierCode, string productBarcode, string stationId)
- {
- #region 新建CarrierBind实体,并将数据插入carrierBind表格
- // 创建 CarrierBind 实体,并通过传入的参数设置字段值
- var CarrierBind_sumRecord = new CarrierBind
- {
- CarrierCode = carrierCode,
- ProductBarcode = productBarcode
- };
- // 检查记录是否已经存在
- var CarrierBind_exists = db.Queryable<CarrierBind>().Where(x => x.CarrierCode == carrierCode);
- try
- {
- if (CarrierBind_exists != null)
- {
- db.Deleteable<CarrierBind>().Where(x => x.CarrierCode == carrierCode).ExecuteCommand();
- logNet.WriteInfo($"载具码与产品码已存在绑定关系,先解绑。");
- }
- // 插入数据
- db.Insertable(CarrierBind_sumRecord).ExecuteCommand();
- logNet.WriteInfo($"载具码与产品码绑定成功。");
- return new ResponseMessage { result = true, text = "载具码与产品码绑定成功" };
- }
- catch (Exception ex)
- {
- logNet.WriteError($"载具码与产品码绑定失败,错误" + ex.Message);
- return new ResponseMessage { result = false, text = "载具码与产品码绑定失败,错误" + ex.Message };
- }
- #endregion
- }
- public static ResponseMessage InsertOp10Data( string carrierCode, string productBarcode, int bindOrder,
- int throwingAmount, float cleaningPressure, float cleaningSpeed,
- float airKnifeHeight, float cleaningTime, int cleaningCount,int remainCount)
- {
- // 检查记录是否已经存在
- bool OP10_exists = db.Queryable<CarrierBind>().Any(x => x.CarrierCode == carrierCode &&
- x.ProductBarcode == productBarcode);
- if (OP10_exists)
- {
- var maxBindOrder = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode &&
- x.ProductBarcode == productBarcode)
- .Max(x => x.BindOrder); ;
- bindOrder = maxBindOrder + 1;
- }
- #region 将OP10数据插入testdata表格
- // 创建 Op10 实体,并通过传入的参数设置字段值
- var op10 = new TestData
- {
- CarrierCode = carrierCode, // 载具码
- ProductBarcode = productBarcode, // 产品码
- BindOrder = bindOrder, // 绑定顺序
- OP10_ThrowingAmount = throwingAmount, // 抛料数量
- OP10_CleaningPressure = cleaningPressure, // 清洗气压
- OP10_CleaningSpeed = cleaningSpeed, // 清洗速度
- OP10_AirKnifeHeight = airKnifeHeight, // 风刀高度
- OP10_CleaningTime = cleaningTime, // 清洗时间
- OP10_CleaningCount = cleaningCount, // 清洗次数
- OP10_RemainCount = cleaningCount // 清洗次数
- };
- try
- {
- db.Insertable(op10).ExecuteCommand();
- logNet.WriteError("OP10记录插入成功!");
- return new ResponseMessage { result = true, text = "OP10记录插入成功"};
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP10记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP10记录插入出错!错误码:" + ex.Message };
- }
- #endregion
- }
- // op20绑定部件码
- public static ResponseMessage InsertOp20Product(string carrierCode, string productBarcode, string topCover_Barcode)
- {
- int bindOrder = 1;
- // 检查记录是否已经存在
- bool Product_exists = db.Queryable<ProductBind>().Any(x => x.CarrierCode == carrierCode &&
- x.ProductBarcode == productBarcode);
- if (Product_exists)
- {
- var maxBindOrder = db.Queryable<ProductBind>()
- .Where(x => x.CarrierCode == carrierCode &&
- x.ProductBarcode == productBarcode)
- .Max(x => x.BindOrder); ;
- bindOrder = maxBindOrder + 1;
- }
- #region 新建ProductBind实体,并将数据插入productBind表格
- // 创建 ProductBind 实体,并通过传入的参数设置字段值
- var ProductBind_sumRecord = new ProductBind
- {
- CarrierCode = carrierCode,//载具码
- ProductBarcode = productBarcode, // 产品条码
- BindOrder = bindOrder, // 绑定顺序
- TopCover_Barcode=topCover_Barcode, //部件码
- };
- // 插入数据
- try
- {
- db.Insertable(ProductBind_sumRecord).ExecuteCommand();
- return new ResponseMessage { result = true, text = "OP20-产品码绑定部件码成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP20-产品码绑定部件码出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP20-产品码绑定部件码出错!错误码:" + ex.Message };
- }
- #endregion
- }
- // op20向testdata表格插入数据
- public static ResponseMessage InsertOp20Data(string carrierCode, string productBarcode, int throwCount, int remainCount)
- {
- ////// 检查上一次插入的记录是否存在
- ////var lastRecord = db.Queryable<TestData>()
- //// .Where(x => x.CarrierCode== carrierCode && x.ProductBarcode == productBarcode &&
- //// (x.OP10_ThrowingAmount != 0 ||
- //// x.OP10_CleaningPressure != 0.0f ||
- //// x.OP10_CleaningSpeed != 0.0f ||
- //// x.OP10_AirKnifeHeight != 0.0f ||
- //// x.OP10_CleaningTime != 0.0f ||
- //// x.OP10_CleaningCount != 0 ||
- //// x.OP10_RemainCount != 0)) // 看上一次有无记录数据
- //// .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- //// .Take(1) // 只取一条记录
- //// .First(); // 获取第一条记录,若没有则返回null
- ////if (lastRecord == null)
- ////{
- //// logNet.WriteError("OP20错误:OP10未插入数据!");
- ////}
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode )
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP20记录插入出错:MES主数据未成功创建,无法插入OP20数据!");
- return new ResponseMessage { result = false, text = "OP20错误:MES主数据未成功创建,无法插入OP20数据!" };
- }
- if (lastRecord.OP20_ThrowCount != 0 || lastRecord.OP20_RemainCount != 0)
- {
- logNet.WriteError("OP20记录插入出错:即将插入的行数据已存在,插入失败!");
- return new ResponseMessage { result = false, text = "OP20记录插入出错:即将插入的行数据已存在,插入失败!" };
- }
- lastRecord.OP20_ThrowCount = throwCount;
- lastRecord.OP20_RemainCount = remainCount;
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new { x.OP20_ThrowCount, x.OP20_RemainCount })
- .ExecuteCommand();
- logNet.WriteError("OP20记录插入成功!");
- return new ResponseMessage { result = false, text = "OP20记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP20记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP20记录插入出错!错误码:" + ex.Message };
- }
- }
- // op301向testdata表格插入数据
- public static ResponseMessage InsertOp301Data(string carrierCode, string productBarcode, float gluingSpeed, float pressureAB, float pressureDifferenceAB,
- float productHeightInfo, float periodicWeightData, float remainingGlueAmount)
- {
- ////// 检查上一次插入的记录是否存在
- ////var lastRecord = db.Queryable<TestData>()
- //// .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode &&
- //// (x.OP20_ThrowCount != 0 || x.OP20_RemainCount != 0)) // 只查找有效插入的数据
- //// .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- //// .Take(1) // 只取一条记录
- //// .First(); // 获取第一条记录,若没有则返回null
- ////if (lastRecord == null)
- ////{
- //// logNet.WriteError("OP301错误:OP20插入的记录存在值为空!");
- //// return new ResponseMessage { result = false, text = "OP301错误:OP20插入的记录存在值为空!" };
- ////}
-
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP301错误:MES主数据未成功创建,无法插入OP301数据!");
- return new ResponseMessage { result = false, text = "OP301错误:MES主数据未成功创建,无法插入OP20数据!" };
- }
- if (lastRecord.OP301_GluingSpeed != 0.0f || lastRecord.OP301_PressureAB != 0.0f ||
- lastRecord.OP301_PressureDifferenceAB != 0.0f || lastRecord.OP301_ProductHeightInfo != 0.0f ||
- lastRecord.OP301_PeriodicWeightData != 0.0f || lastRecord.OP301_RemainingGlueAmount != 0.0f)
- {
- logNet.WriteError("OP301记录插入出错:即将插入的行数据已存在,插入失败!");
- return new ResponseMessage { result = false, text = "OP301记录插入出错:即将插入的行数据已存在,插入失败!" };
- }
- lastRecord.OP301_GluingSpeed = gluingSpeed; // 供胶速度
- lastRecord.OP301_PressureAB = pressureAB; // AB管气压
- lastRecord.OP301_PressureDifferenceAB = pressureDifferenceAB; // AB管气压差
- lastRecord.OP301_ProductHeightInfo = productHeightInfo; // 产品测高信息
- lastRecord.OP301_PeriodicWeightData = periodicWeightData; // 定期称重数据
- lastRecord.OP301_RemainingGlueAmount = remainingGlueAmount; // 剩余胶量
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP301_GluingSpeed,
- x.OP301_PressureAB,
- x.OP301_PressureDifferenceAB,
- x.OP301_ProductHeightInfo,
- x.OP301_PeriodicWeightData,
- x.OP301_RemainingGlueAmount
- }).ExecuteCommand();
- logNet.WriteError("OP301记录插入成功!");
- return new ResponseMessage { result = true, text = "OP301记录插入成功"};
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP301记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP301记录插入出错!错误码:" + ex.Message };
- }
- }
- // op302向testdata表格插入数据
- public static ResponseMessage InsertOp302Data(string carrierCode, string productBarcode, float gluingSpeed, float pressureAB, float pressureDifferenceAB,
- float productHeightInfo, float periodicWeightData, float remainingGlueAmount)
- {
- ////// 检查上一次插入的记录是否存在
- ////var lastRecord = db.Queryable<TestData>()
- //// .Where(x => x.OP301_GluingSpeed != 0.0f && x.OP301_PressureA != 0.0f && x.OP301_PressureB != 0.0f &&
- //// x.OP301_PressureDifferenceAB != 0.0f && x.OP301_ProductHeightInfo != 0.0f &&
- //// x.OP301_PeriodicWeightData != 0.0f && x.OP301_RemainingGlueAmount != 0.0f) // 只查找有效插入的数据
- //// .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- //// .Take(1) // 只取一条记录
- //// .First(); // 获取第一条记录,若没有则返回null
- ////if (lastRecord == null)
- ////{
- //// logNet.WriteError("OP302错误:OP301插入的记录存在值为空!");
- //// return new ResponseMessage { result = false, text = "OP302错误:OP301插入的记录存在值为空!" };
- ////}
- ///
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP302记录插入错误:MES主数据未成功创建,无法插入OP302数据!");
- return new ResponseMessage { result = false, text = "OP302记录插入错误:MES主数据未成功创建,无法插入OP20数据!" };
- }
- if (lastRecord.OP302_GluingSpeed != 0.0f || lastRecord.OP302_PressureAB != 0.0f ||
- lastRecord.OP302_PressureDifferenceAB != 0.0f || lastRecord.OP302_ProductHeightInfo != 0.0f ||
- lastRecord.OP302_PeriodicWeightData != 0.0f || lastRecord.OP302_RemainingGlueAmount != 0.0f)
- {
- logNet.WriteError("OP302记录插入出错:即将插入的行数据已存在,插入失败!");
- return new ResponseMessage { result = false, text = "OP302记录插入出错:即将插入的行数据已存在,插入失败!" };
- }
- lastRecord.OP302_GluingSpeed = gluingSpeed; // 供胶速度
- lastRecord.OP302_PressureAB = pressureAB; // AB管气压
- lastRecord.OP302_PressureDifferenceAB = pressureDifferenceAB; // AB管气压差
- lastRecord.OP302_ProductHeightInfo = productHeightInfo; // 产品测高信息
- lastRecord.OP302_PeriodicWeightData = periodicWeightData; // 定期称重数据
- lastRecord.OP302_RemainingGlueAmount = remainingGlueAmount; // 剩余胶量
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP302_GluingSpeed,
- x.OP302_PressureAB,
- x.OP302_PressureDifferenceAB,
- x.OP302_ProductHeightInfo,
- x.OP302_PeriodicWeightData,
- x.OP302_RemainingGlueAmount
- }).ExecuteCommand();
- logNet.WriteError("OP302记录插入成功!");
- return new ResponseMessage { result = true, text = "OP302记录插入成功" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP302记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP302错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- }
- }
- // op40向testdata表格插入数据
- public static ResponseMessage InsertOp40Data(string carrierCode, string productBarcode, float gluePosx, float gluePosy, float glueLineArea,
- float glueLineHeight,int result, string inspectionImagePath)
- {
- //// 检查上一次插入的记录是否存在
- //var lastRecord = db.Queryable<TestData>()
- // .Where(x => x.OP302_GluingSpeed != 0.0f && x.OP302_PressureAB != 0.0f &&
- // x.OP302_PressureDifferenceAB != 0.0f && x.OP302_ProductHeightInfo != 0.0f &&
- // x.OP302_PeriodicWeightData != 0.0f && x.OP302_RemainingGlueAmount != 0.0f) // 只查找有效插入的数据
- // .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- // .Take(1) // 只取一条记录
- // .First(); // 获取第一条记录,若没有则返回null
- //if (lastRecord == null)
- //{
- // logNet.WriteError("OP40错误:OP302插入的记录存在值为空!");
- // return new ResponseMessage { result = false, text = "OP40错误:OP302插入的记录存在值为空!" };
- //}
- //if (lastRecord.OP40_GluePosition != "" || lastRecord.OP40_GlueLineArea != 0.0f ||
- // lastRecord.OP40_GlueLineHeight != 0.0f || lastRecord.OP40_InspectionImagePath != "")
- //{
- // logNet.WriteError("OP40错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- // return new ResponseMessage { result = false, text = "OP40错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- //}
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP40记录插入错误:MES主数据未成功创建,无法插入OP40数据!");
- return new ResponseMessage { result = false, text = "OP40记录插入错误:MES主数据未成功创建,无法插入OP20数据!" };
- }
- if (lastRecord.OP40_GluePosX != 0.0f || lastRecord.OP40_GluePosY != 0.0f || lastRecord.OP40_GlueLineArea != 0.0f ||
- lastRecord.OP40_GlueLineHeight != 0.0f || lastRecord.OP40_Result != 0 || lastRecord.OP40_InspectionImagePath != "")
- {
- logNet.WriteError("OP40记录插入出错:即将插入的行数据已存在,插入失败!");
- return new ResponseMessage { result = false, text = "OP40记录插入出错:即将插入的行数据已存在,插入失败!" };
- }
- lastRecord.OP40_GluePosX = gluePosx; // 胶线位置X偏差
- lastRecord.OP40_GluePosY = gluePosy; // 胶线位置Y偏差
- lastRecord.OP40_GlueLineArea = glueLineArea; // 胶线面积
- lastRecord.OP40_GlueLineHeight = glueLineHeight; // 胶线高度
- lastRecord.OP40_Result = result; // 胶线检测结果
- lastRecord.OP40_InspectionImagePath = inspectionImagePath; // 检测图片路径
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP40_GluePosX,
- x.OP40_GluePosY,
- x.OP40_GlueLineArea,
- x.OP40_GlueLineHeight,
- x.OP40_Result,
- x.OP40_InspectionImagePath
- }).ExecuteCommand();
- logNet.WriteError("OP40记录插入成功!");
- return new ResponseMessage { result = true, text = "OP40记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP40记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = true, text = "OP40记录插入出错!错误码:" + ex.Message };
- }
- }
- //OP50 往product塞数据
- public static ResponseMessage InsertOp50Product( string carrierCode,string productBarcode, string addPCB_Barcode)
- {
-
- #region 往productBind表格中插入addPCB_Barcode数据
- // 检查上一次插入的记录是否存在
- var productBind_lastRecord = db.Queryable<ProductBind>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode && x.BindOrder != 0)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (productBind_lastRecord == null)
- {
- logNet.WriteError("OP50-错误:ProductBind表数据为空!");
- return new ResponseMessage { result = false, text = "OP50-错误:ProductBind表数据为空!" };
- }
- if (productBind_lastRecord.AddPCB_Barcode != "")
- {
- logNet.WriteError("OP50-错误:即将插入ProductBind的数据存在值不为空,插入失败,请按标准操作!");
- return new ResponseMessage { result = false, text = "OP50-错误:即将插入ProductBind的数据存在值不为空,插入失败,请按标准操作!" };
- }
- productBind_lastRecord.AddPCB_Barcode = addPCB_Barcode;
- try
- {
- db.Updateable(productBind_lastRecord)
- .UpdateColumns(x => new {
- x.AddPCB_Barcode
- }).ExecuteCommand();
- logNet.WriteError("OP50部件码绑定产品码成功!");
- return new ResponseMessage { result = false, text = "OP50部件码绑定产品码成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP50部件码绑定产品码失败!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP50部件码绑定产品码失败!错误码:" + ex.Message };
- }
- #endregion
- }
- // op50向testdata表格插入数据
- public static ResponseMessage InsertOp50Data(string carrierCode,string productBarcode,int assemblyStatus,
- int addBoardStatus, float addBoardPressure,int remainCount, string reinspectionImagePath)
- {
- // 检查上一次插入的记录是否存在
- //var lastRecord = db.Queryable<TestData>()
- // .Where(x => x.CarrierCode == CarrierCode && x.ProductBarcode == ProductBarcode && x.OP40_GluePosition != "" && x.OP40_GlueLineArea != 0.0f &&
- // x.OP40_GlueLineHeight != 0.0f && x.OP40_InspectionImagePath != "") // 只查找有效插入的数据
- // .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- // .Take(1) // 只取一条记录
- // .First(); // 获取第一条记录,若没有则返回null
- //if (lastRecord == null)
- //{
- // logNet.WriteError("OP50错误:OP40插入的记录存在值为空!");
- // return new ResponseMessage { result = false, text = "OP50错误:OP40插入的记录存在值为空!!" };
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP50记录插入错误:MES主数据未成功创建,无法插入OP40数据!");
- return new ResponseMessage { result = false, text = "OP50记录插入错误:MES主数据未成功创建,无法插入OP20数据!" };
- }
- if ( lastRecord.OP50_AssemblyStatus != 2 || lastRecord.OP50_ReinspectionImagePath != "" ||
- lastRecord.OP50_AddBoardStatus != 2 || lastRecord.OP50_AddBoardPressure != 0.0f)
- {
- logNet.WriteError("OP50错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- return new ResponseMessage { result = false, text = "OP50错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- }
- lastRecord.OP50_AssemblyStatus = assemblyStatus; // 组装是否到位
- lastRecord.OP50_AddBoardStatus = addBoardStatus; // ADD板有无
- lastRecord.OP50_AddBoardPressure = addBoardPressure; // ADD板压合压力
- lastRecord.OP50_RemainCount = remainCount; // ADD板余料数
- lastRecord.OP50_ReinspectionImagePath = reinspectionImagePath; // ADD板余料数
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP50_AssemblyStatus,
- x.OP50_AddBoardStatus,
- x.OP50_AddBoardPressure,
- x.OP50_RemainCount,
- x.OP50_ReinspectionImagePath
- }).ExecuteCommand();
- logNet.WriteError("OP50记录插入成功!");
- return new ResponseMessage { result = true, text = "OP50记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP50记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP50记录插入出错!错误码:" + ex.Message };
- }
- }
- // op60向testdata表格插入数据
- public static ResponseMessage InsertOp60Data(string CarrierCode,string ProductBarcode, int assemblyStatus, string reinspectionImagePath,
- int topCoverStatus, float topCoverPressure)
- {
- // 检查上一次插入的记录是否存在
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == CarrierCode && x.ProductBarcode == ProductBarcode && x.AddPCB_Barcode != "" && x.OP50_AssemblyStatus != 2 && x.OP50_ReinspectionImagePath != "" &&
- x.OP50_AddBoardStatus != 2 && x.OP50_AddBoardPressure != 0.0f) // 只查找有效插入的数据
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP60错误:OP50插入的记录存在值为空!");
- return new ResponseMessage { result = false, text = "OP60错误:OP50插入的记录存在值为空!" };
- }
- if (lastRecord.OP60_AssemblyStatus != 2 || lastRecord.OP60_ReinspectionImagePath != "" ||
- lastRecord.OP60_TopCoverStatus != 2 || lastRecord.OP60_TopCoverPressure != 0.0f)
- {
- logNet.WriteError("OP60错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- return new ResponseMessage { result = false, text = "OP60错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- }
- lastRecord.OP60_AssemblyStatus = assemblyStatus; // 组装是否到位
- lastRecord.OP60_ReinspectionImagePath = reinspectionImagePath; // 设备复检图片路径
- lastRecord.OP60_TopCoverStatus = topCoverStatus; // 上盖板有无
- lastRecord.OP60_TopCoverPressure = topCoverPressure; // 上盖板压合压力
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP60_AssemblyStatus,
- x.OP60_ReinspectionImagePath,
- x.OP60_TopCoverStatus,
- x.OP60_TopCoverPressure
- }).ExecuteCommand();
- logNet.WriteError("OP60记录插入成功!");
- return new ResponseMessage { result = true, text = "OP60记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP60记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP60记录插入出错!错误码:" + ex.Message };
- }
- }
- // op701向testdata表格插入数据
- public static ResponseMessage InsertOp701Data(string CarrierCode,string ProductBarcode, string torqueCurveDataPath,
- float screwPressure, int turns, float lockTime, string lockOrder, string lockResult)
- {
- // 检查上一次插入的记录是否存在
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == CarrierCode && x.ProductBarcode == ProductBarcode && x.OP60_AssemblyStatus != 2 && x.OP60_ReinspectionImagePath != "" &&
- x.OP60_TopCoverStatus != 2 && x.OP60_TopCoverPressure != 0.0f) // 只查找有效插入的数据
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP701错误:OP60插入的记录存在值为空!");
- return new ResponseMessage { result = false, text = "OP701错误:OP60插入的记录存在值为空!" };
- }
- if (lastRecord.OP701_TorqueCurveDataPath != "" || lastRecord.OP701_ScrewPressure != 0.0f || lastRecord.OP701_Turns != 0 ||
- lastRecord.OP701_LockTime != 0 || lastRecord.OP701_LockOrder != "" || lastRecord.OP701_LockResult != "")
- {
- logNet.WriteError("OP701错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- return new ResponseMessage { result = false, text = "OP701错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- }
- lastRecord.OP701_TorqueCurveDataPath = torqueCurveDataPath;
- lastRecord.OP701_ScrewPressure = screwPressure;
- lastRecord.OP701_Turns = turns;
- lastRecord.OP701_LockTime = lockTime;
- lastRecord.OP701_LockOrder = lockOrder;
- lastRecord.OP701_LockResult = lockResult;
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP701_TorqueCurveDataPath,
- x.OP701_ScrewPressure,
- x.OP701_Turns,
- x.OP701_LockTime,
- x.OP701_LockOrder,
- x.OP701_LockResult
- }).ExecuteCommand();
- logNet.WriteError("OP701记录插入成功!");
- return new ResponseMessage { result = true, text = "OP701记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP701记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP701记录插入出错!错误码:" + ex.Message };
- }
- }
- // op702向testdata表格插入数据
- public static ResponseMessage InsertOp702Data(string CarrierCode,string ProductBarcode, string torqueCurveDataPath,
- float screwPressure, int turns, float lockTime, string lockOrder, string lockResult)
- {
- // 检查上一次插入的记录是否存在
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == CarrierCode && x.ProductBarcode == ProductBarcode && x.OP701_TorqueCurveDataPath != "" && x.OP701_ScrewPressure != 0.0f &&
- x.OP701_Turns != 0 && x.OP701_LockTime != 0 && x.OP701_LockOrder != "" &&
- x.OP701_LockResult != "") // 只查找有效插入的数据
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP702错误:OP701插入的记录存在值为空!");
- return new ResponseMessage { result = false, text = "OP702错误:OP701插入的记录存在值为空!" };
- }
- if (lastRecord.OP702_TorqueCurveDataPath != "" || lastRecord.OP702_ScrewPressure != 0.0f || lastRecord.OP702_Turns != 0 ||
- lastRecord.OP702_LockTime != 0 || lastRecord.OP702_LockOrder != "" || lastRecord.OP702_LockResult != "")
- {
- logNet.WriteError("OP702错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- return new ResponseMessage { result = false, text = "OP702错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- }
- lastRecord.OP702_TorqueCurveDataPath = torqueCurveDataPath;
- lastRecord.OP702_ScrewPressure = screwPressure;
- lastRecord.OP702_Turns = turns;
- lastRecord.OP702_LockTime = lockTime;
- lastRecord.OP702_LockOrder = lockOrder;
- lastRecord.OP702_LockResult = lockResult;
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP702_TorqueCurveDataPath,
- x.OP702_ScrewPressure,
- x.OP702_Turns,
- x.OP702_LockTime,
- x.OP702_LockOrder,
- x.OP702_LockResult
- }).ExecuteCommand();
- logNet.WriteError("OP702记录插入成功!");
- return new ResponseMessage { result = true, text = "OP702记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP702记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP702记录插入出错!错误码:" + ex.Message };
- }
- }
- #region 实体
- // 定义实体类(对应数据库表)载具绑定表
- public class CarrierBind
- {
- // 主键ID,设置为自增且非空
- [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
- public int ID { get; set; } // 主键ID
- // 其他列
- public string CarrierCode { get; set; } = ""; // 载具码
- public string ProductBarcode { get; set; } = ""; // 产品码
- public DateTime CreateTime { get; set; } = DateTime.Now; // 创建时间
- }
- // 定义ProductBind实体类,表示表格结构
- public class ProductBind
- {
- // 主键ID,设置为自增且非空
- [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
- public int ID { get; set; } // 主键ID
- // 其他列
- public DateTime CreateTime { get; set; } = DateTime.Now; // 创建时间
- public string CarrierCode { get; set; } = ""; // 载具码
- public string ProductBarcode { get; set; } = ""; // 产品条码
- public string TopCover_Barcode { get; set; } = ""; // 上盖板条码
- public string AddPCB_Barcode { get; set; } = ""; // ADD板条码
- public int BindOrder { get; set; } = 0; // 绑定顺序
- }
- // 定义TestData实体类,表示表格TestData结构
- public class TestData
- {
- // 主键ID,设置为自增且非空
- [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
- public int ID { get; set; } // 主键ID
- // 其他列
- public DateTime CreateTime { get; set; } = DateTime.Now; // 创建时间
- public string CarrierCode { get; set; } = ""; // 载具码
- public string ProductBarcode { get; set; } = ""; // 产品条码
- public string TopCover_Barcode { get; set; } = ""; // 上盖板条码
- public string AddPCB_Barcode { get; set; } = ""; // ADD板条码
- public int BindOrder { get; set; } = 0; // 绑定顺序
- //op10
- public int OP10_ThrowingAmount { get; set; } = 0; // 抛料数量 // 清洗功率
- public float OP10_CleaningPressure { get; set; } = 0.0f; // 清洗气压
- public float OP10_CleaningSpeed { get; set; } = 0.0f; // 清洗速度
- public float OP10_AirKnifeHeight { get; set; } = 0.0f; // 风刀高度
- public float OP10_CleaningTime { get; set; } = 0.0f; // 清洗时间
- public int OP10_CleaningCount { get; set; } = 0; // 清洗次数
- public int OP10_RemainCount { get; set; } = 0; // 外壳体余料数
- //op20
- public int OP20_ThrowCount { get; set; } = 0; // 抛料数量
- public int OP20_RemainCount { get; set; } = 0; // 上盖余料数
- //op301
- public float OP301_GluingSpeed { get; set; } = 0.0f; // 供胶速度
- public float OP301_PressureAB { get; set; } = 0.0f; // A管气压
- public float OP301_PressureDifferenceAB { get; set; } = 0.0f; // AB管气压差
- public float OP301_ProductHeightInfo { get; set; } = 0.0f; // 产品测高信息
- public float OP301_PeriodicWeightData { get; set; } = 0.0f; // 定期称重数据
- public float OP301_RemainingGlueAmount { get; set; } = 0.0f; // 剩余胶量
- //op302
- public float OP302_GluingSpeed { get; set; } = 0.0f; // 供胶速度
- public float OP302_PressureAB { get; set; } = 0.0f; // A管气压
- public float OP302_PressureDifferenceAB { get; set; } = 0.0f; // AB管气压差
- public float OP302_ProductHeightInfo { get; set; } = 0.0f; // 产品测高信息
- public float OP302_PeriodicWeightData { get; set; } = 0.0f; // 定期称重数据
- public float OP302_RemainingGlueAmount { get; set; } = 0.0f; // 剩余胶量
- //op40
- public float OP40_GluePosX { get; set; } = 0.0f; // 胶线位置X偏差
- public float OP40_GluePosY { get; set; } = 0.0f; // 胶线位置Y偏差
- public float OP40_GlueLineArea { get; set; } = 0.0f; // 胶线面积
- public float OP40_GlueLineHeight { get; set; } = 0.0f; // 胶线高度
- public int OP40_Result { get; set; } = 0; // 胶线检测结果 1:OK 非1:NG
- public string OP40_InspectionImagePath { get; set; } = ""; // 检测图片路径
- //op50
- public int OP50_AssemblyStatus { get; set; } = 0; // 组装是否到位
- public string OP50_ReinspectionImagePath { get; set; } = ""; // 设备复检图片路径
- public int OP50_AddBoardStatus { get; set; } = 0; // ADD板有无
- public int OP50_RemainCount { get; set; } = 0; // ADD板余料数
- public float OP50_AddBoardPressure { get; set; } = 0.0f; // ADD板压合压力
- //op60
- public int OP60_AssemblyStatus { get; set; } = 0; // 组装是否到位
- public int OP60_TopCoverStatus { get; set; } = 0; // 上盖板有无
- public float OP60_TopCoverPressure { get; set; } = 0.0f; // 上盖板压合压力
- public string OP60_ReinspectionImagePath { get; set; } = ""; // 设备复检图片路径
- //op701
- public string OP701_TorqueCurveDataPath { get; set; } = ""; // 扭力曲线数据表位置
- public float OP701_ScrewPressure { get; set; } = 0.0f; // 螺丝压力
- public int OP701_Turns { get; set; } = 0; // 圈数
- public float OP701_LockTime { get; set; } = 0.0f; // 锁附时间
- public string OP701_LockOrder { get; set; } = ""; // 锁附顺序
- public string OP701_LockResult { get; set; } = ""; // 锁附结果
- //op702
- public string OP702_TorqueCurveDataPath { get; set; } = ""; // 扭力曲线数据表位置
- public float OP702_ScrewPressure { get; set; } = 0.0f; // 螺丝压力
- public int OP702_Turns { get; set; } = 0; // 圈数
- public float OP702_LockTime { get; set; } = 0.0f; // 锁附时间
- public string OP702_LockOrder { get; set; } = ""; // 锁附顺序
- public string OP702_LockResult { get; set; } = ""; // 锁附结果
- }
- #endregion
- #endregion
- }
- }
|