OfficialActivitiesRepository.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using AutoMapper;
  2. using MathNet.Numerics.Distributions;
  3. using MathNet.Numerics.Statistics.Mcmc;
  4. using NetTaste;
  5. using Newtonsoft.Json;
  6. using NPOI.SS.Formula.Functions;
  7. using OASystem.Domain;
  8. using OASystem.Domain.AesEncryption;
  9. using OASystem.Domain.Dtos.Resource;
  10. using OASystem.Domain.Entities.Groups;
  11. using OASystem.Domain.Entities.Resource;
  12. using OASystem.Domain.ViewModels.Resource;
  13. using OASystem.Infrastructure.Tools;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Net.NetworkInformation;
  18. using System.Reflection;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. namespace OASystem.Infrastructure.Repositories.Resource
  22. {
  23. public class OfficialActivitiesRepository : BaseRepository<Res_OfficialActivities, OfficialActivitiesView>
  24. {
  25. private readonly IMapper _mapper;
  26. public OfficialActivitiesRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  27. {
  28. _mapper = mapper;
  29. }
  30. /// <summary>
  31. /// 根据Diid查询公务出访数据List
  32. /// </summary>
  33. /// <param name="dto"></param>
  34. /// <returns></returns>
  35. public async Task<JsonView> QueryOfficialActivitiesByDiId(OfficialActivitiesByDiIdDto dto)
  36. {
  37. JsonView result = new JsonView() { Code = StatusCodes.Status200OK, Msg = "暂无数据" };
  38. string sqlWhere = string.Empty;
  39. sqlWhere += string.Format(@"AND o.Isdel={0} AND o.DiId={1} ", 0, dto.DiId);
  40. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  41. {
  42. Regex r = new Regex("AND");
  43. sqlWhere = r.Replace(sqlWhere, "WHERE", 1);
  44. }
  45. string sql = string.Format(@"
  46. SELECT
  47. *,
  48. u.CnName AS CreateUserName,
  49. sd.Name AS OfficialFormName
  50. FROM
  51. Res_OfficialActivities o
  52. LEFT JOIN Sys_SetData sd ON o.OfficialForm = sd.Id
  53. LEFT JOIN Sys_Users u ON o.CreateUserId = u.Id
  54. {0}
  55. ORDER BY
  56. o.[Date] desc ,
  57. o.CreateTime desc
  58. ", sqlWhere);
  59. var OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).ToListAsync();
  60. if (OfficialActivities.Count != 0)
  61. {
  62. if (dto.PageSize == 0 && dto.PageIndex == 0)
  63. {
  64. OfficialActivities.ForEach(x =>
  65. {
  66. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  67. bool b_dt = DateTime.TryParse(x.Date, out DateTime dt);
  68. if (b_dt)
  69. {
  70. if (!string.IsNullOrEmpty(x.Time)) x.Date = dt.ToString("yyyy-MM-dd") + " " + x.Time;
  71. else x.Date = dt.ToString("yyyy-MM-dd HH:mm:ss");
  72. }
  73. });
  74. result = new JsonView() { Code = 200, Msg = "查询成功!", Data = OfficialActivities };
  75. }
  76. else
  77. {
  78. int count = OfficialActivities.Count;
  79. float totalPage = (float)count / dto.PageSize;//总页数
  80. if (totalPage == 0) totalPage = 1;
  81. else totalPage = (int)Math.Ceiling((double)totalPage);
  82. List<OfficialActivitiesView> _OfficialActivities = new List<OfficialActivitiesView>();
  83. for (int i = 0; i < dto.PageSize; i++)
  84. {
  85. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  86. if (RowIndex < OfficialActivities.Count)
  87. {
  88. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  89. DateTime dt;
  90. bool b_dt = DateTime.TryParse(OfficialActivities[RowIndex].Date, out dt);
  91. if (b_dt)
  92. {
  93. OfficialActivities[RowIndex].Date = dt.ToString("yyyy-MM-dd HH:mm");
  94. }
  95. _OfficialActivities.Add(OfficialActivities[RowIndex]);
  96. }
  97. else
  98. {
  99. break;
  100. }
  101. }
  102. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  103. rst.DataList = _OfficialActivities;
  104. rst.DataCount = count;
  105. rst.CurrPageIndex = dto.PageIndex;
  106. rst.CurrPageSize = dto.PageSize;
  107. result = new JsonView() { Code = 200, Msg = "查询成功!", Data = rst };
  108. }
  109. }
  110. else
  111. {
  112. result = new JsonView() { Code = StatusCodes.Status200OK, Msg = "暂无数据!" };
  113. if (dto.PageSize == 0 && dto.PageIndex == 0) { result.Data = OfficialActivities; }
  114. else
  115. {
  116. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  117. rst.DataList = OfficialActivities;
  118. rst.DataCount = 0;
  119. rst.CurrPageIndex = dto.PageIndex;
  120. rst.CurrPageSize = dto.PageSize;
  121. result.Data = rst;
  122. }
  123. }
  124. return result;
  125. }
  126. /// <summary>
  127. /// 根据公务出访Id查询单个数据
  128. /// </summary>
  129. /// <param name="dto"></param>
  130. /// <returns></returns>
  131. /// <exception cref="NotImplementedException"></exception>
  132. public async Task<Result> QueryOfficialActivitiesById(OfficialActivitiesDiIdDto dto)
  133. {
  134. Result result = new Result() { Code = -2, Msg = "未知错误" };
  135. try
  136. {
  137. string sqlWhere = string.Empty;
  138. sqlWhere += string.Format(@"AND oa.Isdel={0} AND oa.DiId={1} AND oa.Id={2}", 0, dto.DiId, dto.Id);
  139. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  140. {
  141. Regex r = new Regex("AND");
  142. sqlWhere = r.Replace(sqlWhere, "WHERE", 1);
  143. }
  144. string sql = string.Format(@"
  145. SELECT
  146. oa.*,
  147. u.CnName AS CreateUserName,
  148. sd.Name AS OfficialFormName
  149. FROM
  150. Res_OfficialActivities oa
  151. LEFT JOIN Sys_Users u ON oa.CreateUserId = u.Id
  152. LEFT JOIN Sys_SetData sd ON oa.OfficialForm = sd.Id
  153. {0}", sqlWhere);
  154. var oa = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).FirstAsync();
  155. var array1 = _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  156. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id)
  157. .Select(x => x.TranslatorId)
  158. .ToArray();
  159. if (array1.Any())
  160. {
  161. oa.TranslatorIdItem = array1;
  162. int translatorId = array1[0];
  163. var translatorInfo = await _sqlSugar.Queryable<Res_TranslatorLibrary>()
  164. .Where(x => x.IsDel == 0 && x.Id == translatorId)
  165. .FirstAsync();
  166. EncryptionProcessor.DecryptProperties(translatorInfo);
  167. oa.TranslatorInfo = _mapper.Map<TranslatorView>(translatorInfo);
  168. if (oa.TranslatorInfo != null)
  169. oa.TranslatorInfo.CurrencyName = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.Id == oa.TranslatorInfo.Currency).First()?.Name ?? "";
  170. }
  171. result = new Result() { Code = 0, Msg = "查询成功!", Data = oa };
  172. }
  173. catch (Exception ex)
  174. {
  175. result = new Result() { Code = -2, Msg = "未知错误" };
  176. }
  177. return result;
  178. }
  179. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  180. {
  181. var result = new Result() { Code = -2, Msg = "未知错误" };
  182. var language = dto?.TranslatorInfo?.Language;
  183. #region 特殊字符转码 037 - 4.28 15:17
  184. if (!string.IsNullOrEmpty(dto.Contact))
  185. {
  186. byte[] utf8Bytes = Encoding.UTF8.GetBytes(dto.Contact);
  187. byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
  188. dto.Contact = Encoding.Unicode.GetString(utf16Bytes);
  189. }
  190. #endregion
  191. _sqlSugar.BeginTran();
  192. //添加到资料库
  193. var res_InvitationData = new Res_InvitationOfficialActivityData
  194. {
  195. Country = dto.Country,
  196. City = dto.Area,
  197. UnitName = dto.Client,
  198. Delegation = dto.DiId.ToString(),
  199. Address = dto.Address,
  200. CreateUserId = dto.CreateUserId,
  201. Contact = dto.Contact,
  202. Job = dto.Job,
  203. Tel = dto.Tel,
  204. Field = dto.Field
  205. };
  206. EncryptionProcessor.EncryptProperties(res_InvitationData);
  207. var isInserTranslator = true;
  208. var transInfo = new Res_TranslatorLibrary();
  209. /*
  210. * 2025-10-17
  211. * 相关文本值 == “-” || == "" || == null 不执行添加
  212. *
  213. */
  214. // 验证指定的字段是否有有效值()
  215. var validFields = new[]
  216. {
  217. dto.TranslatorInfo.Name,
  218. dto.TranslatorInfo.Tel,
  219. };
  220. isInserTranslator = validFields
  221. .Any(value => !string.IsNullOrEmpty(value?.Trim()) && !value.Trim().Equals("-"));
  222. var isSpecialStatus = dto.TranslatorInfo.Id == 147;
  223. if (isInserTranslator)
  224. {
  225. //翻译人员资料
  226. transInfo = _mapper.Map<Res_TranslatorLibrary>(dto.TranslatorInfo);
  227. transInfo.LastUpdateUserId = dto.CreateUserId;
  228. transInfo.LastUpdateTime = DateTime.Now;
  229. transInfo.CreateUserId = dto.CreateUserId;
  230. //if (dto.TranslatorIdItem.Any()) transInfo.Id = dto.TranslatorIdItem[0];
  231. EncryptionProcessor.EncryptProperties(transInfo);
  232. isInserTranslator = true;
  233. }
  234. int DataID = 0;
  235. if (dto.Status == 1)//添加
  236. {
  237. //添加资料
  238. DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  239. var _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  240. _InvitationOfficialActivityData.DataId = DataID;
  241. _InvitationOfficialActivityData.Language = language;
  242. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  243. if (id == 0)
  244. {
  245. _sqlSugar.RollbackTran();
  246. result = new Result() { Code = -1, Msg = "添加失败!" };
  247. }
  248. else
  249. {
  250. var translatorId = transInfo.Id;
  251. if (isInserTranslator)
  252. {
  253. //更新排除特殊 字符 “后期补录信息”
  254. if (!isSpecialStatus)
  255. {
  256. if (translatorId > 0) // 翻译人员资料更新
  257. {
  258. var tiStatus = await _sqlSugar.Updateable<Res_TranslatorLibrary>(transInfo)
  259. .UpdateColumns(x => new
  260. {
  261. x.Area,
  262. x.Name,
  263. x.Sex,
  264. x.Tel,
  265. x.Email,
  266. x.WechatNo,
  267. x.OtherSocialAccounts,
  268. x.Language,
  269. x.Price,
  270. x.Currency,
  271. })
  272. .ExecuteCommandAsync();
  273. if (tiStatus < 1)
  274. {
  275. _sqlSugar.RollbackTran();
  276. return new Result() { Code = -1, Msg = "翻译人员资料更新失败!", Data = new { Id = id } };
  277. }
  278. }
  279. else //添加翻译人员资料
  280. {
  281. translatorId = await _sqlSugar.Insertable(transInfo).ExecuteReturnIdentityAsync();
  282. if (translatorId == 0)
  283. {
  284. _sqlSugar.RollbackTran();
  285. return new Result() { Code = -1, Msg = "翻译人员资料添加失败!", Data = new { Id = id } };
  286. }
  287. }
  288. }
  289. #region 新增(公务信息关联翻译人员) 关联信息
  290. await _sqlSugar.Updateable<Grp_OfficialDutyLinkTranslator>()
  291. .SetColumns(x => new Grp_OfficialDutyLinkTranslator()
  292. {
  293. IsDel = 1,
  294. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  295. DeleteUserId = dto.CreateUserId,
  296. Remark = "更新关联,逻辑删除"
  297. })
  298. .Where(x => x.OfficialDutyId == dto.Id)
  299. .ExecuteCommandAsync();
  300. var linkStatus = await _sqlSugar
  301. .Insertable(new Grp_OfficialDutyLinkTranslator()
  302. {
  303. TranslatorId = translatorId,
  304. OfficialDutyId = id,
  305. CreateUserId = dto.CreateUserId,
  306. Remark = $"公务出访客户资料-->添加"
  307. }).ExecuteCommandAsync();
  308. if (linkStatus < 1)
  309. {
  310. _sqlSugar.RollbackTran();
  311. return new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!", Data = new { Id = id } };
  312. }
  313. #endregion
  314. }
  315. _sqlSugar.CommitTran();
  316. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  317. }
  318. }
  319. else if (dto.Status == 2)//修改
  320. {
  321. var officialActivities = _sqlSugar.Queryable<Res_OfficialActivities>().First(x => x.Id == dto.Id);
  322. if (officialActivities.DataId > 0)
  323. {
  324. res_InvitationData.Id = officialActivities.DataId;
  325. }
  326. else
  327. {
  328. var ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  329. .FirstAsync(a => a.Country == res_InvitationData.Country
  330. && a.City == res_InvitationData.City
  331. && a.UnitName == res_InvitationData.UnitName
  332. && a.IsDel == 0
  333. && a.Address == res_InvitationData.Address);
  334. if (ifNullUp != null)
  335. {
  336. res_InvitationData.Id = ifNullUp.Id;
  337. }
  338. }
  339. if (res_InvitationData.Id == 0)
  340. {
  341. DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  342. }
  343. else
  344. {
  345. DataID = res_InvitationData.Id;
  346. //商邀资料
  347. await _sqlSugar.Updateable(res_InvitationData)
  348. .UpdateColumns(x => new
  349. {
  350. x.Country,
  351. x.City,
  352. x.UnitName,
  353. x.Delegation,
  354. x.Address,
  355. x.CreateUserId,
  356. x.Contact,
  357. x.Job,
  358. x.Tel,
  359. x.Field,
  360. })
  361. .ExecuteCommandAsync();
  362. }
  363. //公务出访
  364. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  365. {
  366. DataSource = dto.DataSource,
  367. Country = dto.Country,
  368. Area = dto.Area,
  369. Type = dto.Type,
  370. Client = dto.Client,
  371. Date = dto.Date,
  372. Time = dto.Time,
  373. Address = dto.Address,
  374. Contact = dto.Contact,
  375. Job = dto.Job,
  376. Tel = dto.Tel,
  377. OfficialForm = dto.OfficialForm,
  378. Field = dto.Field,
  379. ReqSample = dto.ReqSample,
  380. Setting = dto.Setting,
  381. Dresscode = dto.Dresscode,
  382. Attendees = dto.Attendees,
  383. IsNeedTrans = dto.IsNeedTrans,
  384. //Translators = dto.Translators,
  385. Language = language,
  386. Trip = dto.Trip,
  387. CreateUserId = dto.CreateUserId,
  388. Remark = dto.Remark,
  389. IsPay = dto.IsPay,
  390. IsSubmitApproval = dto.IsSubmitApproval,
  391. EmailOrWeChat = dto.EmailOrWeChat,
  392. Website = dto.Website,
  393. Nature = dto.Nature,
  394. DataId = DataID,
  395. });
  396. if (res)
  397. {
  398. if (isInserTranslator)
  399. {
  400. #region 更新(公务信息关联翻译人员) 关联信息
  401. //更新排除特殊 字符 “后期补录信息”
  402. if (!isSpecialStatus)
  403. {
  404. if (transInfo.Id > 0) //资料更新
  405. {
  406. var tiStatus = await _sqlSugar.Updateable<Res_TranslatorLibrary>(transInfo)
  407. .UpdateColumns(x => new
  408. {
  409. x.Area,
  410. x.Name,
  411. x.Sex,
  412. x.Tel,
  413. x.Email,
  414. x.WechatNo,
  415. x.OtherSocialAccounts,
  416. x.Language,
  417. x.Price,
  418. x.Currency,
  419. })
  420. .ExecuteCommandAsync();
  421. if (tiStatus < 1)
  422. {
  423. _sqlSugar.RollbackTran();
  424. return new Result() { Code = -1, Msg = "翻译人员资料更新失败!", Data = new { Id = dto.Id } };
  425. }
  426. }
  427. else // 添加
  428. {
  429. transInfo.Id = await _sqlSugar.Insertable(transInfo).ExecuteReturnIdentityAsync();
  430. if (transInfo.Id == 0)
  431. {
  432. _sqlSugar.RollbackTran();
  433. return new Result() { Code = -1, Msg = "翻译人员资料添加失败!", Data = new { Id = dto.Id } };
  434. }
  435. }
  436. }
  437. await _sqlSugar.Updateable<Grp_OfficialDutyLinkTranslator>()
  438. .SetColumns(x => new Grp_OfficialDutyLinkTranslator()
  439. {
  440. IsDel = 1,
  441. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  442. DeleteUserId = dto.CreateUserId,
  443. Remark = "更新关联,逻辑删除"
  444. })
  445. .Where(x => x.OfficialDutyId == dto.Id)
  446. .ExecuteCommandAsync();
  447. var odltStatus = await _sqlSugar.Insertable(new Grp_OfficialDutyLinkTranslator()
  448. {
  449. TranslatorId = transInfo.Id,
  450. OfficialDutyId = dto.Id,
  451. CreateUserId = dto.CreateUserId,
  452. Remark = $"公务出访客户资料-->添加"
  453. }).ExecuteCommandAsync();
  454. if (odltStatus < 1)
  455. {
  456. _sqlSugar.RollbackTran();
  457. result = new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!" };
  458. }
  459. #endregion
  460. }
  461. _sqlSugar.CommitTran();
  462. result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  463. }
  464. else
  465. {
  466. _sqlSugar.RollbackTran();
  467. result = new Result() { Code = -1, Msg = "公务出访修改失败!" };
  468. }
  469. }
  470. else
  471. {
  472. _sqlSugar.RollbackTran();
  473. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  474. }
  475. return result;
  476. }
  477. public async Task<Result> PostReqReqSampleTips(string country, string Area, string client)
  478. {
  479. if (string.IsNullOrEmpty(country)) return new Result() { Code = -1, Msg = "国家为空!" };
  480. string sqlWhere = string.Empty;
  481. if (!string.IsNullOrEmpty(Area)) sqlWhere = string.Format(@$" And oa.Area Like '%{Area}%'");
  482. if (!string.IsNullOrEmpty(client)) sqlWhere = string.Format(@$" And oa.Client Like '%{client}%'");
  483. string sql = string.Format(@$"Select di.TeamName,oa.Id,oa.Country,oa.Area,oa.Client,oa.ReqSample
  484. From Res_OfficialActivities oa With(NoLock)
  485. Left Join Grp_DelegationInfo di On oa.DiId = di.Id
  486. Where oa.IsDel = 0 And oa.Country='{country}' {sqlWhere}");
  487. //ReqReqSampleTipsView
  488. var _views = await _sqlSugar.SqlQueryable<ReqReqSampleTipsView>(sql).ToListAsync();
  489. if (_views.Count > 0)
  490. {
  491. return new Result() { Code = 0, Msg = "操作成功!", Data = _views };
  492. }
  493. return new Result() { Code = -1, Msg = "暂无相关数据!" };
  494. }
  495. }
  496. }