You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/JobAutoCreateFee/JobHCStlDate.cs

224 lines
10 KiB
C#

using log4net;
using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JobAutoCreateFee.Model;
namespace JobCreateFee
{
public class JobHCStlDate : IJob
{
private ILog log = LogManager.GetLogger(typeof(JobHCStlDate));
public void Execute(IJobExecutionContext context)
{
string connStr = context.JobDetail.JobDataMap.GetString("ConnectString");
using (SqlConnection dbcon = new SqlConnection(connStr))
{
var sql = new StringBuilder();
sql.Append("Update t_crm_client set 帐龄状态='' where isnull(帐龄状态,'')<>'' ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec = new SqlCommand(sql.ToString(), dbcon);
cmdexec.ExecuteNonQuery();
var sql2 = new StringBuilder();
sql2.Append("Update t_crm_client_sales set 帐龄状态='' where isnull(帐龄状态,'')<>'' ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec2 = new SqlCommand(sql2.ToString(), dbcon);
cmdexec2.ExecuteNonQuery();
var sql3 = new StringBuilder();
sql3.Append("Update t_op_seae set 扣单状态='自动放单',扣单原因=CASE WHEN 扣单原因='超出限额' THEN '未超出限额' ELSE CASE WHEN 扣单原因='超出账期' THEN '未超出账期' ELSE '' END END where 扣单状态='自动扣单' ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec3 = new SqlCommand(sql3.ToString(), dbcon);
cmdexec3.ExecuteNonQuery();
log.Debug($"开始更新帐龄...");
var clientnolist = CreateClientNoTtlList(dbcon);
try
{
var strSql = new StringBuilder();
strSql.Append("select distinct t_crm_client_sales.客户简称,t_crm_client_sales.揽货人,t_crm_client.结费优惠期限,t_crm_client_sales.结费金额 ");
strSql.Append("from t_crm_client_sales ");
strSql.Append("inner join t_crm_client on t_crm_client_sales.客户简称=t_crm_client.客户简称 ");
strSql.Append("where t_crm_client_sales.结束日期>=GetDate() and t_crm_client_sales.结费类型 in ('约定时间','约定天数') and t_crm_client_sales.状态='审核' ");
strSql.Append("and t_crm_client_sales.客户简称 in (select distinct 委托单位 from v_op_seae_protocol) ");
strSql.Append("order by t_crm_client_sales.客户简称 desc ");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strSql.ToString();
cmd.CommandTimeout = 120000; //要加这一句
cmd.Connection = dbcon;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adapter.Fill(table);
if (table.Rows.Count > 0)
{
var feeprojectlist = CreateClientList(table);
if (feeprojectlist != null)
{
foreach (var enumFeeproject in feeprojectlist)
{
log.Debug($"开始更新帐龄客户{enumFeeproject.CUSTNAME},揽货人{enumFeeproject.SALE}...");
bool b_cx = false;
bool b_cq = false;
//bool b_nocq = false;
var clientno = clientnolist.Find(x=>x.CUSTNAME== enumFeeproject.CUSTNAME&&x.SALE== enumFeeproject.SALE);
if (clientno != null) {
if (clientno.TTLNO > enumFeeproject.AMT) {
b_cx= true;
}
if (GetNoCt(dbcon, enumFeeproject.CUSTNAME, enumFeeproject.SALE) > 0) {
b_cq = true;
}
if (b_cx && b_cq) {
updateZq(dbcon, "超期超限", enumFeeproject.CUSTNAME, enumFeeproject.SALE);
} else if (b_cx)
{
updateZq(dbcon, "超限", enumFeeproject.CUSTNAME, enumFeeproject.SALE);
}
else if (b_cq)
{
updateZq(dbcon, "超期", enumFeeproject.CUSTNAME, enumFeeproject.SALE);
}
}
}
}
}
}
catch (Exception e)
{
log.Error($"超期超限错误:{e.Message}");
}
try
{
var sql4 = new StringBuilder();
sql4.Append("Update t_op_seae set 扣单状态='自动扣单',扣单原因='有提单号未归档费用确认',扣放单操作人='系统',扣放单操作时间=GETDATE() from t_op_seae b left join t_op_gain g on g.编号=b.编号 ");
sql4.Append(" where b.扣单状态<>'自动扣单' and b.扣单状态<>'扣单' ");
sql4.Append("and g.合计已收<>g.合计应收 and 预抵日期 is not null and 预抵日期<=GETDATE() ");
sql4.Append("AND NOT EXISTS (SELECT BSNO FROM Receipt_Doc D LEFT JOIN t_code_filetype T ON (T.文件类型=D.RECEIPTTYPE) WHERE B.业务编号=D.BSNO AND T.是否必须=1 ) ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec4 = new SqlCommand(sql4.ToString(), dbcon);
cmdexec3.ExecuteNonQuery();
}
catch (Exception e)
{
log.Error($"自动扣单错误:{e.Message}");
}
}
}
static public List<MsClient> CreateClientList(DataTable table)
{
var headList = new List<MsClient>();
if (table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
MsClient data = new MsClient();
data.CUSTNAME = row["客户简称"].ToString();
data.SALE = row["揽货人"].ToString();
if (row["结费优惠期限"] != DBNull.Value)
data.DAYS = Convert.ToInt32(row["结费优惠期限"].ToString());
if (row["结费金额"] != DBNull.Value)
data.AMT = Convert.ToDecimal(row["结费金额"].ToString());
data.AMT = data.AMT + 5000;
headList.Add(data);
}
}
return headList;
}
static public List<MsClient> CreateClientNoTtlList(SqlConnection dbcon)
{
var headList = new List<MsClient>();
var strSql = new StringBuilder();
strSql.Append("select 委托单位,揽货人,sum(未收合计) 未收合计 from v_op_seae_protocol ");
strSql.Append(" group by 委托单位,揽货人 ");
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)
{
MsClient data = new MsClient();
data.CUSTNAME = row["委托单位"].ToString();
data.SALE = row["揽货人"].ToString();
if (row["未收合计"] != DBNull.Value)
data.TTLNO= Convert.ToDecimal(row["未收合计"].ToString());
headList.Add(data);
}
}
return headList;
}
static public bool updateZq(SqlConnection dbcon, string zqstatus,string custname, string sale)
{
var sql = new StringBuilder();
sql.Append("Update t_crm_client set 帐龄状态='"+ zqstatus + "' where 客户简称='"+custname+"' ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec = new SqlCommand(sql.ToString(), dbcon);
cmdexec.ExecuteNonQuery();
var sql2 = new StringBuilder();
sql2.Append("Update t_crm_client_sales set 帐龄状态='" + zqstatus + "' where 客户简称='" + custname + "' and 揽货人='"+ sale + "' ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec2 = new SqlCommand(sql2.ToString(), dbcon);
cmdexec2.ExecuteNonQuery();
var sql3 = new StringBuilder();
sql3.Append("Update t_op_seae set 扣单状态='自动扣单',扣单原因='"+ zqstatus + "',扣放单操作人='系统',扣放单操作时间=GETDATE() from t_op_seae b left join t_op_gain g on g.编号=b.编号 ");
sql3.Append(" where b.委托单位='"+ custname + "' and b.揽货人='"+ sale + "' and g.合计已收<>g.合计应收 ");
if (dbcon.State == 0) dbcon.Open();
SqlCommand cmdexec3 = new SqlCommand(sql3.ToString(), dbcon);
cmdexec3.ExecuteNonQuery();
return true;
}
static public int GetNoCt(SqlConnection dbcon,string custname,string sale)
{
int usdexrate = 0;
var strSql = new StringBuilder();
strSql.Append("select count(*) ct from v_op_seae_protocol where 委托单位='"+ custname + "' and 揽货人='"+sale+"' and GetDate()>=应结日期");
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)
{
usdexrate= Convert.ToInt32(row["ct"].ToString());
}
}
return usdexrate;
}
}
}