InvitationOfficialActivitiesRepository.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. result = new Result() { Code = 0, Msg = "暂无数据!", Data = _DecreasePayments };
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. result = new Result() { Code = -2, Msg = "未知错误" };
  128. }
  129. return result;
  130. }
  131. /// <summary>
  132. /// 商邀费用操作(Status:1.新增,2.修改)
  133. /// </summary>
  134. /// <param name="dto"></param>
  135. /// <returns></returns>
  136. public async Task<Result> OpInvitationOfficialActivities(OpInvitationOfficialActivitiesDto dto)
  137. {
  138. Result result = new Result() { Code = -2, Msg = "程序错误!" };
  139. BeginTran();
  140. try
  141. {
  142. int id = 0;
  143. Grp_InvitationOfficialActivities grp_Invitation = _mapper.Map<Grp_InvitationOfficialActivities>(dto);
  144. if (dto.Status == 1)//添加
  145. {
  146. string selectSql = string.Format(@"select * from Grp_InvitationOfficialActivities where InviterArea='{0}' and Inviter='{1}' and DiId={2} and IsDel={3}"
  147. , dto.InviterArea, dto.Inviter, dto.DiId, 0);
  148. var _InvitationOfficialActivities = await _sqlSugar.SqlQueryable<Grp_InvitationOfficialActivities>(selectSql).FirstAsync();//查询是否存在
  149. if (_InvitationOfficialActivities != null)
  150. {
  151. return result = new Result() { Code = -1, Msg = "该数据已存在,请勿重复添加!" };
  152. }
  153. else
  154. {
  155. id = await AddAsyncReturnId(grp_Invitation);
  156. if (id != 0)//修改或添加商邀资料
  157. {
  158. Res_InvitationOfficialActivityData res_InvitationData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  159. res_InvitationData.Country = dto.InviterArea;
  160. res_InvitationData.UnitName = dto.Inviter;
  161. res_InvitationData.Delegation = dto.DiId.ToString();
  162. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  163. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  164. if (ifNullUp == null)///添加或修改商邀资料
  165. {
  166. res_InvitationData.Remark = dto.OtherInformation;
  167. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  168. if (DataID != 0)
  169. {
  170. result = new Result() { Code = 0, Msg = "添加成功!" };
  171. }
  172. else
  173. {
  174. RollbackTran();
  175. result = new Result() { Code = -1, Msg = "添加失败!" };
  176. }
  177. }
  178. else
  179. {
  180. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  181. {
  182. Contact=dto.Contact,
  183. Tel=dto.Tel,
  184. Email=dto.Email,
  185. Fax=dto.Fax,
  186. Address=dto.Address,
  187. Remark=dto.OtherInformation,
  188. }).ExecuteCommandAsync();
  189. }
  190. Grp_CreditCardPayment C = new Grp_CreditCardPayment();
  191. C.PayDId = dto.PayDId;
  192. C.ConsumptionPatterns = "";
  193. C.ConsumptionDate = "";
  194. C.CTDId = 0;
  195. C.BankNo = "";
  196. C.CardholderName = "";
  197. C.PayMoney = dto.InviteCosts + dto.SendCost;
  198. C.PaymentCurrency = dto.Currency;
  199. //当天汇率
  200. //if (!string.IsNullOrEmpty(hfRate.Value))
  201. // C.DayRate = hfRate.Value;
  202. //else
  203. //C.DayRate = "";
  204. C.CompanyBankNo = "";
  205. C.OtherBankName = "";
  206. C.OtherSideNo = "";
  207. C.OtherSideName = "";
  208. C.Remark = "";
  209. C.CreateUserId = dto.CreateUserId;
  210. C.MFOperator = "";
  211. C.MFOperatorDate = "";
  212. C.IsAuditDM = 0;
  213. C.AuditDMOperate = 0;
  214. C.AuditDMDate = "";
  215. C.IsAuditMF = 0;
  216. C.AuditMFOperate = 0;
  217. C.AuditMFDate = "";
  218. C.IsAuditGM = 0;
  219. C.AuditGMOperate = 21;
  220. C.AuditGMDate = "";
  221. C.IsPay = 0;
  222. C.DIId = dto.DiId;
  223. C.CId = id;
  224. C.CTable = 81;
  225. C.PayPercentage = 0;
  226. C.PayThenMoney = 0;
  227. C.PayPercentageOld = 0;
  228. C.PayThenMoneyOld = 0;
  229. C.UpdateDate = "";
  230. C.Payee = dto.Payee;
  231. C.OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer;
  232. C.ExceedBudget = 0;
  233. //C.RMBPrice = 0.00f;
  234. //设置该团组的汇率
  235. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 81);
  236. if (_TeamRate != null)
  237. {
  238. if (dto.Currency == 49)
  239. {
  240. C.DayRate = _TeamRate.RateU;
  241. C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  242. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  243. }
  244. else if (dto.Currency == 51)
  245. {
  246. C.DayRate = _TeamRate.RateE;
  247. C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  248. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  249. }
  250. else
  251. {
  252. C.DayRate = 1M;
  253. C.RMBPrice = C.PayMoney;
  254. }
  255. }
  256. else
  257. {
  258. C.DayRate = 1M;
  259. C.RMBPrice = C.PayMoney;
  260. }
  261. int cId = await _sqlSugar.Insertable(C).ExecuteReturnIdentityAsync();
  262. if (cId != 0)
  263. {
  264. result = new Result() { Code = 0, Msg = "添加成功!" };
  265. }
  266. else
  267. {
  268. RollbackTran();
  269. result = new Result() { Code = -1, Msg = "添加失败!" };
  270. }
  271. }
  272. }
  273. }
  274. else//修改
  275. {
  276. bool res = await UpdateAsync(a => a.Id == grp_Invitation.Id, a => new Grp_InvitationOfficialActivities
  277. {
  278. DiId = dto.DiId,
  279. InviterArea = dto.InviterArea,
  280. Inviter = dto.Inviter,
  281. InviteTime = dto.InviteTime,
  282. Attachment = dto.Attachment,
  283. InviteCosts = dto.InviteCosts,
  284. Currency = dto.Currency,
  285. SendCost = dto.SendCost,
  286. IsGoOfficaiaBussiness = dto.IsGoOfficaiaBussiness,
  287. Remark = dto.Remark,
  288. });
  289. if (res)
  290. {
  291. Res_InvitationOfficialActivityData res_InvitationData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  292. res_InvitationData.Country = dto.InviterArea;
  293. res_InvitationData.UnitName = dto.Inviter;
  294. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  295. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  296. if (ifNullUp == null)///添加或修改商邀资料
  297. {
  298. res_InvitationData.Remark =dto.OtherInformation;
  299. res_InvitationData.Delegation = dto.DiId.ToString();
  300. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  301. if (DataID != 0)
  302. {
  303. result = new Result() { Code = 0, Msg = "添加成功!" };
  304. }
  305. else
  306. {
  307. RollbackTran();
  308. result = new Result() { Code = -1, Msg = "添加失败!" };
  309. }
  310. } else
  311. {
  312. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  313. {
  314. Contact=dto.Contact,
  315. Tel=dto.Tel,
  316. Email=dto.Email,
  317. Fax=dto.Fax,
  318. Address=dto.Address,
  319. Remark=dto.OtherInformation,
  320. }).ExecuteCommandAsync();
  321. }
  322. Grp_CreditCardPayment grp_CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == grp_Invitation.Id && a.CTable==81 && a.IsDel == 0);
  323. if (grp_CreditCardPayment != null)
  324. {
  325. grp_CreditCardPayment.PayMoney = dto.InviteCosts + dto.SendCost;
  326. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == grp_Invitation.DiId && a.IsDel == 0 && a.IsDel==0 && a.CTable == 81);
  327. if (_TeamRate != null)
  328. {
  329. if (grp_Invitation.Currency == 49)
  330. {
  331. grp_CreditCardPayment.DayRate = _TeamRate.RateU;
  332. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  333. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  334. }
  335. else if (grp_Invitation.Currency == 51)
  336. {
  337. grp_CreditCardPayment.DayRate = _TeamRate.RateE;
  338. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  339. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  340. }
  341. else
  342. {
  343. grp_CreditCardPayment.DayRate = 1M;
  344. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  345. }
  346. }
  347. else
  348. {
  349. grp_CreditCardPayment.DayRate = 1M;
  350. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  351. }
  352. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>().Where(a => a.Id == grp_CreditCardPayment.Id).SetColumns(a => new Grp_CreditCardPayment
  353. {
  354. PayDId = dto.PayDId,
  355. PayMoney = grp_CreditCardPayment.PayMoney,
  356. PaymentCurrency = grp_Invitation.Currency,
  357. Payee = dto.Payee,
  358. OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer,
  359. DayRate = grp_CreditCardPayment.DayRate,
  360. RMBPrice = grp_CreditCardPayment.RMBPrice,
  361. }).ExecuteCommandAsync();
  362. if (CTable != 0)
  363. {
  364. result = new Result() { Code = 0, Msg = "修改成功!" };
  365. }
  366. else
  367. {
  368. result = new Result() { Code = -1, Msg = "修改失败!" };
  369. RollbackTran();
  370. }
  371. }
  372. else
  373. {
  374. RollbackTran();
  375. result = new Result() { Code = -1, Msg = "修改失败!" };
  376. }
  377. }
  378. else
  379. {
  380. RollbackTran();
  381. result = new Result() { Code = -1, Msg = "修改失败!" };
  382. }
  383. }
  384. CommitTran();
  385. }
  386. catch (Exception ex)
  387. {
  388. result = new Result() { Code = -2, Msg = "程序错误!" };
  389. throw;
  390. }
  391. return result;
  392. }
  393. }
  394. }