OfficialActivitiesRepository.cs 22 KB

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