CustomersRepository.cs 24 KB

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