OfficialActivitiesRepository.cs 22 KB

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