using ADODB;
using log4net;
using MSXML2;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UFIDA.U8.Framework.Api.ControlObject.Common;
using UFIDA.U8.Framework.Api.ControlObject.Entity;
using UFIDA.U8.Framework.Api.ControlObject.Json;
using UFIDA.U8.Framework.Api.ControlObject.U8M;
// ReSharper disable StringLiteralTypo
// ReSharper disable CommentTypo
// ReSharper disable IdentifierTypo
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global
namespace UFIDA.U8.Framework.Api.ControlObject.ST
{
///
/// 库存管理
///
public class USERPCO
{
protected readonly ILog logInfo = LogManager.GetLogger("logInfo");
public async Task> Insert(BaseLogin baseLogin, Dictionary dictionary,string cCardNum, string sVouchType,bool bGetCode=false,string codeKey="",bool bCheck=true,bool bBeforCheckStock=true,bool bIsRedVouch=false,string sAddedState="", bool bReMote=false,bool bAudit=false)
{
try
{
var co = "USERPCO.VoucherCO".GetComObject();
var errorMsg = "";
if (!co.IniLogin(baseLogin._vbLogin, ref errorMsg))
{
await Task.Delay(1);
throw new Exception(errorMsg);
}
IXMLDOMDocument2 domHead = new DOMDocument();
IXMLDOMDocument2 domBody = new DOMDocument();
IXMLDOMDocument2 domPosition = new DOMDocument();
errorMsg = "";
if (!co.GetDefaultVoucherDom(sVouchType, cCardNum, ref domHead, ref domBody, ref domPosition, ref errorMsg) || !string.IsNullOrWhiteSpace(errorMsg))
{
throw new Exception(errorMsg);
}
domHead = dictionary.ToDocument2(domHead);
if (!dictionary.TryGetValue("body", out var bodys))
{
await Task.Delay(1);
throw new Exception("无法获取表体数据");
}
if (!(bodys is List> dictionarys) || dictionarys.Count <= 0)
{
await Task.Delay(1);
throw new Exception("无法获取表体数据");
}
if (bGetCode && !string.IsNullOrWhiteSpace(codeKey))
{
errorMsg = "";
var newCode = co.GetNewVouchCode(sVouchType, domHead, ref errorMsg);
logInfo.Debug($"获取新单号:{newCode}");
if (!string.IsNullOrWhiteSpace(newCode))
{
CommonHelper.SetDomKeyValue(domHead, codeKey, newCode);
}
}
domBody = dictionarys.ToDocument2(domBody);
IXMLDOMDocument2 domMsg = new DOMDocument();
errorMsg = "";
baseLogin.Connection.BeginTrans();
var vouchId = "";
var res = new BaseResult();
try
{
res.bSuccess = co.Insert(sVouchType, domHead, domBody, domPosition, ref errorMsg, baseLogin.Connection, ref vouchId, ref domMsg, bCheck, bBeforCheckStock, bIsRedVouch, sAddedState, bReMote);
}
catch (Exception)
{
await Task.Delay(1);
baseLogin.Connection.RollbackTrans();
throw;
}
res.message = errorMsg;
if (res.bSuccess && bAudit)
{
errorMsg = "";
if (!co.Verify(sVouchType, vouchId,ref errorMsg, baseLogin.Connection))
{
baseLogin.Connection.RollbackTrans();
if (!string.IsNullOrWhiteSpace(errorMsg))
throw new Exception(errorMsg);
throw new Exception("审核单据时发生异常,请检查数据");
}
}
if (!res.bSuccess)
{
baseLogin.Connection.RollbackTrans();
if(!string.IsNullOrWhiteSpace(errorMsg))
throw new Exception(errorMsg);
throw new Exception(domMsg.xml);
}
baseLogin.Connection.CommitTrans();
var rst = new Recordset();
rst.Open($"SELECT CardNumber,BTTblName,BTQName,BWTblName,BWQName,VchListQName,VchTblPrimarykeyNames,ReceiptNOFieldName FROM dbo.vouchers WITH(NOLOCK) WHERE CardNumber='{cCardNum}'", baseLogin.Connection);
if (rst.BOF || rst.EOF)
{
res.bSuccess = false;
res.result = $"已在U8生成单据ID为{vouchId}的单据,但是未能查询到该单据的配置信息!";
}
else
{
rst.MoveFirst();
var fieldName = $"{rst.Fields["VchTblPrimarykeyNames"].Value}";
var codeName = $"{rst.Fields["ReceiptNOFieldName"].Value}";
if (string.IsNullOrWhiteSpace(fieldName) || string.IsNullOrWhiteSpace(codeName))
{
res.voucherCode = $"{res.voucherId}";
}
else
{
if (codeName.Contains("{##}"))
{
codeName = codeName.Split('{', '#', '#', '}')[0];
}
var strSql = $"SELECT {codeName} AS cCode FROM {rst.Fields["BTTblName"].Value} WITH(NOLOCK) WHERE {fieldName}='{vouchId}'";
rst.Close();
rst.Open(strSql, baseLogin.Connection);
if (rst.EOF || rst.BOF)
{
res.voucherCode = "";
}
else
{
rst.MoveFirst();
res.voucherCode = $"{rst.Fields["cCode"].Value}";
}
}
}
res.message = "新增成功";
res.result = vouchId;
return res;
//var bodys = dictionary.TryGetValue() dictionary["body"];
}
catch (Exception ex)
{
logInfo.Error(ex.Message, ex);
return new BaseResult { bSuccess = false, message = "新增失败", result = ex.Message };
}
}
public async Task> Update(BaseLogin baseLogin, Dictionary dictionary, string sCardNum , string sVouchType,
bool bCheck = true, bool bBeforCheckStock = true, bool bIsRedVouch = false, string sAddedState = "",
bool bUpdateNeedEas = true)
{
try
{
var co = "USERPCO.VoucherCO".GetComObject();
var errorMsg = "";
if (!co.IniLogin(baseLogin._vbLogin, ref errorMsg))
{
await Task.Delay(1);
throw new Exception(errorMsg);
}
IXMLDOMDocument2 domHead = new DOMDocument();
IXMLDOMDocument2 domBody = new DOMDocument();
IXMLDOMDocument2 domPosition = new DOMDocument();
errorMsg = "";
if (!co.GetDefaultVoucherDom(sVouchType, sCardNum, ref domHead, ref domBody, ref domPosition, ref errorMsg))
{
throw new Exception(errorMsg);
}
domHead = dictionary.ToDocument2(domHead);
var bodys = dictionary["body"] as List> ?? $"{dictionary["body"]}".ToObject>>();
if (bodys==null || bodys.Count<=0)
{
await Task.Delay(1);
throw new Exception("无法获取表体数据");
}
//if (!(bodys is List> dictionarys) || dictionarys.Count <= 0)
//{
// await Task.Delay(1);
// throw new Exception("无法获取表体数据");
//}
domBody = bodys.ToDocument2(domBody);
//IXMLDOMDocument2 domMsg = new DOMDocument();
errorMsg = "";
baseLogin.Connection.BeginTrans();
var vouchId = "";
var res = new BaseResult();
try
{
res.bSuccess = co.Update(sVouchType, domHead, domBody, domPosition);
}
catch (Exception)
{
baseLogin.Connection.RollbackTrans();
throw;
}
if (!res.bSuccess)
{
baseLogin.Connection.RollbackTrans();
res.message = "新增失败";
res.result = errorMsg;
}
else
{
baseLogin.Connection.CommitTrans();
res.message = "新增成功";
res.result = vouchId;
}
return res;
//var bodys = dictionary.TryGetValue() dictionary["body"];
}
catch (Exception ex)
{
logInfo.Error(ex.Message, ex);
return new BaseResult { bSuccess = false, message = "新增失败", result = ex.Message };
}
}
}
}