InvitationOfficialActivitiesRepository.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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,InviteCosts,(select name from Sys_SetData where id=i.Currency 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)
  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.InviteCosts + dto.SendCost;
  259. C.PaymentCurrency = dto.Currency;
  260. //当天汇率
  261. //if (!string.IsNullOrEmpty(hfRate.Value))
  262. // C.DayRate = hfRate.Value;
  263. //else
  264. //C.DayRate = "";
  265. C.CompanyBankNo = "";
  266. C.OtherBankName = "";
  267. C.OtherSideNo = "";
  268. C.OtherSideName = "";
  269. C.Remark = "";
  270. C.CreateUserId = dto.CreateUserId;
  271. C.MFOperator = 0;
  272. C.MFOperatorDate = "";
  273. C.IsAuditDM = 0;
  274. C.AuditDMOperate = 0;
  275. C.AuditDMDate = "";
  276. C.IsAuditMF = 0;
  277. C.AuditMFOperate = 0;
  278. C.AuditMFDate = "";
  279. C.IsAuditGM = 0;
  280. C.AuditGMOperate = 21;
  281. C.AuditGMDate = "";
  282. C.IsPay = 0;
  283. C.DIId = dto.DiId;
  284. C.CId = id;
  285. C.CTable = 81;
  286. C.PayPercentage = 0;
  287. C.PayThenMoney = 0;
  288. C.PayPercentageOld = 0;
  289. C.PayThenMoneyOld = 0;
  290. C.UpdateDate = "";
  291. C.Payee = dto.Payee;
  292. C.OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer;
  293. C.ExceedBudget = 0;
  294. //C.RMBPrice = 0.00f;
  295. //设置该团组的汇率
  296. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 81);
  297. if (_TeamRate != null)
  298. {
  299. if (dto.Currency == 49)
  300. {
  301. C.DayRate = _TeamRate.RateU;
  302. C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  303. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  304. }
  305. else if (dto.Currency == 51)
  306. {
  307. C.DayRate = _TeamRate.RateE;
  308. C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  309. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  310. }
  311. else
  312. {
  313. C.DayRate = 1M;
  314. C.RMBPrice = C.PayMoney;
  315. }
  316. }
  317. else
  318. {
  319. C.DayRate = 1M;
  320. C.RMBPrice = C.PayMoney;
  321. }
  322. int cId = await _sqlSugar.Insertable(C).ExecuteReturnIdentityAsync();
  323. if (cId != 0)
  324. {
  325. result = new Result() { Code = 0, Msg = "添加成功!" };
  326. }
  327. else
  328. {
  329. RollbackTran();
  330. result = new Result() { Code = -1, Msg = "添加失败!" };
  331. }
  332. }
  333. }
  334. }
  335. else//修改
  336. {
  337. bool res = await UpdateAsync(a => a.Id == grp_Invitation.Id, a => new Grp_InvitationOfficialActivities
  338. {
  339. DiId = dto.DiId,
  340. InviterArea = dto.InviterArea,
  341. Inviter = dto.Inviter,
  342. InviteTime = dto.InviteTime,
  343. Attachment = dto.Attachment,
  344. InviteCosts = dto.InviteCosts,
  345. Currency = dto.Currency,
  346. SendCost = dto.SendCost,
  347. IsGoOfficaiaBussiness = dto.IsGoOfficaiaBussiness,
  348. Remark = dto.Remark,
  349. });
  350. if (res)
  351. {
  352. Res_InvitationOfficialActivityData res_InvitationData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  353. res_InvitationData.Country = dto.InviterArea;
  354. res_InvitationData.UnitName = dto.Inviter;
  355. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  356. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  357. if (ifNullUp == null)///添加或修改商邀资料
  358. {
  359. res_InvitationData.Remark =dto.OtherInformation;
  360. res_InvitationData.Delegation = dto.DiId.ToString();
  361. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  362. if (DataID != 0)
  363. {
  364. result = new Result() { Code = 0, Msg = "添加成功!" };
  365. }
  366. else
  367. {
  368. RollbackTran();
  369. result = new Result() { Code = -1, Msg = "添加失败!" };
  370. }
  371. } else
  372. {
  373. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  374. {
  375. Contact=dto.Contact,
  376. Tel=dto.Tel,
  377. Email=dto.Email,
  378. Fax=dto.Fax,
  379. Address=dto.Address,
  380. Remark=dto.OtherInformation,
  381. }).ExecuteCommandAsync();
  382. }
  383. Grp_CreditCardPayment grp_CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == grp_Invitation.Id && a.CTable==81 && a.IsDel == 0);
  384. if (grp_CreditCardPayment != null)
  385. {
  386. grp_CreditCardPayment.PayMoney = dto.InviteCosts + dto.SendCost;
  387. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == grp_Invitation.DiId && a.IsDel == 0 && a.IsDel==0 && a.CTable == 81);
  388. if (_TeamRate != null)
  389. {
  390. if (grp_Invitation.Currency == 49)
  391. {
  392. grp_CreditCardPayment.DayRate = _TeamRate.RateU;
  393. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  394. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  395. }
  396. else if (grp_Invitation.Currency == 51)
  397. {
  398. grp_CreditCardPayment.DayRate = _TeamRate.RateE;
  399. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  400. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  401. }
  402. else
  403. {
  404. grp_CreditCardPayment.DayRate = 1M;
  405. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  406. }
  407. }
  408. else
  409. {
  410. grp_CreditCardPayment.DayRate = 1M;
  411. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  412. }
  413. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>().Where(a => a.Id == grp_CreditCardPayment.Id).SetColumns(a => new Grp_CreditCardPayment
  414. {
  415. PayDId = dto.PayDId,
  416. PayMoney = grp_CreditCardPayment.PayMoney,
  417. PaymentCurrency = grp_Invitation.Currency,
  418. Payee = dto.Payee,
  419. OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer,
  420. DayRate = grp_CreditCardPayment.DayRate,
  421. RMBPrice = grp_CreditCardPayment.RMBPrice,
  422. }).ExecuteCommandAsync();
  423. if (CTable != 0)
  424. {
  425. result = new Result() { Code = 0, Msg = "修改成功!" };
  426. }
  427. else
  428. {
  429. result = new Result() { Code = -1, Msg = "修改失败!" };
  430. RollbackTran();
  431. }
  432. }
  433. else
  434. {
  435. RollbackTran();
  436. result = new Result() { Code = -1, Msg = "修改失败!" };
  437. }
  438. }
  439. else
  440. {
  441. RollbackTran();
  442. result = new Result() { Code = -1, Msg = "修改失败!" };
  443. }
  444. }
  445. CommitTran();
  446. }
  447. catch (Exception ex)
  448. {
  449. result = new Result() { Code = -2, Msg = "程序错误!" };
  450. throw;
  451. }
  452. return result;
  453. }
  454. }
  455. }