InvitationOfficialActivitiesRepository.cs 22 KB

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