OfficialActivitiesRepository.cs 13 KB

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