OfficialActivitiesRepository.cs 8.9 KB

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