CustomersRepository.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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)
  144. {
  145. customersById.IidStr = Iid.GName;
  146. string CountSql = string.Format(@"select sum(InsuranceCosts) as CountCost from Grp_Customers where isdel=0 and Iid=" + customersById.Iid);
  147. DataCountCost dataCount = _sqlSugar.SqlQueryable<DataCountCost>(CountSql).First();
  148. customersById.InsuranceBalance = Math.Round(Iid.RechargeCost - dataCount.CountCost, 2);
  149. }
  150. Sys_SetData Currency = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == customersById.Currency);
  151. if (Currency != null) customersById.CurrencyStr = Currency.Name;
  152. Sys_SetData PayDId = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == creditCardView.PayDId);
  153. if (PayDId != null) creditCardView.PayDIdStr = PayDId.Name;
  154. Sys_SetData CTDId = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == creditCardView.CTDId);
  155. if (CTDId != null) creditCardView.CTDIdStr = CTDId.Name;
  156. creditCardView.OrbitalPrivateTransferStr= creditCardView.OrbitalPrivateTransfer== 0 ? "公转" : "私转";
  157. Sys_SetData PaymentCurrency= _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == creditCardView.PaymentCurrency);
  158. if (PaymentCurrency != null) creditCardView.PaymentCurrencyStr = PaymentCurrency.Name;
  159. var data = new
  160. {
  161. customers = customersById,
  162. creditCard = creditCardView
  163. };
  164. return result = new Result() { Code = 0, Msg = "查询成功!", Data = data };
  165. }
  166. var dataN = new
  167. {
  168. customers = new CustomersView(),
  169. creditCard = new CreditCardView()
  170. };
  171. return result = new Result() { Code = 0, Msg = "暂无数据!", Data = dataN };
  172. }
  173. catch (Exception ex)
  174. {
  175. return result = new Result() { Code = -2, Msg = "未知错误" };
  176. throw;
  177. }
  178. }
  179. public async Task<Result> CustomersInitialize(CustomersInitializeDto dto)
  180. {
  181. Result result = new Result() { Code = -2, Msg = "未知错误" };
  182. try
  183. {
  184. List<Sys_SetData> Payment = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 14 && a.IsDel == 0).ToList();
  185. List<SetDataInfoView> _Payment = _mapper.Map<List<SetDataInfoView>>(Payment);
  186. //卡类型
  187. List<Sys_SetData> BankCard = _sqlSugar.Queryable<Sys_SetData>().Where(a => a.STid == 15 && a.IsDel == 0).ToList();
  188. List<SetDataCurrencyInfoView> _BankCard = _mapper.Map<List<SetDataCurrencyInfoView>>(BankCard).Where(s=> (s.Currency ?? "") == "CNY" ).ToList();
  189. //保险名称
  190. //List<Grp_InsuranceCost> _InsuranceCosts=_sqlSugar.Queryable<Grp_InsuranceCost>().Where(a=>a.IsDel==0).ToList();
  191. //List<InsuranceCostView> insuranceCostViews = new List<InsuranceCostView>();
  192. //foreach (var item in _InsuranceCosts)
  193. //{
  194. // InsuranceCostView costView=new InsuranceCostView();
  195. // costView.Id=item.Id;
  196. // costView.GName=item.GName;
  197. // costView.Balance = 0.00M;
  198. // string CountSql = string.Format(@"select sum(InsuranceCosts) as CountCost from Grp_Customers where isdel=0 and Iid=" + item.Id);
  199. // DataCountCost dataCount = _sqlSugar.SqlQueryable<DataCountCost>(CountSql).First();
  200. // costView.Balance = Math.Round(item.RechargeCost - dataCount.CountCost, 2);
  201. // insuranceCostViews.Add(costView);
  202. //}
  203. //币种
  204. //GeneralTeamRateInfoDto PostGroupTeamRatedto = new GeneralTeamRateInfoDto();
  205. //PostGroupTeamRatedto.DiId = dto.DiId;
  206. //PostGroupTeamRatedto.CTable = 82;
  207. //PostGroupTeamRatedto.PortType = dto.PortType;
  208. //Result teamRateDescAddCurrencyIdViews = _hotel.PostGroupTeamRateByDiIdAndCTableId(PostGroupTeamRatedto);
  209. List<TeamRateDescAddCurrencyIdView> Currency = new List<TeamRateDescAddCurrencyIdView>();
  210. TeamRateDescAddCurrencyIdView cny = new TeamRateDescAddCurrencyIdView() { CurrencyCode = "CNY", CurrencyId = 836, CurrencyName = "人民币", Rate = 1 }; //保险修改,只要人民币
  211. Currency.Add(cny);
  212. //if (teamRateDescAddCurrencyIdViews.Code == 0)
  213. //{
  214. // Currency = teamRateDescAddCurrencyIdViews.Data;
  215. //}
  216. //insuranceCost = insuranceCostViews,
  217. var data = new
  218. {
  219. Payment = _Payment,
  220. CurrencyList = Currency,
  221. BankCard = _BankCard,
  222. };
  223. return result = new Result() { Code = 0, Msg = "查询成功", Data = data };
  224. }
  225. catch (Exception ex)
  226. {
  227. return result = new Result() { Code = -2, Msg = "未知错误" };
  228. throw;
  229. }
  230. }
  231. public async Task<Result> OpCustomers(OpCustomersDto dto)
  232. {
  233. Result result = new Result() { Code = -2, Msg = "未知错误" };
  234. try
  235. {
  236. BeginTran();
  237. int id = dto.Id;
  238. Grp_Customers cus = _mapper.Map<Grp_Customers>(dto);
  239. Grp_CreditCardPayment c = _mapper.Map<Grp_CreditCardPayment>(dto);
  240. c.Remark = dto.CRemark;
  241. c.PayPercentage = 100;
  242. c.CTable = 82;
  243. c.CId = id;
  244. c.IsAuditGM = 0;
  245. c.PayMoney = cus.InsuranceCosts;
  246. c.PaymentCurrency = cus.Currency;
  247. if (c.PayDId == 72)
  248. {
  249. c.IsPay = 1;
  250. }
  251. c.RMBPrice = cus.InsuranceCosts;
  252. c.DayRate = 1;
  253. Grp_TeamRate _TeamRate = _sqlSugar.Queryable<Grp_TeamRate>().First(a => a.DiId == dto.DiId && a.IsDel == 0 && a.CTable == 82);
  254. List<CurrencyInfo> currencyInfos = new List<CurrencyInfo>();
  255. if (_TeamRate != null)
  256. {
  257. Sys_SetData _SetData = _sqlSugar.Queryable<Sys_SetData>().First(a => a.IsDel == 0 && a.Id == cus.Currency);
  258. if (_SetData != null)
  259. {
  260. currencyInfos = CommonFun.GetCurrencyChinaToList(_TeamRate.Remark);
  261. CurrencyInfo CurrencyRate = currencyInfos.FirstOrDefault(a => a.CurrencyCode == _SetData.Name);
  262. if (CurrencyRate != null)
  263. {
  264. c.RMBPrice = c.PayMoney * Convert.ToDecimal(CurrencyRate.Rate);
  265. c.DayRate = CurrencyRate.Rate;
  266. }
  267. }
  268. }
  269. if (dto.Status == 1)//添加
  270. {
  271. Grp_Customers customers = _sqlSugar.Queryable<Grp_Customers>().First(a => a.IsDel == 0 && a.ClientName == dto.ClientName && a.InsuranceCosts == dto.InsuranceCosts);
  272. if (customers != null)
  273. {
  274. return result = new Result() { Code = -1, Msg = "该笔费用已存在,请勿重复添加!" };
  275. }
  276. else
  277. {
  278. id = await AddAsyncReturnId(cus);
  279. if (id != 0)
  280. {
  281. c.CId = id;
  282. int cId = await _sqlSugar.Insertable(c).ExecuteReturnIdentityAsync();
  283. if (cId != 0)
  284. {
  285. result = new Result() { Code = 0, Msg = "添加成功!" };
  286. }
  287. else
  288. {
  289. RollbackTran();
  290. result = new Result() { Code = -1, Msg = "添加失败!" };
  291. }
  292. }
  293. else
  294. {
  295. RollbackTran();
  296. result = new Result() { Code = -1, Msg = "添加失败,请稍后重试!" };
  297. }
  298. }
  299. }
  300. else if (dto.Status == 2)//修改
  301. {
  302. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Grp_Customers
  303. {
  304. Iid = cus.Iid,
  305. ClientName = cus.ClientName,
  306. InsuranceCosts = cus.InsuranceCosts,
  307. Currency = cus.Currency,
  308. Attachment = cus.Attachment,
  309. Remark = cus.Remark
  310. });
  311. if (res)
  312. {
  313. int CTable = await _sqlSugar.Updateable<Grp_CreditCardPayment>().Where(a => a.CId == cus.Id && a.CTable == 82).SetColumns(a => new Grp_CreditCardPayment
  314. {
  315. PayDId = dto.PayDId,
  316. PayMoney = c.PayMoney,
  317. PaymentCurrency = c.PaymentCurrency,
  318. Payee = c.Payee,
  319. OrbitalPrivateTransfer = c.OrbitalPrivateTransfer,
  320. DayRate = c.DayRate,
  321. RMBPrice = c.RMBPrice,
  322. ConsumptionPatterns = c.ConsumptionPatterns,
  323. ConsumptionDate = c.ConsumptionDate,
  324. CTDId = c.CTDId,
  325. CompanyBankNo = c.CompanyBankNo,
  326. OtherBankName = c.OtherBankName,
  327. OtherSideNo = c.OtherSideNo,
  328. OtherSideName = c.OtherSideName,
  329. BankNo = c.BankNo,
  330. CardholderName = c.CardholderName,
  331. Remark = c.Remark,
  332. }).ExecuteCommandAsync();
  333. if (CTable == 0)
  334. {
  335. result = new Result() { Code = -1, Msg = "修改失败!" };
  336. RollbackTran();
  337. }
  338. else
  339. {
  340. result = new Result() { Code = 0, Msg = "修改成功!" };
  341. }
  342. }
  343. else
  344. {
  345. RollbackTran();
  346. result = new Result() { Code = -1, Msg = "修改失败,请稍后重试!" };
  347. }
  348. }
  349. CommitTran();
  350. }
  351. catch (Exception ex)
  352. {
  353. return result = new Result() { Code = -2, Msg = "未知错误" };
  354. throw;
  355. }
  356. return result;
  357. }
  358. }
  359. }