TourClientListRepository.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 : 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. BirthDay = item.BirthDay == null ? null : Convert.ToDateTime(item.BirthDay),
  521. };
  522. var clientAdd = await _sqlSugar.Insertable(_DeleClientInfo).ExecuteReturnIdentityAsync();
  523. if (clientAdd < 0)
  524. {
  525. _Msg += string.Format("{0} 基本信息添加失败,本条客户名单不添加!请重新添加!\r\n", clientName);
  526. _sqlSugar.RollbackTran();
  527. continue;
  528. }
  529. clientId = clientAdd;
  530. if (!string.IsNullOrEmpty(item.IDCardNo))
  531. {
  532. Crm_CustomerCert _CustomerCert = _CustomerCerts.Where(it => it.DcId == clientId && it.CertNo.Equals(item.IDCardNo)).FirstOrDefault();
  533. if (_CustomerCert == null)
  534. {
  535. _CustomerCert = new Crm_CustomerCert()
  536. {
  537. DcId = clientId,
  538. SdId = 773,
  539. CertNo = item.IDCardNo,
  540. CreateUserId = _UserId,
  541. };
  542. var customerCertAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteCommandAsync();
  543. if (customerCertAdd < 0)
  544. {
  545. _Msg += string.Format("{0} 身份证信息添加失败!请前往客户信息确认!\r\n", clientName);
  546. }
  547. }
  548. }
  549. }
  550. else //修改客户基本信息
  551. {
  552. clientId = _DeleClientInfo.Id;
  553. if (!string.IsNullOrEmpty(item.CompanyFullName))
  554. {
  555. var companyInfo = _CustomerCompanies.Where(it => it.CompanyFullName.Equals(item.CompanyFullName)).FirstOrDefault();
  556. if (companyInfo != null) companyId = companyInfo.Id;
  557. else
  558. {
  559. Crm_CustomerCompany _CustomerCompany = new Crm_CustomerCompany()
  560. {
  561. CompanyFullName = item.CompanyFullName,
  562. LastedOpUserId = _UserId,
  563. CreateUserId = _UserId
  564. };
  565. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  566. if (companyAdd < 0)
  567. {
  568. _Msg += string.Format("{0} 公司信息添加失败!请前往客户信息确认!\r\n", clientName);
  569. }
  570. companyId = companyAdd;
  571. }
  572. }
  573. Crm_DeleClient _DeleClient = new Crm_DeleClient() {
  574. LastName = item.LastName,
  575. FirstName = item.FirstName,
  576. Pinyin = item.Pinyin,
  577. Sex = item.Sex,
  578. Phone = item.Phone,
  579. BirthDay = item.BirthDay == null ? null : Convert.ToDateTime(item.BirthDay)
  580. };
  581. var clientEdit = await _sqlSugar.Updateable(_DeleClient).UpdateColumns(it =>
  582. new
  583. {
  584. it.LastName,
  585. it.FirstName,
  586. it.Sex,
  587. it.Phone,
  588. it.BirthDay
  589. }
  590. )
  591. .Where(it => it.Id == clientId)
  592. .ExecuteCommandAsync();
  593. if (clientEdit < 0)
  594. {
  595. _Msg += string.Format("{0} 基本信息修改失败,!请前往客户信息修改!\r\n", clientName);
  596. }
  597. }
  598. //团组客户信息名单操作
  599. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  600. {
  601. DiId = _DiId,
  602. ClientId = clientId,
  603. ShippingSpaceTypeId = item.ShippingSpaceTypeId,
  604. CreateUserId = _UserId,
  605. ShippingSpaceSpecialNeeds = item.ShippingSpaceSpecialNeeds,
  606. HotelSpecialNeeds = item.HotelSpecialNeeds,
  607. MealSpecialNeeds = item.MealSpecialNeeds,
  608. Remark = item.Remark
  609. };
  610. //判断现有团组客户名单是否存在该客户
  611. var QuerFirstClient = _sqlSugar.Queryable<Grp_TourClientList>().First(x => x.ClientId == _TourClientList.ClientId && x.DiId == _TourClientList.DiId && x.IsDel == 0);
  612. if (QuerFirstClient != null)
  613. {
  614. QuerFirstClient.IsDel = 1;
  615. QuerFirstClient.DeleteUserId = dto.UserId;
  616. QuerFirstClient.DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  617. var UpdateTrue = _sqlSugar.Updateable(QuerFirstClient).UpdateColumns(x => new { x.IsDel,x.DeleteUserId,x.DeleteTime}).ExecuteCommand() > 0;
  618. }
  619. var tourClientAdd = await _sqlSugar.Insertable(_TourClientList).ExecuteReturnIdentityAsync();
  620. if (tourClientAdd < 0)
  621. {
  622. _Msg += string.Format("{0} 接团客户名单添加失败!请检查数据完整性后重新添加!\r\n", clientName);
  623. _sqlSugar.RollbackTran();
  624. continue;
  625. }
  626. _sqlSugar.CommitTran();
  627. }
  628. if (!string.IsNullOrEmpty(_Msg)) _result.Msg = _Msg;
  629. else _result.Msg = string.Format("操作成功!");
  630. _result.Code = 0;
  631. return _result;
  632. }
  633. public async Task<Result> OperMultiple(List<TourClientListProcessInfo> _TourClientListInfos , int diid,int userId)
  634. {
  635. if (diid < 0)
  636. {
  637. _result.Msg = string.Format(@"请传入有效的团组Id参数!");
  638. return _result;
  639. }
  640. if (_TourClientListInfos == null || _TourClientListInfos.Count < 0)
  641. {
  642. _result.Msg = string.Format(@"接团客户名单信息集合为空,不执行批量添加!");
  643. return _result;
  644. }
  645. _sqlSugar.BeginTran();
  646. try
  647. {
  648. _sqlSugar.Updateable<Grp_TourClientList>().SetColumns(x=> new Grp_TourClientList { IsDel = 1 } ).Where(x=>x.DiId == diid).ExecuteCommand();
  649. foreach (var item in _TourClientListInfos)
  650. {
  651. //客户信息
  652. var clientInfo = await _sqlSugar.Queryable<Crm_DeleClient>().Where(it => it.IsDel == 0 &&
  653. it.LastName.Equals(item.LastName) &&
  654. it.FirstName.Equals(item.FirstName) &&
  655. it.Phone.Equals(item.Phone)
  656. ).FirstAsync();
  657. Crm_CustomerCompany _CustomerCompany = new Crm_CustomerCompany()
  658. {
  659. CompanyFullName = item.CompanyFullName,
  660. LastedOpUserId = userId,
  661. CreateUserId = userId
  662. };
  663. Crm_CustomerCert _CustomerCert = new Crm_CustomerCert()
  664. {
  665. SdId = 773,
  666. CertNo = item.IDCardNo,
  667. CreateUserId = userId
  668. };
  669. Crm_DeleClient _DeleClient = new Crm_DeleClient()
  670. {
  671. LastName = item.LastName,
  672. FirstName = item.FirstName,
  673. Pinyin = item.Pinyin,
  674. Phone = item.Phone,
  675. Sex = item.Sex,
  676. BirthDay = item.BirthDay == null ? null : Convert.ToDateTime(item.BirthDay),
  677. Job = item.Job,
  678. CreateUserId = userId
  679. };
  680. int clientId = -1;
  681. int crmCompanyId = -1;
  682. //客户单位/公司 操作
  683. //1.添加 or 修改 公司基本信息
  684. if (clientInfo == null)
  685. {
  686. var companyInfo = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0 &&
  687. it.CompanyFullName.Equals(item.CompanyFullName)
  688. ).FirstAsync();
  689. if (companyInfo != null) crmCompanyId = companyInfo.Id;
  690. else
  691. {
  692. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  693. if (companyAdd < 0)
  694. {
  695. _result.Msg = "客户公司信息添加失败!";
  696. _sqlSugar.RollbackTran();
  697. return _result;
  698. }
  699. crmCompanyId = companyAdd;
  700. }
  701. }
  702. else
  703. {
  704. var companyInfo = await _sqlSugar.Queryable<Crm_CustomerCompany>().Where(it => it.IsDel == 0 &&
  705. it.Id == clientInfo.CrmCompanyId
  706. ).FirstAsync();
  707. if (companyInfo != null)
  708. {
  709. crmCompanyId = companyInfo.Id;
  710. if (!companyInfo.CompanyFullName.Equals(item.CompanyFullName))
  711. {
  712. companyInfo.CompanyFullName = item.CompanyFullName;
  713. var companyEdit = await _sqlSugar.Updateable(companyInfo).UpdateColumns(it =>
  714. new
  715. {
  716. it.CompanyFullName,
  717. }
  718. )
  719. .Where(it => it.Id == companyInfo.Id)
  720. .ExecuteCommandAsync();
  721. if (companyEdit < 0)
  722. {
  723. _result.Msg = "客户公司信息修改失败!";
  724. _sqlSugar.RollbackTran();
  725. return _result;
  726. }
  727. }
  728. }
  729. else
  730. {
  731. var companyAdd = await _sqlSugar.Insertable(_CustomerCompany).ExecuteReturnIdentityAsync();
  732. if (companyAdd < 0)
  733. {
  734. _result.Msg = "客户公司信息添加失败!";
  735. _sqlSugar.RollbackTran();
  736. return _result;
  737. }
  738. crmCompanyId = companyAdd;
  739. }
  740. }
  741. //客户人员 操作
  742. //2.添加 or 修改 客户基本信息
  743. if (clientInfo != null) //该客户存在 修改信息
  744. {
  745. clientId = clientInfo.Id;
  746. var clientEdit = await _sqlSugar.Updateable(_DeleClient).UpdateColumns(it =>
  747. new
  748. {
  749. it.LastName,
  750. it.FirstName,
  751. it.Pinyin,
  752. it.Sex,
  753. it.Phone,
  754. it.BirthDay
  755. }
  756. )
  757. .Where(it => it.Id == clientId)
  758. .ExecuteCommandAsync();
  759. if (clientEdit < 0)
  760. {
  761. _result.Msg = "客户基础信息修改失败!";
  762. _sqlSugar.RollbackTran();
  763. return _result;
  764. }
  765. }
  766. else //不存在添加 客户信息
  767. {
  768. _DeleClient.CrmCompanyId = crmCompanyId;
  769. var clientAdd = await _sqlSugar.Insertable(_DeleClient).ExecuteReturnIdentityAsync();
  770. if (clientAdd < 0)
  771. {
  772. _result.Msg = "客户基本信息添加失败!";
  773. _sqlSugar.RollbackTran();
  774. return _result;
  775. }
  776. clientId = clientAdd;
  777. }
  778. //客户身份证操作
  779. //3.添加 or 修改 身份证信息
  780. _CustomerCert.DcId = clientId;
  781. if (clientInfo == null)
  782. {
  783. var certInfo = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 &&
  784. it.SdId == 773 && //卡类型 身份证
  785. it.CertNo == item.IDCardNo //人员Id
  786. ).FirstAsync();
  787. if (certInfo == null)
  788. {
  789. var certAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteReturnIdentityAsync();
  790. if (certAdd < 0)
  791. {
  792. _result.Msg = "客户身份证添加失败!";
  793. _sqlSugar.RollbackTran();
  794. return _result;
  795. }
  796. }
  797. }
  798. else
  799. {
  800. var certInfo = await _sqlSugar.Queryable<Crm_CustomerCert>().Where(it => it.IsDel == 0 &&
  801. it.SdId == 773 && //卡类型 身份证
  802. it.DcId == clientInfo.Id //人员Id
  803. ).FirstAsync();
  804. if (certInfo == null)
  805. {
  806. var certAdd = await _sqlSugar.Insertable(_CustomerCert).ExecuteReturnIdentityAsync();
  807. if (certAdd < 0)
  808. {
  809. _result.Msg = "客户身份证添加失败!";
  810. _sqlSugar.RollbackTran();
  811. return _result;
  812. }
  813. }
  814. else
  815. {
  816. var certEdit = await _sqlSugar.Updateable(_CustomerCert).UpdateColumns(it =>
  817. new
  818. {
  819. it.CertNo,
  820. }
  821. )
  822. .Where(it => it.Id == certInfo.Id)
  823. .ExecuteCommandAsync();
  824. if (certEdit < 0)
  825. {
  826. _result.Msg = "客户身份证修改失败!";
  827. _sqlSugar.RollbackTran();
  828. return _result;
  829. }
  830. }
  831. }
  832. //团组客户信息名单操作
  833. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  834. {
  835. DiId = diid,
  836. ClientId = clientId,
  837. ShippingSpaceTypeId = item.ShippingSpaceTypeId,
  838. ShippingSpaceSpecialNeeds = item.ShippingSpaceSpecialNeeds,
  839. HotelSpecialNeeds = item.HotelSpecialNeeds,
  840. MealSpecialNeeds = item.MealSpecialNeeds,
  841. Remark = item.Remark,
  842. CreateUserId = userId,
  843. };
  844. var tourClientAdd = await _sqlSugar.Insertable(_TourClientList).ExecuteReturnIdentityAsync();
  845. if (tourClientAdd < 0)
  846. {
  847. _result.Msg = "接团客户名单添加失败!";
  848. _sqlSugar.RollbackTran();
  849. return _result;
  850. }
  851. }
  852. _result.Code = 0;
  853. _sqlSugar.CommitTran();
  854. }
  855. catch (Exception ex)
  856. {
  857. _sqlSugar.RollbackTran();
  858. _result.Msg = "程序异常 -- " + ex.Message;
  859. }
  860. return _result;
  861. }
  862. /// <summary>
  863. /// Del
  864. /// </summary>
  865. /// <param name="portId"></param>
  866. /// <param name="id"></param>
  867. /// <returns></returns>
  868. public async Task<Result> _Del(int id, int userId)
  869. {
  870. if (id > 0)
  871. {
  872. Grp_TourClientList _TourClientList = new Grp_TourClientList()
  873. {
  874. Id = id,
  875. IsDel = 1,
  876. DeleteUserId = userId,
  877. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  878. };
  879. var certEdit = await _sqlSugar.Updateable(_TourClientList).UpdateColumns(it =>
  880. new
  881. {
  882. it.IsDel,
  883. it.DeleteUserId,
  884. it.DeleteTime,
  885. }
  886. )
  887. .Where(it => it.Id == id)
  888. .ExecuteCommandAsync();
  889. if (certEdit < 0)
  890. {
  891. _result.Msg = string.Format(@"接团客户名单删除修改!");
  892. return _result;
  893. }
  894. _result.Code = 0;
  895. }
  896. else
  897. {
  898. _result.Msg = string.Format(@"请传入有效的Id参数");
  899. }
  900. return _result;
  901. }
  902. /// <summary>
  903. /// 分割客户名称
  904. /// </summary>
  905. /// <param name="ids"></param>
  906. /// <returns></returns>
  907. public string _ResolveCustomerName(string ids)
  908. {
  909. string clientStr = "";
  910. if (string.IsNullOrEmpty(ids)) return clientStr;
  911. List<int> intList = new List<int>();
  912. //if (ids.Contains(","))
  913. //{
  914. // string[] numbers = ids.Split(',');
  915. // foreach (string numberStr in numbers)
  916. // {
  917. // if (int.TryParse(numberStr.Trim(), out int number))
  918. // {
  919. // intList.Add(number);
  920. // }
  921. // }
  922. //}
  923. //else
  924. //{
  925. // if (int.TryParse(ids.Trim(), out int number))
  926. // {
  927. // intList.Add(number);
  928. // }
  929. //}
  930. string sql = string.Format(@$"Select * From Crm_GroupCustomer Where IsDel = 0 And Id In ({ids})");
  931. var infos = _sqlSugar.SqlQueryable<Crm_GroupCustomer>(sql).ToList();
  932. if (infos.Count > 0)
  933. {
  934. var infos1 = infos.Select(it => it.LastName + it.FirstName).ToList();
  935. clientStr = string.Join(",", infos1);
  936. }
  937. return clientStr;
  938. }
  939. private class CustomerNameInfo
  940. {
  941. public int Id { get; set; }
  942. public string Name { get; set; }
  943. }
  944. }
  945. }