OfficialActivitiesRepository.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql)
  151. .FirstAsync();
  152. //OfficialActivities.ScreenshotOfMailUrls.ForEach(url => { url = AppSettingsHelper.Get("GrpFileBaseUrl") + url; });
  153. OfficialActivities.TranslatorIdItem = _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  154. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id)
  155. .Select(x => x.TranslatorId)
  156. .ToArray();
  157. result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities };
  158. }
  159. catch (Exception ex)
  160. {
  161. result = new Result() { Code = -2, Msg = "未知错误" };
  162. }
  163. return result;
  164. }
  165. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  166. {
  167. Result result = new Result() { Code = -2, Msg = "未知错误" };
  168. #region 特殊字符转码 037 - 4.28 15:17
  169. if (!string.IsNullOrEmpty(dto.Contact))
  170. {
  171. byte[] utf8Bytes = Encoding.UTF8.GetBytes(dto.Contact);
  172. byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
  173. dto.Contact = Encoding.Unicode.GetString(utf16Bytes);
  174. }
  175. #endregion
  176. _sqlSugar.BeginTran();
  177. //添加到资料库
  178. Res_InvitationOfficialActivityData res_InvitationData = new Res_InvitationOfficialActivityData();
  179. res_InvitationData.Country = dto.Country;
  180. res_InvitationData.City = dto.Area;
  181. res_InvitationData.UnitName = dto.Client;
  182. res_InvitationData.Delegation = dto.DiId.ToString();
  183. res_InvitationData.Address = dto.Address;
  184. res_InvitationData.CreateUserId = dto.CreateUserId;
  185. res_InvitationData.Contact = dto.Contact;
  186. res_InvitationData.Job = dto.Job;
  187. res_InvitationData.Tel = dto.Tel;
  188. res_InvitationData.Field = dto.Field;
  189. EncryptionProcessor.EncryptProperties(res_InvitationData);
  190. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  191. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName && a.IsDel == 0 && a.Address == res_InvitationData.Address);
  192. if (ifNullUp == null)
  193. {
  194. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  195. }
  196. else
  197. {
  198. res_InvitationData.Id = ifNullUp.Id;
  199. await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>(res_InvitationData)
  200. .UpdateColumns(x => new
  201. {
  202. x.Country,
  203. x.City,
  204. x.UnitName,
  205. x.Delegation,
  206. x.Address,
  207. x.CreateUserId,
  208. x.Contact,
  209. x.Job,
  210. x.Tel,
  211. x.Field
  212. })
  213. .ExecuteCommandAsync();
  214. }
  215. if (dto.Status == 1)//添加
  216. {
  217. var res_InvitationOfficial = await _sqlSugar.Queryable<Res_OfficialActivities>().FirstAsync(x => x.Client == dto.Client && x.Address == dto.Address && x.IsDel == 0 && x.DiId == dto.DiId);
  218. if (res_InvitationOfficial != null)
  219. {
  220. _sqlSugar.RollbackTran();
  221. result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  222. }
  223. else//不存在,可添加
  224. {
  225. Res_OfficialActivities _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  226. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  227. if (id == 0)
  228. {
  229. _sqlSugar.RollbackTran();
  230. result = new Result() { Code = -1, Msg = "添加失败!" };
  231. }
  232. else
  233. {
  234. #region 新增(公务信息关联翻译人员) 关联信息
  235. if (dto.TranslatorIdItem != null && dto.TranslatorIdItem.Length > 0)
  236. {
  237. var officialDutyLinkTranslators = new List<Grp_OfficialDutyLinkTranslator>();
  238. foreach (var translatorId in dto.TranslatorIdItem)
  239. {
  240. officialDutyLinkTranslators.Add(new Grp_OfficialDutyLinkTranslator()
  241. {
  242. TranslatorId = translatorId,
  243. OfficialDutyId = id,
  244. CreateUserId = dto.CreateUserId,
  245. Remark = $"公务出访客户资料-->添加"
  246. });
  247. }
  248. if (officialDutyLinkTranslators.Count > 0)
  249. {
  250. await _sqlSugar.Insertable(officialDutyLinkTranslators).ExecuteCommandAsync();
  251. }
  252. }
  253. #endregion
  254. _sqlSugar.CommitTran();
  255. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  256. }
  257. }
  258. }
  259. else if (dto.Status == 2)//修改
  260. {
  261. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  262. {
  263. Country = dto.Country,
  264. Area = dto.Area,
  265. Type = dto.Type,
  266. Client = dto.Client,
  267. Date = dto.Date,
  268. Time = dto.Time,
  269. Address = dto.Address,
  270. Contact = dto.Contact,
  271. Job = dto.Job,
  272. Tel = dto.Tel,
  273. OfficialForm = dto.OfficialForm,
  274. Field = dto.Field,
  275. ReqSample = dto.ReqSample,
  276. Setting = dto.Setting,
  277. Dresscode = dto.Dresscode,
  278. Attendees = dto.Attendees,
  279. IsNeedTrans = dto.IsNeedTrans,
  280. //Translators = dto.Translators,
  281. Language = dto.language,
  282. Trip = dto.Trip,
  283. CreateUserId = dto.CreateUserId,
  284. Remark = dto.Remark,
  285. IsPay = dto.IsPay,
  286. IsSubmitApproval = dto.IsSubmitApproval,
  287. EmailOrWeChat = dto.EmailOrWeChat,
  288. Website = dto.Website,
  289. Nature = dto.Nature,
  290. });
  291. if (res)
  292. {
  293. #region 更新(公务信息关联翻译人员) 关联信息
  294. var officialDutyLinkTranslators_select = await _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  295. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id)
  296. .ToListAsync();
  297. //删除
  298. var officialDutyLinkTranslatorIds = officialDutyLinkTranslators_select.Select(x => x.Id).ToList();
  299. if (officialDutyLinkTranslatorIds.Count > 0)
  300. {
  301. await _sqlSugar.Updateable<Grp_OfficialDutyLinkTranslator>()
  302. .SetColumnsIF(officialDutyLinkTranslatorIds.Count > 0, x => new Grp_OfficialDutyLinkTranslator()
  303. {
  304. DeleteUserId = dto.CreateUserId,
  305. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  306. IsDel = 1,
  307. Remark = $"公务出访客户资料-->删除"
  308. })
  309. .Where(x => officialDutyLinkTranslatorIds.Contains(x.Id))
  310. .ExecuteCommandAsync();
  311. }
  312. //添加
  313. if (dto.TranslatorIdItem != null && dto.TranslatorIdItem.Length > 0)
  314. {
  315. var officialDutyLinkTranslators = new List<Grp_OfficialDutyLinkTranslator>();
  316. foreach (var translatorId in dto.TranslatorIdItem)
  317. {
  318. officialDutyLinkTranslators.Add(new Grp_OfficialDutyLinkTranslator()
  319. {
  320. TranslatorId = translatorId,
  321. OfficialDutyId = dto.Id,
  322. CreateUserId = dto.CreateUserId,
  323. Remark = $"公务出访客户资料-->更新"
  324. });
  325. }
  326. if (officialDutyLinkTranslators.Count > 0)
  327. {
  328. await _sqlSugar.Insertable(officialDutyLinkTranslators).ExecuteCommandAsync();
  329. }
  330. }
  331. #endregion
  332. _sqlSugar.CommitTran();
  333. result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  334. }
  335. else
  336. {
  337. _sqlSugar.RollbackTran();
  338. result = new Result() { Code = -1, Msg = "修改失败!" };
  339. }
  340. }
  341. else
  342. {
  343. _sqlSugar.RollbackTran();
  344. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  345. }
  346. return result;
  347. }
  348. public async Task<Result> PostReqReqSampleTips(string country, string Area,string client)
  349. {
  350. if (string.IsNullOrEmpty(country)) return new Result() { Code = -1, Msg = "国家为空!" };
  351. string sqlWhere = string.Empty;
  352. if (!string.IsNullOrEmpty(Area)) sqlWhere = string.Format(@$" And oa.Area Like '%{Area}%'");
  353. if (!string.IsNullOrEmpty(client)) sqlWhere = string.Format(@$" And oa.Client Like '%{client}%'");
  354. string sql = string.Format(@$"Select di.TeamName,oa.Id,oa.Country,oa.Area,oa.Client,oa.ReqSample
  355. From Res_OfficialActivities oa With(NoLock)
  356. Left Join Grp_DelegationInfo di On oa.DiId = di.Id
  357. Where oa.IsDel = 0 And oa.Country='{country}' {sqlWhere}");
  358. //ReqReqSampleTipsView
  359. var _views = await _sqlSugar.SqlQueryable<ReqReqSampleTipsView>(sql).ToListAsync();
  360. if (_views.Count > 0 )
  361. {
  362. return new Result() { Code = 0, Msg = "操作成功!", Data = _views };
  363. }
  364. return new Result() { Code = -1, Msg = "暂无相关数据!" };
  365. }
  366. }
  367. }