OfficialActivitiesRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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<Result> QueryOfficialActivitiesByDiId(OfficialActivitiesByDiIdDto dto)
  29. {
  30. Result result = new Result() { Code = -2, Msg = "未知错误" };
  31. try
  32. {
  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(@"select *,(select CnName from Sys_Users where o.CreateUserId=Id) as CreateUserName,OfficialForm
  41. from Res_OfficialActivities o {0} order by o.CreateTime desc", sqlWhere);
  42. List<OfficialActivitiesView> OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).ToListAsync();
  43. List<Sys_SetData> data = await _sqlSugar.Queryable<Sys_SetData>().Where(a => a.IsDel == 0 && a.STid == 38).ToListAsync();
  44. if (OfficialActivities.Count != 0)
  45. {
  46. List<Sys_SetData> sdList = Query<Sys_SetData>(s => s.STid == 38).ToList();
  47. if (dto.PageSize == 0 && dto.PageIndex == 0)
  48. {
  49. foreach (var temp in OfficialActivities)
  50. {
  51. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  52. DateTime dt;
  53. bool b_dt = DateTime.TryParse(temp.Date, out dt);
  54. if (b_dt)
  55. {
  56. if (!string.IsNullOrEmpty(temp.Time)) temp.Date = dt.ToString("yyyy-MM-dd") + " " + temp.Time;
  57. else temp.Date = dt.ToString("yyyy-MM-dd HH:mm:ss");
  58. }
  59. //2024年4月26日 17:43 -蒋金辰 -出访形式
  60. Sys_SetData tempForm = sdList.FirstOrDefault(s => s.Id == temp.OfficialForm);
  61. if (tempForm != null)
  62. {
  63. temp.OfficialFormName = tempForm.Name;
  64. }
  65. else
  66. {
  67. temp.OfficialFormName = "未知";
  68. }
  69. }
  70. result = new Result() { Code = 0, 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. //2024年4月26日 17:43 -蒋金辰 -出访形式
  92. Sys_SetData tempForm = sdList.FirstOrDefault(s => s.Id == OfficialActivities[RowIndex].OfficialForm);
  93. if (tempForm != null)
  94. {
  95. OfficialActivities[RowIndex].OfficialFormName = tempForm.Name;
  96. }
  97. else
  98. {
  99. OfficialActivities[RowIndex].OfficialFormName = "未知";
  100. }
  101. _OfficialActivities.Add(OfficialActivities[RowIndex]);
  102. }
  103. else
  104. {
  105. break;
  106. }
  107. }
  108. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  109. rst.DataList = _OfficialActivities;
  110. rst.DataCount = count;
  111. rst.CurrPageIndex = dto.PageIndex;
  112. rst.CurrPageSize = dto.PageSize;
  113. result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  114. }
  115. }
  116. else
  117. {
  118. result = new Result() { Code = 0, Msg = "暂无数据!", Data = OfficialActivities };
  119. }
  120. }
  121. catch (Exception ex)
  122. {
  123. result = new Result() { Code = -2, Msg = "未知错误" };
  124. }
  125. return result;
  126. }
  127. /// <summary>
  128. /// 根据公务出访Id查询单个数据
  129. /// </summary>
  130. /// <param name="dto"></param>
  131. /// <returns></returns>
  132. /// <exception cref="NotImplementedException"></exception>
  133. public async Task<Result> QueryOfficialActivitiesById(OfficialActivitiesDiIdDto dto)
  134. {
  135. Result result = new Result() { Code = -2, Msg = "未知错误" };
  136. try
  137. {
  138. string sqlWhere = string.Empty;
  139. sqlWhere += string.Format(@"And o.Isdel={0} And o.DiId={1} And o.Id={2}", 0, dto.DiId, dto.Id);
  140. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  141. {
  142. Regex r = new Regex("And");
  143. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  144. }
  145. string sql = string.Format(@"select *,(select CnName from Sys_Users where o.CreateUserId=Id) as CreateUserName,(select Name from Sys_SetData
  146. where Id=o.OfficialForm) as OfficialFormName from Res_OfficialActivities o {0}", sqlWhere);
  147. OfficialActivitiesView OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).FirstAsync();
  148. result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities };
  149. }
  150. catch (Exception ex)
  151. {
  152. result = new Result() { Code = -2, Msg = "未知错误" };
  153. }
  154. return result;
  155. }
  156. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  157. {
  158. Result result = new Result() { Code = -2, Msg = "未知错误" };
  159. #region 特殊字符转码 037 - 4.28 15:17
  160. if (!string.IsNullOrEmpty(dto.Contact))
  161. {
  162. byte[] utf8Bytes = Encoding.UTF8.GetBytes(dto.Contact);
  163. byte[] utf16Bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, utf8Bytes);
  164. dto.Contact = Encoding.Unicode.GetString(utf16Bytes);
  165. }
  166. #endregion
  167. if (dto.Status == 1)//添加
  168. {
  169. _sqlSugar.BeginTran();
  170. //处理sql注入 2024-05-22 袁
  171. //string selectSql = string.Format(@"select * from Res_OfficialActivities where Client='{0}' and Address='{1}' and IsDel='{2}'", dto.Client, dto.Address, 0);
  172. //var res_InvitationOfficial = await _sqlSugar.SqlQueryable<Res_OfficialActivities>(selectSql).FirstAsync();//查询是否存在
  173. var res_InvitationOfficial = await _sqlSugar.Queryable< Res_OfficialActivities >().FirstAsync(x=>x.Client == dto.Client && x.Address == dto.Address && x.IsDel == 0);
  174. if (res_InvitationOfficial != null)
  175. {
  176. _sqlSugar.RollbackTran();
  177. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  178. }
  179. else//不存在,可添加
  180. {
  181. Res_OfficialActivities _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  182. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  183. if (id == 0)
  184. {
  185. _sqlSugar.RollbackTran();
  186. return result = new Result() { Code = -1, Msg = "添加失败!" };
  187. }
  188. //添加到资料库
  189. Res_InvitationOfficialActivityData res_InvitationData = new Res_InvitationOfficialActivityData();
  190. res_InvitationData.Country = dto.Country;
  191. res_InvitationData.City = dto.Area;
  192. res_InvitationData.UnitName = dto.Client;
  193. res_InvitationData.Delegation = dto.DiId.ToString();
  194. res_InvitationData.Address = dto.Address;
  195. res_InvitationData.CreateUserId = dto.CreateUserId;
  196. res_InvitationData.Contact = dto.Contact;
  197. res_InvitationData.Job = dto.Job;
  198. res_InvitationData.Tel = dto.Tel;
  199. Res_InvitationOfficialActivityData ifNullUp = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync
  200. (a => a.Country == res_InvitationData.Country && a.City == res_InvitationData.City && a.UnitName == res_InvitationData.UnitName && a.IsDel == 0 && a.Address == res_InvitationData.Address);
  201. if (ifNullUp == null)
  202. {
  203. int DataID = await _sqlSugar.Insertable(res_InvitationData).ExecuteReturnIdentityAsync();
  204. if (DataID != 0)
  205. {
  206. _sqlSugar.CommitTran();
  207. return new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  208. }
  209. else
  210. {
  211. _sqlSugar.RollbackTran();
  212. return new Result() { Code = -1, Msg = "添加失败!" };
  213. }
  214. }
  215. }
  216. }
  217. else if (dto.Status == 2)//修改
  218. {
  219. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  220. {
  221. Country = dto.Country,
  222. Area = dto.Area,
  223. Type = dto.Type,
  224. Client = dto.Client,
  225. Date = dto.Date,
  226. Time = dto.Time,
  227. Address = dto.Address,
  228. Contact = dto.Contact,
  229. Job = dto.Job,
  230. Tel = dto.Tel,
  231. OfficialForm = dto.OfficialForm,
  232. Field = dto.Field,
  233. ReqSample = dto.ReqSample,
  234. Setting = dto.Setting,
  235. Dresscode = dto.Dresscode,
  236. Attendees = dto.Attendees,
  237. IsNeedTrans = dto.IsNeedTrans,
  238. Translators = dto.Translators,
  239. language = dto.language,
  240. Trip = dto.Trip,
  241. CreateUserId = dto.CreateUserId,
  242. Remark = dto.Remark,
  243. });
  244. if (!res)
  245. {
  246. return result = new Result() { Code = -1, Msg = "修改失败!" };
  247. }
  248. return result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  249. }
  250. else
  251. {
  252. return new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  253. }
  254. return result;
  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. }