TourClientListRepository.cs 51 KB

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