InvitationOfficialActivitiesRepository.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. using AutoMapper;
  2. using MathNet.Numerics.Statistics.Mcmc;
  3. using NPOI.SS.Formula.Functions;
  4. using OASystem.Domain;
  5. using OASystem.Domain.Dtos.Groups;
  6. using OASystem.Domain.Entities.Financial;
  7. using OASystem.Domain.Entities.Groups;
  8. using OASystem.Domain.Entities.Resource;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Domain.ViewModels.QiYeWeChat;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace OASystem.Infrastructure.Repositories.Groups
  18. {
  19. public class InvitationOfficialActivitiesRepository : BaseRepository<Grp_InvitationOfficialActivities, InvitationOfficialActivitiesListDto>
  20. {
  21. private readonly IMapper _mapper;
  22. public InvitationOfficialActivitiesRepository(SqlSugarClient sqlSugar, IMapper mapper) :
  23. base(sqlSugar)
  24. {
  25. this._mapper = mapper;
  26. }
  27. /// <summary>
  28. /// 根据商邀费用Id查询C表和商邀资料及费用表
  29. /// </summary>
  30. /// <param name="dto"></param>
  31. /// <returns></returns>
  32. public async Task<Result> InvitationOfficialActivitiesById(InvitationOfficialActivitiesByIdDto dto)
  33. {
  34. Result result = new Result() { Code = -2, Msg = "程序错误" };
  35. try
  36. {
  37. Grp_InvitationOfficialActivities grp_Invitation = _sqlSugar.Queryable<Grp_InvitationOfficialActivities>().First(a => a.Id == dto.Id && a.IsDel == 0);
  38. Grp_CreditCardPayment grp_CreditCard = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == dto.Id && a.IsDel == 0 && a.CTable==81);
  39. Res_InvitationOfficialActivityData res_InvitationOfficialActivityData = new Res_InvitationOfficialActivityData();
  40. if (grp_Invitation!=null)
  41. {
  42. res_InvitationOfficialActivityData = _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().First(a =>a.Country==grp_Invitation.InviterArea && a.UnitName==grp_Invitation.Inviter && a.IsDel == 0);
  43. if (dto.PortType == 1)
  44. {
  45. var data = new
  46. {
  47. _Invitation = grp_Invitation,
  48. _InvitationData = res_InvitationOfficialActivityData,
  49. _CreditCard = grp_CreditCard,
  50. };
  51. result = new Result() { Code = 0, Msg = "查询成功!", Data = data };
  52. }
  53. else
  54. {
  55. InvitationOfficialActivitiesByIdView invitation = _mapper.Map<InvitationOfficialActivitiesByIdView>(grp_Invitation);
  56. invitation.PayDId = grp_CreditCard.PayDId;
  57. invitation.OrbitalPrivateTransfer = grp_CreditCard.OrbitalPrivateTransfer;
  58. invitation.Payee = grp_CreditCard.Payee;
  59. invitation.Address = res_InvitationOfficialActivityData.Address;
  60. invitation.Contact = res_InvitationOfficialActivityData.Contact;
  61. invitation.Job = res_InvitationOfficialActivityData.Job;
  62. invitation.Tel = res_InvitationOfficialActivityData.Tel;
  63. invitation.Email = res_InvitationOfficialActivityData.Email;
  64. invitation.Fax = res_InvitationOfficialActivityData.Fax;
  65. invitation.OtherInformation = res_InvitationOfficialActivityData.Remark;
  66. result = new Result() { Code = 0, Msg = "查询成功!", Data = invitation };
  67. }
  68. }
  69. else
  70. {
  71. if (dto.PortType == 1)
  72. {
  73. var data = new
  74. {
  75. _Invitation = grp_Invitation,
  76. _InvitationData = res_InvitationOfficialActivityData,
  77. _CreditCard = grp_CreditCard,
  78. };
  79. result = new Result() { Code = 0, Msg = "暂无数据!", Data = data };
  80. }
  81. else
  82. {
  83. InvitationOfficialActivitiesByIdView invitation = _mapper.Map<InvitationOfficialActivitiesByIdView>(grp_Invitation);
  84. result = new Result() { Code = 0, Msg = "暂无数据!", Data = invitation };
  85. }
  86. }
  87. }
  88. catch (Exception)
  89. {
  90. result = new Result() { Code = -2, Msg = "程序错误" };
  91. throw;
  92. }
  93. return result;
  94. }
  95. /// <summary>
  96. /// 根据团组id查询商邀费用数据
  97. /// </summary>
  98. /// <param name="dto"></param>
  99. /// <returns></returns>
  100. public async Task<Result> InvitationOfficialActivitiesList(InvitationOfficialActivitiesListDto dto)
  101. {
  102. Result result = new Result() { Code = -2, Msg = "未知错误" };
  103. try
  104. {
  105. string sqlWhere = string.Empty;
  106. if (!string.IsNullOrWhiteSpace(dto.Inviter))
  107. {
  108. sqlWhere += string.Format(@" And Inviter like '%{0}%'", dto.Inviter);
  109. }
  110. if (!string.IsNullOrWhiteSpace(dto.StartInviteTime) && !string.IsNullOrWhiteSpace(dto.EndInviteTime))
  111. {
  112. sqlWhere += string.Format(@" And i.InviteTime between '{0}' and '{1}'", dto.StartInviteTime, dto.EndInviteTime);
  113. }
  114. sqlWhere += string.Format(@" And i.DiId={0} And i.IsDel={1}", dto.DiId, 0);
  115. string UserId = "";
  116. List<Grp_GroupsTaskAssignment> gtaUIdList = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DiId && a.IsDel == 0 && a.CTId == 81).ToList();
  117. foreach (Grp_GroupsTaskAssignment gta in gtaUIdList)
  118. UserId += gta.UId + ",";
  119. if (!string.IsNullOrWhiteSpace(UserId))
  120. {
  121. UserId = UserId.Substring(0, UserId.Length - 1);
  122. }
  123. else
  124. {
  125. UserId = "0";
  126. }
  127. sqlWhere += string.Format(@" And i.CreateUserId in ({0})", UserId);
  128. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  129. {
  130. Regex r = new Regex("And");
  131. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  132. }
  133. string sql = string.Format(@"select Id,DiId,InviterArea,Inviter,InviteTime,InviteCost,(select name from Sys_SetData where id=i.InviteCurrency and IsDel={0})
  134. as Currency,IsGoOfficaiaBussiness,(select IsAuditGM from Grp_CreditCardPayment where CTable=81 and CId=i.Id
  135. and IsDel={0}) as 'isAudit',Attachment from Grp_InvitationOfficialActivities i {1}", 0, sqlWhere);
  136. List<InvitationOfficialActivitiesView> _DecreasePayments = await _sqlSugar.SqlQueryable<InvitationOfficialActivitiesView>(sql).ToListAsync();
  137. if (_DecreasePayments.Count != 0)
  138. {
  139. if (dto.PageIndex != 0 && dto.PageSize != 0)
  140. {
  141. int count = _DecreasePayments.Count;
  142. float totalPage = (float)count / dto.PageSize;//总页数
  143. if (totalPage == 0) totalPage = 1;
  144. else totalPage = (int)Math.Ceiling((double)totalPage);
  145. List<InvitationOfficialActivitiesView> invitationOfficialActivities = new List<InvitationOfficialActivitiesView>();
  146. for (int i = 0; i < dto.PageSize; i++)
  147. {
  148. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  149. if (RowIndex < _DecreasePayments.Count)
  150. {
  151. invitationOfficialActivities.Add(_DecreasePayments[RowIndex]);
  152. }
  153. else
  154. {
  155. break;
  156. }
  157. }
  158. ListViewBase<InvitationOfficialActivitiesView> rst = new ListViewBase<InvitationOfficialActivitiesView>();
  159. rst.DataList = invitationOfficialActivities;
  160. rst.DataCount = count;
  161. rst.CurrPageIndex = dto.PageIndex;
  162. rst.CurrPageSize = dto.PageSize;
  163. return result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  164. }
  165. else
  166. {
  167. return result = new Result() { Code = 0, Msg = "查询成功!", Data = _DecreasePayments };
  168. }
  169. }
  170. else
  171. {
  172. if (dto.PageIndex != 0 && dto.PageSize != 0)
  173. {
  174. ListViewBase<InvitationOfficialActivitiesView> rst = new ListViewBase<InvitationOfficialActivitiesView>();
  175. rst.DataList = _DecreasePayments;
  176. rst.DataCount = _DecreasePayments.Count;
  177. rst.CurrPageIndex = dto.PageIndex;
  178. rst.CurrPageSize = dto.PageSize;
  179. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = rst };
  180. }
  181. else
  182. {
  183. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = _DecreasePayments };
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. return result = new Result() { Code = -2, Msg = "未知错误" };
  190. }
  191. }
  192. /// <summary>
  193. /// 商邀费用操作(Status:1.新增,2.修改)
  194. /// </summary>
  195. /// <param name="dto"></param>
  196. /// <returns></returns>
  197. public async Task<Result> OpInvitationOfficialActivities(OpInvitationOfficialActivitiesDto dto, Func<int, int, int, Task<Result>> fn)
  198. {
  199. Result result = new Result() { Code = -2, Msg = "程序错误!" };
  200. BeginTran();
  201. try
  202. {
  203. int id = 0;
  204. Grp_InvitationOfficialActivities grp_Invitation = _mapper.Map<Grp_InvitationOfficialActivities>(dto);
  205. if (dto.Status == 1)//添加
  206. {
  207. string selectSql = string.Format(@"select * from Grp_InvitationOfficialActivities where InviterArea='{0}' and Inviter='{1}' and DiId={2} and IsDel={3}"
  208. , dto.InviterArea, dto.Inviter, dto.DiId, 0);
  209. var _InvitationOfficialActivities = await _sqlSugar.SqlQueryable<Grp_InvitationOfficialActivities>(selectSql).FirstAsync();//查询是否存在
  210. if (_InvitationOfficialActivities != null)
  211. {
  212. return result = new Result() { Code = -1, Msg = "该数据已存在,请勿重复添加!" };
  213. }
  214. else
  215. {
  216. id = await AddAsyncReturnId(grp_Invitation);
  217. if (id != 0)//修改或添加商邀资料
  218. {
  219. Res_InvitationOfficialActivityData res_InvitationData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  220. res_InvitationData.Country = dto.InviterArea;
  221. res_InvitationData.UnitName = dto.Inviter;
  222. res_InvitationData.Delegation = dto.DiId.ToString();
  223. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  224. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  225. if (ifNullUp == null)///添加或修改商邀资料
  226. {
  227. res_InvitationData.Remark = dto.OtherInformation;
  228. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  229. if (DataID != 0)
  230. {
  231. result = new Result() { Code = 0, Msg = "添加成功!" };
  232. }
  233. else
  234. {
  235. RollbackTran();
  236. result = new Result() { Code = -1, Msg = "添加失败!" };
  237. }
  238. }
  239. else
  240. {
  241. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  242. {
  243. Contact = dto.Contact,
  244. Tel = dto.Tel,
  245. Email = dto.Email,
  246. Fax = dto.Fax,
  247. Address = dto.Address,
  248. Remark = dto.OtherInformation,
  249. }).ExecuteCommandAsync();
  250. }
  251. Grp_CreditCardPayment C = new Grp_CreditCardPayment();
  252. C.PayDId = dto.PayDId;
  253. C.ConsumptionPatterns = "";
  254. C.ConsumptionDate = "";
  255. C.CTDId = 0;
  256. C.BankNo = "";
  257. C.CardholderName = "";
  258. C.PayMoney = dto.InviteCost + dto.SendCost;
  259. C.PaymentCurrency = dto.inviteCurrency;
  260. C.CompanyBankNo = "";
  261. C.OtherBankName = "";
  262. C.OtherSideNo = "";
  263. C.OtherSideName = "";
  264. C.Remark = "";
  265. C.CreateUserId = dto.CreateUserId;
  266. C.MFOperator = 0;
  267. C.MFOperatorDate = "";
  268. C.IsAuditDM = 0;
  269. C.AuditDMOperate = 0;
  270. C.AuditDMDate = "";
  271. C.IsAuditMF = 0;
  272. C.AuditMFOperate = 0;
  273. C.AuditMFDate = "";
  274. C.IsAuditGM = 0;
  275. C.AuditGMOperate = 0;
  276. C.AuditGMDate = "";
  277. C.IsPay = 0;
  278. C.DIId = dto.DiId;
  279. C.CId = id;
  280. C.CTable = 81;
  281. C.PayPercentage = 0;
  282. C.PayThenMoney = 0;
  283. C.PayPercentageOld = 0;
  284. C.PayThenMoneyOld = 0;
  285. C.UpdateDate = "";
  286. C.Payee = dto.Payee;
  287. C.OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer;
  288. C.ExceedBudget = 0;
  289. //获取新汇率 int diId,int CId, int currencyId
  290. var rate = await fn(dto.DiId, 81, dto.inviteCurrency);
  291. if (rate.Code == 0)
  292. {
  293. var rateInfo = (rate.Data as CurrencyInfo);
  294. if (rateInfo is not null)
  295. {
  296. C.DayRate = rateInfo.Rate;
  297. C.RMBPrice = rateInfo.Rate * C.PayMoney;
  298. }
  299. else
  300. {
  301. C.DayRate = 1;
  302. C.RMBPrice = C.PayMoney;
  303. }
  304. }
  305. #region 旧汇率注释
  306. //设置该团组的汇率
  307. //Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 81);
  308. //if (_TeamRate != null)
  309. //{
  310. // if (dto.inviteCurrency == 49)
  311. // {
  312. // C.DayRate = _TeamRate.RateU;
  313. // C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  314. // //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  315. // }
  316. // else if (dto.inviteCurrency == 51)
  317. // {
  318. // C.DayRate = _TeamRate.RateE;
  319. // C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  320. // //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  321. // }
  322. // else
  323. // {
  324. // C.DayRate = 1M;
  325. // C.RMBPrice = C.PayMoney;
  326. // }
  327. //}
  328. //else
  329. //{
  330. // C.DayRate = 1M;
  331. // C.RMBPrice = C.PayMoney;
  332. //}
  333. #endregion
  334. int cId = await _sqlSugar.Insertable(C).ExecuteReturnIdentityAsync();
  335. if (cId != 0)
  336. {
  337. result = new Result() { Code = 0, Msg = "添加成功!" };
  338. }
  339. else
  340. {
  341. RollbackTran();
  342. result = new Result() { Code = -1, Msg = "添加失败!" };
  343. }
  344. }
  345. }
  346. }
  347. else//修改
  348. {
  349. var thisTime = new DateTime();
  350. DateTime.TryParse(dto.InviteTime, out thisTime);
  351. bool res = await UpdateAsync(a => a.Id == grp_Invitation.Id, a => new Grp_InvitationOfficialActivities
  352. {
  353. DiId = dto.DiId,
  354. InviterArea = dto.InviterArea,
  355. Inviter = dto.Inviter,
  356. InviteTime = thisTime,
  357. Attachment = dto.Attachment,
  358. InviteCost = dto.InviteCost,
  359. InviteCurrency = dto.inviteCurrency,
  360. SendCost = dto.SendCost,
  361. IsGoOfficaiaBussiness = dto.IsGoOfficaiaBussiness,
  362. Remark = dto.Remark,
  363. });
  364. if (res)
  365. {
  366. Res_InvitationOfficialActivityData res_InvitationData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  367. res_InvitationData.Country = dto.InviterArea;
  368. res_InvitationData.UnitName = dto.Inviter;
  369. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  370. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  371. if (ifNullUp == null)///添加或修改商邀资料
  372. {
  373. res_InvitationData.Remark = dto.OtherInformation;
  374. res_InvitationData.Delegation = dto.DiId.ToString();
  375. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  376. if (DataID != 0)
  377. {
  378. result = new Result() { Code = 0, Msg = "添加成功!" };
  379. }
  380. else
  381. {
  382. RollbackTran();
  383. result = new Result() { Code = -1, Msg = "添加失败!" };
  384. }
  385. }
  386. else
  387. {
  388. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  389. {
  390. Contact = dto.Contact,
  391. Tel = dto.Tel,
  392. Email = dto.Email,
  393. Fax = dto.Fax,
  394. Address = dto.Address,
  395. Remark = dto.OtherInformation,
  396. }).ExecuteCommandAsync();
  397. }
  398. Grp_CreditCardPayment grp_CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == grp_Invitation.Id && a.CTable == 81 && a.IsDel == 0);
  399. if (grp_CreditCardPayment != null)
  400. {
  401. grp_CreditCardPayment.PayMoney = dto.InviteCost + dto.SendCost;
  402. //获取新汇率 int diId,int CId, int currencyId
  403. var rate = await fn(dto.DiId, 81, dto.inviteCurrency);
  404. grp_CreditCardPayment.DayRate = 1;
  405. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  406. if (rate.Code == 0)
  407. {
  408. var rateInfo = (rate.Data as CurrencyInfo);
  409. if (rateInfo is not null)
  410. {
  411. grp_CreditCardPayment.DayRate = rateInfo.Rate;
  412. grp_CreditCardPayment.RMBPrice = rateInfo.Rate * grp_CreditCardPayment.PayMoney;
  413. }
  414. }
  415. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>().Where(a => a.Id == grp_CreditCardPayment.Id).SetColumns(a => new Grp_CreditCardPayment
  416. {
  417. PayDId = dto.PayDId,
  418. PayMoney = grp_CreditCardPayment.PayMoney,
  419. PaymentCurrency = grp_Invitation.InviteCurrency,
  420. Payee = dto.Payee,
  421. OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer,
  422. DayRate = grp_CreditCardPayment.DayRate,
  423. RMBPrice = grp_CreditCardPayment.RMBPrice,
  424. }).ExecuteCommandAsync();
  425. if (CTable != 0)
  426. {
  427. result = new Result() { Code = 0, Msg = "修改成功!" };
  428. }
  429. else
  430. {
  431. result = new Result() { Code = -1, Msg = "修改失败!" };
  432. RollbackTran();
  433. }
  434. }
  435. else
  436. {
  437. RollbackTran();
  438. result = new Result() { Code = -1, Msg = "修改失败!" };
  439. }
  440. }
  441. else
  442. {
  443. RollbackTran();
  444. result = new Result() { Code = -1, Msg = "修改失败!" };
  445. }
  446. }
  447. CommitTran();
  448. }
  449. catch (Exception ex)
  450. {
  451. result = new Result() { Code = -2, Msg = "程序错误!" };
  452. throw;
  453. }
  454. return result;
  455. }
  456. }
  457. }