InvitationOfficialActivitiesRepository.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 && a.CTable==81);
  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 like '%{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.Country = dto.InviterArea;
  130. res_InvitationData.UnitName = dto.Inviter;
  131. res_InvitationData.Delegation = dto.DiId.ToString();
  132. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  133. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  134. if (ifNullUp == null)///添加或修改商邀资料
  135. {
  136. res_InvitationData.Remark = dto.OtherInformation;
  137. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  138. if (DataID != 0)
  139. {
  140. result = new Result() { Code = 0, Msg = "添加成功!" };
  141. }
  142. else
  143. {
  144. RollbackTran();
  145. result = new Result() { Code = -1, Msg = "添加失败!" };
  146. }
  147. }
  148. else
  149. {
  150. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  151. {
  152. Contact=dto.Contact,
  153. Tel=dto.Tel,
  154. Email=dto.Email,
  155. Fax=dto.Fax,
  156. Address=dto.Address,
  157. Remark=dto.OtherInformation,
  158. }).ExecuteCommandAsync();
  159. }
  160. Grp_CreditCardPayment C = new Grp_CreditCardPayment();
  161. C.PayDId = dto.PayDId;
  162. C.ConsumptionPatterns = "";
  163. C.ConsumptionDate = "";
  164. C.CTDId = 0;
  165. C.BankNo = "";
  166. C.CardholderName = "";
  167. C.PayMoney = dto.InviteCosts + dto.SendCost;
  168. C.PaymentCurrency = dto.Currency;
  169. //当天汇率
  170. //if (!string.IsNullOrEmpty(hfRate.Value))
  171. // C.DayRate = hfRate.Value;
  172. //else
  173. //C.DayRate = "";
  174. C.CompanyBankNo = "";
  175. C.OtherBankName = "";
  176. C.OtherSideNo = "";
  177. C.OtherSideName = "";
  178. C.Remark = "";
  179. C.CreateUserId = dto.CreateUserId;
  180. C.MFOperator = "";
  181. C.MFOperatorDate = "";
  182. C.IsAuditDM = 0;
  183. C.AuditDMOperate = 0;
  184. C.AuditDMDate = "";
  185. C.IsAuditMF = 0;
  186. C.AuditMFOperate = 0;
  187. C.AuditMFDate = "";
  188. C.IsAuditGM = 0;
  189. C.AuditGMOperate = 21;
  190. C.AuditGMDate = "";
  191. C.IsPay = 0;
  192. C.DIId = dto.DiId;
  193. C.CId = id;
  194. C.CTable = 81;
  195. C.PayPercentage = 0;
  196. C.PayThenMoney = 0;
  197. C.PayPercentageOld = 0;
  198. C.PayThenMoneyOld = 0;
  199. C.UpdateDate = "";
  200. C.Payee = dto.Payee;
  201. C.OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer;
  202. C.ExceedBudget = 0;
  203. //C.RMBPrice = 0.00f;
  204. //设置该团组的汇率
  205. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 81);
  206. if (_TeamRate != null)
  207. {
  208. if (dto.Currency == 49)
  209. {
  210. C.DayRate = _TeamRate.RateU;
  211. C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  212. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  213. }
  214. else if (dto.Currency == 51)
  215. {
  216. C.DayRate = _TeamRate.RateE;
  217. C.RMBPrice = C.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  218. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  219. }
  220. else
  221. {
  222. C.DayRate = 1M;
  223. C.RMBPrice = C.PayMoney;
  224. }
  225. }
  226. else
  227. {
  228. C.DayRate = 1M;
  229. C.RMBPrice = C.PayMoney;
  230. }
  231. int cId = await _sqlSugar.Insertable(C).ExecuteReturnIdentityAsync();
  232. if (cId != 0)
  233. {
  234. result = new Result() { Code = 0, Msg = "添加成功!" };
  235. }
  236. else
  237. {
  238. RollbackTran();
  239. result = new Result() { Code = -1, Msg = "添加失败!" };
  240. }
  241. }
  242. }
  243. }
  244. else//修改
  245. {
  246. bool res = await UpdateAsync(a => a.Id == grp_Invitation.Id, a => new Grp_InvitationOfficialActivities
  247. {
  248. DiId = dto.DiId,
  249. InviterArea = dto.InviterArea,
  250. Inviter = dto.Inviter,
  251. InviteTime = dto.InviteTime,
  252. Attachment = dto.Attachment,
  253. InviteCosts = dto.InviteCosts,
  254. Currency = dto.Currency,
  255. SendCost = dto.SendCost,
  256. IsGoOfficaiaBussiness = dto.IsGoOfficaiaBussiness,
  257. Remark = dto.Remark,
  258. });
  259. if (res)
  260. {
  261. Res_InvitationOfficialActivityData res_InvitationData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  262. res_InvitationData.Country = dto.InviterArea;
  263. res_InvitationData.UnitName = dto.Inviter;
  264. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  265. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName);
  266. if (ifNullUp == null)///添加或修改商邀资料
  267. {
  268. res_InvitationData.Remark =dto.OtherInformation;
  269. res_InvitationData.Delegation = dto.DiId.ToString();
  270. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  271. if (DataID != 0)
  272. {
  273. result = new Result() { Code = 0, Msg = "添加成功!" };
  274. }
  275. else
  276. {
  277. RollbackTran();
  278. result = new Result() { Code = -1, Msg = "添加失败!" };
  279. }
  280. } else
  281. {
  282. int CTable = await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>().Where(a => a.Id == ifNullUp.Id).SetColumns(a => new Res_InvitationOfficialActivityData
  283. {
  284. Contact=dto.Contact,
  285. Tel=dto.Tel,
  286. Email=dto.Email,
  287. Fax=dto.Fax,
  288. Address=dto.Address,
  289. Remark=dto.OtherInformation,
  290. }).ExecuteCommandAsync();
  291. }
  292. Grp_CreditCardPayment grp_CreditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == grp_Invitation.Id && a.CTable==81 && a.IsDel == 0);
  293. if (grp_CreditCardPayment != null)
  294. {
  295. grp_CreditCardPayment.PayMoney = dto.InviteCosts + dto.SendCost;
  296. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == grp_Invitation.DiId && a.IsDel == 0 && a.IsDel==0 && a.CTable == 81);
  297. if (_TeamRate != null)
  298. {
  299. if (grp_Invitation.Currency == 49)
  300. {
  301. grp_CreditCardPayment.DayRate = _TeamRate.RateU;
  302. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * Convert.ToDecimal(_TeamRate.RateU);
  303. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateU);
  304. }
  305. else if (grp_Invitation.Currency == 51)
  306. {
  307. grp_CreditCardPayment.DayRate = _TeamRate.RateE;
  308. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney * Convert.ToDecimal(_TeamRate.RateE);
  309. //ccp.PayMoney = ccp.PayMoney * float.Parse(tr.RateE);
  310. }
  311. else
  312. {
  313. grp_CreditCardPayment.DayRate = 1M;
  314. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  315. }
  316. }
  317. else
  318. {
  319. grp_CreditCardPayment.DayRate = 1M;
  320. grp_CreditCardPayment.RMBPrice = grp_CreditCardPayment.PayMoney;
  321. }
  322. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>().Where(a => a.Id == grp_CreditCardPayment.Id).SetColumns(a => new Grp_CreditCardPayment
  323. {
  324. PayDId = dto.PayDId,
  325. PayMoney = grp_CreditCardPayment.PayMoney,
  326. PaymentCurrency = grp_Invitation.Currency,
  327. Payee = dto.Payee,
  328. OrbitalPrivateTransfer = dto.OrbitalPrivateTransfer,
  329. DayRate = grp_CreditCardPayment.DayRate,
  330. RMBPrice = grp_CreditCardPayment.RMBPrice,
  331. }).ExecuteCommandAsync();
  332. if (CTable != 0)
  333. {
  334. result = new Result() { Code = 0, Msg = "修改成功!" };
  335. }
  336. else
  337. {
  338. result = new Result() { Code = -1, Msg = "修改失败!" };
  339. RollbackTran();
  340. }
  341. }
  342. else
  343. {
  344. RollbackTran();
  345. result = new Result() { Code = -1, Msg = "修改失败!" };
  346. }
  347. }
  348. else
  349. {
  350. RollbackTran();
  351. result = new Result() { Code = -1, Msg = "修改失败!" };
  352. }
  353. }
  354. CommitTran();
  355. }
  356. catch (Exception ex)
  357. {
  358. result = new Result() { Code = -2, Msg = "程序错误!" };
  359. throw;
  360. }
  361. return result;
  362. }
  363. }
  364. }