using DSWeb.Common.DB; using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.Entity.Migrations; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Text; namespace JobCreateFee { public class JobYtD7Fee : IJob { private ILog log = LogManager.GetLogger(typeof(JobYtD7Fee)); //private const string CfgFileName = "starttime.cfg"; //private static string CfgFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, CfgFileName); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string reqUrl = context.JobDetail.JobDataMap.GetString("YTReqUrl"); string YTUserName = context.JobDetail.JobDataMap.GetString("YTUserName"); string YTPassWord = context.JobDetail.JobDataMap.GetString("YTPassWord"); string CustName = context.JobDetail.JobDataMap.GetString("CustName"); string CustPsw = context.JobDetail.JobDataMap.GetString("CustPsw"); int reqTimeout = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("RequestTimeout")); string YTD7Condition = context.JobDetail.JobDataMap.GetString("YTD7Condition"); string YTD7NoUseCORPID = context.JobDetail.JobDataMap.GetString("YTD7NoUseCORPID"); YTFEEHelper.feeName = context.JobDetail.JobDataMap.GetString("feeName"); YTFEEHelper.ydFeeName = context.JobDetail.JobDataMap.GetString("ydFeeName"); YTFEEHelper.secondPrintFeeName = context.JobDetail.JobDataMap.GetString("secondPrintFeeName"); YTFEEHelper.feeCustName = context.JobDetail.JobDataMap.GetString("feeCustName"); YTFEEHelper.CarrierCustName = context.JobDetail.JobDataMap.GetString("CarrierCustName"); YTFEEHelper.connStr = connStr; YTFEEHelper.告警邮箱 = context.JobDetail.JobDataMap.GetString("AlertMail"); int DAYS = Convert.ToInt32(context.JobDetail.JobDataMap.GetString("DAYS")); string ENTEROPERATOR = context.JobDetail.JobDataMap.GetString("ENTEROPERATOR"); var yesterday = DateTime.Now.AddDays(DAYS).ToString("yyyy-MM-dd"); JsonYtSend reqObj = new JsonYtSend(); reqObj.custname = CustName; reqObj.custpsw = CustPsw; reqObj.username = YTUserName; reqObj.password = YTPassWord; reqObj.start_time = yesterday; reqObj.end_time = yesterday; var sendstr = JsonConvert.SerializeObject(reqObj); log.Debug($"发送包:{sendstr}"); //发送请求数据 string rtn = WebRequestHelper.DoPost(reqUrl, sendstr, reqTimeout * 1000); //var testrtnobject = new josnYtData(); //testrtnobject.status = "1"; //var testfee = new JsonYtFee(); //testfee.applicant_code = "0005559535"; //testfee.barcode = "1079136935036731"; //testfee.bookingno = "I240369027"; //testfee.ctn_no = "CGMU5629072"; //testfee.ctn_type = "40RH"; //testfee.fee = "1.0"; //testfee.operatetime = "2024-03-19 16:02:28"; //testfee.operatetype = "打印通知单"; //testfee.printType = "纸面"; //testfee.trans_port_code = "PECLL"; //testfee.vessel = "CMA CGM IGUACU"; //testfee.voyage = "0MHIME"; //testfee.yard = "青岛港联欣国际物流有限公司"; //testrtnobject.message.Add(testfee); //var rtn = JsonConvert.SerializeObject(testrtnobject); log.Debug($"数据返回:{rtn}"); //var objRtn = JsonConvert.DeserializeAnonymousType(rtn, new { status = "", message = "" }); var objRtn = JsonConvert.DeserializeObject(rtn); if (objRtn.status == "1") { //var parseJsonObj = JsonConvert.DeserializeObject>(objRtn.message); try { //1 记录进t_op_ytfee_log 表 //2 遍历信息,将同提单号的费用合起来 //3 遍历提单号,寻找业务,判断业务能否增加费用 //3.1 业务是否已退仓 //3.2 业务是否费用锁定 //3.3 业务会计期间是否已锁帐 //以上情况分别发送邮件提醒 //4 如能增加费用 则插入费用 using (SqlConnection dbcon = new SqlConnection(connStr)) { var hp = new YTFEEHelper(); var FEELIST = new List(); dbcon.Open(); foreach (var returndata in objRtn.message) { //测试数据 //if (returndata.bookingno != "QGD0721089") continue; if (returndata.fee == "" || returndata.fee == null) returndata.fee = "0"; if (returndata.ydFee == "" || returndata.ydFee == null) returndata.ydFee = "0"; if (returndata.secondPrintFee == "" || returndata.secondPrintFee == null) returndata.secondPrintFee = "0"; var D7BS = GETD7OPSEAEBS(returndata.bookingno, YTD7Condition, dbcon); string sql = " INSERT INTO t_op_ytfee_log([bsno],[vessel],[voyage],[bookingno],[ctn_type],[ctn_no],[yard],[operatetime],[operatetype]"; sql = sql + ",[printType],[fee],[ydFee],[secondPrintFee],[applicant_code]) values ('" + D7BS.BSNO + "','" + returndata.vessel + "'"; sql = sql + ",'" + returndata.voyage + "','" + returndata.bookingno + "','" + returndata.ctn_type + "','" + returndata.ctn_no + "','" + returndata.yard + "'"; sql = sql + ",'" + returndata.operatetime + "','" + returndata.operatetype + "','" + returndata.printType + "','" + returndata.fee + "','" + returndata.ydFee + "','" + returndata.secondPrintFee + "','" + returndata.applicant_code + "')"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); D7BS.ENTEROPERATOR = ENTEROPERATOR; hp.addYTFee(returndata, D7BS, YTD7NoUseCORPID); } //hp.makefee(); hp.SendMail(); dbcon.Close(); hp.makefee_应付(); } } catch (Exception e) { log.Debug($"记录插入错误:{e.Message}"); } } else { log.Debug($"数据返回错误:{objRtn.message}"); } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } public class MAILINFO { public SqlConnection dbcon { get; set; } public string sendto { get; set; } public string title { get; set; } public string body { get; set; } public string smtpid { get; set; } } public static int SaveMail(MAILINFO mailinfo) { return SaveMail(mailinfo.dbcon, mailinfo.sendto, mailinfo.title, mailinfo.body, mailinfo.smtpid); } public static int SaveMail(SqlConnection dbcon, string sendto, string title, string body, string smtpid) { var sqlstr = $" INSERT INTO [Mail_Send] ([GID],[SendTo],[Title],[Body],[SendStatus],[TryCount],[CreateTime],[SmtpConfig],[Sender],[ShowName]) select newid(),'{sendto}','{title}','{body.Replace("'", "''")}','Create',0,GETDATE(),'{smtpid}','','' "; SqlCommand cmd = new SqlCommand(sqlstr, dbcon); return cmd.ExecuteNonQuery(); } static public D7BS GETD7OPSEAEBS(string MBLNO,string YTD7Condition, SqlConnection dbcon) { var newbs = new D7BS(); newbs.BSNO = ""; var strSql = new StringBuilder(); strSql.Append("SELECT TOP 1 BSNO,CORPID, CUSTOMERNAME,FEESTATUS,ISCANCEL,isnull((select top 1 CTNALL from op_ctn where bsno=op_seae.BSNO and CTNALL like '20%'),'') CTN20,isnull((select top 1 CTNALL from op_ctn where bsno=op_seae.BSNO and CTNALL like '40%'),'') CTN40 from op_seae where MBLNO like '" + MBLNO + "%'"); if (!string.IsNullOrWhiteSpace(YTD7Condition)) { strSql.Append(" and " + YTD7Condition); } SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); var BSNO = ""; if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { newbs.BSNO = row["BSNO"].ToString(); newbs.CUSTOMERNAME = row["CUSTOMERNAME"].ToString(); newbs.FEESTATUS = row["FEESTATUS"].ToString() == "True" ? true : false; newbs.ISCANCEL = row["ISCANCEL"].ToString() == "1" ? true : false; newbs.CTN20 = row["CTN20"].ToString(); newbs.CTN40 = row["CTN40"].ToString(); newbs.CORPID = row["CORPID"].ToString(); } } return newbs; } // static public D6BS GETDELETED6BS(string MBLNO, SqlConnection dbcon) // { // var newbs = new D6BS(); // var strSql = new StringBuilder(); // strSql.Append("SELECT TOP 1 删除人 CUSTOMERNAME from t_op_delete where 主提单号='" + MBLNO + "'"); // SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); // DataTable table = new DataTable(); // adapter.Fill(table); // var BSNO = ""; // if (table.Rows.Count > 0) // { // foreach (DataRow row in table.Rows) // { // newbs.CUSTOMERNAME = row["CUSTOMERNAME"].ToString(); // } // } // return newbs; // } //} public class D7BS { public string BSNO { get; set; } public string CUSTOMERNAME { get; set; } public string FEETYPE { get; set; } public bool FEESTATUS { get; set; } public bool ISCANCEL { get; set; } public decimal AMOUNT { get; set; } public int QUANTITY { get; set; } public string CTN20 { get; set; }//业务中的20尺柜 public string CTN40 { get; set; }//业务中的20尺柜 public string CTN { get { if (!string.IsNullOrWhiteSpace(CTN40)) { return CTN40; } else { return CTN20; } } } public string ENTEROPERATOR { get; set; } public string CORPID { get; set; } } public class YTFEEHelper { private ILog log = LogManager.GetLogger(typeof(JobYtD7Fee)); public static string feeCustName { get; set; } = "上海亿通国际"; public static string CarrierCustName { get; set; } = "达飞轮船(中国)"; public static string feeName { get; set; } = "小票费"; public static string ydFeeName { get; set; } = "调箱费"; public static string secondPrintFeeName { get; set; } = "取消小票费"; public static string 告警邮箱 { get; set; } = "hhytcma@logisticsyt.com"; public static string connStr { get; set; } //public List YTFeeList { get; set; } = new List(); public List YTSUMFeeList { get; set; } = new List(); //public List noFeeList { get; set; } = new List(); public List NOFeeYTSUMFeeList { get; set; } = new List(); //public List 无业务List { get; set; } = new List(); //public List 退舱业务List { get; set; } = new List(); //public List 费用锁定业务List { get; set; } = new List(); //void add无业务(SqlConnection dbcon, string sendto, string title, string body, string smtpid) //{ // if (无业务List.Exists(x => x.title == title)) // { // } // else { // var newrec = new MAILINFO(); // newrec.dbcon= dbcon; // newrec.title = title; // newrec.body = body; // newrec.smtpid = smtpid; // 无业务List.Add(newrec); // } //} //private addmailList public void addYTFee(JsonYtFee addrec, D7BS ds7info,string YTD7NoUseCORPID="") { if (string.IsNullOrWhiteSpace(ds7info.BSNO) || ds7info.ISCANCEL || ds7info.FEESTATUS) { //using (SqlConnection dbcon = new SqlConnection(connStr)) //{ // //var FEELIST = new List(); // dbcon.Open(); // if (string.IsNullOrWhiteSpace(ds7info.BSNO)) // { // SaveMail(dbcon, // 告警邮箱, // addrec.bookingno + ds7info.CUSTOMERNAME + "此单号没有找到业务," + addrec.printType + ",金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢", // addrec.bookingno + "此单号没有找到业务," + addrec.printType + ",金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢", "1"); // return; // } // else // if (ds7info.ISCANCEL) // { // SaveMail(dbcon, 告警邮箱, addrec.bookingno + ds7info.CUSTOMERNAME + "此单号已退仓,但仍产生了费用。" + addrec.printType + "金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢", addrec.bookingno + "此单号已退仓,但仍产生了费用。" + addrec.printType + "金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢", "1"); // return; // } // else // if (ds7info.FEESTATUS) // { // SaveMail(dbcon, 告警邮箱, addrec.bookingno + ds7info.CUSTOMERNAME + "此单号已费用锁定,但仍产生了费用。" + addrec.printType + "金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢", addrec.bookingno + "此单号已费用锁定,但仍产生了费用。请知悉,谢谢", "1"); // return; // } //} if (string.IsNullOrWhiteSpace(ds7info.BSNO)) { addNoFee(addrec, ds7info, addrec.bookingno + ds7info.CUSTOMERNAME + "此单号没有找到业务,请知悉,谢谢。" , addrec.bookingno + ds7info.CUSTOMERNAME + "此单号没有找到业务。" + addrec.printType+ "金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢"); } else if (ds7info.ISCANCEL) { addNoFee(addrec, ds7info, addrec.bookingno + ds7info.CUSTOMERNAME + "此单号已退仓,但仍产生了费用,请知悉,谢谢。", addrec.bookingno + ds7info.CUSTOMERNAME + "此单号已退仓,但仍产生了费用。" + addrec.printType + "金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢"); } else if (ds7info.FEESTATUS) { addNoFee(addrec, ds7info, addrec.bookingno + ds7info.CUSTOMERNAME + "此单号已费用锁定,但仍产生了费用,请知悉,谢谢。", addrec.bookingno + ds7info.CUSTOMERNAME + "此单号已费用锁定,但仍产生了费用。" + addrec.printType + "金额:" + (Convert.ToDecimal(addrec.ydFee) + Convert.ToDecimal(addrec.secondPrintFee) + Convert.ToDecimal(addrec.fee)).ToString() + ",请知悉,谢谢"); } } else { if (!string.IsNullOrWhiteSpace(YTD7NoUseCORPID) && YTD7NoUseCORPID.IndexOf(ds7info.CORPID) >= 0) { //如果YTD7NoUseCORPID 不处理公司id不为空 且 YTD7NoUseCORPID当中包含ds7info.CORPID //则不报警 也不记录费用 log.Debug($"亿通账单 跳过业务:{JsonConvert.SerializeObject(addrec)}"); } else { log.Debug($"亿通账单 添加费用:{JsonConvert.SerializeObject(addrec)}"); addFee(addrec, ds7info); } } } private void addFee(JsonYtFee addrec, D7BS ds7info) { if (!YTSUMFeeList.Exists(x => x.MBLNO == addrec.bookingno)) { var newrec = new YTSumFee(addrec); newrec.BSNO = ds7info.BSNO; newrec.CTN20 = ds7info.CTN20; newrec.CTN40 = ds7info.CTN40; newrec.ENTEROPERATOR = ds7info.ENTEROPERATOR; YTSUMFeeList.Add(newrec); } else { YTSUMFeeList.First(x => x.MBLNO == addrec.bookingno).addfee(addrec); } } private void addNoFee(JsonYtFee addrec, D7BS ds7info,string mailtitle, string mailbody) { if (!NOFeeYTSUMFeeList.Exists(x => x.MBLNO == addrec.bookingno)) { var newrec = new YTSumFee(addrec); newrec.BSNO = ds7info.BSNO; newrec.CTN20 = ds7info.CTN20; newrec.CTN40 = ds7info.CTN40; //newrec.ENTEROPERATOR = NOFEEReason; newrec.mailtitle = mailtitle; newrec.mailbody = mailbody; NOFeeYTSUMFeeList.Add(newrec); } else { NOFeeYTSUMFeeList.First(x => x.MBLNO == addrec.bookingno).addfee(addrec); } } public void SendMail() { if (NOFeeYTSUMFeeList != null && NOFeeYTSUMFeeList.Count > 0) { using (SqlConnection dbcon = new SqlConnection(connStr)) { dbcon.Open(); foreach (var nofeeitem in NOFeeYTSUMFeeList) { SaveMail(dbcon, 告警邮箱, nofeeitem.mailtitle, nofeeitem.mailbody, "1"); } } } } private List FeeList { get; set; } = new List(); private List InsertFeeList { get; set; } = new List(); private List updFeeList { get; set; } = new List(); public void makefee_应付() { if (YTSUMFeeList.Count > 0) { foreach (var item in YTSUMFeeList) { makefee_应付(item); } var cdc = new CommonDataContext(connStr); var bsnoList = FeeList.Select(s=>s.BSNO).Distinct().ToList(); var CurrFeeList = cdc.ch_fee.Where(x => bsnoList.Contains(x.BSNO) && x.FEETYPE == 2 && (x.FEESTATUS == 1 || x.FEESTATUS == 6)).ToList(); foreach (var fee in FeeList) { if (CurrFeeList.Exists(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE)) { //var updrec = CurrFeeList.First(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE); CurrFeeList.First(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE).AMOUNT += fee.AMOUNT; CurrFeeList.First(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE).NOTAXAMOUNT += fee.NOTAXAMOUNT; CurrFeeList.First(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE).QUANTITY += fee.QUANTITY; //ChFeeHelper.setTax(ref updrec); CurrFeeList.First(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE).MODIFIEDUSER = fee.ENTEROPERATOR; CurrFeeList.First(x => x.BSNO == fee.BSNO && x.FEENAME == fee.FEENAME && x.UNITPRICE == fee.UNITPRICE).MODIFIEDTIME = DateTime.Now; //updFeeList.Add(fee); } else { fee.ENTERDATE= DateTime.Now; InsertFeeList.Add(fee); } } cdc.ch_fee.AddRange(InsertFeeList); cdc.SaveChanges(); } } private void makefee_应付(YTSumFee YTSumFee) { if (YTSumFee.fee_amount > 0) { //var newfee = ChFeeHelper.getNewChfee(); //newfee.BSNO = YTSumFee.BSNO; //newfee.CUSTOMERNAME = feeCustName; //newfee.FEETYPE = 2; //newfee.AMOUNT = YTSumFee.fee_amount; //newfee.QUANTITY = YTSumFee.fee_count; //newfee.UNIT = "箱"; //newfee.ENTEROPERATOR = YTSumFee.OP; //newfee.EXCHANGERATE = 1; //newfee.FEENAME = "小票费"; var newfee = ChFeeHelper.getChFee(YTSumFee.ENTEROPERATOR, YTSumFee.BSNO, feeCustName, 2, "小票费", YTSumFee.fee_amount, YTSumFee.CTN, YTSumFee.fee_count, "", ""); newfee.CUSTOMERTYPE = "其他"; newfee.INPUTMODE = "导入亿通账单"; ChFeeHelper.setTax(ref newfee); FeeList.Add(newfee); } if (YTSumFee.ydFee1_amount > 0) { //var newfee = ChFeeHelper.getNewChfee(); //newfee.BSNO = YTSumFee.BSNO; //newfee.CUSTOMERNAME = CarrierCustName; //newfee.FEETYPE = 2; //newfee.AMOUNT = YTSumFee.ydFee1_amount; //newfee.QUANTITY = YTSumFee.ydFee1_count; //newfee.UNIT = "20'"; //newfee.ENTEROPERATOR = YTSumFee.OP; //newfee.EXCHANGERATE = 1; //newfee.FEENAME = "调箱费"; var newfee = ChFeeHelper.getChFee(YTSumFee.ENTEROPERATOR, YTSumFee.BSNO, CarrierCustName, 2, "调箱费", YTSumFee.ydFee1_amount, YTSumFee.CTN20, YTSumFee.ydFee1_count, "", ""); newfee.CUSTOMERTYPE = "其他"; newfee.INPUTMODE = "导入亿通账单"; ChFeeHelper.setTax(ref newfee); FeeList.Add(newfee); } if (YTSumFee.ydFee2_amount > 0) { //var newfee = ChFeeHelper.getNewChfee(); //newfee.BSNO = YTSumFee.BSNO; //newfee.CUSTOMERNAME = CarrierCustName; //newfee.FEETYPE = 2; //newfee.AMOUNT = YTSumFee.ydFee2_amount; //newfee.QUANTITY = YTSumFee.ydFee2_count; //newfee.UNIT = "40'"; //newfee.ENTEROPERATOR = YTSumFee.OP; //newfee.EXCHANGERATE = 1; //newfee.FEENAME = "调箱费"; var newfee = ChFeeHelper.getChFee(YTSumFee.ENTEROPERATOR, YTSumFee.BSNO, CarrierCustName, 2, "调箱费", YTSumFee.ydFee2_amount, YTSumFee.CTN40, YTSumFee.ydFee2_count, "", ""); newfee.CUSTOMERTYPE = "其他"; newfee.INPUTMODE = "导入亿通账单"; ChFeeHelper.setTax(ref newfee); FeeList.Add(newfee); } if (YTSumFee.secondPrintFee_amount > 0) { //var newfee = ChFeeHelper.getNewChfee(); //newfee.BSNO = YTSumFee.BSNO; //newfee.CUSTOMERNAME = CarrierCustName; //newfee.FEETYPE = 2; //newfee.AMOUNT = YTSumFee.secondPrintFee_amount; //newfee.QUANTITY = YTSumFee.secondPrintFee_count; //newfee.UNIT = "箱"; //newfee.ENTEROPERATOR = YTSumFee.OP; //newfee.EXCHANGERATE = 1; //newfee.FEENAME = "取消小票费"; var newfee = ChFeeHelper.getChFee(YTSumFee.ENTEROPERATOR, YTSumFee.BSNO, CarrierCustName, 2, "取消小票费", YTSumFee.secondPrintFee_amount, YTSumFee.CTN, YTSumFee.secondPrintFee_count, "", ""); newfee.CUSTOMERTYPE = "其他"; newfee.INPUTMODE = "导入亿通账单"; ChFeeHelper.setTax(ref newfee); FeeList.Add(newfee); } } } } }