From a573db6b336e9105467ecf4b4dedf55c362a3224 Mon Sep 17 00:00:00 2001 From: maqiao <625215135@qq.com> Date: Mon, 28 Jul 2025 15:24:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CreateAccountBookConsole.sln | 31 ++++ CreateAccountBookConsole/App.config | 12 ++ .../CreateAccountBookConsole.csproj | 17 ++ .../Models/AccountBookG2.cs | 33 ++++ .../Models/ChannelStock.cs | 27 +++ .../Models/SpecialAccountBook.cs | 29 ++++ CreateAccountBookConsole/Program.cs | 154 +++++++++++++++++ CreateAccountBookConsole/SqlHelper.cs | 160 ++++++++++++++++++ 8 files changed, 463 insertions(+) create mode 100644 CreateAccountBookConsole.sln create mode 100644 CreateAccountBookConsole/App.config create mode 100644 CreateAccountBookConsole/CreateAccountBookConsole.csproj create mode 100644 CreateAccountBookConsole/Models/AccountBookG2.cs create mode 100644 CreateAccountBookConsole/Models/ChannelStock.cs create mode 100644 CreateAccountBookConsole/Models/SpecialAccountBook.cs create mode 100644 CreateAccountBookConsole/Program.cs create mode 100644 CreateAccountBookConsole/SqlHelper.cs diff --git a/CreateAccountBookConsole.sln b/CreateAccountBookConsole.sln new file mode 100644 index 0000000..4d650b9 --- /dev/null +++ b/CreateAccountBookConsole.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CreateAccountBookConsole", "CreateAccountBookConsole\CreateAccountBookConsole.csproj", "{6BADF332-E903-457C-A9DB-9201506B8722}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6BADF332-E903-457C-A9DB-9201506B8722}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6BADF332-E903-457C-A9DB-9201506B8722}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6BADF332-E903-457C-A9DB-9201506B8722}.Debug|x64.ActiveCfg = Debug|x64 + {6BADF332-E903-457C-A9DB-9201506B8722}.Debug|x64.Build.0 = Debug|x64 + {6BADF332-E903-457C-A9DB-9201506B8722}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6BADF332-E903-457C-A9DB-9201506B8722}.Release|Any CPU.Build.0 = Release|Any CPU + {6BADF332-E903-457C-A9DB-9201506B8722}.Release|x64.ActiveCfg = Release|x64 + {6BADF332-E903-457C-A9DB-9201506B8722}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {5499F1B6-15BC-4D80-8709-65DC9AF0D558} + EndGlobalSection +EndGlobal diff --git a/CreateAccountBookConsole/App.config b/CreateAccountBookConsole/App.config new file mode 100644 index 0000000..3fbea7f --- /dev/null +++ b/CreateAccountBookConsole/App.config @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/CreateAccountBookConsole/CreateAccountBookConsole.csproj b/CreateAccountBookConsole/CreateAccountBookConsole.csproj new file mode 100644 index 0000000..d022ef7 --- /dev/null +++ b/CreateAccountBookConsole/CreateAccountBookConsole.csproj @@ -0,0 +1,17 @@ + + + + Exe + net6.0 + enable + enable + AnyCPU;x64 + + + + + + + + + diff --git a/CreateAccountBookConsole/Models/AccountBookG2.cs b/CreateAccountBookConsole/Models/AccountBookG2.cs new file mode 100644 index 0000000..a196350 --- /dev/null +++ b/CreateAccountBookConsole/Models/AccountBookG2.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CreateAccountBookConsole.Models +{ + /// + /// 日结存、总结存表 + /// + public class AccountBookG2 + { + public int Id { get; set; } + public int DrugId { get; set; } + public int Type { get; set; } + public string Department { get;set; } + public string InvoiceNo { get; set; } + public string OrderNo { get; set; } + public string ManuNo { get; set; } + public string EffDate { get; set;} + public int YesterDayQuantity { get; set; } + public int AddQuantity { get; set; } + public int OutQuantity { get; set; } + public int ManuStock { get; set; } + public int TotalStock { get; set; } + public int UserId1 { get; set; } + public int UserId2 { get; set;} + public string MachineId { get; set; } + public string CreateDate { get; set; } + public DateTime CreateTime { get; set; } + } +} diff --git a/CreateAccountBookConsole/Models/ChannelStock.cs b/CreateAccountBookConsole/Models/ChannelStock.cs new file mode 100644 index 0000000..544944f --- /dev/null +++ b/CreateAccountBookConsole/Models/ChannelStock.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CreateAccountBookConsole.Models +{ + public class ChannelStock + { + public string Id { get; set; } + public string Chnguid { get; set; } + public string MachineId { get; set; } + public int RowNo { get; set; } + public int ColNo { get; set; } + public int PosNo { get;set; } + public string DrugId { get; set; } + public string ManuNO { get;set; } + public string EffDate { get; set; } + public int Quantity { get; set; } + public int DrawerType { get; set; } + public int BoardType { get; set; } + + public int ManuNoQuantity { get; set; } + public int TotalQuantity { get; set; } + } +} diff --git a/CreateAccountBookConsole/Models/SpecialAccountBook.cs b/CreateAccountBookConsole/Models/SpecialAccountBook.cs new file mode 100644 index 0000000..0a04596 --- /dev/null +++ b/CreateAccountBookConsole/Models/SpecialAccountBook.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CreateAccountBookConsole.Models +{ + /// + /// 专用账册 + /// + public class SpecialAccountBook + { + public int Id { get; set; } + public string YearDM { get; set; } + public string ManuNo { get; set; } + public string EffDate { get; set; } + public int BeforeQuantity { get; set; } + public int InQuantity { get; set; } + public int TotalQuantity { get; set; } + public string DrawerNo { get; set; } + public int Quantity { get; set; } + public string PharmacyUser { get; set; } + public string InPharmacy { get; set; } + public string OperatorUser { get; set; } + public string OperatorCheck { get;set; } + public DateTime CreateTime { get; set; } + } +} diff --git a/CreateAccountBookConsole/Program.cs b/CreateAccountBookConsole/Program.cs new file mode 100644 index 0000000..71af24b --- /dev/null +++ b/CreateAccountBookConsole/Program.cs @@ -0,0 +1,154 @@ +// See https://aka.ms/new-console-template for more information + +using CreateAccountBookConsole; +using CreateAccountBookConsole.Models; +using FluentScheduler; +using MySqlConnector; +using System.Configuration; +using System.Data; +using System.Reflection.PortableExecutable; + +JobManager.Initialize(); + +//JobManager.AddJob( +// () => Console.WriteLine($"{DateTime.Now}1 minutes just passed."), +// s => s.ToRunEvery(1).Minutes() +//); + +Console.WriteLine("定时系统已启动"); +//湘潭急诊DM1设备每天凌晨生成当日结存数据及总结存数据 +JobManager.AddJob(() => CreateClass.CreateMethod(), s => s.ToRunEvery(1).Days().At(00, 00)); +//湘潭手术室DM3设备每天凌晨生成当日结存数据 +JobManager.AddJob(() => CreateClass.CreateMethodForSSS(), s => s.ToRunEvery(1).Days().At(00, 00)); +//JobManager.JobStart += info => +//{ +// Console.WriteLine($"{DateTime.Now}定时任务开始执行"); +//}; +//JobManager.JobEnd += info => +//{ +// Console.WriteLine($"{DateTime.Now}定时任务执行结束"); +//}; + +//CreateClass.CreateMethodForSSS(); +Console.ReadLine(); + +public class CreateClass +{ + /// + /// 生成日结存及总结存数据 + /// + public static void CreateMethod() + { + try + { + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + if (conn.State != ConnectionState.Open) + conn.Open(); + MySqlTransaction transaction = conn.BeginTransaction(); + try + { + using (MySqlCommand cmd = conn.CreateCommand()) + { + cmd.Connection = conn; + cmd.Transaction = transaction; + //1)查询库存,为每一个批次的药品生成日结存数据 + string sql = $@"SELECT machine_id,drug_id,manu_no,eff_date,sum(quantity) manuQuantity,(SELECT sum(quantity) from channel_stock tCS WHERE MACHINE_ID='DM1' and drawer_type=1 and quantity>0 and tCS.drug_id=bCS.drug_id) totalQuantity + FROM channel_stock bCS + WHERE MACHINE_ID='DM1' and drawer_type=1 and quantity>0 GROUP BY drug_id,manu_no,eff_date"; + DataSet dsQuantity = SqlHelper.ExecuteQuery(sql); + if (dsQuantity != null && dsQuantity.Tables[0] != null && dsQuantity.Tables[0].Rows.Count > 0) + { + for (int i = 0; i < dsQuantity.Tables[0].Rows.Count; i++) + { + + + string insertSpecial = $@"INSERT INTO account_book_g2(DRUG_ID,TYPE,DEPARTMENT,INVOICE_NO,MANU_NO,eff_date,add_quantity,out_quantity,manu_stock,total_stock,machine_id,create_date) + values('{dsQuantity.Tables[0].Rows[i]["drug_id"].ToString()}',3,'急诊药房','日结','{dsQuantity.Tables[0].Rows[i]["manu_no"].ToString()}', + '{dsQuantity.Tables[0].Rows[i]["eff_date"].ToString()}',0,0,'{dsQuantity.Tables[0].Rows[i]["manuQuantity"].ToString()}','{dsQuantity.Tables[0].Rows[i]["totalQuantity"].ToString()}','{dsQuantity.Tables[0].Rows[i]["machine_id"].ToString()}','{DateTime.Now.ToString("yyyy-MM-dd")}')"; + cmd.CommandText = insertSpecial; + cmd.ExecuteNonQuery(); + } + + } + transaction.Commit(); + Console.WriteLine($"{DateTime.Now}生成账册成功"); + } + } + catch (Exception ex) + { + transaction.Rollback(); + Console.WriteLine($"{DateTime.Now}生账册失败:{ex.Message}"); + } + } + } + catch (Exception ex) + { + + Console.WriteLine($"{DateTime.Now}专用账册生成失败:{ex.Message}"); + } + + + + + } + + /// + /// 生成日结存及总结存数据 + /// + public static void CreateMethodForSSS() + { + try + { + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + if (conn.State != ConnectionState.Open) + conn.Open(); + MySqlTransaction transaction = conn.BeginTransaction(); + try + { + using (MySqlCommand cmd = conn.CreateCommand()) + { + cmd.Connection = conn; + cmd.Transaction = transaction; + //1)查询库存,为每一个批次的药品生成日结存数据 + string sql = $@"SELECT machine_id,drug_id,manu_no,eff_date,IF(sum(quantity) IS null,0,sum(quantity)) manuQuantity,(SELECT IF(sum(quantity)is NULL,0,sum(quantity)) from channel_stock tCS WHERE MACHINE_ID in('DM3','DM5') and drawer_type=1 and quantity>0 and tCS.drug_id=bCS.drug_id) totalQuantity + FROM channel_stock bCS + WHERE MACHINE_ID in('DM1','DM5') and drawer_type=1 and quantity>0 GROUP BY drug_id,manu_no,eff_date"; + DataSet dsQuantity = SqlHelper.ExecuteQuery(sql); + if (dsQuantity != null && dsQuantity.Tables[0] != null && dsQuantity.Tables[0].Rows.Count > 0) + { + for (int i = 0; i < dsQuantity.Tables[0].Rows.Count; i++) + { + + + string insertSpecial = $@"INSERT INTO account_book_g2(DRUG_ID,TYPE,DEPARTMENT,INVOICE_NO,MANU_NO,eff_date,add_quantity,out_quantity,manu_stock,total_stock,machine_id,create_date,create_time) + values('{dsQuantity.Tables[0].Rows[i]["drug_id"].ToString()}',3,'手术室','日结','{dsQuantity.Tables[0].Rows[i]["manu_no"].ToString()}', + '{dsQuantity.Tables[0].Rows[i]["eff_date"].ToString()}',0,0,{dsQuantity.Tables[0].Rows[i]["manuQuantity"].ToString()},{dsQuantity.Tables[0].Rows[i]["totalQuantity"].ToString()},'DM3','{DateTime.Now.ToString("yyyy-MM-dd")}','{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')"; + cmd.CommandText = insertSpecial; + cmd.ExecuteNonQuery(); + } + + } + transaction.Commit(); + Console.WriteLine($"{DateTime.Now}CreateMethodForSSS生成账册成功"); + } + } + catch (Exception ex) + { + transaction.Rollback(); + Console.WriteLine($"{DateTime.Now}CreateMethodForSSS生账册失败:{ex.Message}"); + } + } + } + catch (Exception ex) + { + + Console.WriteLine($"{DateTime.Now} CreateMethodForSSS账册生成失败:{ex.Message}"); + } + + + + + } +} diff --git a/CreateAccountBookConsole/SqlHelper.cs b/CreateAccountBookConsole/SqlHelper.cs new file mode 100644 index 0000000..61c494a --- /dev/null +++ b/CreateAccountBookConsole/SqlHelper.cs @@ -0,0 +1,160 @@ +using MySqlConnector; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CreateAccountBookConsole +{ + public class SqlHelper + { + public static DataSet ExecuteQuery(string sql, params MySqlParameter[] param) + { + DataSet ds = new DataSet(); + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + try + { + conn.Open(); + + using (MySqlCommand cmd = new MySqlCommand(sql, conn)) + { + if (param != null) + cmd.Parameters.AddRange(param); + MySqlDataAdapter da = new MySqlDataAdapter(cmd); + da.Fill(ds); + if (param != null) + cmd.Parameters.Clear(); + return ds; + } + } + catch (Exception ex) + { + throw ex; + } + } + } + + public static int ExecuteScalar(string sql, params MySqlParameter[] param) + { + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + try + { + conn.Open(); + MySqlCommand cmd = new MySqlCommand(sql, conn); + if (param != null) + cmd.Parameters.AddRange(param); + int affectCount = int.Parse(cmd.ExecuteScalar().ToString()); + + if (param != null) + cmd.Parameters.Clear();//添加这一句即可解决此问题 + return affectCount; + } + catch (Exception ex) + { + throw ex; + } + } + } + + /// + /// 执行增、删、改的方法 + /// + /// 预计执行的非SELECT查询语句 + /// SQL语句中的可变参数 + /// 返回受影响的行数 + public static int ExecuteNonQuery(string sql, params MySqlParameter[] param) + { + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + try + { + conn.Open(); + MySqlCommand cmd = new MySqlCommand(sql, conn); + if (param != null) + cmd.Parameters.AddRange(param); + int affectCount = cmd.ExecuteNonQuery(); + if (param != null) + cmd.Parameters.Clear();//添加这一句即可解决此问题 + return affectCount; + } + catch (Exception ex) + { + throw ex; + } + } + } + + public static long ExecuteNonQueryAndGetLastInsertedId(string sql, params MySqlParameter[] param) + { + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + try + { + conn.Open(); + MySqlCommand cmd = new MySqlCommand(sql, conn); + if (param != null) + cmd.Parameters.AddRange(param); + int affectCount = cmd.ExecuteNonQuery(); + if (param != null) + cmd.Parameters.Clear(); + return cmd.LastInsertedId; + } + catch (Exception ex) + { + throw ex; + } + } + } + + + public static bool ExecuteNonQueryList(List sqlList, List paramList) + { + using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString)) + { + try + { + conn.Open(); + MySqlTransaction transaction = conn.BeginTransaction(); + MySqlCommand cmd = conn.CreateCommand(); + cmd.Transaction = transaction; + + try + { + for (int i = 0; i < sqlList.Count; i++) + { + cmd.CommandText = sqlList[i]; + cmd.Parameters.AddRange(paramList[i]); + cmd.ExecuteNonQuery(); + cmd.Parameters.Clear();//添加这一句即可解决此问题 + } + transaction.Commit(); + return true; + } + catch (Exception ex) + { + //logs.Log.WriteLine("错误原因:" + ex.Message); + try + { + //logs.Log.WriteLine("事物回滚"); + transaction.Rollback();//出现错误就回滚... + } + catch (Exception ex1) + { + throw ex1; + } + throw ex; + } + } + catch (Exception ex) + { + throw ex; + } + } + } + } +}