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.CreateTime desc
  56. ", sqlWhere);
  57. var OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).ToListAsync();
  58. if (OfficialActivities.Count != 0)
  59. {
  60. if (dto.PageSize == 0 && dto.PageIndex == 0)
  61. {
  62. OfficialActivities.ForEach(x =>
  63. {
  64. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  65. DateTime dt;
  66. bool b_dt = DateTime.TryParse(x.Date, out 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. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  115. rst.DataList = OfficialActivities;
  116. rst.DataCount = 0;
  117. rst.CurrPageIndex = dto.PageIndex;
  118. rst.CurrPageSize = dto.PageSize;
  119. result.Data = rst;
  120. }
  121. }
  122. return result;
  123. }
  124. /// <summary>
  125. /// 根据公务出访Id查询单个数据
  126. /// </summary>
  127. /// <param name="dto"></param>
  128. /// <returns></returns>
  129. /// <exception cref="NotImplementedException"></exception>
  130. public async Task<Result> QueryOfficialActivitiesById(OfficialActivitiesDiIdDto dto)
  131. {
  132. Result result = new Result() { Code = -2, Msg = "未知错误" };
  133. try
  134. {
  135. string sqlWhere = string.Empty;
  136. sqlWhere += string.Format(@"AND oa.Isdel={0} AND oa.DiId={1} AND oa.Id={2}", 0, dto.DiId, dto.Id);
  137. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  138. {
  139. Regex r = new Regex("AND");
  140. sqlWhere = r.Replace(sqlWhere, "WHERE", 1);
  141. }
  142. string sql = string.Format(@"
  143. SELECT
  144. oa.*,
  145. u.CnName AS CreateUserName,
  146. sd.Name AS OfficialFormName
  147. FROM
  148. Res_OfficialActivities oa
  149. LEFT JOIN Sys_Users u ON oa.CreateUserId = u.Id
  150. LEFT JOIN Sys_SetData sd ON oa.OfficialForm = sd.Id
  151. {0}", sqlWhere);
  152. var oa = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).FirstAsync();
  153. var array1 = _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  154. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id)
  155. .Select(x => x.TranslatorId)
  156. .ToArray();
  157. if (array1.Any())
  158. {
  159. oa.TranslatorIdItem = array1;
  160. int translatorId = array1[0];
  161. var translatorInfo = await _sqlSugar.Queryable<Res_TranslatorLibrary>()
  162. .Where(x => x.IsDel == 0 && x.Id == translatorId)
  163. .FirstAsync();
  164. EncryptionProcessor.DecryptProperties(translatorInfo);
  165. oa.TranslatorInfo = _mapper.Map<TranslatorView>(translatorInfo);
  166. if(oa.TranslatorInfo != null)
  167. oa.TranslatorInfo.CurrencyName = _sqlSugar.Queryable<Sys_SetData>().Where(x => x.Id == oa.TranslatorInfo.Currency).First()?.Name ?? "";
  168. }
  169. result = new Result() { Code = 0, Msg = "查询成功!", Data = oa };
  170. }
  171. catch (Exception ex)
  172. {
  173. result = new Result() { Code = -2, Msg = "未知错误" };
  174. }
  175. return result;
  176. }
  177. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  178. {
  179. var result = new Result() { Code = -2, Msg = "未知错误" };
  180. var language = dto?.TranslatorInfo?.Language;
  181. #region 特殊字符转码 037 - 4.28 15:17
  182. if (!string.IsNullOrEmpty(dto.Contact))
  183. {
  184. byte[] utf8Bytes = Encoding.UTF8.GetBytes(dto.Contact);
  185. byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
  186. dto.Contact = Encoding.Unicode.GetString(utf16Bytes);
  187. }
  188. #endregion
  189. _sqlSugar.BeginTran();
  190. //添加到资料库
  191. var res_InvitationData = new Res_InvitationOfficialActivityData
  192. {
  193. Country = dto.Country,
  194. City = dto.Area,
  195. UnitName = dto.Client,
  196. Delegation = dto.DiId.ToString(),
  197. Address = dto.Address,
  198. CreateUserId = dto.CreateUserId,
  199. Contact = dto.Contact,
  200. Job = dto.Job,
  201. Tel = dto.Tel,
  202. Field = dto.Field
  203. };
  204. EncryptionProcessor.EncryptProperties(res_InvitationData);
  205. var isInserTranslator = true;
  206. /*
  207. * 2025-04-28
  208. * 翻译人员ID = 0 && 相关文本值 == “-” 不执行添加
  209. *
  210. */
  211. // 获取所有string类型的公共实例属性 排除币种名称
  212. var pTypes = new List<string>
  213. {
  214. "CurrencyName",
  215. };
  216. var stringProperties = dto.TranslatorInfo.GetType()
  217. .GetProperties(BindingFlags.Public | BindingFlags.Instance)
  218. .Where(p => p.PropertyType == typeof(string) && !pTypes.Contains(p.Name));
  219. // 检查是否有任何属性的值为 -
  220. int valCount = 0;
  221. foreach (var property in stringProperties)
  222. {
  223. var value = (string)property.GetValue(dto.TranslatorInfo) ?? "";
  224. if (value.Trim().Equals("-")) { valCount++; }
  225. }
  226. if (stringProperties.Count() == valCount) isInserTranslator = false;
  227. var transInfo = new Res_TranslatorLibrary();
  228. if (isInserTranslator)
  229. {
  230. //翻译人员资料
  231. transInfo = _mapper.Map<Res_TranslatorLibrary>(dto.TranslatorInfo);
  232. transInfo.LastUpdateUserId = dto.CreateUserId;
  233. transInfo.LastUpdateTime = DateTime.Now;
  234. transInfo.CreateUserId = dto.CreateUserId;
  235. if (dto.TranslatorIdItem.Any()) transInfo.Id = dto.TranslatorIdItem[0];
  236. EncryptionProcessor.EncryptProperties(transInfo);
  237. isInserTranslator = true;
  238. }
  239. int DataID = 0;
  240. if (dto.Status == 1)//添加
  241. {
  242. //添加资料
  243. DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  244. var _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  245. _InvitationOfficialActivityData.DataId = DataID;
  246. _InvitationOfficialActivityData.Language = language;
  247. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  248. if (id == 0)
  249. {
  250. _sqlSugar.RollbackTran();
  251. result = new Result() { Code = -1, Msg = "添加失败!" };
  252. }
  253. else
  254. {
  255. var translatorId = transInfo.Id;
  256. if (isInserTranslator)
  257. {
  258. if (translatorId > 0) // 翻译人员资料更新
  259. {
  260. var tiStatus = await _sqlSugar.Updateable<Res_TranslatorLibrary>(transInfo)
  261. .UpdateColumns(x => new
  262. {
  263. x.Area,
  264. x.Name,
  265. x.Sex,
  266. x.Tel,
  267. x.Email,
  268. x.WechatNo,
  269. x.OtherSocialAccounts,
  270. x.Language,
  271. x.Price,
  272. x.Currency,
  273. })
  274. .ExecuteCommandAsync();
  275. if (tiStatus < 1)
  276. {
  277. _sqlSugar.RollbackTran();
  278. return new Result() { Code = -1, Msg = "翻译人员资料更新失败!", Data = new { Id = id } };
  279. }
  280. }
  281. else //添加翻译人员资料
  282. {
  283. translatorId = await _sqlSugar.Insertable(transInfo).ExecuteReturnIdentityAsync();
  284. if (translatorId == 0)
  285. {
  286. _sqlSugar.RollbackTran();
  287. return new Result() { Code = -1, Msg = "翻译人员资料添加失败!", Data = new { Id = id } };
  288. }
  289. }
  290. #region 新增(公务信息关联翻译人员) 关联信息
  291. var linkStatus = await _sqlSugar
  292. .Insertable(new Grp_OfficialDutyLinkTranslator()
  293. {
  294. TranslatorId = translatorId,
  295. OfficialDutyId = id,
  296. CreateUserId = dto.CreateUserId,
  297. Remark = $"公务出访客户资料-->添加"
  298. }).ExecuteCommandAsync();
  299. if (linkStatus < 1)
  300. {
  301. _sqlSugar.RollbackTran();
  302. return new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!", Data = new { Id = id } };
  303. }
  304. #endregion
  305. }
  306. _sqlSugar.CommitTran();
  307. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  308. }
  309. }
  310. else if (dto.Status == 2)//修改
  311. {
  312. var officialActivities = _sqlSugar.Queryable<Res_OfficialActivities>().First(x => x.Id == dto.Id);
  313. if (officialActivities.DataId > 0)
  314. {
  315. res_InvitationData.Id = officialActivities.DataId;
  316. }
  317. else
  318. {
  319. var ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  320. .FirstAsync(a => a.Country == res_InvitationData.Country
  321. && a.City == res_InvitationData.City
  322. && a.UnitName == res_InvitationData.UnitName
  323. && a.IsDel == 0
  324. && a.Address == res_InvitationData.Address);
  325. if (ifNullUp != null)
  326. {
  327. res_InvitationData.Id = ifNullUp.Id;
  328. }
  329. }
  330. if (res_InvitationData.Id == 0 )
  331. {
  332. DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  333. }
  334. else
  335. {
  336. DataID = res_InvitationData.Id;
  337. //商邀资料
  338. await _sqlSugar.Updateable(res_InvitationData)
  339. .UpdateColumns(x => new
  340. {
  341. x.Country,
  342. x.City,
  343. x.UnitName,
  344. x.Delegation,
  345. x.Address,
  346. x.CreateUserId,
  347. x.Contact,
  348. x.Job,
  349. x.Tel,
  350. x.Field,
  351. })
  352. .ExecuteCommandAsync();
  353. }
  354. //公务出访
  355. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  356. {
  357. DataSource = dto.DataSource,
  358. Country = dto.Country,
  359. Area = dto.Area,
  360. Type = dto.Type,
  361. Client = dto.Client,
  362. Date = dto.Date,
  363. Time = dto.Time,
  364. Address = dto.Address,
  365. Contact = dto.Contact,
  366. Job = dto.Job,
  367. Tel = dto.Tel,
  368. OfficialForm = dto.OfficialForm,
  369. Field = dto.Field,
  370. ReqSample = dto.ReqSample,
  371. Setting = dto.Setting,
  372. Dresscode = dto.Dresscode,
  373. Attendees = dto.Attendees,
  374. IsNeedTrans = dto.IsNeedTrans,
  375. //Translators = dto.Translators,
  376. Language = language,
  377. Trip = dto.Trip,
  378. CreateUserId = dto.CreateUserId,
  379. Remark = dto.Remark,
  380. IsPay = dto.IsPay,
  381. IsSubmitApproval = dto.IsSubmitApproval,
  382. EmailOrWeChat = dto.EmailOrWeChat,
  383. Website = dto.Website,
  384. Nature = dto.Nature,
  385. DataId = DataID,
  386. });
  387. if (res)
  388. {
  389. if (isInserTranslator)
  390. {
  391. #region 更新(公务信息关联翻译人员) 关联信息
  392. if (transInfo.Id > 0) //资料更新
  393. {
  394. var tiStatus = await _sqlSugar.Updateable<Res_TranslatorLibrary>(transInfo)
  395. .UpdateColumns(x => new
  396. {
  397. x.Area,
  398. x.Name,
  399. x.Sex,
  400. x.Tel,
  401. x.Email,
  402. x.WechatNo,
  403. x.OtherSocialAccounts,
  404. x.Language,
  405. x.Price,
  406. x.Currency,
  407. })
  408. .ExecuteCommandAsync();
  409. if (tiStatus < 1)
  410. {
  411. _sqlSugar.RollbackTran();
  412. return new Result() { Code = -1, Msg = "翻译人员资料更新失败!", Data = new { Id = dto.Id } };
  413. }
  414. var dutyLink_select = await _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  415. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id && x.TranslatorId == transInfo.Id)
  416. .FirstAsync();
  417. if (dutyLink_select == null)
  418. {
  419. var odltStatus = await _sqlSugar.Insertable(new Grp_OfficialDutyLinkTranslator()
  420. {
  421. TranslatorId = transInfo.Id,
  422. OfficialDutyId = dto.Id,
  423. CreateUserId = dto.CreateUserId,
  424. Remark = $"公务出访客户资料-->添加"
  425. }).ExecuteCommandAsync();
  426. if (odltStatus < 1)
  427. {
  428. _sqlSugar.RollbackTran();
  429. result = new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!" };
  430. }
  431. }
  432. }
  433. else // 添加
  434. {
  435. transInfo.Id = await _sqlSugar.Insertable(transInfo).ExecuteReturnIdentityAsync();
  436. if (transInfo.Id == 0)
  437. {
  438. _sqlSugar.RollbackTran();
  439. return new Result() { Code = -1, Msg = "翻译人员资料添加失败!", Data = new { Id = dto.Id } };
  440. }
  441. var odltStatus = await _sqlSugar.Insertable(new Grp_OfficialDutyLinkTranslator()
  442. {
  443. TranslatorId = transInfo.Id,
  444. OfficialDutyId = dto.Id,
  445. CreateUserId = dto.CreateUserId,
  446. Remark = $"公务出访客户资料-->添加"
  447. }).ExecuteCommandAsync();
  448. if (odltStatus < 1)
  449. {
  450. _sqlSugar.RollbackTran();
  451. result = new Result() { Code = -1, Msg = "公务出访关联翻译人员资料添加失败!" };
  452. }
  453. }
  454. #endregion
  455. }
  456. _sqlSugar.CommitTran();
  457. result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  458. }
  459. else
  460. {
  461. _sqlSugar.RollbackTran();
  462. result = new Result() { Code = -1, Msg = "公务出访修改失败!" };
  463. }
  464. }
  465. else
  466. {
  467. _sqlSugar.RollbackTran();
  468. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  469. }
  470. return result;
  471. }
  472. public async Task<Result> PostReqReqSampleTips(string country, string Area,string client)
  473. {
  474. if (string.IsNullOrEmpty(country)) return new Result() { Code = -1, Msg = "国家为空!" };
  475. string sqlWhere = string.Empty;
  476. if (!string.IsNullOrEmpty(Area)) sqlWhere = string.Format(@$" And oa.Area Like '%{Area}%'");
  477. if (!string.IsNullOrEmpty(client)) sqlWhere = string.Format(@$" And oa.Client Like '%{client}%'");
  478. string sql = string.Format(@$"Select di.TeamName,oa.Id,oa.Country,oa.Area,oa.Client,oa.ReqSample
  479. From Res_OfficialActivities oa With(NoLock)
  480. Left Join Grp_DelegationInfo di On oa.DiId = di.Id
  481. Where oa.IsDel = 0 And oa.Country='{country}' {sqlWhere}");
  482. //ReqReqSampleTipsView
  483. var _views = await _sqlSugar.SqlQueryable<ReqReqSampleTipsView>(sql).ToListAsync();
  484. if (_views.Count > 0 )
  485. {
  486. return new Result() { Code = 0, Msg = "操作成功!", Data = _views };
  487. }
  488. return new Result() { Code = -1, Msg = "暂无相关数据!" };
  489. }
  490. }
  491. }