160 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			160 lines
		
	
	
		
			8.0 KiB
		
	
	
	
		
			C#
		
	
	
	
// 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($"定时系统已启动,启动时间{DateTime.Now}");
 | 
						||
//湘潭急诊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, 01));
 | 
						||
//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)查询库存,为每一个批次的药品生成日结存数据
 | 
						||
                        string sql = $@"SELECT machine_id,drug_id,manu_no,eff_date,IF(sum(quantity) IS null,0,sum(quantity)) manuQuantity
 | 
						||
                                        FROM channel_stock bCS
 | 
						||
                                        WHERE ((MACHINE_ID='DM3' and drawer_type=1) OR machine_id='DM5') AND   quantity>0 GROUP BY drug_id,manu_no";
 | 
						||
                        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 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());
 | 
						||
                                }
 | 
						||
                                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()},{emptyQuantity},'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}");
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |