OfficialActivitiesRepository.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /*
  208. * 2025-04-28
  209. * 翻译人员ID = 0 && 相关文本值 == “-” 不执行添加
  210. *
  211. */
  212. // 获取所有string类型的公共实例属性 排除币种名称
  213. var pTypes = new List<string>
  214. {
  215. "CurrencyName",
  216. };
  217. var stringProperties = dto.TranslatorInfo.GetType()
  218. .GetProperties(BindingFlags.Public | BindingFlags.Instance)
  219. .Where(p => p.PropertyType == typeof(string) && !pTypes.Contains(p.Name));
  220. // 检查是否有任何属性的值为 -
  221. int valCount = 0;
  222. foreach (var property in stringProperties)
  223. {
  224. var value = (string)property.GetValue(dto.TranslatorInfo) ?? "";
  225. if (value.Trim().Equals("-")) { valCount++; }
  226. }
  227. if (stringProperties.Count() == valCount) isInserTranslator = false;
  228. var transInfo = new Res_TranslatorLibrary();
  229. if (isInserTranslator)
  230. {
  231. //翻译人员资料
  232. transInfo = _mapper.Map<Res_TranslatorLibrary>(dto.TranslatorInfo);
  233. transInfo.LastUpdateUserId = dto.CreateUserId;
  234. transInfo.LastUpdateTime = DateTime.Now;
  235. transInfo.CreateUserId = dto.CreateUserId;
  236. if (dto.TranslatorIdItem.Any()) transInfo.Id = dto.TranslatorIdItem[0];
  237. EncryptionProcessor.EncryptProperties(transInfo);
  238. isInserTranslator = true;
  239. }
  240. int DataID = 0;
  241. if (dto.Status == 1)//添加
  242. {
  243. //添加资料
  244. DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  245. var _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  246. _InvitationOfficialActivityData.DataId = DataID;
  247. _InvitationOfficialActivityData.Language = language;
  248. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  249. if (id == 0)
  250. {
  251. _sqlSugar.RollbackTran();
  252. result = new Result() { Code = -1, Msg = "添加失败!" };
  253. }
  254. else
  255. {
  256. var translatorId = transInfo.Id;
  257. if (isInserTranslator)
  258. {
  259. if (translatorId > 0) // 翻译人员资料更新
  260. {
  261. var tiStatus = await _sqlSugar.Updateable<Res_TranslatorLibrary>(transInfo)
  262. .UpdateColumns(x => new
  263. {
  264. x.Area,
  265. x.Name,
  266. x.Sex,
  267. x.Tel,
  268. x.Email,
  269. x.WechatNo,
  270. x.OtherSocialAccounts,
  271. x.Language,
  272. x.Price,
  273. x.Currency,
  274. })
  275. .ExecuteCommandAsync();
  276. if (tiStatus < 1)
  277. {
  278. _sqlSugar.RollbackTran();
  279. return new Result() { Code = -1, Msg = "翻译人员资料更新失败!", Data = new { Id = id } };
  280. }
  281. }
  282. else //添加翻译人员资料
  283. {
  284. translatorId = await _sqlSugar.Insertable(transInfo).ExecuteReturnIdentityAsync();
  285. if (translatorId == 0)
  286. {
  287. _sqlSugar.RollbackTran();
  288. return new Result() { Code = -1, Msg = "翻译人员资料添加失败!", Data = new { Id = id } };
  289. }
  290. }
  291. #region 新增(公务信息关联翻译人员) 关联信息
  292. var linkStatus = await _sqlSugar
  293. .Insertable(new Grp_OfficialDutyLinkTranslator()
  294. {
  295. TranslatorId = translatorId,
  296. OfficialDutyId = id,
  297. CreateUserId = dto.CreateUserId,
  298. Remark = $"公务出访客户资料-->添加"
  299. }).ExecuteCommandAsync();
  300. if (linkStatus < 1)
  301. {
  302. _sqlSugar.RollbackTran();
  303. return new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!", Data = new { Id = id } };
  304. }
  305. #endregion
  306. }
  307. _sqlSugar.CommitTran();
  308. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  309. }
  310. }
  311. else if (dto.Status == 2)//修改
  312. {
  313. var officialActivities = _sqlSugar.Queryable<Res_OfficialActivities>().First(x => x.Id == dto.Id);
  314. if (officialActivities.DataId > 0)
  315. {
  316. res_InvitationData.Id = officialActivities.DataId;
  317. }
  318. else
  319. {
  320. var ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  321. .FirstAsync(a => a.Country == res_InvitationData.Country
  322. && a.City == res_InvitationData.City
  323. && a.UnitName == res_InvitationData.UnitName
  324. && a.IsDel == 0
  325. && a.Address == res_InvitationData.Address);
  326. if (ifNullUp != null)
  327. {
  328. res_InvitationData.Id = ifNullUp.Id;
  329. }
  330. }
  331. if (res_InvitationData.Id == 0)
  332. {
  333. DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  334. }
  335. else
  336. {
  337. DataID = res_InvitationData.Id;
  338. //商邀资料
  339. await _sqlSugar.Updateable(res_InvitationData)
  340. .UpdateColumns(x => new
  341. {
  342. x.Country,
  343. x.City,
  344. x.UnitName,
  345. x.Delegation,
  346. x.Address,
  347. x.CreateUserId,
  348. x.Contact,
  349. x.Job,
  350. x.Tel,
  351. x.Field,
  352. })
  353. .ExecuteCommandAsync();
  354. }
  355. //公务出访
  356. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  357. {
  358. DataSource = dto.DataSource,
  359. Country = dto.Country,
  360. Area = dto.Area,
  361. Type = dto.Type,
  362. Client = dto.Client,
  363. Date = dto.Date,
  364. Time = dto.Time,
  365. Address = dto.Address,
  366. Contact = dto.Contact,
  367. Job = dto.Job,
  368. Tel = dto.Tel,
  369. OfficialForm = dto.OfficialForm,
  370. Field = dto.Field,
  371. ReqSample = dto.ReqSample,
  372. Setting = dto.Setting,
  373. Dresscode = dto.Dresscode,
  374. Attendees = dto.Attendees,
  375. IsNeedTrans = dto.IsNeedTrans,
  376. //Translators = dto.Translators,
  377. Language = language,
  378. Trip = dto.Trip,
  379. CreateUserId = dto.CreateUserId,
  380. Remark = dto.Remark,
  381. IsPay = dto.IsPay,
  382. IsSubmitApproval = dto.IsSubmitApproval,
  383. EmailOrWeChat = dto.EmailOrWeChat,
  384. Website = dto.Website,
  385. Nature = dto.Nature,
  386. DataId = DataID,
  387. });
  388. if (res)
  389. {
  390. if (isInserTranslator)
  391. {
  392. #region 更新(公务信息关联翻译人员) 关联信息
  393. if (transInfo.Id > 0) //资料更新
  394. {
  395. var tiStatus = await _sqlSugar.Updateable<Res_TranslatorLibrary>(transInfo)
  396. .UpdateColumns(x => new
  397. {
  398. x.Area,
  399. x.Name,
  400. x.Sex,
  401. x.Tel,
  402. x.Email,
  403. x.WechatNo,
  404. x.OtherSocialAccounts,
  405. x.Language,
  406. x.Price,
  407. x.Currency,
  408. })
  409. .ExecuteCommandAsync();
  410. if (tiStatus < 1)
  411. {
  412. _sqlSugar.RollbackTran();
  413. return new Result() { Code = -1, Msg = "翻译人员资料更新失败!", Data = new { Id = dto.Id } };
  414. }
  415. var dutyLink_select = await _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  416. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id && x.TranslatorId == transInfo.Id)
  417. .FirstAsync();
  418. if (dutyLink_select == null)
  419. {
  420. var odltStatus = await _sqlSugar.Insertable(new Grp_OfficialDutyLinkTranslator()
  421. {
  422. TranslatorId = transInfo.Id,
  423. OfficialDutyId = dto.Id,
  424. CreateUserId = dto.CreateUserId,
  425. Remark = $"公务出访客户资料-->添加"
  426. }).ExecuteCommandAsync();
  427. if (odltStatus < 1)
  428. {
  429. _sqlSugar.RollbackTran();
  430. result = new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!" };
  431. }
  432. }
  433. }
  434. else // 添加
  435. {
  436. transInfo.Id = await _sqlSugar.Insertable(transInfo).ExecuteReturnIdentityAsync();
  437. if (transInfo.Id == 0)
  438. {
  439. _sqlSugar.RollbackTran();
  440. return new Result() { Code = -1, Msg = "翻译人员资料添加失败!", Data = new { Id = dto.Id } };
  441. }
  442. var odltStatus = await _sqlSugar.Insertable(new Grp_OfficialDutyLinkTranslator()
  443. {
  444. TranslatorId = transInfo.Id,
  445. OfficialDutyId = dto.Id,
  446. CreateUserId = dto.CreateUserId,
  447. Remark = $"公务出访客户资料-->添加"
  448. }).ExecuteCommandAsync();
  449. if (odltStatus < 1)
  450. {
  451. _sqlSugar.RollbackTran();
  452. result = new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!" };
  453. }
  454. }
  455. #endregion
  456. }
  457. _sqlSugar.CommitTran();
  458. result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  459. }
  460. else
  461. {
  462. _sqlSugar.RollbackTran();
  463. result = new Result() { Code = -1, Msg = "公务出访修改失败!" };
  464. }
  465. }
  466. else
  467. {
  468. _sqlSugar.RollbackTran();
  469. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  470. }
  471. return result;
  472. }
  473. public async Task<Result> PostReqReqSampleTips(string country, string Area, string client)
  474. {
  475. if (string.IsNullOrEmpty(country)) return new Result() { Code = -1, Msg = "国家为空!" };
  476. string sqlWhere = string.Empty;
  477. if (!string.IsNullOrEmpty(Area)) sqlWhere = string.Format(@$" And oa.Area Like '%{Area}%'");
  478. if (!string.IsNullOrEmpty(client)) sqlWhere = string.Format(@$" And oa.Client Like '%{client}%'");
  479. string sql = string.Format(@$"Select di.TeamName,oa.Id,oa.Country,oa.Area,oa.Client,oa.ReqSample
  480. From Res_OfficialActivities oa With(NoLock)
  481. Left Join Grp_DelegationInfo di On oa.DiId = di.Id
  482. Where oa.IsDel = 0 And oa.Country='{country}' {sqlWhere}");
  483. //ReqReqSampleTipsView
  484. var _views = await _sqlSugar.SqlQueryable<ReqReqSampleTipsView>(sql).ToListAsync();
  485. if (_views.Count > 0)
  486. {
  487. return new Result() { Code = 0, Msg = "操作成功!", Data = _views };
  488. }
  489. return new Result() { Code = -1, Msg = "暂无相关数据!" };
  490. }
  491. }
  492. }