OfficialActivitiesRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities };
  153. }
  154. catch (Exception ex)
  155. {
  156. result = new Result() { Code = -2, Msg = "未知错误" };
  157. }
  158. return result;
  159. }
  160. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  161. {
  162. Result result = new Result() { Code = -2, Msg = "未知错误" };
  163. #region 特殊字符转码 037 - 4.28 15:17
  164. if (!string.IsNullOrEmpty(dto.Contact))
  165. {
  166. byte[] utf8Bytes = Encoding.UTF8.GetBytes(dto.Contact);
  167. byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
  168. dto.Contact = Encoding.Unicode.GetString(utf16Bytes);
  169. }
  170. #endregion
  171. if (dto.Status == 1)//添加
  172. {
  173. _sqlSugar.BeginTran();
  174. //处理sql注入 2024-05-22 袁
  175. //string selectSql = string.Format(@"select * from Res_OfficialActivities where Client='{0}' and Address='{1}' and IsDel='{2}'", dto.Client, dto.Address, 0);
  176. //var res_InvitationOfficial = await _sqlSugar.SqlQueryable<Res_OfficialActivities>(selectSql).FirstAsync();//查询是否存在
  177. 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);
  178. if (res_InvitationOfficial != null)
  179. {
  180. _sqlSugar.RollbackTran();
  181. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  182. }
  183. else//不存在,可添加
  184. {
  185. Res_OfficialActivities _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  186. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  187. if (id == 0)
  188. {
  189. _sqlSugar.RollbackTran();
  190. return result = new Result() { Code = -1, Msg = "添加失败!" };
  191. }
  192. //添加到资料库
  193. Res_InvitationOfficialActivityData res_InvitationData = new Res_InvitationOfficialActivityData();
  194. res_InvitationData.Country = dto.Country;
  195. res_InvitationData.City = dto.Area;
  196. res_InvitationData.UnitName = dto.Client;
  197. res_InvitationData.Delegation = dto.DiId.ToString();
  198. res_InvitationData.Address = dto.Address;
  199. res_InvitationData.CreateUserId = dto.CreateUserId;
  200. res_InvitationData.Contact = dto.Contact;
  201. res_InvitationData.Job = dto.Job;
  202. res_InvitationData.Tel = dto.Tel;
  203. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  204. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName && a.IsDel == 0 && a.Address == res_InvitationData.Address);
  205. if (ifNullUp == null)
  206. {
  207. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  208. if (DataID != 0)
  209. {
  210. _sqlSugar.CommitTran();
  211. return new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  212. }
  213. else
  214. {
  215. _sqlSugar.RollbackTran();
  216. return new Result() { Code = -1, Msg = "添加失败!" };
  217. }
  218. }
  219. else
  220. {
  221. _sqlSugar.CommitTran();
  222. result.Code = 0;
  223. result.Msg = "添加成功!";
  224. return result;
  225. }
  226. }
  227. }
  228. else if (dto.Status == 2)//修改
  229. {
  230. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  231. {
  232. Country = dto.Country,
  233. Area = dto.Area,
  234. Type = dto.Type,
  235. Client = dto.Client,
  236. Date = dto.Date,
  237. Time = dto.Time,
  238. Address = dto.Address,
  239. Contact = dto.Contact,
  240. Job = dto.Job,
  241. Tel = dto.Tel,
  242. OfficialForm = dto.OfficialForm,
  243. Field = dto.Field,
  244. ReqSample = dto.ReqSample,
  245. Setting = dto.Setting,
  246. Dresscode = dto.Dresscode,
  247. Attendees = dto.Attendees,
  248. IsNeedTrans = dto.IsNeedTrans,
  249. Translators = dto.Translators,
  250. Language = dto.language,
  251. Trip = dto.Trip,
  252. CreateUserId = dto.CreateUserId,
  253. Remark = dto.Remark,
  254. IsSubmitApproval = dto.IsSubmitApproval,
  255. });
  256. if (!res)
  257. {
  258. return result = new Result() { Code = -1, Msg = "修改失败!" };
  259. }
  260. return result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  261. }
  262. else
  263. {
  264. return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  265. }
  266. }
  267. public async Task<Result> PostReqReqSampleTips(string country, string Area,string client)
  268. {
  269. if (string.IsNullOrEmpty(country)) return new Result() { Code = -1, Msg = "国家为空!" };
  270. string sqlWhere = string.Empty;
  271. if (!string.IsNullOrEmpty(Area)) sqlWhere = string.Format(@$" And oa.Area Like '%{Area}%'");
  272. if (!string.IsNullOrEmpty(client)) sqlWhere = string.Format(@$" And oa.Client Like '%{client}%'");
  273. string sql = string.Format(@$"Select di.TeamName,oa.Id,oa.Country,oa.Area,oa.Client,oa.ReqSample
  274. From Res_OfficialActivities oa With(NoLock)
  275. Left Join Grp_DelegationInfo di On oa.DiId = di.Id
  276. Where oa.IsDel = 0 And oa.Country='{country}' {sqlWhere}");
  277. //ReqReqSampleTipsView
  278. var _views = await _sqlSugar.SqlQueryable<ReqReqSampleTipsView>(sql).ToListAsync();
  279. if (_views.Count > 0 )
  280. {
  281. return new Result() { Code = 0, Msg = "操作成功!", Data = _views };
  282. }
  283. return new Result() { Code = -1, Msg = "暂无相关数据!" };
  284. }
  285. }
  286. }