TourClientListRepository.cs 51 KB

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