TourClientListRepository.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. using AutoMapper;
  2. using MathNet.Numerics.Distributions;
  3. using NPOI.SS.Formula.PTG;
  4. using NPOI.Util;
  5. using OASystem.Domain;
  6. using OASystem.Domain.AesEncryption;
  7. using OASystem.Domain.Dtos.CRM;
  8. using OASystem.Domain.Dtos.Groups;
  9. using OASystem.Domain.Entities.Customer;
  10. using OASystem.Domain.Entities.Groups;
  11. using OASystem.Domain.ViewModels.Financial;
  12. using OASystem.Domain.ViewModels.Groups;
  13. using OASystem.Infrastructure.Tools;
  14. using Org.BouncyCastle.Utilities.Encoders;
  15. using StackExchange.Redis;
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using System.Diagnostics;
  20. using System.Globalization;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Threading.Tasks;
  24. namespace OASystem.Infrastructure.Repositories.Groups
  25. {
  26. /// <summary>
  27. /// 接团客户名单
  28. /// 仓库
  29. /// </summary>
  30. public class TourClientListRepository :BaseRepository<Grp_TourClientList,TourClientListView>
  31. {
  32. private readonly Result _result;
  33. private readonly IMapper _mapper;
  34. public TourClientListRepository(SqlSugarClient sqlSugar, IMapper mapper)
  35. : base(sqlSugar)
  36. {
  37. _result = new Result() { Code = -1, Msg = "操作失败!" };
  38. _mapper = mapper;
  39. }
  40. /// <summary>
  41. /// 根据团组Id客人名单List
  42. /// </summary>
  43. /// <param name="portId"></param>
  44. /// <param name="diId"></param>
  45. /// <returns></returns>
  46. public async Task<List<SimplClientInfo>> _GuestNameItemByDiId(int portId, int diId)
  47. {
  48. List<SimplClientInfo> simplClients = new List<SimplClientInfo>();
  49. if (portId == 1 || portId == 2 || portId == 3) // 1 web 2 Android 3 ios
  50. {
  51. string sql = $@"Select b.Id,b.Pinyin,b.lastName,b.firstName,b.phone From Grp_TourClientList a, Crm_DeleClient b
  52. Where a.clientid = b.id and a.isdel = 0 and a.diId = {diId}";
  53. simplClients = await _sqlSugar.SqlQueryable<SimplClientInfo>(sql).ToListAsync();
  54. foreach (var item in simplClients)
  55. {
  56. EncryptionProcessor.DecryptProperties(item);
  57. }
  58. }
  59. else
  60. {
  61. _result.Msg = "请传入有效的PortType参数,1 Web 2 Android 3 IOS!";
  62. }
  63. return simplClients;
  64. }
  65. /// <summary>
  66. /// 根据团组Id查询List
  67. /// </summary>
  68. /// <param name="portId"></param>
  69. /// <param name="diId"></param>
  70. /// <returns></returns>
  71. public async Task<Result> _ItemByDiId(int portId, int diId)
  72. {
  73. if (portId == 1 || portId == 2 || portId == 3 ) // 1 web 2 Android 3 ios
  74. {
  75. string sql = string.Format(@"
  76. SELECT
  77. tcl.Id,
  78. tcl.DiId,
  79. tcl.IsAccompany,
  80. temp.*,
  81. u.CnName AS Operator,
  82. tcl.CreateTime AS OperatingTime
  83. FROM
  84. Grp_TourClientList tcl
  85. LEFT JOIN (
  86. SELECT
  87. dc.Id AS DcId,
  88. dc.LastName,
  89. dc.FirstName,
  90. ccom.CompanyFullName,
  91. dc.Job,
  92. cc.CertNo AS IDCardNo,
  93. dc.Sex,
  94. dc.BirthDay
  95. FROM
  96. Crm_DeleClient dc
  97. LEFT JOIN Crm_CustomerCompany ccom ON dc.CrmCompanyId = ccom.Id
  98. AND ccom.IsDel = 0
  99. LEFT JOIN Crm_CustomerCert cc ON dc.Id = cc.DcId
  100. AND cc.SdId = 773
  101. AND cc.IsDel = 0
  102. WHERE
  103. dc.IsDel = 0
  104. ) temp ON temp.DcId = tcl.ClientId
  105. LEFT JOIN Sys_Users u ON tcl.CreateUserId = u.Id
  106. WHERE
  107. tcl.IsDel = 0
  108. AND tcl.DiId = {0}", diId);
  109. var data = await _sqlSugar.SqlQueryable<TourClientListByDiIdView>(sql).ToListAsync();
  110. foreach (var item in data)
  111. {
  112. EncryptionProcessor.DecryptProperties(item);
  113. }
  114. _result.Code = 0;
  115. _result.Data = data;
  116. }
  117. else
  118. {
  119. _result.Msg = "请传入有效的PortType参数,1 Web 2 Android 3 IOS!";
  120. }
  121. return _result;
  122. }
  123. /// <summary>
  124. /// 基础数据 Init
  125. /// </summary>
  126. /// <param name="portId"></param>
  127. /// <returns></returns>
  128. public async Task<JsonView> _BasicDataInit(int portId)
  129. {
  130. var view = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  131. if (portId == 1 || portId == 2 || portId == 3) // 1 web 2 Android 3 ios
  132. {
  133. var setData = await _sqlSugar.Queryable<Sys_SetData>().Where(it => it.IsDel == 0) .ToListAsync();
  134. var shippingSpaceTypeData = setData.Where(it => it.STid == 44).ToList(); //舱位类型
  135. var shippingSpaceTypeData1 = _mapper.Map<List<SetDataInfoView>>(shippingSpaceTypeData);
  136. //var passportTypeData = setData.Where(it => it.STid == 74).ToList(); //护照类型
  137. //List<SetDataInfoView> _PassportTypeData = _mapper.Map<List<SetDataInfoView>>(passportTypeData);
  138. //客户信息资料
  139. var clientInfoSql = string.Format(@"Select dc.Id As DcId,dc.LastName,dc.FirstName,dc.Pinyin,dc.Sex,ccom.CompanyFullName,dc.Job,
  140. cc1.CertNo As IDCardNo,dc.Phone,dc.BirthDay
  141. From Crm_DeleClient dc
  142. Left Join Crm_CustomerCompany ccom On dc.CrmCompanyId = ccom.Id And ccom.IsDel = 0
  143. Left Join Crm_CustomerCert cc1 On dc.Id = cc1.DcId And cc1.SdId = 773 And cc1.IsDel = 0
  144. Where dc.IsDel = 0");
  145. var clientData = _sqlSugar.SqlQueryable<TourClientListDetailsView>(clientInfoSql).ToList();
  146. //公司信息
  147. var clientCompanySql = string.Format(@"Select Id,CompanyFullName From Crm_CustomerCompany Where IsDel = 0");
  148. var clientCompanyData = _sqlSugar.SqlQueryable<CustomerCompanyCiew>(clientCompanySql).ToList();
  149. clientCompanyData = clientCompanyData.DistinctBy(it => it.CompanyFullName).ToList();
  150. foreach ( var item in clientData)
  151. {
  152. if (item.FirstName.Length > 5 && item.LastName.Length > 5)
  153. {
  154. EncryptionProcessor.DecryptProperties(item);
  155. }
  156. }
  157. foreach (var item in clientCompanyData)
  158. {
  159. if (item.CompanyFullName.Length > 5)
  160. {
  161. EncryptionProcessor.DecryptProperties(item);
  162. }
  163. }
  164. var _view = new {
  165. ShippingSpaceTypeData = shippingSpaceTypeData1,
  166. ClientData = clientData,
  167. ClientCompanyData = clientCompanyData
  168. //PassportTypeData = _PassportTypeData
  169. };
  170. view.Code = StatusCodes.Status200OK;
  171. view.Data = _view;
  172. }
  173. else
  174. {
  175. view.Msg = "请传入有效的PortType参数,1 Web 2 Android 3 IOS!";
  176. }
  177. return view;
  178. }
  179. /// <summary>
  180. /// 根据Id查询Details
  181. /// </summary>
  182. /// <param name="portId"></param>
  183. /// <param name="id"></param>
  184. /// <returns></returns>
  185. public async Task<Result> _Details(int portId, int id)
  186. {
  187. if (portId == 1 || portId == 2 || portId == 3) // 1 web 2 Android 3 ios
  188. {
  189. string sql = string.Format(@"
  190. SELECT
  191. tcl.Id,
  192. tcl.DiId,
  193. tcl.IsAccompany,
  194. temp.*,
  195. tcl.ShippingSpaceTypeId,
  196. tcl.ShippingSpaceSpecialNeeds,
  197. tcl.HotelSpecialNeeds,
  198. tcl.MealSpecialNeeds,
  199. tcl.Remark
  200. FROM
  201. Grp_TourClientList tcl
  202. LEFT JOIN (
  203. SELECT
  204. dc.Id AS DcId,
  205. dc.LastName,
  206. dc.FirstName,
  207. dc.Pinyin,
  208. dc.Sex,
  209. ccom.CompanyFullName,
  210. dc.Job,
  211. cc1.CertNo AS IDCardNo,
  212. dc.Phone,
  213. dc.BirthDay,
  214. cc2.PassportType,
  215. cc2.CertNo AS PassportNo,
  216. cc2.Country,
  217. cc2.Area,
  218. cc2.IssueDt,
  219. cc2.ExpiryDt
  220. FROM
  221. Crm_DeleClient dc
  222. LEFT JOIN Crm_CustomerCompany ccom ON dc.CrmCompanyId = ccom.Id
  223. AND ccom.IsDel = 0
  224. LEFT JOIN Crm_CustomerCert cc1 ON dc.Id = cc1.DcId
  225. AND cc1.SdId = 773
  226. AND cc1.IsDel = 0
  227. LEFT JOIN Crm_CustomerCert cc2 ON dc.Id = cc2.DcId
  228. AND cc2.SdId = 774
  229. AND cc2.IsDel = 0
  230. WHERE
  231. dc.IsDel = 0
  232. ) temp ON temp.DcId = tcl.ClientId
  233. WHERE
  234. tcl.IsDel = 0
  235. AND tcl.Id = {0}", id);
  236. var data = await _sqlSugar.SqlQueryable<TourClientListDetailsView>(sql).FirstAsync();
  237. if (data != null)
  238. {
  239. EncryptionProcessor.DecryptProperties(data);
  240. data.BirthDay = data.BirthDay?.DateFormat("yyyy-MM-dd") ?? "";
  241. _result.Code = 0;
  242. _result.Data = data;
  243. }
  244. }
  245. else
  246. {
  247. _result.Msg = "请传入有效的PortType参数,1 Web 2 Android 3 IOS!";
  248. }
  249. return _result;
  250. }
  251. /// <summary>
  252. /// Add Or Edit
  253. /// </summary>
  254. /// <param name="portId"></param>
  255. /// <param name="id"></param>
  256. /// <returns></returns>
  257. public async Task<Result> _AddOrEdit(TourClientListAddOrEditDto dto)
  258. {
  259. if (string.IsNullOrEmpty(dto.CompanyFullName))
  260. {
  261. _result.Msg = "客户单位名称为空!";
  262. return _result;
  263. }
  264. //if (string.IsNullOrEmpty(dto.IDCardNo))
  265. //{
  266. // _result.Msg = "客户身份证No为空!";
  267. // return _result;
  268. //}
  269. if (dto.ShippingSpaceTypeId < 0)
  270. {
  271. _result.Msg = "舱位类型为空!";
  272. return _result;
  273. }
  274. if (dto.Id >= 0)
  275. {
  276. #region 参数处理
  277. _sqlSugar.BeginTran();
  278. int clientId = -1;
  279. int crmCompanyId = -1;
  280. string idNo = "", birthDay = "";
  281. int sex = -1;
  282. #region 身份证验证
  283. if (!string.IsNullOrEmpty(dto.IDCardNo))
  284. {
  285. var idBool = dto.IDCardNo.IsValidChineseId();
  286. if (idBool)
  287. {
  288. idNo = dto.IDCardNo;
  289. DateTime? birthDayDt = CommonFun.GetBirthDateFromIdentityCard(dto.IDCardNo);
  290. if (birthDayDt != null)
  291. {
  292. birthDay = birthDayDt?.ToString("yyyy-MM-dd") ?? "";
  293. }
  294. sex = CommonFun.GetGenderFromIdentityCard(dto.IDCardNo);
  295. }
  296. #region dto重新赋值
  297. if (string.IsNullOrEmpty(dto.BirthDay)) dto.BirthDay = birthDay;
  298. if (dto.Sex < 0) dto.Sex = sex;
  299. #endregion
  300. }
  301. #endregion
  302. EncryptionProcessor.EncryptProperties(dto);
  303. var clientInfo = await _sqlSugar.Queryable<Crm_DeleClient>().Where(it => it.IsDel == 0
  304. && it.LastName.Equals(dto.LastName)
  305. && it.FirstName.Equals(dto.FirstName)
  306. //&& it.Phone.Equals(dto.Phone)
  307. ).FirstAsync();
  308. Crm_CustomerCompany _CustomerCompany = new Crm_CustomerCompany()
  309. {
  310. CompanyFullName = dto.CompanyFullName,
  311. LastedOpUserId = dto.UserId,
  312. CreateUserId = dto.UserId
  313. };
  314. Crm_CustomerCert _CustomerCert = new Crm_CustomerCert()
  315. {
  316. SdId = 773,
  317. CertNo = dto.IDCardNo,
  318. CreateUserId = dto.UserId
  319. };
  320. Crm_DeleClient _DeleClient = new Crm_DeleClient()
  321. {
  322. LastName = dto.LastName,
  323. FirstName = dto.FirstName,
  324. Pinyin = dto.Pinyin,
  325. Phone = dto.Phone,
  326. Sex = dto.Sex,
  327. BirthDay = dto.BirthDay == "" ? null : Convert.ToDateTime(dto.BirthDay),
  328. Job = dto.Job,
  329. CreateUserId = dto.UserId
  330. };
  331. //客户单位/公司 操作
  332. //1.添加 or 修改 公司基本信息
  333. if (clientInfo == null)
  334. {
  335. var companyInfo = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0 &&
  336. it.CompanyFullName.Equals(dto.CompanyFullName)
  337. ).FirstAsync();
  338. if (companyInfo != null) crmCompanyId = companyInfo.Id;
  339. else
  340. {
  341. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  342. if (companyAdd < 0)
  343. {
  344. _result.Msg = "客户公司信息添加失败!";
  345. _sqlSugar.RollbackTran();
  346. return _result;
  347. }
  348. crmCompanyId = companyAdd;
  349. }
  350. }
  351. else
  352. {
  353. var companyInfo = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0 &&
  354. it.CompanyFullName.Equals(dto.CompanyFullName)
  355. ).FirstAsync();
  356. if (companyInfo != null)
  357. {
  358. crmCompanyId = companyInfo.Id;
  359. if (companyInfo.CompanyFullName.Equals(dto.CompanyFullName))
  360. {
  361. companyInfo.CompanyFullName = _CustomerCompany.CompanyFullName;
  362. var companyEdit = await _sqlSugar.Updateable(companyInfo).UpdateColumns(it =>
  363. new
  364. {
  365. it.CompanyFullName,
  366. }
  367. )
  368. .Where(it => it.Id == companyInfo.Id)
  369. .ExecuteCommandAsync();
  370. if (companyEdit < 0)
  371. {
  372. _result.Msg = "客户公司信息修改失败!";
  373. _sqlSugar.RollbackTran();
  374. return _result;
  375. }
  376. }
  377. }
  378. else
  379. {
  380. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  381. if (companyAdd < 0)
  382. {
  383. _result.Msg = "客户公司信息添加失败!";
  384. _sqlSugar.RollbackTran();
  385. return _result;
  386. }
  387. crmCompanyId = companyAdd;
  388. }
  389. }
  390. _DeleClient.CrmCompanyId = crmCompanyId;
  391. //客户人员 操作
  392. //2.添加 or 修改 客户基本信息
  393. if (clientInfo != null) //该客户存在 修改信息
  394. {
  395. clientId = clientInfo.Id;
  396. var clientEdit = await _sqlSugar.Updateable(_DeleClient).UpdateColumns(it =>
  397. new
  398. {
  399. it.CrmCompanyId,
  400. it.LastName,
  401. it.FirstName,
  402. it.Pinyin,
  403. it.Job,
  404. it.Sex,
  405. it.Phone,
  406. it.BirthDay
  407. }
  408. )
  409. .Where(it => it.Id == clientId)
  410. .ExecuteCommandAsync();
  411. if (clientEdit < 0)
  412. {
  413. _result.Msg = "客户基础信息修改失败!";
  414. _sqlSugar.RollbackTran();
  415. return _result;
  416. }
  417. }
  418. else //不存在添加 客户信息
  419. {
  420. _DeleClient.CrmCompanyId = crmCompanyId;
  421. var clientAdd = await _sqlSugar.Insertable(_DeleClient).ExecuteReturnIdentityAsync();
  422. if (clientAdd < 0)
  423. {
  424. _result.Msg = "客户基本信息添加失败!";
  425. _sqlSugar.RollbackTran();
  426. return _result;
  427. }
  428. clientId = clientAdd;
  429. }
  430. //客户身份证操作
  431. //3.添加 or 修改 身份证信息
  432. _CustomerCert.DcId = clientId;
  433. if (clientInfo == null)
  434. {
  435. var certInfo = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 &&
  436. it.SdId == 773 && //卡类型 身份证
  437. it.CertNo == dto.IDCardNo //人员Id
  438. ).FirstAsync();
  439. if (certInfo == null)
  440. {
  441. var certAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteReturnIdentityAsync();
  442. if (certAdd < 0)
  443. {
  444. _result.Msg = "客户身份证添加失败!";
  445. _sqlSugar.RollbackTran();
  446. return _result;
  447. }
  448. }
  449. }
  450. else
  451. {
  452. var certInfo = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 &&
  453. it.SdId == 773 && //卡类型 身份证
  454. it.DcId == clientInfo.Id //人员Id
  455. ).FirstAsync();
  456. if (certInfo == null)
  457. {
  458. var certAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteReturnIdentityAsync();
  459. if (certAdd < 0)
  460. {
  461. _result.Msg = "客户身份证添加失败!";
  462. _sqlSugar.RollbackTran();
  463. return _result;
  464. }
  465. }
  466. else
  467. {
  468. var certEdit = await _sqlSugar.Updateable(_CustomerCert).UpdateColumns(it =>
  469. new
  470. {
  471. it.CertNo,
  472. }
  473. )
  474. .Where(it => it.Id == certInfo.Id)
  475. .ExecuteCommandAsync();
  476. if (certEdit < 0)
  477. {
  478. _result.Msg = "客户身份证修改失败!";
  479. _sqlSugar.RollbackTran();
  480. return _result;
  481. }
  482. }
  483. }
  484. //团组客户信息名单操作
  485. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  486. {
  487. IsAccompany = dto.IsAccompany,
  488. DiId = dto.DiId,
  489. ClientId = clientId,
  490. ShippingSpaceTypeId = dto.ShippingSpaceTypeId,
  491. ShippingSpaceSpecialNeeds = dto.ShippingSpaceSpecialNeeds,
  492. HotelSpecialNeeds = dto.HotelSpecialNeeds,
  493. MealSpecialNeeds = dto.MealSpecialNeeds,
  494. Remark = dto.Remark ?? "",
  495. CreateUserId = dto.UserId
  496. };
  497. #endregion
  498. if (dto.Id == 0) // 添加
  499. {
  500. var tourClientAdd = await _sqlSugar.Insertable(_TourClientList).ExecuteReturnIdentityAsync();
  501. if (tourClientAdd < 0)
  502. {
  503. _result.Msg = "接团客户名单添加失败!";
  504. _sqlSugar.RollbackTran();
  505. return _result;
  506. }
  507. }
  508. else if (dto.Id > 0) //修改
  509. {
  510. var certEdit = await _sqlSugar.Updateable(_TourClientList).UpdateColumns(it =>
  511. new
  512. {
  513. it.IsAccompany,
  514. it.ClientId,
  515. it.ShippingSpaceTypeId,
  516. it.ShippingSpaceSpecialNeeds,
  517. it.HotelSpecialNeeds,
  518. it.MealSpecialNeeds,
  519. it.Remark,
  520. }
  521. )
  522. .Where(it => it.Id == dto.Id)
  523. .ExecuteCommandAsync();
  524. if (certEdit < 0)
  525. {
  526. _result.Msg = "接团客户名单失败修改!";
  527. _sqlSugar.RollbackTran();
  528. return _result;
  529. }
  530. }
  531. _result.Code = 0;
  532. _sqlSugar.CommitTran();
  533. }
  534. else
  535. {
  536. _result.Msg = "请传入有效的id参数!";
  537. }
  538. return _result;
  539. }
  540. /// <summary>
  541. /// AddMultiple
  542. /// 添加多个
  543. /// </summary>
  544. /// <param name="portId"></param>
  545. /// <param name="id"></param>
  546. /// <returns></returns>
  547. public async Task<Result> _AddMultiple(TourClientListAddMultipleDto dto)
  548. {
  549. if (dto.DiId < 0)
  550. {
  551. _result.Msg = string.Format(@"请传入有效的团组Id参数!");
  552. return _result;
  553. }
  554. List<TourClientListInfo> _TourClientListInfos = dto.TourClientListInfos;
  555. if (_TourClientListInfos == null || _TourClientListInfos.Count < 0)
  556. {
  557. _result.Msg = string.Format(@"接团客户名单信息集合为空,不执行批量添加!");
  558. return _result;
  559. }
  560. List<Crm_DeleClient> _DeleClients = await _sqlSugar.Queryable<Crm_DeleClient>().Where(it => it.IsDel == 0).ToListAsync();
  561. List<Crm_CustomerCompany> _CustomerCompanies = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0).ToListAsync();
  562. List<Crm_CustomerCert> _CustomerCerts = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 && it.SdId == 773).ToListAsync(); // 身份证类型证件信息
  563. string _Msg = "";
  564. int _DiId = dto.DiId;
  565. int _UserId = dto.UserId;
  566. foreach (var item in _TourClientListInfos)
  567. {
  568. EncryptionProcessor.EncryptProperties(item);
  569. _sqlSugar.BeginTran();
  570. int companyId = -1;
  571. int clientId = -1;
  572. string clientName = string.Format(@"{0}{1}", item.LastName, item.FirstName);
  573. Crm_DeleClient _DeleClientInfo = _DeleClients.Where(it => it.LastName.Equals(item.LastName) &&
  574. it.FirstName.Equals(item.FirstName) &&
  575. it.Phone.Equals(item.Phone)).FirstOrDefault();
  576. if (_DeleClientInfo == null) //添加
  577. {
  578. if (!string.IsNullOrEmpty(item.CompanyFullName))
  579. {
  580. var companyInfo = _CustomerCompanies.Where(it => it.CompanyFullName.Equals(item.CompanyFullName)).FirstOrDefault();
  581. if (companyInfo != null) companyId = companyInfo.Id;
  582. else
  583. {
  584. Crm_CustomerCompany _CustomerCompany = new Crm_CustomerCompany()
  585. {
  586. CompanyFullName = item.CompanyFullName,
  587. LastedOpUserId = _UserId,
  588. CreateUserId = _UserId
  589. };
  590. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  591. if (companyAdd < 0)
  592. {
  593. _Msg += string.Format("{0} 公司信息添加失败!请前往客户信息确认!\r\n", clientName);
  594. }
  595. companyId = companyAdd;
  596. }
  597. }
  598. _DeleClientInfo = new Crm_DeleClient()
  599. {
  600. LastName = item.LastName,
  601. FirstName = item.FirstName,
  602. Pinyin = item.Pinyin,
  603. CrmCompanyId = companyId,
  604. Job = item.Job,
  605. Sex = item.Sex,
  606. Phone = item.Phone
  607. };
  608. string temp_birthDay = "";
  609. DateTime tempDt_Birthday;
  610. bool b_birth = DateTime.TryParse(item.BirthDay, out tempDt_Birthday);
  611. _DeleClientInfo.BirthDay = b_birth ? tempDt_Birthday : null;
  612. var clientAdd = await _sqlSugar.Insertable(_DeleClientInfo).ExecuteReturnIdentityAsync();
  613. if (clientAdd < 0)
  614. {
  615. _Msg += string.Format("{0} 基本信息添加失败,本条客户名单不添加!请重新添加!\r\n", clientName);
  616. _sqlSugar.RollbackTran();
  617. continue;
  618. }
  619. clientId = clientAdd;
  620. if (!string.IsNullOrEmpty(item.IDCardNo))
  621. {
  622. Crm_CustomerCert _CustomerCert = _CustomerCerts.Where(it => it.DcId == clientId && it.CertNo.Equals(item.IDCardNo)).FirstOrDefault();
  623. if (_CustomerCert == null)
  624. {
  625. _CustomerCert = new Crm_CustomerCert()
  626. {
  627. DcId = clientId,
  628. SdId = 773,
  629. CertNo = item.IDCardNo,
  630. CreateUserId = _UserId,
  631. };
  632. var customerCertAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteCommandAsync();
  633. if (customerCertAdd < 0)
  634. {
  635. _Msg += string.Format("{0} 身份证信息添加失败!请前往客户信息确认!\r\n", clientName);
  636. }
  637. }
  638. }
  639. }
  640. else //修改客户基本信息
  641. {
  642. clientId = _DeleClientInfo.Id;
  643. if (!string.IsNullOrEmpty(item.CompanyFullName))
  644. {
  645. var companyInfo = _CustomerCompanies.Where(it => it.CompanyFullName.Equals(item.CompanyFullName)).FirstOrDefault();
  646. if (companyInfo != null) companyId = companyInfo.Id;
  647. else
  648. {
  649. Crm_CustomerCompany _CustomerCompany = new Crm_CustomerCompany()
  650. {
  651. CompanyFullName = item.CompanyFullName,
  652. LastedOpUserId = _UserId,
  653. CreateUserId = _UserId
  654. };
  655. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnEntityAsync();
  656. if (companyAdd == null)
  657. {
  658. _Msg += string.Format("{0} 公司信息添加失败!请前往客户信息确认!\r\n", clientName);
  659. }
  660. _CustomerCompanies.Add(companyAdd);
  661. companyId = companyAdd.Id;
  662. }
  663. }
  664. Crm_DeleClient _DeleClient = new Crm_DeleClient() {
  665. CrmCompanyId = companyId,
  666. LastName = item.LastName,
  667. FirstName = item.FirstName,
  668. Pinyin = item.Pinyin,
  669. Sex = item.Sex,
  670. Phone = item.Phone
  671. };
  672. string temp_birthDay = "";
  673. DateTime tempDt_Birthday;
  674. bool b_birth = DateTime.TryParse(item.BirthDay, out tempDt_Birthday);
  675. _DeleClient.BirthDay = b_birth ? tempDt_Birthday : null;
  676. var clientEdit = await _sqlSugar.Updateable(_DeleClient).UpdateColumns(it =>
  677. new
  678. {
  679. it.CrmCompanyId,
  680. it.LastName,
  681. it.FirstName,
  682. it.Sex,
  683. it.Phone,
  684. it.BirthDay
  685. }
  686. )
  687. .Where(it => it.Id == clientId)
  688. .ExecuteCommandAsync();
  689. if (clientEdit < 0)
  690. {
  691. _Msg += string.Format("{0} 基本信息修改失败,!请前往客户信息修改!\r\n", clientName);
  692. }
  693. }
  694. //团组客户信息名单操作
  695. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  696. {
  697. DiId = _DiId,
  698. IsAccompany = item.IsAccompany,
  699. ClientId = clientId,
  700. ShippingSpaceTypeId = item.ShippingSpaceTypeId,
  701. CreateUserId = _UserId,
  702. ShippingSpaceSpecialNeeds = item.ShippingSpaceSpecialNeeds,
  703. HotelSpecialNeeds = item.HotelSpecialNeeds,
  704. MealSpecialNeeds = item.MealSpecialNeeds,
  705. Remark = item.Remark
  706. };
  707. //判断现有团组客户名单是否存在该客户
  708. var QuerFirstClient = _sqlSugar.Queryable<Grp_TourClientList>().First(x => x.ClientId == _TourClientList.ClientId && x.DiId == _TourClientList.DiId && x.IsDel == 0);
  709. if (QuerFirstClient != null)
  710. {
  711. QuerFirstClient.IsDel = 1;
  712. QuerFirstClient.DeleteUserId = dto.UserId;
  713. QuerFirstClient.DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  714. var UpdateTrue = _sqlSugar.Updateable(QuerFirstClient).UpdateColumns(x => new { x.IsDel,x.DeleteUserId,x.DeleteTime}).ExecuteCommand() > 0;
  715. }
  716. var tourClientAdd = await _sqlSugar.Insertable(_TourClientList).ExecuteReturnIdentityAsync();
  717. if (tourClientAdd < 0)
  718. {
  719. _Msg += string.Format("{0} 接团客户名单添加失败!请检查数据完整性后重新添加!\r\n", clientName);
  720. _sqlSugar.RollbackTran();
  721. continue;
  722. }
  723. _sqlSugar.CommitTran();
  724. }
  725. if (!string.IsNullOrEmpty(_Msg)) _result.Msg = _Msg;
  726. else _result.Msg = string.Format("操作成功!");
  727. _result.Code = 0;
  728. return _result;
  729. }
  730. public async Task<Result> OperMultiple(List<TourClientListProcessInfo> _TourClientListInfos , int diid,int userId)
  731. {
  732. if (diid < 0)
  733. {
  734. _result.Msg = string.Format(@"请传入有效的团组Id参数!");
  735. return _result;
  736. }
  737. if (_TourClientListInfos == null || _TourClientListInfos.Count < 0)
  738. {
  739. _result.Msg = string.Format(@"接团客户名单信息集合为空,不执行批量添加!");
  740. return _result;
  741. }
  742. _sqlSugar.BeginTran();
  743. try
  744. {
  745. _sqlSugar.Updateable<Grp_TourClientList>().SetColumns(x=> new Grp_TourClientList { IsDel = 1 } ).Where(x=>x.DiId == diid).ExecuteCommand();
  746. foreach (var item in _TourClientListInfos)
  747. {
  748. EncryptionProcessor.EncryptProperties(item);
  749. //客户信息
  750. var clientInfo = await _sqlSugar.Queryable<Crm_DeleClient>().Where(it => it.IsDel == 0 &&
  751. it.LastName.Equals(item.LastName) &&
  752. it.FirstName.Equals(item.FirstName) &&
  753. it.Phone.Equals(item.Phone)
  754. ).FirstAsync();
  755. Crm_CustomerCompany _CustomerCompany = new Crm_CustomerCompany()
  756. {
  757. CompanyFullName = item.CompanyFullName,
  758. LastedOpUserId = userId,
  759. CreateUserId = userId
  760. };
  761. Crm_CustomerCert _CustomerCert = new Crm_CustomerCert()
  762. {
  763. SdId = 773,
  764. CertNo = item.IDCardNo,
  765. CreateUserId = userId
  766. };
  767. Crm_DeleClient _DeleClient = new Crm_DeleClient()
  768. {
  769. LastName = item.LastName,
  770. FirstName = item.FirstName,
  771. Pinyin = item.Pinyin,
  772. Phone = item.Phone,
  773. Sex = item.Sex,
  774. BirthDay = string.IsNullOrEmpty(item.BirthDay) ? null : Convert.ToDateTime(item.BirthDay),
  775. Job = item.Job,
  776. CreateUserId = userId
  777. };
  778. int clientId = -1;
  779. int crmCompanyId = -1;
  780. //客户单位/公司 操作
  781. //1.添加 or 修改 公司基本信息
  782. if (clientInfo == null)
  783. {
  784. var companyInfo = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0 &&
  785. it.CompanyFullName.Equals(item.CompanyFullName)
  786. ).FirstAsync();
  787. if (companyInfo != null) crmCompanyId = companyInfo.Id;
  788. else
  789. {
  790. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  791. if (companyAdd < 0)
  792. {
  793. _result.Msg = "客户公司信息添加失败!";
  794. _sqlSugar.RollbackTran();
  795. return _result;
  796. }
  797. crmCompanyId = companyAdd;
  798. }
  799. }
  800. else
  801. {
  802. var companyInfo = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0 &&
  803. it.Id == clientInfo.CrmCompanyId
  804. ).FirstAsync();
  805. if (companyInfo != null)
  806. {
  807. crmCompanyId = companyInfo.Id;
  808. if (!companyInfo.CompanyFullName.Equals(item.CompanyFullName))
  809. {
  810. companyInfo.CompanyFullName = item.CompanyFullName;
  811. var companyEdit = await _sqlSugar.Updateable(companyInfo).UpdateColumns(it =>
  812. new
  813. {
  814. it.CompanyFullName,
  815. }
  816. )
  817. .Where(it => it.Id == companyInfo.Id)
  818. .ExecuteCommandAsync();
  819. if (companyEdit < 0)
  820. {
  821. _result.Msg = "客户公司信息修改失败!";
  822. _sqlSugar.RollbackTran();
  823. return _result;
  824. }
  825. }
  826. }
  827. else
  828. {
  829. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  830. if (companyAdd < 0)
  831. {
  832. _result.Msg = "客户公司信息添加失败!";
  833. _sqlSugar.RollbackTran();
  834. return _result;
  835. }
  836. crmCompanyId = companyAdd;
  837. }
  838. }
  839. //客户人员 操作
  840. //2.添加 or 修改 客户基本信息
  841. if (clientInfo != null) //该客户存在 修改信息
  842. {
  843. clientId = clientInfo.Id;
  844. _DeleClient.CrmCompanyId = crmCompanyId;
  845. var clientEdit = await _sqlSugar.Updateable(_DeleClient).UpdateColumns(it =>
  846. new
  847. {
  848. it.LastName,
  849. it.FirstName,
  850. it.Pinyin,
  851. it.Sex,
  852. it.Phone,
  853. it.BirthDay
  854. }
  855. )
  856. .Where(it => it.Id == clientId)
  857. .ExecuteCommandAsync();
  858. if (clientEdit < 0)
  859. {
  860. _result.Msg = "客户基础信息修改失败!";
  861. _sqlSugar.RollbackTran();
  862. return _result;
  863. }
  864. }
  865. else //不存在添加 客户信息
  866. {
  867. _DeleClient.CrmCompanyId = crmCompanyId;
  868. var clientAdd = await _sqlSugar.Insertable(_DeleClient).ExecuteReturnIdentityAsync();
  869. if (clientAdd < 0)
  870. {
  871. _result.Msg = "客户基本信息添加失败!";
  872. _sqlSugar.RollbackTran();
  873. return _result;
  874. }
  875. clientId = clientAdd;
  876. }
  877. //客户身份证操作
  878. //3.添加 or 修改 身份证信息
  879. _CustomerCert.DcId = clientId;
  880. if (clientInfo == null)
  881. {
  882. var certInfo = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 &&
  883. it.SdId == 773 && //卡类型 身份证
  884. it.CertNo == item.IDCardNo //人员Id
  885. ).FirstAsync();
  886. if (certInfo == null)
  887. {
  888. var certAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteReturnIdentityAsync();
  889. if (certAdd < 0)
  890. {
  891. _result.Msg = "客户身份证添加失败!";
  892. _sqlSugar.RollbackTran();
  893. return _result;
  894. }
  895. }
  896. }
  897. else
  898. {
  899. var certInfo = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 &&
  900. it.SdId == 773 && //卡类型 身份证
  901. it.DcId == clientInfo.Id //人员Id
  902. ).FirstAsync();
  903. if (certInfo == null)
  904. {
  905. var certAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteReturnIdentityAsync();
  906. if (certAdd < 0)
  907. {
  908. _result.Msg = "客户身份证添加失败!";
  909. _sqlSugar.RollbackTran();
  910. return _result;
  911. }
  912. }
  913. else
  914. {
  915. var certEdit = await _sqlSugar.Updateable(_CustomerCert).UpdateColumns(it =>
  916. new
  917. {
  918. it.CertNo,
  919. }
  920. )
  921. .Where(it => it.Id == certInfo.Id)
  922. .ExecuteCommandAsync();
  923. if (certEdit < 0)
  924. {
  925. _result.Msg = "客户身份证修改失败!";
  926. _sqlSugar.RollbackTran();
  927. return _result;
  928. }
  929. }
  930. }
  931. //团组客户信息名单操作
  932. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  933. {
  934. DiId = diid,
  935. ClientId = clientId,
  936. ShippingSpaceTypeId = item.ShippingSpaceTypeId,
  937. ShippingSpaceSpecialNeeds = item.ShippingSpaceSpecialNeeds,
  938. HotelSpecialNeeds = item.HotelSpecialNeeds,
  939. MealSpecialNeeds = item.MealSpecialNeeds,
  940. Remark = item.Remark,
  941. CreateUserId = userId,
  942. IsAccompany = item.IsAccompany,
  943. };
  944. var tourClientAdd = await _sqlSugar.Insertable(_TourClientList).ExecuteReturnIdentityAsync();
  945. if (tourClientAdd < 0)
  946. {
  947. _result.Msg = "接团客户名单添加失败!";
  948. _sqlSugar.RollbackTran();
  949. return _result;
  950. }
  951. }
  952. _result.Code = 0;
  953. _sqlSugar.CommitTran();
  954. }
  955. catch (Exception ex)
  956. {
  957. _sqlSugar.RollbackTran();
  958. _result.Msg = "程序异常 -- " + ex.Message;
  959. }
  960. return _result;
  961. }
  962. /// <summary>
  963. /// Del
  964. /// </summary>
  965. /// <param name="portId"></param>
  966. /// <param name="id"></param>
  967. /// <returns></returns>
  968. public async Task<Result> _Del(int id, int userId)
  969. {
  970. if (id > 0)
  971. {
  972. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  973. {
  974. Id = id,
  975. IsDel = 1,
  976. DeleteUserId = userId,
  977. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  978. };
  979. var certEdit = await _sqlSugar.Updateable(_TourClientList).UpdateColumns(it =>
  980. new
  981. {
  982. it.IsDel,
  983. it.DeleteUserId,
  984. it.DeleteTime,
  985. }
  986. )
  987. .Where(it => it.Id == id)
  988. .ExecuteCommandAsync();
  989. if (certEdit < 0)
  990. {
  991. _result.Msg = string.Format(@"接团客户名单删除修改!");
  992. return _result;
  993. }
  994. _result.Code = 0;
  995. }
  996. else
  997. {
  998. _result.Msg = string.Format(@"请传入有效的Id参数");
  999. }
  1000. return _result;
  1001. }
  1002. /// <summary>
  1003. /// 分割客户名称
  1004. /// </summary>
  1005. /// <param name="ids"></param>
  1006. /// <returns></returns>
  1007. public string _ResolveCustomerName(string ids)
  1008. {
  1009. string clientStr = "";
  1010. if (string.IsNullOrEmpty(ids)) return clientStr;
  1011. List<int> intList = new List<int>();
  1012. //if (ids.Contains(","))
  1013. //{
  1014. // string[] numbers = ids.Split(',');
  1015. // foreach (string numberStr in numbers)
  1016. // {
  1017. // if (int.TryParse(numberStr.Trim(), out int number))
  1018. // {
  1019. // intList.Add(number);
  1020. // }
  1021. // }
  1022. //}
  1023. //else
  1024. //{
  1025. // if (int.TryParse(ids.Trim(), out int number))
  1026. // {
  1027. // intList.Add(number);
  1028. // }
  1029. //}
  1030. string sql = string.Format(@$"Select * From Crm_DeleClient Where IsDel = 0 And Id In ({ids})");
  1031. var infos = _sqlSugar.SqlQueryable<Crm_DeleClient>(sql).ToList();
  1032. if (infos.Count > 0)
  1033. {
  1034. var infos1 = infos.Select(it => it.LastName + it.FirstName).ToList();
  1035. clientStr = string.Join(",", infos1);
  1036. }
  1037. return clientStr;
  1038. }
  1039. private class CustomerNameInfo
  1040. {
  1041. public int Id { get; set; }
  1042. public string Name { get; set; }
  1043. }
  1044. }
  1045. }