CustomersRepository.cs 24 KB

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