12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using HslCommunication.LogNet;
- using SqlSugar;
- 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 bool result { get; set; }
- public string text { get; set; }
- }
- /// <summary>
- /// SQL连接
- /// </summary>
- private static SqlConnection connection = null;
- public static string connString = "";
- public static SqlSugarClient Db
- {
- get { return _db; }
- }
- public static void DatabaseConnection()
- {
- _db = new SqlSugarClient(new ConnectionConfig
- {
- ConnectionString = connString, // 请确保connString在此作用域内已定义或作为参数传递进来
- DbType = SqlSugar.DbType.SqlServer,
- IsAutoCloseConnection = true,
- InitKeyType = InitKeyType.Attribute
- });
- }
- //连接本地数据库
- public static SqlSugarClient _db = new SqlSugarClient(new ConnectionConfig
- {
- ConnectionString = connString, // 连接到 SqlServer(不指定数据库)
- DbType = SqlSugar.DbType.SqlServer, // 数据库类型:SqlServer
- IsAutoCloseConnection = true, // 自动关闭连接
- InitKeyType = InitKeyType.Attribute // 使用实体类属性来初始化表结构
- });
- public static void DBInitWork()
- {
- //以月为单位存
- 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);
- DatabaseConnection();
- //创建数据库和表
- 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,
- [StationInReturn_body] [nvarchar](MAX) 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,
- [StationOut_body] [nvarchar](MAX) NULL,
- [StationOutReturn_body] [nvarchar](MAX) 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 进出站相关数据记录
- // 提供静态属性来访问 SqlSugarClient 实例
- //连网数据库
- 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(Db, "carrierbind"))
- {
- CreateTables<CarrierBind>(Db);
- }
- 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)
- {
- try
- {
- var Carrierdt = Db.Queryable<CarrierBind>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderByDescending(x => x.ID)
- .Take(1)
- .ToList();
- if (Carrierdt != null && Carrierdt.Count > 0)
- {
- return Carrierdt.First().ProductBarcode;
- }
- else
- {
- return string.Empty;
- }
- }
- catch (Exception ex)
- {
- return string.Empty;
- }
- }
- //载具码和产品码解除绑定关系
- public static ResponseMessage DelCarrierBind(string carrierCode)
- {
- // 检查记录是否已经存在
- var CarrierBind_exists = Db.Queryable<CarrierBind>().Where(x => x.CarrierCode == carrierCode);
- try
- {
- Db.Deleteable<CarrierBind>().Where(x => x.CarrierCode == carrierCode).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 InsertCarrierBind(string carrierCode, string productBarcode)
- {
- #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 = remainCount // 清洗次数
- };
- 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;
- try
- {
- // 检查记录是否已经存在
- 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, //部件码
- };
- // 插入数据
- 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未插入数据!");
- ////}
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .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;
- Db.Updateable(lastRecord)
- .UpdateColumns(x => new { x.OP20_ThrowCount, x.OP20_RemainCount })
- .ExecuteCommand();
- logNet.WriteError("OP20记录插入成功!");
- return new ResponseMessage { result = true, 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,
- string productHeightInfo, string periodicWeightData, string 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插入的记录存在值为空!" };
- ////}
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .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主数据未成功创建,无法插入OP301数据!" };
- }
- //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; // 剩余胶量
- 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,
- string productHeightInfo, string periodicWeightData, string 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插入的记录存在值为空!" };
- ////}
- ///
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .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主数据未成功创建,无法插入OP302数据!" };
- }
- //if (lastRecord.OP302_GluingSpeed != 0.0f || lastRecord.OP302_PressureAB != 0.0f ||
- // lastRecord.OP302_PressureDifferenceAB != 0.0f || lastRecord.OP302_ProductHeightInfo != "" ||
- // lastRecord.OP302_PeriodicWeightData !="" || lastRecord.OP302_RemainingGlueAmount != "")
- //{
- // 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; // 剩余胶量
- 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, string gluePosx, string gluePosy, string glueLineArea,
- string 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错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- //}
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .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主数据未成功创建,无法插入OP40数据!" };
- }
- ////if (lastRecord.OP40_GluePosX != "" || lastRecord.OP40_GluePosY != "" || lastRecord.OP40_GlueLineArea != "" ||
- //// lastRecord.OP40_GlueLineHeight != "" || 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.ToString(); // 胶线检测结果
- lastRecord.OP40_InspectionImagePath = inspectionImagePath; // 检测图片路径
- 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数据
- // 检查上一次插入的记录是否存在
- try
- {
- var productBind_lastRecord = Db.Queryable<ProductBind>()
- .Where(x => x.CarrierCode == carrierCode && x.ProductBarcode == productBarcode)
- .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;
- 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插入的记录存在值为空!!" };
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP50记录插入错误:MES主数据未成功创建,无法插入OP50数据!");
- return new ResponseMessage { result = false, text = "OP50记录插入错误:MES主数据未成功创建,无法插入OP50数据!" };
- }
- ////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板余料数
- 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,
- int topCoverStatus, float topCoverPressure, string reinspectionImagePath)
- {
- // 检查上一次插入的记录是否存在
- //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插入的记录存在值为空!" };
- // }
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP60记录插入错误:MES主数据未成功创建,无法插入OP60数据!");
- return new ResponseMessage { result = false, text = "OP60记录插入错误:MES主数据未成功创建,无法插入OP60数据!" };
- }
- //if (lastRecord.OP60_AssemblyStatus != 0 || lastRecord.OP60_ReinspectionImagePath != "" ||
- // lastRecord.OP60_TopCoverStatus != 0 || lastRecord.OP60_TopCoverPressure != 0.0f)
- //{
- // logNet.WriteError("OP50记录插入错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- // return new ResponseMessage { result = false, text = "OP50记录插入错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- //}
- lastRecord.OP60_AssemblyStatus = assemblyStatus; // 组装是否到位
- lastRecord.OP60_TopCoverStatus = topCoverStatus; // 上盖板有无
- lastRecord.OP60_TopCoverPressure = topCoverPressure; // 上盖板压合压力
- lastRecord.OP60_ReinspectionImagePath = reinspectionImagePath; // 设备复检图片路径
- 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 lockTime, string lockOrder, string lockResult, int remainCount)
- {
- // 检查上一次插入的记录是否存在
- //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插入的记录存在值为空!" };
- //}
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP701记录插入错误:MES主数据未成功创建,无法插入OP701数据!");
- return new ResponseMessage { result = false, text = "OP701记录插入错误:MES主数据未成功创建,无法插入OP701数据!" };
- }
- //if (lastRecord.OP701_LockTime != "" || lastRecord.OP701_LockOrder != "" || lastRecord.OP701_LockResult != "")
- //{
- // logNet.WriteError("OP701错误:即将插入的数据存在值不为空,插入失败,请按标准操作!");
- // return new ResponseMessage { result = false, text = "OP701错误:即将插入的数据存在值不为空,插入失败,请按标准操作!" };
- //}
- lastRecord.OP701_LockTime = lockTime;
- lastRecord.OP701_LockOrder = lockOrder;
- lastRecord.OP701_LockResult = lockResult;
- lastRecord.OP701_RemainCount = remainCount;
- Db.Updateable(lastRecord)
- .UpdateColumns(x => new
- {
- x.OP701_LockTime,
- x.OP701_LockOrder,
- x.OP701_LockResult,
- x.OP701_RemainCount
- }).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 lockTime, string lockOrder, string lockResult, int remainCount)
- {
- //// 检查上一次插入的记录是否存在
- //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插入的记录存在值为空!" };
- //}
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP702记录插入错误:MES主数据未成功创建,无法插入OP702数据!");
- return new ResponseMessage { result = false, text = "OP702记录插入错误:MES主数据未成功创建,无法插入OP702数据!" };
- }
- lastRecord.OP702_LockTime = lockTime;
- lastRecord.OP702_LockOrder = lockOrder;
- lastRecord.OP702_LockResult = lockResult;
- lastRecord.OP702_RemainCount = remainCount;
- Db.Updateable(lastRecord)
- .UpdateColumns(x => new
- {
- x.OP702_LockTime,
- x.OP702_LockOrder,
- x.OP702_LockResult,
- x.OP702_RemainCount
- }).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 };
- }
- }
- // op80向testdata表格插入数据
- public static ResponseMessage InsertOp80Data(string carrierCode, string productBarcode, string screwHeights,
- string screwResults)
- {
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP80记录插入错误:MES主数据未成功创建,无法插入OP80数据!");
- return new ResponseMessage { result = false, text = "OP80记录插入错误:MES主数据未成功创建,无法插入OP80数据!" };
- }
- lastRecord.OP80_ScrewHeights = screwHeights;
- lastRecord.OP80_ScrewResults = screwResults;
- Db.Updateable(lastRecord)
- .UpdateColumns(x => new
- {
- x.OP80_ScrewHeights,
- x.OP80_ScrewResults
- }).ExecuteCommand();
- logNet.WriteError("OP80记录插入成功!");
- return new ResponseMessage { result = true, text = "OP80记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP80记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP80记录插入出错!错误码:" + ex.Message };
- }
- }
- // op90向testdata表格插入数据
- public static ResponseMessage InsertOp90Data(string carrierCode, string productBarcode, int throwCount,
- int remainCount)
- {
- try
- {
- var lastRecord = Db.Queryable<TestData>()
- .Where(x => x.CarrierCode == carrierCode)
- .OrderBy(x => x.ID, OrderByType.Desc) // 按照ID降序查找最新记录
- .Take(1) // 只取一条记录
- .First(); // 获取第一条记录,若没有则返回null
- if (lastRecord == null)
- {
- logNet.WriteError("OP90记录插入错误:MES主数据未成功创建,无法插入OP90数据!");
- return new ResponseMessage { result = false, text = "OP90记录插入错误:MES主数据未成功创建,无法插入OP90数据!" };
- }
- lastRecord.OP90_ThrowCount = throwCount;
- lastRecord.OP90_RemainCount = remainCount;
- Db.Updateable(lastRecord)
- .UpdateColumns(x => new
- {
- x.OP80_ScrewHeights,
- x.OP80_ScrewResults
- }).ExecuteCommand();
- logNet.WriteError("OP90记录插入成功!");
- return new ResponseMessage { result = true, text = "OP90记录插入成功!" };
- }
- catch (Exception ex)
- {
- logNet.WriteError("OP80记录插入出错!错误码:" + ex.Message);
- return new ResponseMessage { result = false, text = "OP90记录插入出错!错误码:" + 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 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管气压差
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP301_ProductHeightInfo { get; set; } = ""; // 产品测高信息
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP301_PeriodicWeightData { get; set; } = ""; // 定期称重数据
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP301_RemainingGlueAmount { get; set; } = ""; // 剩余胶量
- //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管气压差
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP302_ProductHeightInfo { get; set; } = ""; // 产品测高信息
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP302_PeriodicWeightData { get; set; } = ""; // 定期称重数据
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP302_RemainingGlueAmount { get; set; } = ""; // 剩余胶量
- //op40
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP40_GluePosX { get; set; } = ""; // 胶线位置X偏差
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP40_GluePosY { get; set; } = ""; // 胶线位置Y偏差
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP40_GlueLineArea { get; set; } = ""; // 胶线面积
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP40_GlueLineHeight { get; set; } = ""; // 胶线高度
- public string OP40_Result { get; set; } = ""; // 胶线检测结果 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
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP701_LockTime { get; set; } = ""; // 锁附时间
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP701_LockOrder { get; set; } = ""; // 锁附顺序
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP701_LockResult { get; set; } = ""; // 锁附结果
- public int OP701_RemainCount { get; set; } = 0; // 螺丝余料数
- //op702
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP702_LockTime { get; set; } = ""; // 锁附时间
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP702_LockOrder { get; set; } = ""; // 锁附顺序
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP702_LockResult { get; set; } = ""; // 锁附结果
- public int OP702_RemainCount { get; set; } = 0; // 螺丝余料数
- //op80
- [SqlSugar.SugarColumn(ColumnDataType = "varchar(500)")]
- public string OP80_ScrewHeights { get; set; } = ""; // 螺丝高度
- public string OP80_ScrewResults { get; set; } = ""; // 螺丝检测结果
- //op90
- public int OP90_ThrowCount { get; set; } = 0; // 抛料次数
- public int OP90_RemainCount { get; set; } = 0; // 料箱余料数
- }
- #endregion
- }
- }
|