OfficialActivitiesRepository.cs 13 KB

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