Files
zszq-trs/YLErpDAL/BLL/GenerateDocument/BatchGenerateDocs.cs
T
2024-05-09 14:06:26 +08:00

143 lines
4.8 KiB
C#

using YLErp.Commons;
using YLErp.DBModels.Consts;
using YLErp.Model;
namespace YLErp.BLL.GenerateDocument
{
public abstract class BatchGenerateDocs
{
protected YLContext db = new YLContext();
private static Dictionary<string, string> tradeTypeDic;
private static Dictionary<string, string> contractTypeDic;
static BatchGenerateDocs()
{
tradeTypeDic = new Dictionary<string, string>
{
{"香草期权","" },
{"亚式期权","Asian" },
{"价差期权","Spread" }
};
contractTypeDic = new Dictionary<string, string>
{
{ContractTypeEnum.Trade,"GenerateConfirmBook" },
{ContractTypeEnum.AdditionalTrade,"GenerateAdditionalConfirmBook" },
{ContractTypeEnum.Clearing,"GenerateSettleBill" },
{ContractTypeEnum.UnWind,"GenerateUnwindReport" }
};
}
protected BatchGenerateDocs() { }
protected BatchGenerateDocs(List<int> tradeIds, string docType, int optId, string optName, List<int> tradeCashIds)
{
TradeIdList = tradeIds;
DocType = docType;
OptId = optId;
OptName = optName;
}
public List<int> TradeIdList { get; set; }
public string DocType { get; set; }
public int OptId { get; set; }
public string OptName { get; set; }
protected virtual string ContractType { get; }
public abstract List<trade_contract_group> GetDatas();
public virtual ReturnInfo<List<string>> GenerateBooks()
{
var tradeList = GetDatas();
if (tradeList.Count == 0)
{
return Return.Fail<List<string>>("没有相关的交易!");
}
var fileList = new List<string>();
var errorList = new List<string>();
foreach (var tradeContractGroup in tradeList)
{
if (tradeContractGroup.trade_cash == null)
{
errorList.Add($"{tradeContractGroup.trade.TradeNumber}生成失败,因为该交易没有现金交割记录");
continue;
}
var baseGenerate = GetGenerateInstance(tradeContractGroup);
var returnInfo = baseGenerate.GenerateBook();
if (!returnInfo.Success)
{
errorList.Add($"{tradeContractGroup.trade.TradeNumber}生成失败,因为{returnInfo.Msg}");
continue;
}
fileList.Add(returnInfo.Content);
}
if (errorList.Count > 0)
{
return Return.Fail(string.Join("\r\n", errorList), fileList);
}
return Return.Success(fileList);
}
//protected virtual string GetContractType(trade_contract_group tradeContractGroup)
//{
// return ContractTypeEnum;
//}
protected BaseGenerate GetGenerateInstance(trade_contract_group tradeContractGroup)
{
string tradeTypeClass;
var tradeType = tradeContractGroup.trade.TradeType;
if (tradeType == "结构化交易" && tradeContractGroup.trade.StructureType.IndexOf("价差") > -1)
{
tradeType = "价差期权";
}
if (!tradeTypeDic.TryGetValue(tradeType, out tradeTypeClass))
{
tradeTypeClass = "";
}
string className;
if (!contractTypeDic.TryGetValue(ContractType, out className))
{
className = "GenerateConfirmBook";
}
#region 按照优先级,选择可以使用的类
var typeFullName = $"YLErp.BLL.GenerateDocument.{PS.Config.CompanyAbbreviation}.{tradeTypeClass}{className}";
var type = Type.GetType(typeFullName, false, true);
if (type == null)
{
typeFullName = $"YLErp.BLL.GenerateDocument.{PS.Config.CompanyAbbreviation}.{className}";
type = Type.GetType(typeFullName, false, true);
}
if (type == null)
{
typeFullName = $"YLErp.BLL.GenerateDocument.{tradeTypeClass}General{className}";
type = Type.GetType(typeFullName, false, true);
}
if (type == null)
{
typeFullName = $"YLErp.BLL.GenerateDocument.General{className}";
type = Type.GetType(typeFullName, false, true);
}
#endregion
if (type == null)
{
throw new ServiceException("此功能暂不支持");
}
return Activator.CreateInstance(type, tradeContractGroup, DocType, OptId, OptName) as BaseGenerate;
}
}
}