OfficialActivitiesRepository.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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.Dtos.Resource;
  8. using OASystem.Domain.Entities.Groups;
  9. using OASystem.Domain.Entities.Resource;
  10. using OASystem.Domain.ViewModels.Resource;
  11. using OASystem.Infrastructure.Tools;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace OASystem.Infrastructure.Repositories.Resource
  18. {
  19. public class OfficialActivitiesRepository : BaseRepository<Res_OfficialActivities, OfficialActivitiesView>
  20. {
  21. private readonly IMapper _mapper;
  22. public OfficialActivitiesRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  23. {
  24. _mapper = mapper;
  25. }
  26. /// <summary>
  27. /// 根据Diid查询公务出访数据List
  28. /// </summary>
  29. /// <param name="dto"></param>
  30. /// <returns></returns>
  31. public async Task<JsonView> QueryOfficialActivitiesByDiId(OfficialActivitiesByDiIdDto dto)
  32. {
  33. JsonView result = new JsonView() { Code = StatusCodes.Status200OK, Msg = "暂无数据" };
  34. string sqlWhere = string.Empty;
  35. sqlWhere += string.Format(@"AND o.Isdel={0} AND o.DiId={1} ", 0, dto.DiId);
  36. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  37. {
  38. Regex r = new Regex("AND");
  39. sqlWhere = r.Replace(sqlWhere, "WHERE", 1);
  40. }
  41. string sql = string.Format(@"
  42. SELECT
  43. *,
  44. u.CnName AS CreateUserName,
  45. sd.Name AS OfficialFormName
  46. FROM
  47. Res_OfficialActivities o
  48. LEFT JOIN Sys_SetData sd ON o.OfficialForm = sd.Id
  49. LEFT JOIN Sys_Users u ON o.CreateUserId = u.Id
  50. {0}
  51. ORDER BY
  52. o.CreateTime desc
  53. ", sqlWhere);
  54. var OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).ToListAsync();
  55. if (OfficialActivities.Count != 0)
  56. {
  57. if (dto.PageSize == 0 && dto.PageIndex == 0)
  58. {
  59. OfficialActivities.ForEach(x =>
  60. {
  61. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  62. DateTime dt;
  63. bool b_dt = DateTime.TryParse(x.Date, out dt);
  64. if (b_dt)
  65. {
  66. if (!string.IsNullOrEmpty(x.Time)) x.Date = dt.ToString("yyyy-MM-dd") + " " + x.Time;
  67. else x.Date = dt.ToString("yyyy-MM-dd HH:mm:ss");
  68. }
  69. });
  70. result = new JsonView() { Code = 200, Msg = "查询成功!", Data = OfficialActivities };
  71. }
  72. else
  73. {
  74. int count = OfficialActivities.Count;
  75. float totalPage = (float)count / dto.PageSize;//总页数
  76. if (totalPage == 0) totalPage = 1;
  77. else totalPage = (int)Math.Ceiling((double)totalPage);
  78. List<OfficialActivitiesView> _OfficialActivities = new List<OfficialActivitiesView>();
  79. for (int i = 0; i < dto.PageSize; i++)
  80. {
  81. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  82. if (RowIndex < OfficialActivities.Count)
  83. {
  84. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  85. DateTime dt;
  86. bool b_dt = DateTime.TryParse(OfficialActivities[RowIndex].Date, out dt);
  87. if (b_dt)
  88. {
  89. OfficialActivities[RowIndex].Date = dt.ToString("yyyy-MM-dd HH:mm");
  90. }
  91. _OfficialActivities.Add(OfficialActivities[RowIndex]);
  92. }
  93. else
  94. {
  95. break;
  96. }
  97. }
  98. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  99. rst.DataList = _OfficialActivities;
  100. rst.DataCount = count;
  101. rst.CurrPageIndex = dto.PageIndex;
  102. rst.CurrPageSize = dto.PageSize;
  103. result = new JsonView() { Code = 200, Msg = "查询成功!", Data = rst };
  104. }
  105. }
  106. else
  107. {
  108. result = new JsonView() { Code = StatusCodes.Status200OK, Msg = "暂无数据!" };
  109. if (dto.PageSize == 0 && dto.PageIndex == 0) { result.Data = OfficialActivities; }
  110. else {
  111. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  112. rst.DataList = OfficialActivities;
  113. rst.DataCount = 0;
  114. rst.CurrPageIndex = dto.PageIndex;
  115. rst.CurrPageSize = dto.PageSize;
  116. result.Data = rst;
  117. }
  118. }
  119. return result;
  120. }
  121. /// <summary>
  122. /// 根据公务出访Id查询单个数据
  123. /// </summary>
  124. /// <param name="dto"></param>
  125. /// <returns></returns>
  126. /// <exception cref="NotImplementedException"></exception>
  127. public async Task<Result> QueryOfficialActivitiesById(OfficialActivitiesDiIdDto dto)
  128. {
  129. Result result = new Result() { Code = -2, Msg = "未知错误" };
  130. try
  131. {
  132. string sqlWhere = string.Empty;
  133. sqlWhere += string.Format(@"AND oa.Isdel={0} AND oa.DiId={1} AND oa.Id={2}", 0, dto.DiId, dto.Id);
  134. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  135. {
  136. Regex r = new Regex("AND");
  137. sqlWhere = r.Replace(sqlWhere, "WHERE", 1);
  138. }
  139. string sql = string.Format(@"
  140. SELECT
  141. oa.*,
  142. u.CnName AS CreateUserName,
  143. sd.Name AS OfficialFormName
  144. FROM
  145. Res_OfficialActivities oa
  146. LEFT JOIN Sys_Users u ON oa.CreateUserId = u.Id
  147. LEFT JOIN Sys_SetData sd ON oa.OfficialForm = sd.Id
  148. {0}", sqlWhere);
  149. var OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql)
  150. .FirstAsync();
  151. //OfficialActivities.ScreenshotOfMailUrls.ForEach(url => { url = AppSettingsHelper.Get("GrpFileBaseUrl") + url; });
  152. OfficialActivities.TranslatorIdItem = _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  153. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id)
  154. .Select(x => x.TranslatorId)
  155. .ToArray();
  156. result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities };
  157. }
  158. catch (Exception ex)
  159. {
  160. result = new Result() { Code = -2, Msg = "未知错误" };
  161. }
  162. return result;
  163. }
  164. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  165. {
  166. Result result = new Result() { Code = -2, Msg = "未知错误" };
  167. #region 特殊字符转码 037 - 4.28 15:17
  168. if (!string.IsNullOrEmpty(dto.Contact))
  169. {
  170. byte[] utf8Bytes = Encoding.UTF8.GetBytes(dto.Contact);
  171. byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
  172. dto.Contact = Encoding.Unicode.GetString(utf16Bytes);
  173. }
  174. #endregion
  175. _sqlSugar.BeginTran();
  176. //添加到资料库
  177. Res_InvitationOfficialActivityData res_InvitationData = new Res_InvitationOfficialActivityData();
  178. res_InvitationData.Country = dto.Country;
  179. res_InvitationData.City = dto.Area;
  180. res_InvitationData.UnitName = dto.Client;
  181. res_InvitationData.Delegation = dto.DiId.ToString();
  182. res_InvitationData.Address = dto.Address;
  183. res_InvitationData.CreateUserId = dto.CreateUserId;
  184. res_InvitationData.Contact = dto.Contact;
  185. res_InvitationData.Job = dto.Job;
  186. res_InvitationData.Tel = dto.Tel;
  187. res_InvitationData.Field = dto.Field;
  188. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  189. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName && a.IsDel == 0 && a.Address == res_InvitationData.Address);
  190. if (ifNullUp == null)
  191. {
  192. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  193. }
  194. else
  195. {
  196. res_InvitationData.Id = ifNullUp.Id;
  197. await _sqlSugar.Updateable<Res_InvitationOfficialActivityData>(res_InvitationData).ExecuteCommandAsync();
  198. }
  199. if (dto.Status == 1)//添加
  200. {
  201. 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);
  202. if (res_InvitationOfficial != null)
  203. {
  204. _sqlSugar.RollbackTran();
  205. result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  206. }
  207. else//不存在,可添加
  208. {
  209. Res_OfficialActivities _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  210. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  211. if (id == 0)
  212. {
  213. _sqlSugar.RollbackTran();
  214. result = new Result() { Code = -1, Msg = "添加失败!" };
  215. }
  216. else
  217. {
  218. #region 新增(公务信息关联翻译人员) 关联信息
  219. if (dto.TranslatorIdItem != null && dto.TranslatorIdItem.Length > 0)
  220. {
  221. var officialDutyLinkTranslators = new List<Grp_OfficialDutyLinkTranslator>();
  222. foreach (var translatorId in dto.TranslatorIdItem)
  223. {
  224. officialDutyLinkTranslators.Add(new Grp_OfficialDutyLinkTranslator()
  225. {
  226. TranslatorId = translatorId,
  227. OfficialDutyId = id,
  228. CreateUserId = dto.CreateUserId,
  229. Remark = $"公务出访客户资料-->添加"
  230. });
  231. }
  232. if (officialDutyLinkTranslators.Count > 0)
  233. {
  234. await _sqlSugar.Insertable(officialDutyLinkTranslators).ExecuteCommandAsync();
  235. }
  236. }
  237. #endregion
  238. _sqlSugar.CommitTran();
  239. result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  240. }
  241. }
  242. }
  243. else if (dto.Status == 2)//修改
  244. {
  245. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  246. {
  247. Country = dto.Country,
  248. Area = dto.Area,
  249. Type = dto.Type,
  250. Client = dto.Client,
  251. Date = dto.Date,
  252. Time = dto.Time,
  253. Address = dto.Address,
  254. Contact = dto.Contact,
  255. Job = dto.Job,
  256. Tel = dto.Tel,
  257. OfficialForm = dto.OfficialForm,
  258. Field = dto.Field,
  259. ReqSample = dto.ReqSample,
  260. Setting = dto.Setting,
  261. Dresscode = dto.Dresscode,
  262. Attendees = dto.Attendees,
  263. IsNeedTrans = dto.IsNeedTrans,
  264. //Translators = dto.Translators,
  265. Language = dto.language,
  266. Trip = dto.Trip,
  267. CreateUserId = dto.CreateUserId,
  268. Remark = dto.Remark,
  269. IsPay = dto.IsPay,
  270. IsSubmitApproval = dto.IsSubmitApproval,
  271. EmailOrWeChat = dto.EmailOrWeChat,
  272. Website = dto.Website,
  273. Nature = dto.Nature,
  274. });
  275. if (res)
  276. {
  277. #region 更新(公务信息关联翻译人员) 关联信息
  278. var officialDutyLinkTranslators_select = await _sqlSugar.Queryable<Grp_OfficialDutyLinkTranslator>()
  279. .Where(x => x.IsDel == 0 && x.OfficialDutyId == dto.Id)
  280. .ToListAsync();
  281. //删除
  282. var officialDutyLinkTranslatorIds = officialDutyLinkTranslators_select.Select(x => x.Id).ToList();
  283. if (officialDutyLinkTranslatorIds.Count > 0)
  284. {
  285. await _sqlSugar.Updateable<Grp_OfficialDutyLinkTranslator>()
  286. .SetColumnsIF(officialDutyLinkTranslatorIds.Count > 0, x => new Grp_OfficialDutyLinkTranslator()
  287. {
  288. DeleteUserId = dto.CreateUserId,
  289. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  290. IsDel = 1,
  291. Remark = $"公务出访客户资料-->删除"
  292. })
  293. .Where(x => officialDutyLinkTranslatorIds.Contains(x.Id))
  294. .ExecuteCommandAsync();
  295. }
  296. //添加
  297. if (dto.TranslatorIdItem != null && dto.TranslatorIdItem.Length > 0)
  298. {
  299. var officialDutyLinkTranslators = new List<Grp_OfficialDutyLinkTranslator>();
  300. foreach (var translatorId in dto.TranslatorIdItem)
  301. {
  302. officialDutyLinkTranslators.Add(new Grp_OfficialDutyLinkTranslator()
  303. {
  304. TranslatorId = translatorId,
  305. OfficialDutyId = dto.Id,
  306. CreateUserId = dto.CreateUserId,
  307. Remark = $"公务出访客户资料-->更新"
  308. });
  309. }
  310. if (officialDutyLinkTranslators.Count > 0)
  311. {
  312. await _sqlSugar.Insertable(officialDutyLinkTranslators).ExecuteCommandAsync();
  313. }
  314. }
  315. #endregion
  316. _sqlSugar.CommitTran();
  317. result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  318. }
  319. else
  320. {
  321. _sqlSugar.RollbackTran();
  322. result = new Result() { Code = -1, Msg = "修改失败!" };
  323. }
  324. }
  325. else
  326. {
  327. _sqlSugar.RollbackTran();
  328. result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  329. }
  330. return result;
  331. }
  332. public async Task<Result> PostReqReqSampleTips(string country, string Area,string client)
  333. {
  334. if (string.IsNullOrEmpty(country)) return new Result() { Code = -1, Msg = "国家为空!" };
  335. string sqlWhere = string.Empty;
  336. if (!string.IsNullOrEmpty(Area)) sqlWhere = string.Format(@$" And oa.Area Like '%{Area}%'");
  337. if (!string.IsNullOrEmpty(client)) sqlWhere = string.Format(@$" And oa.Client Like '%{client}%'");
  338. string sql = string.Format(@$"Select di.TeamName,oa.Id,oa.Country,oa.Area,oa.Client,oa.ReqSample
  339. From Res_OfficialActivities oa With(NoLock)
  340. Left Join Grp_DelegationInfo di On oa.DiId = di.Id
  341. Where oa.IsDel = 0 And oa.Country='{country}' {sqlWhere}");
  342. //ReqReqSampleTipsView
  343. var _views = await _sqlSugar.SqlQueryable<ReqReqSampleTipsView>(sql).ToListAsync();
  344. if (_views.Count > 0 )
  345. {
  346. return new Result() { Code = 0, Msg = "操作成功!", Data = _views };
  347. }
  348. return new Result() { Code = -1, Msg = "暂无相关数据!" };
  349. }
  350. }
  351. }