using Newtonsoft.Json; using NPOI.SS.Formula.PTG; using OASystem.Domain; using OASystem.Domain.Dtos.Financial; using OASystem.Domain.Dtos.Groups; using OASystem.Domain.Entities.Financial; using OASystem.Domain.Entities.Groups; using OASystem.Domain.ViewModels.Financial; using OASystem.Domain.ViewModels.Groups; using OASystem.Infrastructure.Repositories.System; using Serilog; using SqlSugar; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OASystem.Infrastructure.Repositories.Groups { /// /// 团组汇率 仓库 /// public class TeamRateRepository : BaseRepository { private readonly SetDataRepository _setDataRep; private readonly DelegationInfoRepository _deleInfoRep; public TeamRateRepository(SqlSugarClient sqlSugar, SetDataRepository setDataRep, DelegationInfoRepository deleInfoRep) : base(sqlSugar) { _setDataRep = setDataRep; _deleInfoRep = deleInfoRep; } /// /// 团组汇率 Rep /// /// /// public async Task GetGroupRateDataSource(TeamRateDto dto) { Result result = new() { Code = -2 }; Stopwatch stopwatch = Stopwatch.StartNew(); GroupNameDto groupNameDto = new GroupNameDto() { PortType = dto.PortType }; var groups = await _deleInfoRep.GetGroupNameList(groupNameDto); var teamRateDatas = await _setDataRep.GetSetDataBySTId(_setDataRep, 66); // STid=66 全球币种 dynamic _data = null; if (dto.PortType == 1) { //List sys_SetDatas = new List(); //sys_SetDatas = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(teamRateDatas.Data)); //List teamRateDescViews = new List(); //teamRateDescViews = sys_SetDatas.Select(it => new TeamRateDescView() { CurrencyName = it.Remark, CurrencyCode = it.Name, Rate = 0.0000M }).ToList(); ///* // * 放在首位币种 // * 人民币 CNY Rate = 1,美元 USD,欧元 EUR,港币 HKD,日元 JPY ,澳大利 AUD,英镑 GBP // * 依次排序 // */ ////人民币 //var cnyData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("CNY")); //if (cnyData != null) { teamRateDescViews.Remove(cnyData); cnyData.Rate = 1.0000M; teamRateDescViews.Insert(0, cnyData); } ////美元 //var usdData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("USD")); //if (usdData != null) { teamRateDescViews.Remove(usdData); teamRateDescViews.Insert(1, usdData); } ////欧元 //var eurData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("EUR")); //if (eurData != null) { teamRateDescViews.Remove(eurData); teamRateDescViews.Insert(2, eurData); } ////港币 //var hkdData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("HKD")); //if (hkdData != null) { teamRateDescViews.Remove(hkdData); teamRateDescViews.Insert(3, hkdData); } ////日元 //var jpyData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("JPY")); //if (jpyData != null) { teamRateDescViews.Remove(jpyData); teamRateDescViews.Insert(4, jpyData); } ////澳大利 //var audData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("AUD")); //if (audData != null) { teamRateDescViews.Remove(audData); teamRateDescViews.Insert(5, audData); } ////英镑 //var gbpData = teamRateDescViews.Find(it => it.CurrencyCode.Equals("GBP")); //if (gbpData != null) { teamRateDescViews.Remove(gbpData); teamRateDescViews.Insert(6, gbpData); } _data = new { GroupData = groups.Data, TeamRateData = await PostGroupTeamRateHot() }; } else if (dto.PortType == 2) { } else if (dto.PortType == 3) { } stopwatch.Stop(); result.Code = 0; result.Msg = $"查询成功!耗时:{stopwatch.ElapsedMilliseconds / 1000}s"; result.Data = _data; return result; } /// /// 团组汇率 更改数据 /// /// /// public async Task GetGroupRateChangeData() { Result result = new() { Code = -2 }; string teamRateInfoSql = string.Format(@"Select * From Grp_TeamRate tr Where tr.IsDel = 0"); var teamRateInfo = await _sqlSugar.SqlQueryable(teamRateInfoSql).ToListAsync(); dynamic _data = null; #region 团组汇率 - 详细汇率信息 List otherCurrencyData1 = new List(); var otherCurrencyData = await _setDataRep.GetSetDataBySTId(_setDataRep, 13); if (otherCurrencyData.Code == 0 && otherCurrencyData.Data != null) { string otherCurrencyStr = JsonConvert.SerializeObject(otherCurrencyData.Data); otherCurrencyData1 = JsonConvert.DeserializeObject>(otherCurrencyStr); //var otherCurrency = otherCurrencyData1 } foreach (Grp_TeamRate item1 in teamRateInfo) { string rateStr = string.Empty; if (item1.RateU != 0) rateStr += "美元(USD):" + item1.RateU + "|"; if (item1.RateE != 0) rateStr += "欧元(EUR):" + item1.RateE + "|"; if (item1.RateJ != 0) rateStr += "日元(JPY):" + item1.RateJ + "|"; if (item1.RateH != 0) rateStr += "港币(HKD):" + item1.RateH + "|"; if (item1.RateN != 0) rateStr += "新西兰元(NZD):" + item1.RateN + "|"; if (item1.RateS != 0) rateStr += "新加坡币(SGD):" + item1.RateS + "|"; if (item1.RateA != 0) rateStr += "澳大利亚元(AUD):" + item1.RateA + "|"; if (item1.RateC != 0) rateStr += "加拿大元(CAD):" + item1.RateC + "|"; if (item1.RateT != 0) rateStr += "泰国铢(THB):" + item1.RateT + "|"; if (item1.RateBL != 0) rateStr += "波兰兹罗提(PLN):" + item1.RateBL + "|"; if (item1.RateHB != 0) rateStr += "韩国元(KRW):" + item1.RateHB + "|"; if (item1.RateFJD != 0) rateStr += "斐济元(FJD):" + item1.RateFJD + "|"; if (item1.RateTL != 0) rateStr += "土耳其里拉(TRY):" + item1.RateTL + "|"; if (item1.RateRP != 0) rateStr += "印度尼西亚卢比(IDR):" + item1.RateRP + "|"; if (item1.RatePeso != 0) rateStr += "菲律宾比索(PHP):" + item1.RatePeso + "|"; if (item1.RateMYR != 0) rateStr += "林吉特(MYR):" + item1.RateMYR + "|"; if (item1.RateCZK != 0) rateStr += "捷克货币(CZK):" + item1.RateCZK + "|"; if (item1.RateMXN != 0) rateStr += "墨西哥比索(MXN):" + item1.RateMXN + "|"; if (item1.RateMOP != 0) rateStr += "澳门元(MOP):" + item1.RateMOP + "|"; if (item1.RateARS != 0) rateStr += "阿根廷比索(ARS):" + item1.RateARS + "|"; if (item1.RateHUF != 0) rateStr += "匈牙利福林(HUF):" + item1.RateHUF + "|"; if (item1.RateRUB != 0) rateStr += "卢布(RUB):" + item1.RateRUB + "|"; if (!string.IsNullOrEmpty(item1.OtherRateCode)) { rateStr += $"{otherCurrencyData1.Find(it => it.Name.Equals(item1.OtherRateCode))?.Remark ?? "Unknown"}({item1.OtherRateCode}):{item1.OtherPrice}|"; } if (!rateStr.Contains("CNY")) { rateStr += "人民币(CNY):1.0000|"; } if (rateStr.Length > 0) { rateStr = rateStr.Substring(0, rateStr.Length - 1); } item1.Remark = rateStr; } #endregion try { var _result = _sqlSugar.Updateable(teamRateInfo) .UpdateColumns(it => new { it.Remark }) //更新列 .WhereColumns(it => new { it.DiId, it.CTable }) //更新条件 .ExecuteCommand(); result.Code = 0; result.Msg = "数据修改成功!"; result.Data = _data; } catch (Exception ex) { result.Msg = ex.Message; } return result; } /// /// 团组汇率 Rep /// /// /// public async Task GetGroupRateInfoByDiid(TeamRateInfoDto dto) { Result result = new() { Code = -2 }; GroupInfoDto groupInfoDto = new GroupInfoDto() { Id = dto.Diid, PortType = dto.PortType, PageIndex = dto.PageIndex, PageSize = dto.PageSize }; var groupInfo = await _deleInfoRep.GetGroupInfo(groupInfoDto); string teamRateInfoSql = string.Format(@"Select sd.Name,tr.* From Grp_TeamRate tr Left Join Sys_SetData sd On sd.IsDel=0 And sd.STid=16 And tr.CTable = sd.Id Where tr.DiId = {0} And tr.IsDel = 0", dto.Diid); var teamRateInfo = await _sqlSugar.SqlQueryable(teamRateInfoSql).ToListAsync(); dynamic _data = null; if (dto.PortType == 1) { #region 团组汇率 List teamRateModels = new List(); foreach (TeamRateInfoView item in teamRateInfo) { TeamRateModelView teamRateModelInfo = new TeamRateModelView(); teamRateModelInfo.Id = item.Id; teamRateModelInfo.CTableId = item.CTable; teamRateModelInfo.CTableName = item.Name; List teamRateDescViews = new List(); #region 拆分remark里的汇率 if (item.Remark.Contains("|")) { string[] currencyArr = item.Remark.Split("|"); foreach (string currency in currencyArr) { string[] currency1 = currency.Split(":"); string[] currency2 = currency1[0].Split("("); TeamRateDescView rateDescView = new TeamRateDescView() { CurrencyCode = currency2[1].Replace(")", "").TrimEnd(), CurrencyName = currency2[0], Rate = decimal.Parse(currency1[1]), }; teamRateDescViews.Add(rateDescView); } } else { if (!string.IsNullOrEmpty(item.Remark)) { string[] currency1 = item.Remark.Split(":"); string[] currency2 = currency1[0].Split("("); TeamRateDescView rateDescView = new TeamRateDescView() { CurrencyCode = currency2[1].Replace(")", "").TrimEnd(), CurrencyName = currency2[0], Rate = decimal.Parse(currency1[1]), }; teamRateDescViews.Add(rateDescView); } } #endregion teamRateModelInfo.TeamRates = teamRateDescViews; teamRateModels.Add(teamRateModelInfo); } #endregion _data = new { GroupInfo = groupInfo.Data, TeamRateData = teamRateModels }; } else if (dto.PortType == 2) { } else if (dto.PortType == 3) { } result.Code = 0; result.Msg = "查询成功!"; result.Data = _data; return result; } /// /// 团组汇率 更新 or 添加 /// /// /// public async Task PostGroupRateUpdate(TeamRateUpdateDto dto) { Result result = new() { Code = -2 }; int addCount = 0, updateCount = 0; if (dto.PortType == 1) { try { _sqlSugar.BeginTran(); int res = -1; foreach (TeamRateUpdateInfo item in dto.teamRateUpdateInfos) { Grp_TeamRate teamRateInfo = new Grp_TeamRate(); string currencyStr = string.Empty; foreach (TeamRateDescView currency in item.teamRates) { currencyStr += string.Format(@"{0}({1}):{2}|", currency.CurrencyName, currency.CurrencyCode, currency.Rate); } if (currencyStr.Length > 0) { currencyStr = currencyStr.Substring(0, currencyStr.Length - 1); } teamRateInfo.DiId = dto.DiId; teamRateInfo.CreateUserId = dto.CreateUserId; teamRateInfo.Id = item.Id; teamRateInfo.CreateTime = DateTime.Now; teamRateInfo.CTable = item.CTable; teamRateInfo.Remark = currencyStr; var teamRateDetails = _sqlSugar.Queryable().Where(it => it.DiId == dto.DiId && it.CTable == item.CTable).First(); if (teamRateDetails == null) //添加 { res = await _sqlSugar.Insertable(teamRateInfo).ExecuteCommandAsync(); if (res > 0) { addCount++; } } else //修改 { teamRateInfo.Id = teamRateDetails.Id; res = await _sqlSugar.Updateable(teamRateInfo) .UpdateColumns(it => it.Remark) .WhereColumns(it => new { it.DiId, it.CTable }) .ExecuteCommandAsync(); if (res > 0) { //团组汇率更改成功;更改C表对应汇率 及rmb金额 var ccpInfos = _sqlSugar.Queryable().Where(it => it.IsDel == 0 && it.DIId == dto.DiId && it.CTable == item.CTable).ToList(); var currData = _sqlSugar.Queryable().Where(it => it.IsDel == 0 && it.STid == 66).ToList(); if (ccpInfos.Count > 0) { foreach (var ccpInfo in ccpInfos) { string currCode = currData.Find(it => it.Id == ccpInfo.PaymentCurrency)?.Name ?? ""; decimal thisRate = item.teamRates.Find(it => it.CurrencyCode.Equals(currCode))?.Rate ?? 0.00M; if (thisRate > 0.00M) { ccpInfo.DayRate = thisRate; ccpInfo.RMBPrice = ccpInfo.PayMoney * thisRate; } } var ccpStatus = _sqlSugar.Updateable(ccpInfos) .UpdateColumns(it => new { it.DayRate, it.RMBPrice }) .WhereColumns(it => it.Id) .ExecuteCommand(); } updateCount++; } } if (res < 1) { _sqlSugar.RollbackTran(); result.Msg = "操作失败!"; break; } } if (res > 0) { _sqlSugar.CommitTran(); result.Code = 0; result.Msg = "操作成功"; } } catch (Exception ex) { result.Msg = ex.Message; } } else if (dto.PortType == 2) { } else if (dto.PortType == 3) { } result.Code = 0; result.Msg = string.Format(@"操作成功!添加:{0}条;更新:{1};", addCount, updateCount); return result; } /// /// 团组汇率 Rep /// 根据团组Id 查询汇率 /// /// /// public async Task> PostGroupRateInfoByDiId(int diId) { List teamRateModels = new List(); string teamRateInfoSql = string.Format(@"Select sd.Name,tr.* From Grp_TeamRate tr Left Join Sys_SetData sd On sd.IsDel=0 And sd.STid=16 And tr.CTable = sd.Id Where tr.DiId = {0} And tr.IsDel = 0", diId); var teamRateInfo = await _sqlSugar.SqlQueryable(teamRateInfoSql).ToListAsync(); #region 团组汇率 foreach (TeamRateInfoView item in teamRateInfo) { TeamRateModelView teamRateModelInfo = new TeamRateModelView(); teamRateModelInfo.Id = item.Id; teamRateModelInfo.CTableId = item.CTable; teamRateModelInfo.CTableName = item.Name; //拆分remark里的汇率 teamRateModelInfo.TeamRates = GroupCurrencySplittings(item.Remark); teamRateModels.Add(teamRateModelInfo); } #endregion return teamRateModels; } /// /// 更改团组对应模块 对应币种汇率 /// /// 团组Id /// 团组模块 /// 币种code /// 币种汇率 /// public async Task UpdateGroupRateByDiIdAndCTableId(int diId, int cTableId, string currCode, decimal currRate) { if (diId < 1) return false; if (cTableId < 1) return false; var _info = await PostGroupTeamRateByDiIdAndCTableId(1, diId, cTableId); TeamRateUpdateDto OPDto = new TeamRateUpdateDto(); OPDto.DiId = diId; var teamRates = new List(); if (_info == null) { var currData = _sqlSugar.Queryable().Where(it => it.IsDel == 0 && it.STid == 66 && it.Name.Equals(currCode)).First(); teamRates.Add( new TeamRateUpdateInfo() { Id = 0, CTable = cTableId, teamRates = new List { new TeamRateDescView() { CurrencyName = currData?.Remark, CurrencyCode = currCode, Rate = currRate } } } ); } else { var rateCurrInfo = new TeamRateUpdateInfo(); rateCurrInfo.CTable = cTableId; var currInfos = new List(); foreach (var item in _info.TeamRates) { var currInfo = new TeamRateDescView(); currInfo.CurrencyName = item.CurrencyName; currInfo.CurrencyCode = item.CurrencyCode; if (item.CurrencyCode.Equals(currCode)) currInfo.Rate = currRate; else currInfo.Rate = item.Rate; currInfos.Add(currInfo); } rateCurrInfo.teamRates = currInfos; teamRates.Add(rateCurrInfo); } OPDto.teamRateUpdateInfos = teamRates; await PostGroupRateUpdate(OPDto); return true; } /// /// 团组汇率币种拆分 /// 团组币种按指定格式拆分 返回集合 /// /// /// public List GroupCurrencySplittings(string currStr) { List _view = new List(); if (!string.IsNullOrEmpty(currStr)) { if (currStr.Contains("|")) { string[] currencyArr = currStr.Split("|"); foreach (string currency in currencyArr) { _view.Add(GroupCurrencySplittingSingle(currency)); } } else { _view.Add(GroupCurrencySplittingSingle(currStr)); } } return _view; } /// /// 团组汇率币种拆分 /// 团组币种按指定格式拆分 返回集合 /// return TeamRateDescView /// /// /// public TeamRateDescView GroupCurrencySplittingSingle(string currStr) { TeamRateDescView _view = new TeamRateDescView(); if (!string.IsNullOrEmpty(currStr)) { string[] currency1 = currStr.Split(":"); string[] currency2 = currency1[0].Split("("); _view = new TeamRateDescView() { CurrencyCode = currency2[1].Replace(")", "").TrimEnd(), CurrencyName = currency2[0], Rate = decimal.Parse(currency1[1]), }; } return _view; } /// /// 团组汇率币种拆分 注意返回 实体类 /// 团组币种按指定格式拆分 返回集合 /// return TeamRateDescView /// /// /// public TeamRateDescAddCurrencyIdView GroupCurrencySplittingSingle(List currs,string currStr) { TeamRateDescAddCurrencyIdView _view = new TeamRateDescAddCurrencyIdView(); if (!string.IsNullOrEmpty(currStr)) { string[] currency1 = currStr.Split(":"); string[] currency2 = currency1[0].Split("("); string currencyCode = currency2[1].Replace(")", "").TrimEnd(); SetDataInfoView dataInfoView = new SetDataInfoView(); dataInfoView = currs.Where(it => it.Name == currencyCode).FirstOrDefault(); int currencyId = currs.Where(it => it.Name == currencyCode).FirstOrDefault().Id; _view = new TeamRateDescAddCurrencyIdView() { CurrencyId = dataInfoView.Id, CurrencyCode = currencyCode, CurrencyName = currency2[0], Rate = decimal.Parse(currency1[1]), }; } return _view; } /// /// 团组汇率 币种 Item (来源:团组汇率) /// 根据 团组Id And 业务类型(CTable)Id /// api处理CTable = 285,默认返回CNY /// /// /// /// /// public async Task PostGroupTeamRateByDiIdAndCTableId(int portType, int diId, int cTable) { TeamRateModelGeneralView _view = new TeamRateModelGeneralView(); if (diId < 1) { return _view; } if (cTable < 1) { return _view; } if (portType == 1 || portType == 2 || portType == 3) { string teamRateInfoSql = string.Format(@"Select sd.Name,tr.* From Grp_TeamRate tr Left Join Sys_SetData sd On sd.IsDel=0 And sd.STid=16 And tr.CTable = sd.Id Where tr.IsDel = 0 And tr.DiId = {0} And tr.CTable = {1}", diId, cTable); var teamRateInfo = _sqlSugar.SqlQueryable(teamRateInfoSql).ToList(); #region 团组汇率 #region 单独处理Ctable 币种汇率 默认 为CNY if (cTable == 285) { _view = new TeamRateModelGeneralView() { Id = 0, CTableId = 285, CTableName = "其他款项与收款退还", TeamRates = new List() { new TeamRateDescAddCurrencyIdView() { CurrencyId = 836, CurrencyCode = "CNY", CurrencyName = "人民币", Rate = 1.0000M, } } }; return _view; } #endregion List currencyDatas = new List(); #region 获取所有币种 string sql = string.Format(@"select * from Sys_SetData where STid = {0} and isdel = 0", 66); var DBdata = _setDataRep.GetListBySqlWithNolock(sql); if (DBdata == null || DBdata.Count == 0) { return _view; } currencyDatas = DBdata.Select(x => new SetDataInfoView { Name = x.Name, Id = x.Id, Remark = x.Remark, }).ToList(); #endregion foreach (TeamRateInfoView item in teamRateInfo) { TeamRateModelGeneralView teamRateModelInfo = new TeamRateModelGeneralView(); teamRateModelInfo.Id = item.Id; teamRateModelInfo.CTableId = item.CTable; teamRateModelInfo.CTableName = item.Name; List teamRateDescViews = new List(); #region 拆分remark里的汇率 if (item.Remark.Contains("|")) { string[] currencyArr = item.Remark.Split("|"); foreach (string currency in currencyArr) { teamRateDescViews.Add(GroupCurrencySplittingSingle(currencyDatas, currency)); } } else { teamRateDescViews.Add(GroupCurrencySplittingSingle(currencyDatas, item.Remark)); } #endregion if (teamRateDescViews.Count > 0) { teamRateDescViews = teamRateDescViews.OrderBy(it => it.CurrencyId).ToList(); } teamRateModelInfo.TeamRates = teamRateDescViews; _view = teamRateModelInfo; } if (_view.TeamRates == null) { _view.TeamRates = new List(); } #endregion } return _view; } /// /// 获取团组相关模块的currencyList /// /// /// /// /// public async Task> PostGroupTeamRateItemByDiIdAndCTableId(int portType, int diId, int cTable) { List teamRateDescAddCurrencyIdViews = new List(); TeamRateModelGeneralView teamRateModelGeneralView = await PostGroupTeamRateByDiIdAndCTableId(portType, diId, cTable); teamRateDescAddCurrencyIdViews = teamRateModelGeneralView.TeamRates; return teamRateDescAddCurrencyIdViews; } /// /// 获取团组汇率 币种使用次数 /// /// public async Task> PostGroupTeamRateHot() { List _view = new List(); string teamRateInfoSql = string.Format(@"Select sd.Name,tr.* From Grp_TeamRate tr Left Join Sys_SetData sd On sd.IsDel=0 And sd.STid=16 And tr.CTable = sd.Id Where tr.IsDel = 0 "); var teamRateInfo = _sqlSugar.SqlQueryable(teamRateInfoSql).ToList(); #region 获取所有币种 string sql = string.Format(@"Select * From Sys_SetData Where STid = {0} And IsDel = 0", 66); var currencyDatas = _sqlSugar.SqlQueryable(sql).ToList(); _view = currencyDatas.Select(it => new CurrencyHot() { CurrencyId = it.Id, CurrencyCode = it.Name, CurrencyName = it.Remark}).ToList(); #endregion List teamRateDatas = new List(); foreach (TeamRateInfoView item in teamRateInfo) { List teamRateDescViews = new List(); #region 拆分remark里的汇率 if (item.Remark.Contains("|")) { string[] currencyArr = item.Remark.Split("|"); foreach (string currency in currencyArr) { teamRateDescViews.Add(GroupCurrencySplittingSingle(currencyDatas, currency)); } } else { teamRateDescViews.Add(GroupCurrencySplittingSingle(currencyDatas, item.Remark)); } #endregion if (teamRateDescViews.Count > 0) { teamRateDescViews = teamRateDescViews.OrderBy(it => it.CurrencyId).ToList(); teamRateDatas.AddRange(teamRateDescViews); } } foreach (var item in _view) { if (item.CurrencyId == 836) // 人民币 { item.Rate = 1.0000M; } item.Number = teamRateDatas.Where(it => item.CurrencyId == it.CurrencyId).ToList().Count; } if (_view.Count > 0) { _view = _view.OrderByDescending(it => it.Number).ToList(); } return _view; } } }