using DS.WMS.Core.Invoice.Entity;
using DS.WMS.Core.QuarztJobs.Dtos;
using DS.WMS.Core.QuarztJobs.Interface;
using DS.WMS.Core.QuarztJobs.Other;
using DS.WMS.Core.Sys.Entity;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using SqlSugar;
using System.Net.Http.Headers;
namespace DS.WMS.Core.QuarztJobs.Method
{
///
/// 进项发票相关
///
public class InInvoiceService : IInInvoiceJobService
{
ISqlSugarClient? db;
public InInvoiceService(IServiceProvider serviceProvider)
{
db = serviceProvider.GetRequiredService();
}
///
/// 定时获取进项发票数据
///
///
public async Task GetInInvoiceData()
{
//获取组织机构请求授权列表
if (db==null)
{
return;
}
var orgauthlist= db.Queryable().ClearFilter().Where(t => t.Deleted == false&&t.Type=="ininvice").ToList();
//遍历授权数据集
foreach (var item in orgauthlist)
{
//组织请求远程接口api数据
Dictionary header = new Dictionary();
header.Add("USER_KEY", item.Key);
header.Add("USER_SECRET", item.Secret);
InInvoicePost model = new InInvoicePost();
model.kprqks = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss"); //往前查24小时的进项发票数据
model.kprqjs = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//model.kprqks = "2024-09-01 00:00:00";// DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss"); //往前查24小时的进项发票数据
//model.kprqjs = "2024-09-07 00:00:00"; //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
var result = await HttpHellp.PostAsync("http://47.105.115.105:26650/api/XSD/GetInInvoiceDataList", JsonConvert.SerializeObject(model), header);
//处理返回结果
var info= JsonConvert.DeserializeObject(result);
if (info!=null&&info.Data.data!=null)
{
var dbLink = await db.Queryable().ClearFilter().Where(t => t.TenantId == item.TenantId).FirstAsync();
SqlSugarClient? tenantDb = null;
foreach (var itemdata in info.Data.data)
{
//查询租户数据库中是否有当前数据
tenantDb = new SqlSugarClient(new ConnectionConfig
{
ConfigId = dbLink.Id,
ConnectionString = dbLink.Connection,
DbType = dbLink.DbType,
IsAutoCloseConnection = true
});
var ininviceinfo = await tenantDb.Queryable().Where(x => x.InvoiceNumber == itemdata.fphm).FirstAsync();
//数据库中发票数据为空,获取发票详情数据
if (ininviceinfo==null)
{
GetInInvoiceDataInfo gidi = new GetInInvoiceDataInfo();
gidi.fphm = itemdata.fphm;
var resultinfo = await HttpHellp.PostAsync("http://47.105.115.105:26650/api/XSD/GetInInvoiceDataInfo", JsonConvert.SerializeObject(gidi), header);
var Iinfo = JsonConvert.DeserializeObject(resultinfo);
if (Iinfo.code == 0) //调整
{
//添加发票数据
InInvoice inInvoice = new InInvoice();
inInvoice.InvoicingDate = Iinfo.data.kprq;
inInvoice.InvoiceTypeCode = Iinfo.data.fplxdm;
inInvoice.InvoiceNumber = Iinfo.data.fphm;
inInvoice.TotalAmount = Iinfo.data.hjje;
inInvoice.TotalTax = Iinfo.data.hjse;
inInvoice.TotalWithTax = Iinfo.data.jshj;
inInvoice.InvoiceStatus = Iinfo.data.fpzt;
inInvoice.Invoicer = Iinfo.data.kpr;
inInvoice.OriginalInvoiceNumber = Iinfo.data.yfphm;
inInvoice.BuyerInvoiceName = Iinfo.data.gfkpmc;
inInvoice.BuyerInvoiceTaxNumber = Iinfo.data.gfkpsh;
inInvoice.SellerInvoiceName = Iinfo.data.xfkpmc;
inInvoice.SellerInvoiceTaxNumber = Iinfo.data.xfkpsh;
inInvoice.IsDetailObtained = Iinfo.data.IsGetDtl;
inInvoice.SellerIdentificationNumber = Iinfo.data.xfsbh;
inInvoice.FinalCheckCode = Iinfo.data.final_jym;
inInvoice.BuyerName = Iinfo.data.gfmc;
inInvoice.CurrentTime = Iinfo.data.dqsj;
inInvoice.DeductionAmount = Iinfo.data.kce;
inInvoice.VehicleInvoiceTypeCode = Iinfo.data.jdfplxdm;
inInvoice.TaxControlCode = Iinfo.data.skm;
inInvoice.BuyerTaxNumber = Iinfo.data.ghdwdm;
inInvoice.PaperInvoiceNumber = Iinfo.data.zzfphm;
inInvoice.Payee = Iinfo.data.skr;
inInvoice.ItemFlag = Iinfo.data.qdbz;
inInvoice.InvoiceUnitCode = Iinfo.data.kpdwdm;
inInvoice.BuyerBankAccount = Iinfo.data.ghdwyhzh;
inInvoice.Amount = Iinfo.data.je;
inInvoice.SellerName = Iinfo.data.xfmc;
inInvoice.SpecialElements = Iinfo.data.tdys;
inInvoice.ReimbursementStatus = Iinfo.data.bxzt;
inInvoice.BuyerBankAccountNumber = Iinfo.data.gfyhzh;
inInvoice.SpecialElementTypeCode = Iinfo.data.tdyslxDm;
inInvoice.OperatorCode = Iinfo.data.czydm;
inInvoice.BuyerAddressPhone = Iinfo.data.ghdwdzdh;
inInvoice.IsPaperInvoice = Iinfo.data.sfwzzfp;
inInvoice.InvoiceRequestNumber = Iinfo.data.fpqqlsh;
inInvoice.TaxAmount = Iinfo.data.se;
inInvoice.Remarks = Iinfo.data.bz;
inInvoice.BuyerIdentificationNumber = Iinfo.data.gfsbh;
inInvoice.BuyerFullName = Iinfo.data.ghdwmc;
inInvoice.SellerTaxNumber = Iinfo.data.xhdwdm;
inInvoice.InvoiceCode = Iinfo.data.fpdm;
inInvoice.SellerAddressPhone = Iinfo.data.xhdwdzdh;
inInvoice.DetailList = Iinfo.data.zbmx;
inInvoice.SellerAddressPhoneExtra = Iinfo.data.xddzdh;
inInvoice.SellerBankAccountNumber = Iinfo.data.xfyhzh;
inInvoice.Reviewer = Iinfo.data.fhr;
inInvoice.BuyerAddressPhoneExtra = Iinfo.data.gfdzdh;
inInvoice.SellerFullName = Iinfo.data.xhdwmc;
inInvoice.MachineCode = Iinfo.data.jqbh;
inInvoice.ItemOrServiceName = Iinfo.data.zyspmc;
inInvoice.SellerBankAccount = Iinfo.data.xhdwyhzh;
inInvoice.PdfFilePath = Iinfo.data.PDFfile;
inInvoice.OfdFilePath = Iinfo.data.OFDfile;
inInvoice.XmlFilePath = Iinfo.data.XMLfile;
inInvoice.AllFileDownloadPath = Iinfo.data.Allfile;
inInvoice.OrgId = item.OrgId;
inInvoice.CreateTime = DateTime.Now;
var datainv = await tenantDb.Insertable(inInvoice).ExecuteReturnEntityAsync();
foreach (var dtl in Iinfo.data.List)
{
//添加发票详情数据
InInvoiceDetail inInvoicedtl = new InInvoiceDetail();
inInvoicedtl.Id= SnowFlakeSingle.Instance.NextId();
inInvoicedtl.PId = datainv.Id;
inInvoicedtl.DetailId = dtl.mxid;
inInvoicedtl.InvoiceCode = dtl.fpdm;
inInvoicedtl.InvoiceNumber = dtl.fphm;
inInvoicedtl.InvoiceDetailLineNumber = dtl.fpmxxh;
inInvoicedtl.InvoiceLineNature = dtl.fphxz;
inInvoicedtl.Amount = dtl.je;
inInvoicedtl.TaxRate = dtl.sl;
inInvoicedtl.TaxAmount = dtl.se;
inInvoicedtl.ProductName = dtl.spmc;
inInvoicedtl.Specification = dtl.ggxh;
inInvoicedtl.Unit = dtl.dw;
inInvoicedtl.Quantity = dtl.spsl;
inInvoicedtl.UnitPrice = dtl.spdj;
inInvoicedtl.ProductCode = dtl.spbm;
inInvoicedtl.Version = dtl.bb;
inInvoicedtl.PreferentialPolicyFlag = dtl.yhzcbs;
inInvoicedtl.InvoicingDate = dtl.kprq;
inInvoicedtl.InvoicingUnitCode = dtl.kpdwdm;
inInvoicedtl.Name = dtl.mc;
inInvoicedtl.SerialNumber = dtl.xh;
inInvoicedtl.OrgId = item.OrgId;
inInvoicedtl.CreateTime = DateTime.Now;
await tenantDb.Insertable(inInvoicedtl).ExecuteCommandAsync();
}
}
}
}
}
}
}
}
}