OfficialActivitiesRepository.cs 14 KB

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