OfficialActivitiesRepository.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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,(select Name from Sys_SetData
  41. where Id=o.OfficialForm) as OfficialFormName 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. if (dto.PageSize == 0 && dto.PageIndex == 0)
  47. {
  48. foreach (var temp in OfficialActivities)
  49. {
  50. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  51. DateTime dt;
  52. bool b_dt = DateTime.TryParse(temp.Date, out dt);
  53. if (b_dt)
  54. {
  55. temp.Date = dt.ToString("yyyy-MM-dd HH:mm");
  56. }
  57. }
  58. result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities };
  59. }
  60. else
  61. {
  62. int count = OfficialActivities.Count;
  63. float totalPage = (float)count / dto.PageSize;//总页数
  64. if (totalPage == 0) totalPage = 1;
  65. else totalPage = (int)Math.Ceiling((double)totalPage);
  66. List<OfficialActivitiesView> _OfficialActivities = new List<OfficialActivitiesView>();
  67. for (int i = 0; i < dto.PageSize; i++)
  68. {
  69. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  70. if (RowIndex < OfficialActivities.Count)
  71. {
  72. //2024年4月1日 11:55:44 -蒋金辰 -日期处理
  73. DateTime dt;
  74. bool b_dt = DateTime.TryParse(OfficialActivities[RowIndex].Date, out dt);
  75. if (b_dt)
  76. {
  77. OfficialActivities[RowIndex].Date = dt.ToString("yyyy-MM-dd HH:mm");
  78. }
  79. _OfficialActivities.Add(OfficialActivities[RowIndex]);
  80. }
  81. else
  82. {
  83. break;
  84. }
  85. }
  86. ListViewBase<OfficialActivitiesView> rst = new ListViewBase<OfficialActivitiesView>();
  87. rst.DataList = _OfficialActivities;
  88. rst.DataCount = count;
  89. rst.CurrPageIndex = dto.PageIndex;
  90. rst.CurrPageSize = dto.PageSize;
  91. result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  92. }
  93. }
  94. else
  95. {
  96. result = new Result() { Code = 0, Msg = "暂无数据!", Data = OfficialActivities };
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. result = new Result() { Code = -2, Msg = "未知错误" };
  102. }
  103. return result;
  104. }
  105. /// <summary>
  106. /// 根据公务出访Id查询单个数据
  107. /// </summary>
  108. /// <param name="dto"></param>
  109. /// <returns></returns>
  110. /// <exception cref="NotImplementedException"></exception>
  111. public async Task<Result> QueryOfficialActivitiesById(OfficialActivitiesDiIdDto dto)
  112. {
  113. Result result = new Result() { Code = -2, Msg = "未知错误" };
  114. try
  115. {
  116. string sqlWhere = string.Empty;
  117. sqlWhere += string.Format(@"And o.Isdel={0} And o.DiId={1} And o.Id={2}", 0, dto.DiId, dto.Id);
  118. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  119. {
  120. Regex r = new Regex("And");
  121. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  122. }
  123. string sql = string.Format(@"select *,(select CnName from Sys_Users where o.CreateUserId=Id) as CreateUserName,(select Name from Sys_SetData
  124. where Id=o.OfficialForm) as OfficialFormName from Res_OfficialActivities o {0}", sqlWhere);
  125. OfficialActivitiesView OfficialActivities = await _sqlSugar.SqlQueryable<OfficialActivitiesView>(sql).FirstAsync();
  126. result = new Result() { Code = 0, Msg = "查询成功!", Data = OfficialActivities };
  127. }
  128. catch (Exception ex)
  129. {
  130. result = new Result() { Code = -2, Msg = "未知错误" };
  131. }
  132. return result;
  133. }
  134. public async Task<Result> OpOfficialActivities(OpOfficialActivitiesDto dto)
  135. {
  136. Result result = new Result() { Code = -2, Msg = "未知错误" };
  137. try
  138. {
  139. if (dto.Status == 1)//添加
  140. {
  141. string selectSql = string.Format(@"select * from Res_OfficialActivities where Client='{0}' and Address='{1}' and IsDel='{2}'", dto.Client, dto.Address, 0);
  142. var res_InvitationOfficial = await _sqlSugar.SqlQueryable<Res_OfficialActivities>(selectSql).FirstAsync();//查询是否存在
  143. if (res_InvitationOfficial != null)
  144. {
  145. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  146. }
  147. else//不存在,可添加
  148. {
  149. Res_OfficialActivities _InvitationOfficialActivityData = _mapper.Map<Res_OfficialActivities>(dto);
  150. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  151. if (id == 0)
  152. {
  153. return result = new Result() { Code = -1, Msg = "添加失败!" };
  154. }
  155. return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  156. }
  157. }
  158. else if (dto.Status == 2)//修改
  159. {
  160. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_OfficialActivities
  161. {
  162. Type = dto.Type,
  163. Client = dto.Client,
  164. Date = dto.Date,
  165. Time = dto.Time,
  166. Address = dto.Address,
  167. Contact = dto.Contact,
  168. Job = dto.Job,
  169. Tel = dto.Tel,
  170. OfficialForm = dto.OfficialForm,
  171. Setting = dto.Setting,
  172. Dresscode = dto.Dresscode,
  173. Attendees = dto.Attendees,
  174. IsNeedTrans = dto.IsNeedTrans,
  175. Translators = dto.Translators,
  176. language = dto.language,
  177. Trip = dto.Trip,
  178. CreateUserId = dto.CreateUserId,
  179. Remark = dto.Remark,
  180. });
  181. if (!res)
  182. {
  183. return result = new Result() { Code = -1, Msg = "修改失败!" };
  184. }
  185. return result = new Result() { Code = 0, Msg = "修改成功!", Data = new { Id = dto.Id } };
  186. }
  187. else
  188. {
  189. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. return result = new Result() { Code = -2, Msg = "程序错误!" };
  195. }
  196. }
  197. }
  198. }