InvitationOfficialActivitiesRepository.cs 20 KB

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