using log4net; using Newtonsoft.Json; using Quartz; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Text; namespace JobCreateFee { public class JobCreateTask : IJob { private ILog log = LogManager.GetLogger(typeof(JobCreateTask)); public void Execute(IJobExecutionContext context) { log.Debug($"Execute开始"); try { string connStr = context.JobDetail.JobDataMap.GetString("ConnectString"); string Admin = context.JobDetail.JobDataMap.GetString("Admin"); string InvquerySql = context.JobDetail.JobDataMap.GetString("InvQuerySql"); using (SqlConnection dbcon = new SqlConnection(connStr)) { log.Debug($"清空未开票任务"); dbcon.Open(); var sql = "DELETE FROM op_task where TASKTYPE='未开票' and TASKSTATUS='未开始'"; SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); log.Debug($"生成未开票任务"); var strSql = new StringBuilder(); strSql.Append(" F.FEETYPE=1 AND F.INVOICE<>F.AMOUNT AND DATEDIFF([day],GETDATE(),ISNULL(B.STLDATE,B.ETD))<=10 "); if (!string.IsNullOrEmpty(InvquerySql)) strSql.Append(" AND "+ InvquerySql); var NoInvList = GetNoInv(strSql.ToString(),dbcon); if (NoInvList != null && NoInvList.Count != 0) { foreach (var NoInv in NoInvList) { insertTask(NoInv.BSNO, NoInv.MBLNO,NoInv.SALEDEPT+"_"+NoInv.OPLBNAME+"_"+NoInv.CUSTOMERNAME + "_RMB应收:" + NoInv.RMBDR + "_USD应收:" + NoInv.USDDR, "未开票",NoInv.OP, NoInv.CUSTOMERNAME, dbcon); } } log.Debug($"更新欠费待收款任务"); var sql21 = "DELETE FROM op_task where TASKTYPE='欠费待收款' and TASKSTATUS='未开始' and EXISTS (select 1 from v_op_bs where op_task.BSNO=v_op_bs.BSNO AND DATEDIFF([day],GETDATE(),ISNULL(v_op_bs.STLDATE,v_op_bs.ETD))>7) "; SqlCommand cmd21 = new SqlCommand(sql21, dbcon); cmd21.ExecuteNonQuery(); var sql2 = "UPDATE op_task SET ISCOMPLETE=1,TASKSTATUS='已完成',COMPLETETYPE='自动',COMPLETETIME=GETDATE() where TASKTYPE='欠费待收款' and TASKSTATUS='未开始' and NOT EXISTS (select 1 from v_op_gain_sum where op_task.BSNO=v_op_gain_sum.BSNO AND STLTTLDR<>TTLDR and TTLDR<>0) "; SqlCommand cmd2 = new SqlCommand(sql2, dbcon); cmd2.ExecuteNonQuery(); var strSql2 = new StringBuilder(); strSql2.Append(" F.FEETYPE=1 AND F.SETTLEMENT<>F.AMOUNT AND DATEDIFF([day],GETDATE(),ISNULL(B.STLDATE,B.ETD))<=7 AND NOT EXISTS (SELECT 1 FROM op_task WHERE op_task.BSNO=B.BSNO AND TASKTYPE='欠费待收款' )"); var NoStlList = GetNoStl(strSql2.ToString(), dbcon); if (NoStlList != null && NoStlList.Count != 0) { foreach (var NoInv in NoStlList) { insertTask(NoInv.BSNO, NoInv.MBLNO, NoInv.SALEDEPT + "_" + NoInv.OPLBNAME + "_RMB未收:" + NoInv.RMBDR + "_USD未收:" + NoInv.USDDR + "_RMB开票:" + NoInv.INVRMBDR + "_USD开票:" + NoInv.INVUSDDR, "欠费待收款", NoInv.OP, NoInv.CUSTOMERNAME, dbcon); } } log.Debug($"更新超额度欠费任务"); var sql3 = "DELETE FROM op_task where TASKTYPE='超额度欠费' and TASKSTATUS='未开始'"; SqlCommand cmd3 = new SqlCommand(sql3, dbcon); cmd3.ExecuteNonQuery(); var NoCustStlList = GetCustNoStl( dbcon); if (NoCustStlList != null && NoCustStlList.Count != 0) { foreach (var NoInv in NoCustStlList) { insertTask("","", NoInv.CUSTOMERNAME + "_" + NoInv.CUSTZQ + "_欠费额度:" + NoInv.ALLOWAMOUNT + "_总欠费:" + NoInv.NOSTLAMOUNT, "超额度欠费", Admin,NoInv.CUSTOMERNAME, dbcon); } } } } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.StackTrace); } } static public List GetNoInv(string connStr, SqlConnection dbcon) { var headList = new List(); var strSql = new StringBuilder(); strSql.Append("SELECT B.BSNO,B.SALE,B.OP,B.SALECORPID,B.OPLBNAME,B.MBLNO,F.CUSTOMERNAME"); strSql.Append(",(select [NAME] from [company] where GID=B.SALECORPID) as SALECORP"); strSql.Append(",isnull(SUM(CASE F.FEETYPE WHEN 1 THEN (CASE F.CURRENCY WHEN 'RMB' THEN ISNULL(F.AMOUNT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR"); strSql.Append(",isnull(SUM(CASE F.FEETYPE WHEN 1 THEN (CASE F.CURRENCY WHEN 'USD' THEN ISNULL(F.AMOUNT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR"); strSql.Append(" from CH_FEE F INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) where 1=1 AND " + connStr); strSql.Append(" GROUP BY B.BSNO,B.SALE,B.OP,B.SALECORPID,B.OPLBNAME,B.MBLNO,F.CUSTOMERNAME"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var Feedata = new MsOpSeaeFee(); Feedata.BSNO = row["BSNO"].ToString(); Feedata.SALE = row["SALE"].ToString(); Feedata.OP = row["OP"].ToString(); Feedata.SALEDEPT = row["SALECORP"].ToString(); Feedata.OPLBNAME = row["OPLBNAME"].ToString(); Feedata.MBLNO = row["MBLNO"].ToString(); Feedata.CUSTOMERNAME = row["CUSTOMERNAME"].ToString(); if (row["RMBDR"] != DBNull.Value) Feedata.RMBDR = Convert.ToDecimal(row["RMBDR"].ToString()); if (row["USDDR"] != DBNull.Value) Feedata.USDDR = Convert.ToDecimal(row["USDDR"].ToString()); headList.Add(Feedata); } } return headList; } static public List GetNoStl(string connStr, SqlConnection dbcon) { var headList = new List(); var strSql = new StringBuilder(); strSql.Append("SELECT B.BSNO,B.SALE,B.OP,B.SALEDEPT,B.OPLBNAME,B.MBLNO,F.CUSTOMERNAME"); strSql.Append(",isnull(SUM(CASE F.FEETYPE WHEN 1 THEN (CASE F.CURRENCY WHEN 'RMB' THEN ISNULL(F.AMOUNT,0)-ISNULL(F.SETTLEMENT,0) ELSE 0 END) ELSE 0 END),0) AS RMBDR"); strSql.Append(",isnull(SUM(CASE F.FEETYPE WHEN 1 THEN (CASE F.CURRENCY WHEN 'USD' THEN ISNULL(F.AMOUNT,0)-ISNULL(F.SETTLEMENT,0) ELSE 0 END)ELSE 0 END),0) AS USDDR"); strSql.Append(",isnull(SUM(CASE F.FEETYPE WHEN 1 THEN (CASE F.CURRENCY WHEN 'RMB' THEN ISNULL(F.INVOICE,0) ELSE 0 END) ELSE 0 END),0) AS INVRMBDR"); strSql.Append(",isnull(SUM(CASE F.FEETYPE WHEN 1 THEN (CASE F.CURRENCY WHEN 'USD' THEN ISNULL(F.INVOICE,0) ELSE 0 END)ELSE 0 END),0) AS INVUSDDR"); strSql.Append(" from CH_FEE F INNER JOIN V_OP_BILL B ON (F.BSNO=B.BSNO) where 1=1 AND " + connStr); strSql.Append(" GROUP BY B.BSNO,B.SALE,B.OP,B.SALEDEPT,B.OPLBNAME,B.MBLNO,F.CUSTOMERNAME"); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var Feedata = new MsOpSeaeFee(); Feedata.BSNO = row["BSNO"].ToString(); Feedata.SALE = row["SALE"].ToString(); Feedata.OP = row["OP"].ToString(); Feedata.SALEDEPT = row["SALEDEPT"].ToString(); Feedata.OPLBNAME = row["OPLBNAME"].ToString(); Feedata.MBLNO = row["MBLNO"].ToString(); Feedata.CUSTOMERNAME = row["CUSTOMERNAME"].ToString(); if (row["RMBDR"] != DBNull.Value) Feedata.RMBDR = Convert.ToDecimal(row["RMBDR"].ToString()); if (row["USDDR"] != DBNull.Value) Feedata.USDDR = Convert.ToDecimal(row["USDDR"].ToString()); if (row["INVRMBDR"] != DBNull.Value) Feedata.INVRMBDR = Convert.ToDecimal(row["INVRMBDR"].ToString()); if (row["INVUSDDR"] != DBNull.Value) Feedata.INVUSDDR = Convert.ToDecimal(row["INVUSDDR"].ToString()); headList.Add(Feedata); } } return headList; } static public List GetCustNoStl(SqlConnection dbcon) { var headList = new List(); var strSql = new StringBuilder(); strSql.Append("SELECT * FROM ("); strSql.Append("SELECT A.ALLOWAMOUNT,C.SHORTNAME,[dbo].[GetCustZq](C.SHORTNAME) ZQ,ISNULL((SELECT SUM((AMOUNT-SETTLEMENT)*EXCHANGERATE) FROM CH_FEE WHERE FEETYPE=1 AND CUSTOMERNAME=C.SHORTNAME),0) NOSTLAMOUNT"); strSql.Append(" FROM Info_Client_ACCDATE A "); strSql.Append(" left join info_client C ON C.GID=A.LINKGID"); strSql.Append(" WHERE ISNULL(ALLOWAMOUNT,0)>0"); strSql.Append(" ) AS T WHERE NOSTLAMOUNT>ALLOWAMOUNT "); SqlDataAdapter adapter = new SqlDataAdapter(strSql.ToString(), dbcon); DataTable table = new DataTable(); adapter.Fill(table); if (table.Rows.Count > 0) { foreach (DataRow row in table.Rows) { var Feedata = new MsCustNoStlFee(); Feedata.CUSTOMERNAME = row["SHORTNAME"].ToString(); Feedata.CUSTZQ = row["ZQ"].ToString(); if (row["ALLOWAMOUNT"] != DBNull.Value) Feedata.ALLOWAMOUNT = Convert.ToDecimal(row["ALLOWAMOUNT"].ToString()); if (row["NOSTLAMOUNT"] != DBNull.Value) Feedata.NOSTLAMOUNT = Convert.ToDecimal(row["NOSTLAMOUNT"].ToString()); headList.Add(Feedata); } } return headList; } private string insertTask(string BSNO,string MBLNO,string TASKTITLE,string TASKTYPE,string OP,string CUSTNAME,SqlConnection dbcon) { string sql = " INSERT INTO [op_task] (GID,[TASKNO] ,[PTASKNO],[TASKTYPE],[TASKSOURCE],[TASKSTATUS],[TASKTITLE] "; sql = sql + ",[CREATEUSER] ,[CREATETIME] ,[TASKBEGINDATE] ,[COMPLETETYPE] ,[COMPLETETIME] ,[KPI],[MBLNO],[FILENO],[MAILNO],[FILEPATH],[ISPUBLIC],[OP],[BSNO],ISCOMPLETE,CUSTNAME)"; sql = sql + " SELECT NEWID(),NEWID(),null, '"+ TASKTYPE + "', '任务生成', '未开始' as [任务状态], '" + TASKTITLE + "' as [任务说明],"; sql = sql + "'DEMO-SA' as [发起人],GETDATE() as [录入日期],GETDATE() as[任务开始时间], '手动' as [完成方式],null as [完成时间] ,null as [KPI值] , '" + MBLNO + "' as [提单号], "; sql = sql + " null as [文件编号], '' as [邮件编号], '' as [电子档案路径], 0 as 是否公共,'"+ OP + "','" + BSNO + "' as 编号,0,'"+CUSTNAME+"'"; // log.Debug($"插入语句:{sql}"); SqlCommand cmd = new SqlCommand(sql, dbcon); cmd.ExecuteNonQuery(); return ""; } } [JsonObject] public class MsOpSeaeFee { public string BSNO { get; set; } public string SALE { get; set; } public string OP { get; set; } public string SALEDEPT { get; set; } public string OPLBNAME { get; set; } public string MBLNO { get; set; } public string CUSTOMERNAME { get; set; } public decimal RMBDR { get; set; } public decimal USDDR { get; set; } public decimal INVRMBDR { get; set; } public decimal INVUSDDR { get; set; } public decimal INVAMOUNT { get; set; } } [JsonObject] public class MsCustNoStlFee { public string CUSTOMERNAME { get; set; } public string CUSTZQ { get; set; } public decimal ALLOWAMOUNT { get; set; } public decimal NOSTLAMOUNT { get; set; } } }