InvitationOfficialActivitiesRepository.cs 20 KB

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