CustomersRepository.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. using AutoMapper;
  2. using NPOI.POIFS.Crypt.Dsig;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Financial;
  5. using OASystem.Domain.Dtos.Groups;
  6. using OASystem.Domain.Entities.Financial;
  7. using OASystem.Domain.Entities.Groups;
  8. using OASystem.Domain.ViewModels.Financial;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Domain.ViewModels.QiYeWeChat;
  11. using OASystem.Infrastructure.Tools;
  12. using SqlSugar;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace OASystem.Infrastructure.Repositories.Groups
  19. {
  20. public class CustomersRepository : BaseRepository<Grp_Customers, Grp_Ommission>
  21. {
  22. private readonly IMapper _mapper;
  23. private readonly HotelPriceRepository _hotel;
  24. public CustomersRepository(SqlSugarClient sqlSugar, IMapper mapper, HotelPriceRepository hotel) : base(sqlSugar)
  25. {
  26. _mapper = mapper;
  27. _hotel= hotel;
  28. }
  29. /// <summary>
  30. /// 根据团组Id查询保险费用列表
  31. /// </summary>
  32. /// <param name="dto"></param>
  33. /// <returns></returns>
  34. public async Task<Result> CustomersByDiId(CustomersByDiIdDto dto)
  35. {
  36. Result result = new Result() { Code = -2, Msg = "未知错误" };
  37. try
  38. {
  39. string UserId = "";
  40. List<Grp_GroupsTaskAssignment> gtaUIdList = _sqlSugar.Queryable<Grp_GroupsTaskAssignment>().Where(a => a.DIId == dto.DiId && a.IsDel == 0 && a.CTId == 82).ToList();
  41. foreach (Grp_GroupsTaskAssignment gta in gtaUIdList)
  42. UserId += gta.UId + ",";
  43. if (!string.IsNullOrWhiteSpace(UserId))
  44. {
  45. UserId = UserId.Substring(0, UserId.Length - 1);
  46. }
  47. else
  48. {
  49. UserId = "0";
  50. }
  51. string sqlWhere = string.Empty;
  52. if (!string.IsNullOrWhiteSpace(dto.ClientName))
  53. {
  54. sqlWhere = string.Format(@"Where h.DiId={0} and h.IsDel={1} And h.CreateUserId in ({2}) and h.ClientName like '%{3}%'", dto.DiId, 0, UserId,dto.ClientName);
  55. }
  56. else
  57. {
  58. sqlWhere = string.Format(@"Where h.DiId={0} and h.IsDel={1} And h.CreateUserId in ({2})", dto.DiId, 0, UserId);
  59. }
  60. int startIndex = (dto.PageIndex - 1) * dto.PageSize + 1;
  61. int endIndex = startIndex + dto.PageSize - 1;
  62. if (dto.PortType == 1)
  63. {
  64. string sql = string.Format(@"select h.Id,h.ClientName,InsuranceCosts,Currency,s.Name as CurrencyStr,Attachment,c.OrbitalPrivateTransfer,u.CnName,c.IsAuditGM
  65. From Grp_Customers h
  66. Join Grp_CreditCardPayment c on h.Id=c.CId and c.CTable=82 and c.isdel=0
  67. left Join Sys_SetData s on h.Currency=s.Id
  68. left Join Sys_Users u on u.Id=h.CreateUserId {0} order by c.IsAuditGM,c.PayPercentage,h.CreateTime desc", sqlWhere);
  69. List<CustomersView> Customers = _sqlSugar.SqlQueryable<CustomersView>(sql).ToList();
  70. foreach (var item in Customers)
  71. {
  72. if (item.IsAuditGM == 0) item.IsAuditGMStr = "未审核";
  73. else if (item.IsAuditGM == 1) item.IsAuditGMStr = "已通过";
  74. else if (item.IsAuditGM == 2) item.IsAuditGMStr = "未通过";
  75. else if (item.IsAuditGM == 3) item.IsAuditGMStr = "自动审核";
  76. if (item.OrbitalPrivateTransfer == 0) item.OrbitalPrivateTransferStr = "公转";
  77. else if (item.OrbitalPrivateTransfer == 1) item.OrbitalPrivateTransferStr = "私转";
  78. }
  79. return result = new Result() { Code = 0, Msg = "查询成功!", Data = Customers };
  80. }
  81. else if (dto.PortType == 2 || dto.PortType == 3)
  82. {
  83. string sql = string.Format(@"Select * From (
  84. Select row_number() over (order by c.IsAuditGM,c.PayPercentage,h.CreateTime desc) as RowNumber,h.Id,h.ClientName,InsuranceCosts,
  85. Currency,s.Name as CurrencyStr,Attachment,c.OrbitalPrivateTransfer,u.CnName,c.IsAuditGM
  86. From Grp_Customers h
  87. Join Grp_CreditCardPayment c on h.Id=c.CId and c.CTable=82 and c.isdel=0
  88. left Join Sys_SetData s on h.Currency=s.Id
  89. left Join Sys_Users u on u.Id=h.CreateUserId {0}
  90. ) temp Where RowNumber Between {1} and {2}", sqlWhere, startIndex, endIndex);
  91. List<CustomersView> Customers = _sqlSugar.SqlQueryable<CustomersView>(sql).ToList();
  92. foreach (var item in Customers)
  93. {
  94. if (item.IsAuditGM == 0) item.IsAuditGMStr = "未审核";
  95. else if (item.IsAuditGM == 1) item.IsAuditGMStr = "已通过";
  96. else if (item.IsAuditGM == 2) item.IsAuditGMStr = "未通过";
  97. else if (item.IsAuditGM == 3) item.IsAuditGMStr = "自动审核";
  98. if (item.OrbitalPrivateTransfer == 0) item.OrbitalPrivateTransferStr = "公转";
  99. else if (item.OrbitalPrivateTransfer == 1) item.OrbitalPrivateTransferStr = "私转";
  100. }
  101. string CountSql = string.Format(@"Select COUNT(1) as Count From (
  102. select h.Id,h.ClientName,InsuranceCosts,Currency,s.Name as CurrencyStr,Attachment,c.OrbitalPrivateTransfer,u.CnName,c.IsAuditGM
  103. From Grp_Customers h
  104. Join Grp_CreditCardPayment c on h.Id=c.CId and c.CTable=82 and c.isdel=0
  105. left Join Sys_SetData s on h.Currency=s.Id
  106. left Join Sys_Users u on u.Id=h.CreateUserId {0}
  107. ) temp", sqlWhere);
  108. DataCount dataCount = _sqlSugar.SqlQueryable<DataCount>(CountSql).First();
  109. int count = dataCount.Count;
  110. float totalPage = (float)count / dto.PageSize;//总页数
  111. if (totalPage == 0) totalPage = 1;
  112. else totalPage = (int)Math.Ceiling((double)totalPage);
  113. ListViewBase<CustomersView> rst = new ListViewBase<CustomersView>();
  114. rst.DataList = Customers;
  115. rst.DataCount = count;
  116. rst.CurrPageIndex = dto.PageIndex;
  117. rst.CurrPageSize = dto.PageSize;
  118. return result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  119. }
  120. else
  121. {
  122. return result = new Result() { Code = -2, Msg = "请传入PortType参数,1 Web 2 Android 3 IOS" };
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. return result = new Result() { Code = -2, Msg = "未知错误" };
  128. throw;
  129. }
  130. }
  131. public async Task<Result> CustomersById(CustomersByIdDto dto)
  132. {
  133. Result result = new Result() { Code = -2, Msg = "未知错误" };
  134. try
  135. {
  136. Grp_Customers grp_Customers = _sqlSugar.Queryable<Grp_Customers>().First(a=>a.Id==dto.Id && a.IsDel==0);
  137. CustomersByIdView customersById = _mapper.Map<CustomersByIdView>(grp_Customers);
  138. if (customersById != null)
  139. {
  140. Grp_CreditCardPayment creditCardPayment = _sqlSugar.Queryable<Grp_CreditCardPayment>().First(a => a.CId == dto.Id && a.IsDel == 0 && a.CTable == 82);
  141. CreditCardView creditCardView = _mapper.Map<CreditCardView>(creditCardPayment);
  142. Grp_InsuranceCost Iid = _sqlSugar.Queryable<Grp_InsuranceCost>().First(a => a.IsDel == 0 && a.Id == customersById.Iid);
  143. if (Iid != null) customersById.IidStr = Iid.GName;
  144. string CountSql = string.Format(@"select sum(InsuranceCosts) as CountCost from Grp_Customers where isdel=0 and Iid=" + customersById.Iid);
  145. DataCountCost dataCount = _sqlSugar.SqlQueryable<DataCountCost>(CountSql).First();
  146. customersById.InsuranceBalance = Math.Round(Iid.RechargeCost - dataCount.CountCost, 2);
  147. Sys_SetData Currency = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == customersById.Currency);
  148. if (Currency != null) customersById.CurrencyStr = Currency.Name;
  149. Sys_SetData PayDId = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == creditCardView.PayDId);
  150. if (PayDId != null) creditCardView.PayDIdStr = PayDId.Name;
  151. Sys_SetData CTDId = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == creditCardView.CTDId);
  152. if (CTDId != null) creditCardView.CTDIdStr = CTDId.Name;
  153. creditCardView.OrbitalPrivateTransferStr= creditCardView.OrbitalPrivateTransfer== 0 ? "公转" : "私转";
  154. Sys_SetData PaymentCurrency= _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == creditCardView.PaymentCurrency);
  155. if (PaymentCurrency != null) creditCardView.PaymentCurrencyStr = PaymentCurrency.Name;
  156. var data = new
  157. {
  158. customers = customersById,
  159. creditCard = creditCardView
  160. };
  161. return result = new Result() { Code = 0, Msg = "查询成功!", Data = data };
  162. }
  163. var dataN = new
  164. {
  165. customers = new CustomersView(),
  166. creditCard = new CreditCardView()
  167. };
  168. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = dataN };
  169. }
  170. catch (Exception ex)
  171. {
  172. return result = new Result() { Code = -2, Msg = "未知错误" };
  173. throw;
  174. }
  175. }
  176. public async Task<Result> CustomersInitialize(CustomersInitializeDto dto)
  177. {
  178. Result result = new Result() { Code = -2, Msg = "未知错误" };
  179. try
  180. {
  181. List<Sys_SetData> Payment = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 14 && a.IsDel == 0).ToList();
  182. List<SetDataInfoView> _Payment = _mapper.Map<List<SetDataInfoView>>(Payment);
  183. //卡类型
  184. List<Sys_SetData> BankCard = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 15 && a.IsDel == 0).ToList();
  185. List<SetDataInfoView> _BankCard = _mapper.Map<List<SetDataInfoView>>(BankCard);
  186. //保险名称
  187. List<Grp_InsuranceCost> _InsuranceCosts=_sqlSugar.Queryable<Grp_InsuranceCost>().Where(a=>a.IsDel==0).ToList();
  188. List<InsuranceCostView> insuranceCostViews = new List<InsuranceCostView>();
  189. foreach (var item in _InsuranceCosts)
  190. {
  191. InsuranceCostView costView=new InsuranceCostView();
  192. costView.Id=item.Id;
  193. costView.GName=item.GName;
  194. costView.Balance = 0.00M;
  195. string CountSql = string.Format(@"select sum(InsuranceCosts) as CountCost from Grp_Customers where isdel=0 and Iid=" + item.Id);
  196. DataCountCost dataCount = _sqlSugar.SqlQueryable<DataCountCost>(CountSql).First();
  197. costView.Balance = Math.Round(item.RechargeCost - dataCount.CountCost, 2);
  198. insuranceCostViews.Add(costView);
  199. }
  200. //币种
  201. GeneralTeamRateInfoDto PostGroupTeamRatedto = new GeneralTeamRateInfoDto();
  202. PostGroupTeamRatedto.DiId = dto.DiId;
  203. PostGroupTeamRatedto.CTbale = 82;
  204. PostGroupTeamRatedto.PortType = dto.PortType;
  205. Result teamRateDescAddCurrencyIdViews = _hotel.PostGroupTeamRateByDiIdAndCTableId(PostGroupTeamRatedto);
  206. List<TeamRateDescAddCurrencyIdView> Currency = new List<TeamRateDescAddCurrencyIdView>();
  207. if (teamRateDescAddCurrencyIdViews.Code == 0)
  208. {
  209. Currency = teamRateDescAddCurrencyIdViews.Data;
  210. }
  211. var data = new
  212. {
  213. insuranceCost= insuranceCostViews,
  214. Payment = _Payment,
  215. CurrencyList = Currency,
  216. BankCard = _BankCard,
  217. };
  218. return result = new Result() { Code = 0, Msg = "查询成功", Data = data };
  219. }
  220. catch (Exception ex)
  221. {
  222. return result = new Result() { Code = -2, Msg = "未知错误" };
  223. throw;
  224. }
  225. }
  226. public async Task<Result> OpCustomers(OpCustomersDto dto)
  227. {
  228. Result result = new Result() { Code = -2, Msg = "未知错误" };
  229. try
  230. {
  231. BeginTran();
  232. int id = dto.Id;
  233. Grp_Customers cus = _mapper.Map<Grp_Customers>(dto);
  234. Grp_CreditCardPayment c = _mapper.Map<Grp_CreditCardPayment>(dto);
  235. c.Remark = dto.CRemark;
  236. c.PayPercentage = 100;
  237. c.CTable = 82;
  238. c.CId = id;
  239. c.IsAuditGM = 0;
  240. c.PayMoney = cus.InsuranceCosts;
  241. c.PaymentCurrency = cus.Currency;
  242. if (c.PayDId == 72)
  243. {
  244. c.IsPay = 1;
  245. }
  246. c.RMBPrice = cus.InsuranceCosts;
  247. c.DayRate = 1;
  248. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 82);
  249. List<CurrencyInfo> currencyInfos = new List<CurrencyInfo>();
  250. if (_TeamRate != null)
  251. {
  252. Sys_SetData _SetData = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == cus.Currency);
  253. if (_SetData != null)
  254. {
  255. currencyInfos = CommonFun.GetCurrencyChinaToList(_TeamRate.Remark);
  256. CurrencyInfo CurrencyRate = currencyInfos.FirstOrDefault(a => a.CurrencyCode == _SetData.Name);
  257. if (CurrencyRate != null)
  258. {
  259. c.RMBPrice = c.PayMoney * Convert.ToDecimal(CurrencyRate.Rate);
  260. c.DayRate = CurrencyRate.Rate;
  261. }
  262. }
  263. }
  264. if (dto.Status == 1)//添加
  265. {
  266. Grp_Customers customers = _sqlSugar.Queryable<Grp_Customers>().First(a => a.IsDel == 0 && a.ClientName == dto.ClientName && a.InsuranceCosts == dto.InsuranceCosts);
  267. if (customers != null)
  268. {
  269. return result = new Result() { Code = -1, Msg = "该笔费用已存在,请勿重复添加!" };
  270. }
  271. else
  272. {
  273. id = await AddAsyncReturnId(cus);
  274. if (id != 0)
  275. {
  276. c.CId = id;
  277. int cId = await _sqlSugar.Insertable(c).ExecuteReturnIdentityAsync();
  278. if (cId != 0)
  279. {
  280. result = new Result() { Code = 0, Msg = "添加成功!" };
  281. }
  282. else
  283. {
  284. RollbackTran();
  285. result = new Result() { Code = -1, Msg = "添加失败!" };
  286. }
  287. }
  288. else
  289. {
  290. RollbackTran();
  291. result = new Result() { Code = -1, Msg = "添加失败,请稍后重试!" };
  292. }
  293. }
  294. }
  295. else if (dto.Status == 2)//修改
  296. {
  297. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Grp_Customers
  298. {
  299. Iid = cus.Iid,
  300. ClientName = cus.ClientName,
  301. InsuranceCosts = cus.InsuranceCosts,
  302. Currency = cus.Currency,
  303. Attachment = cus.Attachment,
  304. Remark = cus.Remark
  305. });
  306. if (res)
  307. {
  308. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>().Where(a => a.CId == cus.Id && a.CTable == 82).SetColumns(a => new Grp_CreditCardPayment
  309. {
  310. PayDId = dto.PayDId,
  311. PayMoney = c.PayMoney,
  312. PaymentCurrency = c.PaymentCurrency,
  313. Payee = c.Payee,
  314. OrbitalPrivateTransfer = c.OrbitalPrivateTransfer,
  315. DayRate = c.DayRate,
  316. RMBPrice = c.RMBPrice,
  317. ConsumptionPatterns = c.ConsumptionPatterns,
  318. ConsumptionDate = c.ConsumptionDate,
  319. CTDId = c.CTDId,
  320. CompanyBankNo = c.CompanyBankNo,
  321. OtherBankName = c.OtherBankName,
  322. OtherSideNo = c.OtherSideNo,
  323. OtherSideName = c.OtherSideName,
  324. BankNo = c.BankNo,
  325. CardholderName = c.CardholderName,
  326. Remark = c.Remark,
  327. }).ExecuteCommandAsync();
  328. if (CTable == 0)
  329. {
  330. result = new Result() { Code = -1, Msg = "修改失败!" };
  331. RollbackTran();
  332. }
  333. else
  334. {
  335. result = new Result() { Code = 0, Msg = "修改成功!" };
  336. }
  337. }
  338. else
  339. {
  340. RollbackTran();
  341. result = new Result() { Code = -1, Msg = "修改失败,请稍后重试!" };
  342. }
  343. }
  344. CommitTran();
  345. }
  346. catch (Exception ex)
  347. {
  348. return result = new Result() { Code = -2, Msg = "未知错误" };
  349. throw;
  350. }
  351. return result;
  352. }
  353. }
  354. }