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/D7MqClient/D7MqClient/InvtoD7.cs

703 lines
34 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using log4net;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Data;
using DSWeb.Common.DB;
using MailAnalyzeTools;
namespace D7MqClient
{
public class InvoiceListItem
{
/// <summary>
///
/// </summary>
public Int64 apiReadTime { get; set; }
/// <summary>
///
/// </summary>
public Int64 apiSendTime { get; set; }
/// <summary>
///
/// </summary>
public string buyerAccount { get; set; }
/// <summary>
///
/// </summary>
public string buyerAddress { get; set; }
/// <summary>
///
/// </summary>
public string buyerName { get; set; }
/// <summary>
///
/// </summary>
public string buyerTaxNum { get; set; }
/// <summary>
///
/// </summary>
public string buyerTel { get; set; }
/// <summary>
///
/// </summary>
public string checker { get; set; }
/// <summary>
///
/// </summary>
public string clerk { get; set; }
/// <summary>
///
/// </summary>
public string compId { get; set; }
/// <summary>
///
/// </summary>
public Int64 createTime { get; set; }
/// <summary>
///
/// </summary>
public string email { get; set; }
/// <summary>
///
/// </summary>
public string gID { get; set; }
/// <summary>
///
/// </summary>
public string invCode { get; set; }
/// <summary>
///
/// </summary>
public string invNum { get; set; }
/// <summary>
///
/// </summary>
public string payee { get; set; }
/// <summary>
///
/// </summary>
public string pdfUrl { get; set; }
/// <summary>
///
/// </summary>
public string pictureUrl { get; set; }
/// <summary>
///
/// </summary>
public string salerAccount { get; set; }
/// <summary>
///
/// </summary>
public string salerAddress { get; set; }
/// <summary>
///
/// </summary>
public string salerName { get; set; }
/// <summary>
///
/// </summary>
public string salerTaxNum { get; set; }
/// <summary>
///
/// </summary>
public string salerTel { get; set; }
/// <summary>
///
/// </summary>
public string serialNo { get; set; }
/// <summary>
///
/// </summary>
public string status { get; set; }
public string message { get; set; }
public invmessage errormessage { get; set; }
public decimal? total { get; set; }
/// <summary>
/// 自动根据发票的gid找到需要合计的
/// </summary>
/// <param name="ls"></param>
/// <returns></returns>
public decimal getAmount(string currency, List<SplitListItem> ls)
{
var _ls = ls.Where(x => x.invoiceId == gID).ToList();
var result = 0M;
if (_ls.Count > 0)
{
if (currency == "RMB")
{
result = _ls.Sum(s => s.amountTotalRMB);
}
else
{
result = _ls.Sum(s => s.amountTotal);
}
}
return result;
}
public decimal getSplitAmount(List<SplitListItem> ls)
{
var _ls = ls.Where(x => x.invoiceId == gID).ToList();
var result = 0M;
if (_ls.Count > 0)
{
if (_ls[0].currency == "RMB")
{
result = _ls.Sum(s => s.splitAmountRMB);
}
else
{
result = _ls.Sum(s => s.splitAmount);
}
}
return result;
}
public decimal getRMBAmount(List<SplitListItem> ls)
{
var _ls = ls.Where(x => x.invoiceId == gID).ToList();
var result = 0M;
if (_ls.Count > 0)
{
result = _ls.Sum(s => s.amountTotalRMB);
}
return result;
}
public decimal getOTAmount(List<SplitListItem> ls)
{
var _ls = ls.Where(x => x.invoiceId == gID).ToList();
var result = 0M;
if (_ls.Count > 0)
{
result = _ls.Sum(s => s.amountTotal);
}
return result;
}
public void setmessage()
{
try
{
if (!string.IsNullOrWhiteSpace(message))
errormessage = JsonConvert.DeserializeAnonymousType(message, new invmessage());
}
catch (Exception e) {
//LogHelper.Debug("错误语句:" + SQLString);
try
{
if (!string.IsNullOrWhiteSpace(message.Replace("\\","")))
errormessage = JsonConvert.DeserializeAnonymousType(message.Replace("\\", ""), new invmessage());
}
catch (Exception) {
}
}
}
}
public class invmessage
{
//"message": "{\"code\":1,\"message\":\"调用金税盘失败\",\"postTime\":1660614772000,\"serialNo\":\"3c27c755b2394998839685cf6a3c714a\"}",
public int code { get; set; }
public string message { get; set; }
public long postTime { get; set; }
public string serialNo { get; set; }
}
public class SplitListItem
{
/// <summary>
///
/// </summary>
public decimal amountTotal { get; set; }
/// <summary>
///
/// </summary>
public decimal amountTotalRMB { get; set; }
/// <summary>
///
/// </summary>
public string bSNO { get; set; }
/// <summary>
///
/// </summary>
public string businessId { get; set; }
/// <summary>
///
/// </summary>
public string currency { get; set; }
/// <summary>
///
/// </summary>
public string feeId { get; set; }
/// <summary>
///
/// </summary>
public string gID { get; set; }
/// <summary>
///
/// </summary>
public string invoiceDetailId { get; set; }
/// <summary>
///
/// </summary>
public string invoiceId { get; set; }
/// <summary>
///
/// </summary>
public string linkId { get; set; }
/// <summary>
///
/// </summary>
public decimal splitAmount { get; set; }
/// <summary>
///
/// </summary>
public decimal splitAmountRMB { get; set; }
}
public class InvRoot
{
/// <summary>
///
/// </summary>
public string companyId { get; set; }
/// <summary>
///
/// </summary>
public List<InvoiceListItem> invoiceList { get; set; }
/// <summary>
///
/// </summary>
public string linkId { get; set; }
/// <summary>
///
/// </summary>
public List<SplitListItem> splitList { get; set; }
}
public class InvToD7
{
private static ILog logger = LogManager.GetLogger("D7MqClient");
class MqInfo
{
public string DS7COMPANYID { get; set; }
public string COMPANYID { get; set; }
public string MQURL { get; set; }
public string virtualhost { get; set; }
public string ExchangeName { get; set; }
public string QueueName { get; set; }
public string DbconnStr { get; set; }
private CommonDataContext cdc { get; set; }
public MqInfo() { }
public MqInfo(string corpid, string dbconnStr)
{
COMPANYID = corpid;
MQURL = ConfigurationManager.AppSettings["MQURL"];
virtualhost = ConfigurationManager.AppSettings["virtualhost"];
ExchangeName = "inv.result";
QueueName = "inv.result." + COMPANYID;
//ExchangeName = "output";
//QueueName = "djy.output.cma.file.";
DbconnStr = dbconnStr;
cdc = new CommonDataContext(DbconnStr);
}
public IConnection mqConn;
public void DoGetInfo()
{
//TestDeal();
logger.Debug($"启动接收文件消息" + COMPANYID);
var CompanyQueueName = QueueName;
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri(MQURL);
factory.VirtualHost = virtualhost;
mqConn = factory.CreateConnection();
IModel model = mqConn.CreateModel();
model.ExchangeDeclare(ExchangeName, ExchangeType.Direct, true, false);
model.QueueDeclare(CompanyQueueName, true, false, false);
//model.ExchangeDeclare(ExchangeName, ExchangeType.Direct, false, false);
//model.QueueDeclare(CompanyQueueName, false, false, false, null);
var consumer = new EventingBasicConsumer(model);
consumer.Received += (ch, ea) =>
{
var body = ea.Body;
var strBody = Encoding.UTF8.GetString(body.ToArray());
logger.Debug($"收到消息:{strBody}");
try
{
//回写数据
var InvRoot = JsonConvert.DeserializeAnonymousType(strBody, new InvRoot());
SaveInv(InvRoot);
}
catch (Exception ex)
{
logger.Error($"处理消息时出错:" + ex.Message);
logger.Error(ex.Message);
logger.Error(ex.StackTrace);
}
};
model.BasicConsume(CompanyQueueName, true, consumer);
}
private void TestDeal()
{
var strBody = "{\"companyId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"invoiceList\":[{\"apiReadTime\":1660614772000,\"apiSendTime\":1660614724000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"3c27c755b2394998839685cf6a3c714a\",\"message\":\"{\\\"code\\\":1,\\\"message\\\":\\\"调用金税盘失败\\\",\\\"postTime\\\":1660614772000,\\\"serialNo\\\":\\\"3c27c755b2394998839685cf6a3c714a\\\"}\",\"payee\":\"zwk\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"3c27c755b2394998839685cf6a3c714a\",\"status\":\"fail\"},{\"apiReadTime\":1660614771000,\"apiSendTime\":1660614724000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"bbc69ec0289745d992e9076fcd73e407\",\"invCode\":\"137021616606\",\"invNum\":\"14724755\",\"invoiceDate\":1660614724000,\"payee\":\"zwk\",\"pdfUrl\":\"https://www.chinaeinv.com:943/pdfUnsigned.jspa?c=188D03F009871C521F36\",\"pictureUrl\":\"https://www.chinaeinv.com:943/p.jspa?c=188D03F009871C521F36\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"bbc69ec0289745d992e9076fcd73e407\",\"status\":\"success\",\"total\":2000.00},{\"apiReadTime\":1660614773000,\"apiSendTime\":1660614724000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"c68782186a6a43bcb511e32ca22b92b2\",\"message\":\"{\\\"code\\\":1,\\\"message\\\":\\\"调用金税盘失败\\\",\\\"postTime\\\":1660614773000,\\\"serialNo\\\":\\\"c68782186a6a43bcb511e32ca22b92b2\\\"}\",\"payee\":\"zwk\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"c68782186a6a43bcb511e32ca22b92b2\",\"status\":\"fail\"},{\"apiReadTime\":1660614772000,\"apiSendTime\":1660614724000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"c71288775cf24b38a15caaaa99c423aa\",\"invCode\":\"137021616606\",\"invNum\":\"14725902\",\"invoiceDate\":1660614725000,\"payee\":\"zwk\",\"pdfUrl\":\"https://www.chinaeinv.com:943/pdfUnsigned.jspa?c=8458CA4084740312534C\",\"pictureUrl\":\"https://www.chinaeinv.com:943/p.jspa?c=8458CA4084740312534C\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"c71288775cf24b38a15caaaa99c423aa\",\"status\":\"success\",\"total\":2000.00},{\"apiReadTime\":1660614770000,\"apiSendTime\":1660614724000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"b9d33364a33148498f0f8cf76c0173f8\",\"invCode\":\"137021616606\",\"invNum\":\"14726444\",\"invoiceDate\":1660614726000,\"payee\":\"zwk\",\"pdfUrl\":\"https://www.chinaeinv.com:943/pdfUnsigned.jspa?c=6EEE34F19FC02D10476C\",\"pictureUrl\":\"https://www.chinaeinv.com:943/p.jspa?c=6EEE34F19FC02D10476C\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"b9d33364a33148498f0f8cf76c0173f8\",\"status\":\"success\",\"total\":2000.00},{\"apiReadTime\":1660614773000,\"apiSendTime\":1660614725000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"f527e46f2871456583772af01860a352\",\"invCode\":\"137021616606\",\"invNum\":\"14726981\",\"invoiceDate\":1660614726000,\"payee\":\"zwk\",\"pdfUrl\":\"https://www.chinaeinv.com:943/pdfUnsigned.jspa?c=6D72FB34DCF99164306D\",\"pictureUrl\":\"https://www.chinaeinv.com:943/p.jspa?c=6D72FB34DCF99164306D\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"f527e46f2871456583772af01860a352\",\"status\":\"success\",\"total\":2000.00},{\"apiReadTime\":1660614771000,\"apiSendTime\":1660614725000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"4ab4879360a94596bd28e5c375083c77\",\"invCode\":\"137021616606\",\"invNum\":\"14727567\",\"invoiceDate\":1660614727000,\"payee\":\"zwk\",\"pdfUrl\":\"https://www.chinaeinv.com:943/pdfUnsigned.jspa?c=FF9DF779D9D613AD34E6\",\"pictureUrl\":\"https://www.chinaeinv.com:943/p.jspa?c=FF9DF779D9D613AD34E6\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"4ab4879360a94596bd28e5c375083c77\",\"status\":\"success\",\"total\":1000.00},{\"apiReadTime\":1660614772000,\"apiSendTime\":1660614725000,\"buyerAccount\":\"招商银行股份有限公司济南会展中心支行 531903348710703 \",\"buyerName\":\"山东百特凯瑞国际物流有限公司\",\"buyerTaxNum\":\"91370103689810707B\",\"buyerTel\":\"\",\"checker\":\"sss\",\"clerk\":\"zwk\",\"compId\":\"9f3b3526-4dd4-4997-b974-1f2adb2793c7\",\"createTime\":1660614469000,\"email\":\"market@dongshengsoft.com\",\"gID\":\"fbc27354033945d8826b505f2a3d4a5d\",\"invCode\":\"137021616606\",\"invNum\":\"14728192\",\"invoiceDate\":1660614728000,\"payee\":\"zwk\",\"pdfUrl\":\"https://www.chinaeinv.com:943/pdfUnsigned.jspa?c=D8645507953E97F63851\",\"pictureUrl\":\"https://www.chinaeinv.com:943/p.jspa?c=D8645507953E97F63851\",\"requireRemark\":\"\n\",\"salerAccount\":\"\",\"salerAddress\":\"青岛市市南区延安三路129号1005室\",\"salerName\":\"青岛东胜伟业软件有限公司\",\"salerTaxNum\":\"913702020814028524\",\"salerTel\":\"0532-68628148\",\"serialNo\":\"fbc27354033945d8826b505f2a3d4a5d\",\"status\":\"success\",\"total\":2000.00}],\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitList\":[{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"2a14703deda84978851409cd54f1925a\",\"invoiceDetailId\":\"3e2fdb75fd374b88b0c9af6c5aef0d30\",\"invoiceId\":\"b9d33364a33148498f0f8cf76c0173f8\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"3dda8a51bbd24cd2b1d10cfa39384301\",\"invoiceDetailId\":\"ee126f614be4491e8c0fe7e005c18aaa\",\"invoiceId\":\"4ab4879360a94596bd28e5c375083c77\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":1000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"481ca05901bb45e792cb7af7e17cc366\",\"invoiceDetailId\":\"d4caec2ed57c41b7af03f7635b397612\",\"invoiceId\":\"bbc69ec0289745d992e9076fcd73e407\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"aac2f4352ba34938b8a88d3f88ae301e\",\"invoiceDetailId\":\"72238f3f98b548b3baa0d6f35c81e9d0\",\"invoiceId\":\"c71288775cf24b38a15caaaa99c423aa\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":2000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"B96AA2ED-1643-141A-B940-4660F5F2A189\",\"gID\":\"b78965637b72426e9e79ec8b1ef0783c\",\"invoiceDetailId\":\"6ec60e4489c8498a898df662aa34c7b2\",\"invoiceId\":\"fbc27354033945d8826b505f2a3d4a5d\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"bfacf0523e60424a852b3d8b5f15d057\",\"invoiceDetailId\":\"642e396aaf6a44c790031f1f92e461b4\",\"invoiceId\":\"3c27c755b2394998839685cf6a3c714a\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"caeb6f2eaf5645f491cd06424a519f03\",\"invoiceDetailId\":\"b6d62c9306a0462da0885107a37d3a50\",\"invoiceId\":\"c68782186a6a43bcb511e32ca22b92b2\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00},{\"amountTotal\":0.00,\"amountTotalRMB\":13000.00,\"bSNO\":\"topother272b76d839e94a47b3a6db6c33d90c78\",\"businessId\":\"5d126725fd064eedbbc64bff20d0c5f3\",\"currency\":\"RMB\",\"feeId\":\"54F6550C-BB16-255A-76DD-0585F3407AB3\",\"gID\":\"fc72e31aed8f403f9bd9174ca6afe8b6\",\"invoiceDetailId\":\"cac1db24a8e64916819e4e94575371b2\",\"invoiceId\":\"f527e46f2871456583772af01860a352\",\"linkId\":\"bafcaacb-3350-47c8-b6df-3a5b594b5b5a\",\"splitAmount\":0.00,\"splitAmountRMB\":2000.00}]}";
////
var InvRoot = JsonConvert.DeserializeAnonymousType(strBody, new InvRoot());
SaveInv(InvRoot);
}
private void SaveInv(InvRoot InvRoot)
{
////根据linkid 找到 ch_fee.invlinkgid=linkid的费用
////var feelist = cdc.ch_fee.Where(x => x.INVLINKGID == InvRoot.linkId).ToList();
var codename = InvRoot.invoiceList[0].clerk;
var info = cdc.VW_user.FirstOrDefault(x => x.CODENAME == codename);
DS7COMPANYID = info.COMPANYID;
//遍历invoiceList
foreach (var inv in InvRoot.invoiceList)
{
inv.setmessage();
//根据InvoiceListItem建立发票
var newInv = getNewInv(inv, InvRoot);
if (!string.IsNullOrEmpty(newInv.CUSTOMERNAME))
{
cdc.ch_fee_invoice.Add(newInv);
//增加一条发票明细
var newInvdetail = getNewInvDetail(newInv, inv, InvRoot);
//遍历splitList如果 SplitListItem.invoiceid= 这个票的invoiceId
//增加一个ch_fee,feeid=SplitListItem.feeId
var feedoList = getNewFeeDoList(newInv, inv, InvRoot);
cdc.ch_fee_invoicedetail.Add(newInvdetail);
cdc.ch_fee_do.AddRange(feedoList);
//20220805 根据chfeedo的内容 改写chfee的开票金额信息
var feeidlist = feedoList.Select(s => s.FEEID).ToList();
var feelist = cdc.ch_fee.Where(x => feeidlist.Contains(x.GID)).ToList();
foreach (var feedo in feedoList)
{
if (feelist.Exists(x => x.GID == feedo.FEEID))
{
var updrec = feelist.FirstOrDefault(x => x.GID == feedo.FEEID);
updrec.INVOICE = updrec.INVOICE == null ? 0 : updrec.INVOICE;
updrec.INVOICE += feedo.AMOUNT;
updrec.ORDERINVOICE = updrec.ORDERINVOICE == null ? 0 : updrec.ORDERINVOICE;
updrec.ORDERINVOICE += feedo.AMOUNT;
}
}
cdc.SaveChanges();
}
}
}
private ch_fee_invoice_md getNewInv(InvoiceListItem inv, InvRoot invroot)
{
var result = new ch_fee_invoice_md();
var billno = DbHelperSQL.GetBillNo(invroot.companyId.Replace(" ", ""), "0308");
var _infoclientList = cdc.info_client.Where(x => x.TAXNO == inv.buyerTaxNum).ToList();
if (_infoclientList.Count > 0)
{
var _infoclient = _infoclientList[0];
var _firstfee = invroot.splitList.FirstOrDefault(x => x.invoiceId == inv.gID);
var _firstthisfee = cdc.ch_fee.FirstOrDefault(x => x.GID == _firstfee.feeId);
result.GID = Guid.NewGuid().ToString();
result.BILLNO = billno;
//填充购方信息 销方信息 发票号
result.INVOICECODE = inv.invCode;
result.INVOICENO = inv.invNum;
if (string.IsNullOrWhiteSpace(inv.invNum))
{
try
{
result.REMARK = inv.errormessage.message;
}
catch (Exception e) {
}
}
result.CUSTOMERNAME = _infoclient.SHORTNAME;
result.INVOICECUSTNAME = inv.buyerName;
result.INVOICEMAKETIME = TimestampHelper.GetDateTime(inv.createTime);
result.CURRENCY = _firstfee.currency;
result.AMOUNT = inv.getSplitAmount(invroot.splitList);
//result.REMARK=inv.
var clerk = cdc.VW_user.FirstOrDefault(x => inv.clerk == x.CODENAME);
var payee = cdc.VW_user.FirstOrDefault(x => inv.payee == x.CODENAME);
var checker = cdc.VW_user.FirstOrDefault(x => inv.checker == x.CODENAME);
//OPERATOR、clerk开票人 payee收款人 checker复核人
if (clerk != null)
result.OPERATOR = clerk.USERID;
if (payee != null)
result.PAYEE = payee.USERID;
if (checker != null)
result.CHECKER = checker.USERID;
result.INVOICETYPE = 1;
result.OPERATETIME = TimestampHelper.GetDateTime(inv.apiSendTime);
result.EXCHANGERATE = _firstthisfee.EXCHANGERATE;
result.COMPANYID = DS7COMPANYID;
result.CUSTRATENO = inv.buyerTaxNum;
result.TAXCODE = inv.buyerTaxNum;
result.CUSTADDRTEL = inv.buyerAddress + " " + inv.buyerTel;
result.CUSTBANK = inv.buyerAccount;
result.INVOICECATEGORY = 0;
result.INVAMOUNT = inv.getSplitAmount(invroot.splitList);
result.OTCURRAMOUNT = inv.getOTAmount(invroot.splitList);
result.RECVCURR = result.CURRENCY;
result.SALECORPID = DS7COMPANYID;
result.SETRED = false;
result.CREATEUSER = clerk.USERID;
result.CREATETIME = TimestampHelper.GetDateTime(inv.apiSendTime);
result.PUSHEMAIL = inv.email;
result.INVOICESERIALNUM = inv.serialNo;
result.INVOICEINFOURL = inv.pictureUrl;
result.INVOICEPDFURL = inv.pdfUrl;
result.PTORRED = "1";
if (inv.status == "success")
{
result.BILLSTATUS = 1;
result.EINVOICESTATE = "2";
}
else
{
result.INVOICESERIALNUM = "";
result.BILLSTATUS = 0;
result.EINVOICESTATE = "3";
}
//0 未开票
//1 开票中
//2 开票成功
//3 开票失败
}
return result;
}
private ch_fee_invoicedetail_md getNewInvDetail(ch_fee_invoice_md head, InvoiceListItem inv, InvRoot invroot)
{
var result = new ch_fee_invoicedetail_md();
result.GID = Guid.NewGuid().ToString();
//生成一条单条的发票明细
result.PID = head.GID;
result.SerialNo = 1;
result.GOODSNAME = cdc.code_goods_inv.FirstOrDefault(x => x.GOODNAME == "代理海运费").GID;
result.SPEC = "";
result.UNIT = "";
result.PKGS = 1;
result.TAXPRICE = inv.total == null ? 0 : inv.total;
result.PRICE = inv.total == null ? 0 : inv.total;
result.AMOUNT = inv.total == null ? 0 : inv.total;
result.TAXRATE = 0;
result.TAX = 0;
return result;
}
private List<ch_fee_do_md> getNewFeeDoList(ch_fee_invoice_md head, InvoiceListItem inv, InvRoot invroot)
{
var result = new List<ch_fee_do_md>();
//result.GID = Guid.NewGuid().ToString();
//寻找该发票相关的明细
var resultdetailList = invroot.splitList.Where(x => x.invoiceId == inv.gID).ToList();
var feeidlist = resultdetailList.Select(s => s.feeId).ToList();
var feelist = cdc.ch_fee.Where(x => feeidlist.Contains(x.GID)).ToList();
if (resultdetailList != null && resultdetailList.Count > 0)
{
foreach (var resultdetail in resultdetailList)
{
var newdetail = new ch_fee_do_md();
var _thisfee = feelist.FirstOrDefault(x => x.GID == resultdetail.feeId);
newdetail.GID = Guid.NewGuid().ToString();
newdetail.BILLNO = head.BILLNO;
newdetail.BSNO = _thisfee.BSNO;
newdetail.MBLNO = "";
newdetail.HBLNO = "";
newdetail.CUSTOMERNAME = _thisfee.CUSTOMERNAME;
newdetail.BSTYPE = 0;
newdetail.FEEID = _thisfee.GID;
newdetail.FEENAME = _thisfee.FEENAME;
newdetail.CURRENCY = _thisfee.CURRENCY;
newdetail.AMOUNT = resultdetail.splitAmount == 0 ? resultdetail.splitAmountRMB : resultdetail.splitAmount;
newdetail.DOAMOUNT = 0;
newdetail.FEETYPE = 1;
newdetail.CATEGORY = 7;
newdetail.BILLSTATUS = 1;
newdetail.REMARK = null;
newdetail.ISDELETED = false;
newdetail.CREATETIME = DateTime.Now;
newdetail.EXCHANGERATE = _thisfee.EXCHANGERATE;
newdetail.ORIGCURRENCY = _thisfee.CURRENCY;
newdetail.DELETEUSER = null;
newdetail.DELETETIME = null;
newdetail.ORIGAMOUNT = _thisfee.AMOUNT;
newdetail.INVOICESETTLENO = "";
newdetail.COMPANYID = DS7COMPANYID;
//newdetail.ORIGAMOUNT = null;
result.Add(newdetail);
}
}
return result;
}
}
//private static string COMPANYID = ConfigurationManager.AppSettings["COMPANYID"];
//private static string MQURL = ConfigurationManager.AppSettings["MQURL"];
//private static string connStr = ConfigurationManager.AppSettings["ConnectionString"];
//private const string ExchangeName = "inv.result";
//private const string QueueName_Head = "inv.result.";
/// <summary>
/// 根据返回的开票信息 记录开票结果
/// </summary>
/// <param name="InvRoot"></param>
private static List<MqInfo> CORPList = new List<MqInfo>();
private static void addCorp(string corpid, string connstr)
{
CORPList.Add(new MqInfo(corpid, connstr));
}
public InvToD7()
{
ConnectionStringSettingsCollection corpSettlingList = ConfigurationManager.ConnectionStrings;
CORPList = new List<MqInfo>();
string[] keys = ConfigurationManager.AppSettings.AllKeys;
for (int i = 0; i < keys.Length; i++)
{
if (keys[i].IndexOf("DJYUSERID:") == 0)
{
string corpid = keys[i].Replace("DJYUSERID:", "");
//通过Key来索引Value
string value = ConfigurationManager.AppSettings[keys[i]];
addCorp(corpid, value);
}
}
foreach (var corp in CORPList)
{
corp.DoGetInfo();
}
}
public static void StopProcess()
{
foreach (var corp in CORPList)
{
if (corp.mqConn != null && corp.mqConn.IsOpen)
{
corp.mqConn.Close();
corp.mqConn = null;
}
}
}
}
}