InvitationOfficialActivitiesRepository.cs 25 KB

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