using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace WFormSynchronizeData_SMR { public partial class Form1 : Form { public DBHelper oCN = new DBHelper(); public Form1() { InitializeComponent(); } //读取数据 private void btnReadData_Click(object sender, EventArgs e) { int year = DateTime.Now.Year; string month = DateTime.Now.Month.ToString(); string day = DateTime.Now.Day.ToString(); //if (month.Length < 2) { // month = "0" + month; //} //if (day.Length < 2) //{ // day = "0" + day; //} string dataTime = DateTime.Now.ToString("yyyyMMdd"); //CustomWriteLog("1:" + dataTime, DateTime.Now.ToString("yyyy-MM-dd")); ////判断是否有这个文件 bool flag = File.Exists($@"D:\{year}\{month}\{day}\{dataTime}.txt"); //bool flag = File.Exists($@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123.txt"); //CustomWriteLog("2:" + flag, DateTime.Now.ToString("yyyy-MM-dd")); if (flag) { StreamReader stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}.txt", Encoding.GetEncoding("gb2312")); //CustomWriteLog("3:"+ $@"D:\{year}\{month}\{day}\{dataTime}.txt", DateTime.Now.ToString("yyyy-MM-dd")); //StreamReader stream = new StreamReader($@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123.txt",Encoding.GetEncoding("gb2312")); string FileData = stream.ReadToEnd(); stream.Close(); FileData = FileData.Substring(0, FileData.Length - 1); FileData = "[" + FileData + "]"; //CustomWriteLog("4:" + FileData, DateTime.Now.ToString("yyyy-MM-dd")); //JSON序列化转换字典集合 List> list = new List>(); List DataList = JsonConvert.DeserializeObject>(FileData); foreach (JObject item in DataList) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } //CustomWriteLog("5:" + list.Count, DateTime.Now.ToString("yyyy-MM-dd")); //获取当前时间 DateTime ActionTime = DateTime.Parse(DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")); DateTime EndTime = DateTime.Parse(DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd HH:mm:00")); //循环集合 for (int i = 0; i < list.Count; i++) { DateTime NowTime = DateTime.Parse(list[i]["HDate"].ToString()); if (NowTime >= ActionTime && NowTime < EndTime) { string HResult = list[i]["HResult"].ToString(); //CustomWriteLog("6:" + HResult, DateTime.Now.ToString("yyyy-MM-dd")); if (HResult == "OK") { HResultOK(list[i]); } else if (HResult == "NG") { HResultNG(list[i]); } } else { //CustomWriteLog("时间不在保存范围内", DateTime.Now.ToString("yyyy-MM-dd")); } } } } public void HResultOK(Dictionary dic) { try { string HBadCodeSN = dic["HBadCodeSN"].ToString(); HBadCodeSN = TM_ZH(HBadCodeSN); bool flag = SNHBardCodeList(HBadCodeSN); if (flag) { getAdd(dic, HBadCodeSN, "OK"); } } catch (Exception e) { oCN.RollBack(); //objJsonResult.code = "0"; //objJsonResult.count = 0; //objJsonResult.Message = "没有返回任何记录!" + e.ToString(); //objJsonResult.data = null; //return objJsonResult; } } public void HResultNG(Dictionary dic) { try { string HBadCodeSN = dic["HBadCodeSN"].ToString(); HBadCodeSN = TM_ZH(HBadCodeSN); bool flag = SNHBardCodeList(HBadCodeSN); if (flag) { getAdd(dic, HBadCodeSN, "NG"); //开始事务 oCN.BeginTran(); //反写条码状态 oCN.RunProc("update Gy_BarCodeBill set HStatus='不良',HRemark='"+dic["HBadReason"].ToString()+"' where HBarCode='" + HBadCodeSN + "'"); //结束事务 oCN.Commit(); } } catch (Exception e) { //回滚事务 oCN.RollBack(); //objJsonResult.code = "0"; //objJsonResult.count = 0; //objJsonResult.Message = "没有返回任何记录!" + e.ToString(); //objJsonResult.data = null; //return objJsonResult; } } public bool getAdd(Dictionary dic,string HBadCodeSN,string HResult) { try { //获取绑定流转卡 DataSet ds = oCN.RunProcReturn(@"select * from Gy_BarCodeBill where HBarCode='" + HBadCodeSN + "'", "Gy_BarCodeBill"); string HProcExchBillNo = ds.Tables[0].Rows[0]["HSourceBillNo"].ToString(); string HProcNumber = dic["HProcNumber"].ToString(); ds = oCN.RunProcReturn("select * from Gy_Source where HNumber='" + dic["HSouceNumber"].ToString() + "' ", "Gy_Source"); long HSourceID = 0; if (ds.Tables[0].Rows.Count != 0) { HSourceID = long.Parse(ds.Tables[0].Rows[0]["HItemID"].ToString());//生产资源ID } ds = oCN.RunProcReturn("select * from Gy_Department where HNumber='" + dic["HDeptNumber"].ToString() + "' ", "Gy_Department"); long HDeptID = 0; if (ds.Tables[0].Rows.Count != 0) { HDeptID = long.Parse(ds.Tables[0].Rows[0]["HItemID"].ToString());//生产部门 } ds = oCN.RunProcReturn(@"select b.HProcNo,b.HProcID,a.HMaterID,a.HQty,a.HInterID,HEntryID,a.HICMOInterID,a.HICMOEntryID,a.HICMOBillNo from Sc_ProcessExchangeBillMain a inner join Sc_ProcessExchangeBillSub b on a.HInterID=b.HInterID left join Gy_Process p on b.HProcID=p.HItemID where a.HBillNo='" + HProcExchBillNo + "' and p.HNumber='" + HProcNumber + "'", "Sc_ProcessExchangeBillMain"); string sExeReturnInfo = ""; long HMainInterID = 0; string BillType = "3791"; string HBillSubType = "3791"; int HBillStatus = 1; string HMaker = "";//制单人 string HMouldNum = "";//模穴号 int HYear = DateTime.Now.Year; double HPeriod = DateTime.Now.Month; string HRemark = "";//备注 string HSourceName = "";//生产资源名称 double HPieceQty = double.Parse(ds.Tables[0].Rows[0]["HQty"].ToString());//进站PCS数 double HWasterQty = 0;//报废数量 double HPlanPieceQty = 0;//进站PNL数 double HBadPNL = 0;//报废PNL数 long HICMOInterID = long.Parse(ds.Tables[0].Rows[0]["HICMOInterID"].ToString());//任务单ID long HICMOEntryID = long.Parse(ds.Tables[0].Rows[0]["HICMOEntryID"].ToString());//任务单ID string HICMOBillNo = ds.Tables[0].Rows[0]["HICMOBillNo"].ToString();//任务单 int HProcPlanInterID = 0; int HProcPlanEntryID = 0; string HProcPlanBillNo = ""; long HProcExchInterID = long.Parse(ds.Tables[0].Rows[0]["HInterID"].ToString()); long HProcExchEntryID = long.Parse(ds.Tables[0].Rows[0]["HEntryID"].ToString()); long HMaterID = long.Parse(ds.Tables[0].Rows[0]["HMaterID"].ToString());//产品ID long HProcID = long.Parse(ds.Tables[0].Rows[0]["HProcID"].ToString());//当前工序ID double HICMOQty = double.Parse(ds.Tables[0].Rows[0]["HQty"].ToString());//任务单数量 double HPlanQty = double.Parse(ds.Tables[0].Rows[0]["HQty"].ToString());//移交PNL数 DateTime HStationOutTime = DateTime.Now;//汇报时间 long HPayProcID = 0;//核算工序ID long HGroupID = 22;//班组ID 白班 long HEmpID = 0;//操作员ID long HEmpID2 = 0;////操作员2ID string HBarCode = HProcExchBillNo;//条形码 string HAddr = ""; string HBarCodeMaker = ""; long HSourceID2 = 0;//生产资源2ID long HSourceID3 = 0;//生产资源3ID long HSourceID4 = 0;//生产资源4ID long HSourceID5 = 0;//生产资源5ID long HSupID = 0; double HQty = 0;//合格数量 double HPrice = 0; double HMoney = 0; double HBadCount = 0;//不良数量 long HCenterID = 0;//工作中心ID string HProcNo = ds.Tables[0].Rows[0]["HProcNo"].ToString();//流水号 string HOrderProcNO = "";//订单跟踪号 string HSourceNameList = "";//设备清单 string HMainSourceBillType = "3790"; bool HLastSubProc = false;//转下工序 long HEmpID3 = 0;//操作员3ID long HEmpID4 = 0;//操作员4ID long HEmpID5 = 0;//操作员5ID double HDSQty = 0;//折弯刀数 double HChongQty = 0;//NCT冲次数 double HPriceRate = 0;//系数 double HWorkTimes = 0;//工时 long HQCCheckID = 0;//检验员ID long HPRDOrgID = 100199;//组织ID double HmaterOutqty = 0;//白坯发布 double HProcPriceRate = 0;//工价系数 int HTemporaryAreaID = 0;//暂放区 long HInterID = 0; string HBillNo = ""; oCN.BeginTran(); //判断当前流转卡的出站单 是否有数据 ds = oCN.RunProcReturn(@"select * from Sc_StationOutBillMain a left join Gy_Process p on a.HProcID=p.HItemID where HProcExchBillNo='" + HProcExchBillNo + "' and p.HNumber='" + HProcNumber + "'", "Sc_StationOutBillMain"); if (ds.Tables[0].Rows.Count != 0) { HInterID = long.Parse(ds.Tables[0].Rows[0]["HInterID"].ToString()); HBillNo = ds.Tables[0].Rows[0]["HBillNo"].ToString();//递入type得到的单据号 } if (ds.Tables[0].Rows.Count == 0) { HInterID = CreateBillID("3791", ref sExeReturnInfo);//递入type得到的单据ID HBillNo = CreateBillCode_Prod("3791", ref sExeReturnInfo, true);//递入type得到的单据号 long HMainSourceInterID = HInterID;//递入type得到的单据ID string HMainSourceBillNo = HBillNo;//递入type得到的单据号 oCN.RunProc("Insert Into Sc_StationOutBillMain " + "(HBillType,HBillSubType,HInterID,HBillNo,HBillStatus,HDate,HMaker,HMakeDate,HMouldNum" + ",HYear,HPeriod,HRemark,HSourceName,HPieceQty,HWasterQty,HPlanPieceQty,HBadPNL" + ",HICMOInterID,HICMOBillNo,HProcPlanInterID,HProcPlanEntryID,HProcPlanBillNo,HProcExchInterID,HProcExchEntryID" + ",HProcExchBillNo,HMaterID,HProcID,HICMOQty,HPlanQty,HStationOutTime,HSourceID,HPayProcID" + ",HGroupID,HDeptID,HEmpID,HBarCode,HAddr,HBarCodeMaker,HBarCodeMakeDate,HSourceID2,HSourceID3,HSourceID4,HSourceID5" + ",HSupID,HQty,HPrice,HMoney,HBadCount,HCenterID,HProcNo,HOrderProcNO,HSourceNameList" + ",HMainSourceInterID,HMainSourceBillNo,HMainSourceBillType,HLastSubProc" + ",HEmpID2,HEmpID3,HEmpID4,HEmpID5,HDSQty,HChongQty,HPriceRate,HWorkTimes,HQCCheckID,HMainInterID,HPRDOrgID" + ",HmaterOutqty,HProcPriceRate,HTemporaryAreaID" + ") " + " values('" + BillType + "','" + HBillSubType + "'," + HInterID + ",'" + HBillNo + "'," + HBillStatus + ",getdate(),'" + HMaker + "',getdate(),'" + HMouldNum + "'" + "," + HYear + "," + HPeriod + ",'" + HRemark + "','" + HSourceName + "'," + HPieceQty + "," + HWasterQty + "," + HPlanPieceQty + "," + HBadPNL + "," + HICMOInterID + ",'" + HICMOBillNo + "'," + HProcPlanInterID + "," + HProcPlanEntryID + ",'" + HProcPlanBillNo + "'," + HProcExchInterID + "," + HProcExchEntryID + ",'" + HProcExchBillNo + "'," + HMaterID + "," + HProcID + "," + HICMOQty + "," + HPlanQty + ",getdate()," + HSourceID + "," + HPayProcID + "," + HGroupID + "," + HDeptID + "," + HEmpID + ",'" + HBarCode + "','" + HAddr + "','" + HBarCodeMaker + "',getdate()" + "," + HSourceID2 + "," + HSourceID3 + "," + HSourceID4 + "," + HSourceID5 + "," + HSupID + "," + HQty + "," + HPrice + "," + HMoney + "," + HBadCount + "," + HCenterID + "," + HProcNo + ",'" + HOrderProcNO + "'" + ",'" + HSourceNameList + "'" + "," + HMainSourceInterID + ",'" + HMainSourceBillNo + "','" + HMainSourceBillType + "'," + Convert.ToString(HLastSubProc ? 1 : 0) + "," + HEmpID2 + "," + HEmpID3 + "," + HEmpID4 + "," + HEmpID5 + "," + HDSQty + "," + HChongQty + "," + HPriceRate + "," + HWorkTimes + "," + HQCCheckID + "," + HMainInterID + "," + HPRDOrgID + "," + HmaterOutqty + "," + HProcPriceRate + "," + HTemporaryAreaID + ") "); } if (HResult == "OK") { ds = oCN.RunProcReturn("select * from Sc_StationOutBillSub_SN where HInterID='" + HInterID + "' order by HEntryID desc", "Sc_StationOutBillSub_SN"); oCN.RunProc($@"insert into Sc_StationOutBillSub_SN(HInterID,HBillNo_bak,HEntryID,HBarCode,HBarCodeQty,HMakeTime,HRemark,HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney) values({ HInterID}, '{ HBillNo}', {(ds.Tables[0].Rows.Count == 0 ? 1 : int.Parse(ds.Tables[0].Rows[0]["HEntryID"].ToString()) + 1)}, '{HBadCodeSN}', 1, GETDATE(), '', " + HProcExchInterID + "," + HProcExchEntryID + ", '" + HProcExchBillNo + "', '', 0, 0)"); //反写工序出站单的合格数量 oCN.RunProc("update Sc_StationOutBillMain set HQty+=1 where HProcExchInterID='" + HProcExchInterID + "' and HProcExchEntryID=" + HProcExchEntryID); } else if (HResult == "NG") { //反写工序出站单的不良数量 oCN.RunProc("update Sc_StationOutBillMain set HBadCount+=1 where HProcExchInterID='" + HProcExchInterID + "' and HProcExchEntryID=" + HProcExchEntryID); } else { CustomWriteLog("结果不为OK或NG!", DateTime.Now.ToString("yyyy-MM-dd")); return false; } oCN.Commit(); return true; } catch (Exception e) { CustomWriteLog(e.Message, DateTime.Now.ToString("yyyy-MM-dd")); return false; } } //条码解析 public bool SNHBardCodeList(string HBadCodeSN) { try { DataSet ds = oCN.RunProcReturn(@"select * from Gy_BarCodeBill where HBarCode='" + HBadCodeSN + "'", "Gy_BarCodeBill"); //判断条码是否在数据库里 if (ds.Tables[0].Rows.Count == 0) { //判断长度是否为29位 if (HBadCodeSN.Length == 29) { string str1 = HBadCodeSN.Substring(23, 3); ds = oCN.RunProcReturn("select * from Sc_ProcessExchangeBillMain where HProjectNum like'" + str1 + "-1%'", "Sc_ProcessExchangeBillMain"); //判断是否能找到对应的流转卡 if (ds.Tables[0].Rows.Count > 0) { string HProcExchBillNo = ds.Tables[0].Rows[0]["HBillNo"].ToString(); get_HBardBillSave(HBadCodeSN, HProcExchBillNo); } else { CustomWriteLog("条码:"+ HBadCodeSN + ",流转卡不存在!", DateTime.Now.ToString("yyyy-MM-dd")); return false; } } else { CustomWriteLog("条码:" + HBadCodeSN + ",长度不等于29位!", DateTime.Now.ToString("yyyy-MM-dd")); return false; } } else { CustomWriteLog("条码:" + HBadCodeSN + ",条码已存在数据库!", DateTime.Now.ToString("yyyy-MM-dd")); return false; } return true; } catch (Exception e) { CustomWriteLog("条码:" + HBadCodeSN + "," + e.Message, DateTime.Now.ToString("yyyy-MM-dd")); return false; } } //保存条码数据 public void get_HBardBillSave(string HBarCode, string HProcExchBillNo) { try { //查询流转卡数据 DataSet dataSet = oCN.RunProcReturn(@"select a.HPRDORGID, m.HModel 物料规格,m.HName 名物料称,a.HBillNo,a.HInterID,a.HMaterID,m.HNumber 物料代码,o.HNumber 组织代码,a.HUnitID,u.HNumber 单位代码 from Sc_ProcessExchangeBillMain a left join Gy_Material m on a.HMaterID=m.HItemID left join Xt_ORGANIZATIONS o on a.HPRDORGID=o.HItemID left join Gy_Unit u on a.HUnitID=u.HItemID where HBillNo='" + HProcExchBillNo + "'", "Sc_ProcessExchangeBillMain"); string HMaterNumber = dataSet.Tables[0].Rows[0]["物料代码"].ToString(); string HOrgNumber = dataSet.Tables[0].Rows[0]["组织代码"].ToString(); //判断流转卡不能为空 if (HBarCode.Trim() == "") { CustomWriteLog("条码:" + HBarCode + ",流转卡不能为空!", DateTime.Now.ToString("yyyy-MM-dd")); //objJsonResult.code = "0"; //objJsonResult.count = 0; //objJsonResult.Message = "条形码不能为空,不能生成条码!"; //return objJsonResult; } oCN.BeginTran(); //日期获取方式 string sDate = DateTime.Now.ToString(); string HWei = "0"; //尾数 string HBarCodeType = "唯一条码"; Int64 HMaterID = int.Parse(dataSet.Tables[0].Rows[0]["HMaterID"].ToString()); Int64 HAuxPropID = 0; Int64 HUnitID = int.Parse(dataSet.Tables[0].Rows[0]["HUnitID"].ToString()); double HQty2 = 1; string HBatchNo2 = ""; Int64 HSupID = 0; Int64 HGroupID = 0; int HPrintQty = 0; Int64 HSourceInterID = int.Parse(dataSet.Tables[0].Rows[0]["HInterID"].ToString()); Int64 HSourceEntryID = 1; string HSourceBillNo = dataSet.Tables[0].Rows[0]["HBillNo"].ToString(); string HSourceBillType = "3772"; Int64 HBarcodeNo = 0; //托号 Int64 HBarcodeQtys = 0; //总托数 Int64 HDeptID = 0; Int64 HWhID = 0; Int64 HSPID = 0; string HRemark = ""; string HMaterName = dataSet.Tables[0].Rows[0]["名物料称"].ToString(); string HMaterModel = dataSet.Tables[0].Rows[0]["物料规格"].ToString(); string HPinfan = ""; string HMTONo = ""; Int64 HCusID = 0; string HCusType = ""; DateTime HEndDate = DateTime.Now; string HWorkLineName = ""; string HSeOrderBillNo = ""; string HInnerBillNo = ""; bool HGiveAwayFlag = false; Int64 HEntryID = 1; string sExeReturnInfo = ""; Int64 HInterID = CreateBillID_Prod("85", ref sExeReturnInfo); int HOrgID = int.Parse(dataSet.Tables[0].Rows[0]["HPRDORGID"].ToString()); string HCoilNO2 = ""; string HFurnaceNO2 = ""; string HFactory2 = ""; decimal HAuxQty2 = 0; string HheatNO2 = ""; oCN.RunProc("insert into Gy_BarCodeBill (HBarCode,HBarCodeType,HMaterID,HUnitID,HQty" + ",HBatchNo,HSupID,HGroupID,HMaker,HMakeDate,HPrintQty,HinitQty" + ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HEndQty " + ",HBarcodeQtys,HBarcodeNo,HDeptID,HWhID,HSPID,HRemark " + ",HCusID,HCusType,HEndDate,HWorkLineName,HBarCodeDate " + ",HSTOCKORGID,HOWNERID,HSeOrderBillNo,HInterID,HEntryID " + ",HGiveAwayFlag " + ",HMaterName,HMaterModel,HPinfan,HAuxPropID,HMTONo,HInnerBillNo" + ",HProduceDate,HExpiryDate " + ") values (" + "'" + HBarCode + "','" + HBarCodeType + "'," + HMaterID.ToString() + "," + HUnitID.ToString() + "," + HQty2.ToString() + ",'" + HBatchNo2 + "'," + HSupID.ToString() + "," + HGroupID.ToString() + ",'',getdate()," + HPrintQty.ToString() + "," + HQty2.ToString() + ", " + HSourceInterID.ToString() + "," + HSourceEntryID.ToString() + ",'" + HSourceBillNo + "','" + HSourceBillType + "','" + HWei + "'" + ", " + HBarcodeQtys.ToString() + "," + HBarcodeNo.ToString() + "," + HDeptID.ToString() + "," + HWhID.ToString() + "," + HSPID.ToString() + ",'" + HRemark + "'" + ", " + HCusID.ToString() + ",'" + HCusType + "','" + HEndDate.ToShortDateString() + "','" + HWorkLineName + "','" + sDate + "'" + ", " + HOrgID.ToString() + "," + HOrgID.ToString() + ",'" + HSeOrderBillNo + "'," + HInterID.ToString() + "," + HEntryID.ToString() + "" + ", " + BoolToString(HGiveAwayFlag) + ",'" + HMaterName + "','" + HMaterModel + "','" + HPinfan + "'," + HAuxPropID.ToString() + ",'" + HMTONo + "','" + HInnerBillNo + "','','')"); oCN.Commit(); } catch (Exception e) { CustomWriteLog("条码:" + HBarCode + "," + e.Message, DateTime.Now.ToString("yyyy-MM-dd")); oCN.RollBack(); } } public static Int64 CreateBillID_Prod(string BillCode, ref string sReturn) { try { string BillType = ""; DataSet Ds; DBHelper oCn = new DBHelper(); Int64 lID; Ds = oCn.RunProcReturn("exec h_p_Xt_GetMaxBillID '" + BillCode + "'", "h_p_Xt_GetMaxBillID"); if (Ds == null || Ds.Tables[0].Rows.Count == 0) { lID = -1; } else { lID = isLong(Ds.Tables[0].Rows[0]["HBillNo"]); } oCn.CnClose(); oCn.CnDispose(); oCn = null; Ds = null; return lID; } catch (Exception e) { return -2; } } //条码补充 public string TM_ZH(string HBadCodeSN) { try { //HBadCodeSN = "P1" + HBadCodeSN; //HBadCodeSN = HBadCodeSN.Substring(0, 13) + ":" + HBadCodeSN.Substring(13, 15); return HBadCodeSN; } catch (Exception e) { CustomWriteLog("条码:" + HBadCodeSN + "," + e.Message, DateTime.Now.ToString("yyyy-MM-dd")); return ""; } } //判断是否是INT64 public static Int64 isLong(object message) { try { return Convert.ToInt64(message); } catch (Exception e) { return 0; } } //布尔转为字符 public static string BoolToString(object b) { try { if (Convert.ToBoolean(b)) return "1"; else return "0"; } catch (Exception e) { return "0"; } } public static Int64 CreateBillID(string BillCode, ref string sReturn) { string BillType = ""; DataSet Ds; DBHelper oCn = new DBHelper(); Int64 lID; Ds = oCn.RunProcReturn("select * from Gy_BillNumber with (nolock) where BillCode='" + BillCode.Trim() + "'", "Gy_BillNumber"); if (Ds.Tables[0].Rows.Count != 0) { lID = isLong(Ds.Tables[0].Rows[0]["IDNow"].ToString()); BillType = Ds.Tables[0].Rows[0]["BillType"].ToString().Trim(); } else { lID = 0; } //同类型单据 自增1 if (BillType.Trim() != "") { oCn.RunProc("update Gy_BillNumber set IDNow=IDNow+1 where BillType='" + BillType.Trim() + "'"); } oCn.CnClose(); oCn.CnDispose(); oCn = null; Ds = null; return lID; } //得到最大单据号 //得到最大单据号 存储过程 public static string CreateBillCode_Prod(string BillCode, ref string sReturn, bool Add) { try { string BillType = ""; DataSet Ds; DBHelper oCn = new DBHelper(); string sBIllNO; Ds = oCn.RunProcReturn("exec h_p_Xt_GetMaxBillNo '" + BillCode + "'", "h_p_Xt_GetMaxBillNo"); if (Ds == null || Ds.Tables[0].Rows.Count == 0) { sBIllNO = "ERROR"; } else { sBIllNO = isStrNull(Ds.Tables[0].Rows[0]["HBillNo"]); } oCn.CnClose(); oCn.CnDispose(); oCn = null; Ds = null; return sBIllNO; } catch (Exception e) { return "ERROR"; } } //判断是否NULL public static string isStrNull(object message) { try { if (message == null) { return ""; } else { return message.ToString().Trim(); } } catch (Exception e) { return ""; } } private static readonly object lockObj = new object(); //写日志 public static void CustomWriteLog(object obj, string FileName, string filePath = "Vlog", bool isAppend = true) { try { lock (lockObj) { filePath = $@"{filePath}\{FileName}.txt"; filePath = AppDomain.CurrentDomain.BaseDirectory + filePath; if (!System.IO.Directory.Exists(Path.GetDirectoryName(filePath))) { System.IO.Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } bool fileExists = System.IO.File.Exists(filePath); //不存在 则创建该文件 if (!fileExists) { System.IO.File.Create(filePath).Close(); } using (StreamWriter writer = new StreamWriter(filePath, isAppend)) { //存在的时候才写一行 if (fileExists && isAppend) { writer.WriteLine(); } var content = obj is string ? obj : JsonConvert.SerializeObject(obj); writer.WriteLine($"{DateTime.Now} {content}"); } } } catch (Exception ex) { } } //定时读取数据 private void timer1_Tick(object sender, EventArgs e) { int year = DateTime.Now.Year; string month = DateTime.Now.Month.ToString(); string day = DateTime.Now.Day.ToString(); //if (month.Length < 2) { // month = "0" + month; //} //if (day.Length < 2) //{ // day = "0" + day; //} string dataTime = DateTime.Now.ToString("yyyyMMdd"); //CustomWriteLog("1:" + dataTime, DateTime.Now.ToString("yyyy-MM-dd")); ////判断是否有这个文件 bool flag = File.Exists($@"D:\{year}\{month}\{day}\{dataTime}.txt"); //bool flag = File.Exists($@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123.txt"); //CustomWriteLog("2:" + flag, DateTime.Now.ToString("yyyy-MM-dd")); if (flag) { StreamReader stream = new StreamReader($@"D:\{year}\{month}\{day}\{dataTime}.txt", Encoding.GetEncoding("gb2312")); //CustomWriteLog("3:"+ $@"D:\{year}\{month}\{day}\{dataTime}.txt", DateTime.Now.ToString("yyyy-MM-dd")); //StreamReader stream = new StreamReader($@"C:\Users\admin\Desktop\新建文件夹 (2)\20231123.txt",Encoding.GetEncoding("gb2312")); string FileData = stream.ReadToEnd(); stream.Close(); FileData = FileData.Substring(0, FileData.Length - 1); FileData = "[" + FileData + "]"; //CustomWriteLog("4:" + FileData, DateTime.Now.ToString("yyyy-MM-dd")); //JSON序列化转换字典集合 List> list = new List>(); List DataList = JsonConvert.DeserializeObject>(FileData); foreach (JObject item in DataList) { Dictionary dic = new Dictionary(); foreach (var itm in item.Properties()) { dic.Add(itm.Name, itm.Value.ToString()); } list.Add(dic); } //CustomWriteLog("5:" + list.Count, DateTime.Now.ToString("yyyy-MM-dd")); //获取当前时间 DateTime ActionTime = DateTime.Parse(DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm:ss")); DateTime EndTime = DateTime.Parse(DateTime.Now.AddMinutes(1).ToString("yyyy-MM-dd HH:mm:00")); //循环集合 for (int i = 0; i < list.Count; i++) { DateTime NowTime = DateTime.Parse(list[i]["HDate"].ToString()); if (NowTime >= ActionTime && NowTime < EndTime) { string HResult = list[i]["HResult"].ToString(); //CustomWriteLog("6:" + HResult, DateTime.Now.ToString("yyyy-MM-dd")); if (HResult == "OK") { HResultOK(list[i]); } else if (HResult == "NG") { HResultNG(list[i]); } } else { //CustomWriteLog("时间不在保存范围内", DateTime.Now.ToString("yyyy-MM-dd")); } } } } } }