2025-07-28 15:24:19 +08:00
// 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()
//);
2025-09-24 14:00:38 +08:00
Console . WriteLine ( $"定时系统已启动,启动时间{DateTime.Now}" ) ;
2025-07-28 15:24:19 +08:00
//湘潭急诊DM1设备每天凌晨生成当日结存数据及总结存数据
2025-10-10 16:18:40 +08:00
JobManager . AddJob ( ( ) = > CreateClass . CreateMethod ( ) , s = > s . ToRunEvery ( 1 ) . Days ( ) . At ( 00 , 15 ) ) ;
2025-07-28 15:24:19 +08:00
//湘潭手术室DM3设备每天凌晨生成当日结存数据
2025-10-10 16:18:40 +08:00
JobManager . AddJob ( ( ) = > CreateClass . CreateMethodForSSS ( ) , s = > s . ToRunEvery ( 1 ) . Days ( ) . At ( 00 , 20 ) ) ;
2025-07-28 15:24:19 +08:00
//JobManager.JobStart += info =>
//{
// Console.WriteLine($"{DateTime.Now}定时任务开始执行");
//};
//JobManager.JobEnd += info =>
//{
// Console.WriteLine($"{DateTime.Now}定时任务执行结束");
//};
//CreateClass.CreateMethodForSSS();
Console . ReadLine ( ) ;
public class CreateClass
{
/// <summary>
/// 生成日结存及总结存数据
/// </summary>
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}" ) ;
}
}
/// <summary>
/// 生成日结存及总结存数据
/// </summary>
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)查询库存,为每一个批次的药品生成日结存数据
2025-09-24 14:00:38 +08:00
string sql = $ @ "SELECT machine_id,drug_id,manu_no,eff_date,IF(sum(quantity) IS null,0,sum(quantity)) manuQuantity
2025-07-28 15:24:19 +08:00
FROM channel_stock bCS
2025-09-24 14:00:38 +08:00
WHERE ( ( MACHINE_ID = ' DM3 ' and drawer_type = 1 ) OR machine_id = ' DM5 ' ) AND quantity > 0 GROUP BY drug_id , manu_no ";
2025-07-28 15:24:19 +08:00
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 + + )
{
2025-09-24 14:00:38 +08:00
//查询上一条账册中的空瓶数
string emptySql = $ @ "SELECT IFNULL(total_stock,0) total_stock FROM account_book_g2 WHERE MACHINE_ID='DM3' AND (type=1 OR type=2)
AND drug_id = ' { dsQuantity . Tables [ 0 ] . Rows [ i ] [ "drug_id" ] . ToString ( ) } '
and manu_no = ' { dsQuantity . Tables [ 0 ] . Rows [ i ] [ "manu_no" ] . ToString ( ) } ' order by id desc limit 1 ";
int emptyQuantity = 0 ;
DataSet dsEmptyQuantity = SqlHelper . ExecuteQuery ( emptySql ) ;
if ( dsEmptyQuantity ! = null & & dsEmptyQuantity . Tables [ 0 ] ! = null & & dsEmptyQuantity . Tables [ 0 ] . Rows . Count > 0 )
{
emptyQuantity = Convert . ToInt32 ( dsEmptyQuantity . Tables [ 0 ] . Rows [ 0 ] [ "total_stock" ] . ToString ( ) ) ;
}
2025-07-28 15:24:19 +08:00
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 ( ) } ' ,
2025-09-24 14:00:38 +08:00
' { dsQuantity . Tables [ 0 ] . Rows [ i ] [ "eff_date" ] . ToString ( ) } ' , 0 , 0 , { dsQuantity . Tables [ 0 ] . Rows [ i ] [ "manuQuantity" ] . ToString ( ) } , { emptyQuantity } , ' DM3 ',' { DateTime . Now . ToString ( "yyyy-MM-dd" ) } ',' { DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) } ' ) ";
2025-07-28 15:24:19 +08:00
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}" ) ;
}
}
}