InvitationOfficialActivitiesRepository.cs 24 KB

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