InvitationOfficialActivitiesRepository.cs 20 KB

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