TourClientListRepository.cs 47 KB

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