123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350 |
- 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(SqlSugarClient db, string carrierCode, string hydrocooling_Barcode, int bindOrder,
- int throwingAmount, float cleaningPower, float cleaningPressure, float cleaningSpeed,
- float airKnifeHeight, float cleaningTime, int cleaningCount)
- {
- #region 新建CarrierBind实体,并将数据插入carrierBind表格
- // 创建 CarrierBind 实体,并通过传入的参数设置字段值
- var CarrierBind_sumRecord = new CarrierBind
- {
- CarrierCode = carrierCode,
- ProductBarcode = hydrocooling_Barcode,
- };
- // 检查记录是否已经存在
- bool CarrierBind_exists = db.Queryable<CarrierBind>().Any(x => x.CarrierCode == CarrierBind_sumRecord.CarrierCode &&
- x.ProductBarcode == CarrierBind_sumRecord.ProductBarcode);
- if (CarrierBind_exists)
- {
- var maxBindOrder = db.Queryable<CarrierBind>()
- .Where(x => x.CarrierCode == CarrierBind_sumRecord.CarrierCode &&
- x.ProductBarcode == CarrierBind_sumRecord.ProductBarcode);
- db.Deleteable<CarrierBind>().Where(x => x.CarrierCode == carrierCode).ExecuteCommand();
- }
- else
- {
- // 插入数据
- try
- {
- db.Insertable(CarrierBind_sumRecord).ExecuteCommand();
- }
- catch (Exception ex)
- {
- return new ResponseMessage { result = true, text = "OP10数据-载具码与产品码绑定失败,错误码:" + ex.Message };
- }
- }
- #endregion
- #region 新建ProductBind实体,并将数据插入productBind表格
- // 创建 ProductBind 实体,并通过传入的参数设置字段值
- var ProductBind_sumRecord = new ProductBind
- {
- CarrierCode = carrierCode,//载具码
- ProductBarcode = hydrocooling_Barcode, // 水冷壳体条码
- BindOrder = bindOrder // 绑定顺序
- };
- // 检查记录是否已经存在
- bool ProductBind_exists = db.Queryable<ProductBind>().Any(x => x.CarrierCode == ProductBind_sumRecord.CarrierCode &&
- x.ProductBarcode == ProductBind_sumRecord.ProductBarcode &&
- x.BindOrder == ProductBind_sumRecord.BindOrder);
- if (ProductBind_exists)
- {
-
- }
- else
- {
- // 插入数据
- try
- {
- db.Insertable(ProductBind_sumRecord).ExecuteCommand();
- logNet.WriteError("OP10-数据插入到 productbind 表格成功!");
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP10-数据插入到 productbind 表格出错!错误码:" + ex.Message);
- }
- }
- #endregion
- #region 将OP10数据插入testdata表格
- // 检查主键是否存在
- var existingRecord = db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == hydrocooling_Barcode && x.BindOrder == bindOrder)
- .First();
- if (existingRecord != null)
- {
- logNet.WriteError("OP10错误:数据已存在,载具码、水冷壳体码和绑定顺序相同!");
- return new ResponseMessage { result = false, text = "OP10错误:数据已存在,载具码、水冷壳体码和绑定顺序相同!" };
- }
- // 创建 Op10 实体,并通过传入的参数设置字段值
- var op10 = new TestData
- {
- CarrierCode = carrierCode, // 载具码
- ProductBarcode = hydrocooling_Barcode,// 水冷壳体码
- BindOrder = bindOrder, // 绑定顺序
- OP10_ThrowingAmount = throwingAmount, // 抛料数量
- OP10_CleaningPower = cleaningPower, // 清洗功率
- OP10_CleaningPressure = cleaningPressure, // 清洗气压
- OP10_CleaningSpeed = cleaningSpeed, // 清洗速度
- OP10_AirKnifeHeight = airKnifeHeight, // 风刀高度
- OP10_CleaningTime = cleaningTime, // 清洗时间
- OP10_CleaningCount = 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向testdata表格插入数据
- public static ResponseMessage InsertOp20Data( int throwingAmount, string topCover_Barcode)
- {
- // 检查上一次插入的记录是否存在
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.OP10_ThrowingAmount != 0 &&
- x.OP10_CleaningPower != 0.0f &&
- x.OP10_CleaningPressure != 0.0f &&
- x.OP10_CleaningSpeed != 0.0f &&
- x.OP10_AirKnifeHeight != 0.0f &&
- x.OP10_CleaningTime != 0.0f &&
- x.OP10_CleaningCount != 0) // 只查找有效插入的数据
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP20错误:OP10插入的记录存在值为空!");
- }
- if (lastRecord.OP20_ThrowingAmount != 0 || lastRecord.TopCover_Barcode != "")
- {
- logNet.WriteError("OP20错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- }
- #region 往productBind表格中插入topCover_Barcode数据
- // 检查上一次插入的记录是否存在
- var productBind_lastRecord = db.Queryable<ProductBind>()
- .Where(x => x.CarrierCode != "" && x.ProductBarcode != "" && x.BindOrder != 0)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (productBind_lastRecord == null)
- {
- logNet.WriteError("OP20-错误:ProductBind表中最新插入的数据为空!");
- }
- if (productBind_lastRecord.TopCover_Barcode != "")
- {
- logNet.WriteError("OP20-错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- }
- productBind_lastRecord.TopCover_Barcode = topCover_Barcode;
- try
- {
- db.Updateable(productBind_lastRecord)
- .UpdateColumns(x => new {
- x.TopCover_Barcode
- }).ExecuteCommand();
- logNet.WriteError("OP20往ProductBind表中插入TopCover_Barcode成功!");
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP20往ProductBind表中插入TopCover_Barcode出错!错误码:" + ex.Message);
- }
- #endregion
- lastRecord.OP20_ThrowingAmount = throwingAmount;
- lastRecord.TopCover_Barcode = topCover_Barcode;
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new { x.OP20_ThrowingAmount, x.TopCover_Barcode })
- .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( float gluingSpeed, float pressureA, float pressureB, float pressureDifferenceAB,
- float productHeightInfo, float periodicWeightData, float remainingGlueAmount)
- {
- // 检查上一次插入的记录是否存在
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.OP20_ThrowingAmount != 0 && x.TopCover_Barcode != "") // 只查找有效插入的数据
- .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插入的记录存在值为空!" };
- }
- if (lastRecord.OP301_GluingSpeed != 0.0f || lastRecord.OP301_PressureA != 0.0f || lastRecord.OP301_PressureB != 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_PressureA = pressureA; // A管气压
- lastRecord.OP301_PressureB = pressureB; // B管气压
- 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_PressureA,
- x.OP301_PressureB,
- 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( float gluingSpeed, float pressureA, float pressureB, 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插入的记录存在值为空!" };
- }
- if (lastRecord.OP302_GluingSpeed != 0.0f || lastRecord.OP302_PressureA != 0.0f || lastRecord.OP302_PressureB != 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_PressureA = pressureA; // A管气压
- lastRecord.OP302_PressureB = pressureB; // B管气压
- 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_PressureA,
- x.OP302_PressureB,
- 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 gluePosition, float glueLineArea,
- float glueLineHeight, string inspectionImagePath)
- {
- // 检查上一次插入的记录是否存在
- var lastRecord = db.Queryable<TestData>()
- .Where(x => x.OP302_GluingSpeed != 0.0f && x.OP302_PressureA != 0.0f && x.OP302_PressureB != 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错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- }
- lastRecord.OP40_GluePosition = gluePosition; // 点胶位置
- lastRecord.OP40_GlueLineArea = glueLineArea; // 胶线面积
- lastRecord.OP40_GlueLineHeight = glueLineHeight; // 胶线高度
- lastRecord.OP40_InspectionImagePath = inspectionImagePath; // 检测图片路径
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.OP40_GluePosition,
- x.OP40_GlueLineArea,
- x.OP40_GlueLineHeight,
- 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往ProductBind表中插入AddPCB_Barcode成功!");
- return new ResponseMessage { result = false, text = "OP50往ProductBind表中插入AddPCB_Barcode成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP50往ProductBind表中插入AddPCB_Barcode出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP50往ProductBind表中插入AddPCB_Barcode出错!错误码:" + ex.Message };
- }
- #endregion
- }
- // op50向testdata表格插入数据
- public static ResponseMessage InsertOp50Data(string CarrierCode,string ProductBarcode,string addPCB_Barcode, int assemblyStatus,
- string reinspectionImagePath, int addBoardStatus, float addBoardPressure)
- {
- // 检查上一次插入的记录是否存在
- 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插入的记录存在值为空!!" };
- }
- if (lastRecord.AddPCB_Barcode != "" || 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.AddPCB_Barcode = addPCB_Barcode;
- lastRecord.OP50_AssemblyStatus = assemblyStatus; // 组装是否到位
- lastRecord.OP50_ReinspectionImagePath = reinspectionImagePath; // 设备复检图片路径
- lastRecord.OP50_AddBoardStatus = addBoardStatus; // ADD板有无
- lastRecord.OP50_AddBoardPressure = addBoardPressure; // ADD板压合压力
- try
- {
- db.Updateable(lastRecord)
- .UpdateColumns(x => new {
- x.AddPCB_Barcode,
- x.OP50_AssemblyStatus,
- x.OP50_ReinspectionImagePath,
- x.OP50_AddBoardStatus,
- x.OP50_AddBoardPressure
- }).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_CleaningPower { get; set; } = 0.0f; // 清洗功率
- 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; // 清洗次数
- //op20
- public int OP20_ThrowingAmount { get; set; } = 0; // 抛料数量
- //op301
- public float OP301_GluingSpeed { get; set; } = 0.0f; // 供胶速度
- public float OP301_PressureA { get; set; } = 0.0f; // A管气压
- public float OP301_PressureB { get; set; } = 0.0f; // B管气压
- 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_PressureA { get; set; } = 0.0f; // A管气压
- public float OP302_PressureB { get; set; } = 0.0f; // B管气压
- 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 string OP40_GluePosition { get; set; } = ""; // 点胶位置
- public float OP40_GlueLineArea { get; set; } = 0.0f; // 胶线面积
- public float OP40_GlueLineHeight { get; set; } = 0.0f; // 胶线高度
- public string OP40_InspectionImagePath { get; set; } = ""; // 检测图片路径
- //op50
- public int OP50_AssemblyStatus { get; set; } = 2; // 组装是否到位
- public string OP50_ReinspectionImagePath { get; set; } = ""; // 设备复检图片路径
- public int OP50_AddBoardStatus { get; set; } = 2; // ADD板有无
- public float OP50_AddBoardPressure { get; set; } = 0.0f; // ADD板压合压力
- //op60
- public int OP60_AssemblyStatus { get; set; } = 2; // 组装是否到位
- public string OP60_ReinspectionImagePath { get; set; } = ""; // 设备复检图片路径
- public int OP60_TopCoverStatus { get; set; } = 2; // 上盖板有无
- public float OP60_TopCoverPressure { get; set; } = 0.0f; // 上盖板压合压力
- //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
- }
- }
|