OfficialActivitiesRepository.cs 8.7 KB

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