InvitationOfficialActivityDataRepository.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using AutoMapper;
  2. using OASystem.Domain;
  3. using OASystem.Domain.Dtos.Groups;
  4. using OASystem.Domain.Dtos.Resource;
  5. using OASystem.Domain.Entities.Groups;
  6. using OASystem.Domain.Entities.Resource;
  7. using OASystem.Domain.ViewModels;
  8. using OASystem.Domain.ViewModels.Groups;
  9. using OASystem.Domain.ViewModels.Resource;
  10. using OASystem.Infrastructure.Tools;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Security.Cryptography;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. namespace OASystem.Infrastructure.Repositories.Resource
  18. {
  19. public class InvitationOfficialActivityDataRepository : BaseRepository<Res_InvitationOfficialActivityData, InvitationOfficialActivitiesByIdDto>
  20. {
  21. private readonly IMapper _mapper;
  22. public InvitationOfficialActivityDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  23. {
  24. _mapper = mapper;
  25. }
  26. /// <summary>
  27. /// 查询商邀资料列表
  28. /// </summary>
  29. /// <param name="dto"></param>
  30. /// <returns></returns>
  31. public async Task<Result> QueryInvitationOfficialActivityData(QueryInvitationOfficialActivityDataDto dto)
  32. {
  33. Result result = new Result() { Code = -2, Msg = "未知错误" };
  34. try
  35. {
  36. string sqlWhere = string.Empty;
  37. if (!string.IsNullOrWhiteSpace(dto.Country)) { sqlWhere += string.Format(@" And i.Country like '%{0}%'", dto.Country); }
  38. if (!string.IsNullOrWhiteSpace(dto.UnitName)) { sqlWhere += string.Format(@" And i.UnitName like '%{0}%'", dto.UnitName); }
  39. if (!string.IsNullOrWhiteSpace(dto.Contact)) { sqlWhere += string.Format(@" And i.Contact like '%{0}%'", dto.Contact); }
  40. if (!string.IsNullOrWhiteSpace(dto.Delegation)) { sqlWhere += string.Format(@" And ','+i.Delegation+',' like '%,{0},%'", dto.Delegation); }
  41. if (!string.IsNullOrWhiteSpace(dto.Field)) { sqlWhere += string.Format(@" And i.Field like '%{0}%'", dto.Field); }
  42. if (dto.CreateUserId != 0 && !string.IsNullOrWhiteSpace(dto.CreateUserId.ToString())) { sqlWhere += string.Format(@" And i.CreateUserId={0}", dto.CreateUserId); }
  43. if (!string.IsNullOrWhiteSpace(dto.StartCreateTime) && !string.IsNullOrWhiteSpace(dto.EndCreateTime))
  44. {
  45. sqlWhere += string.Format(@" And i.CreateTime between '{0}' and '{1}'", dto.StartCreateTime, dto.EndCreateTime);
  46. }
  47. sqlWhere += string.Format(@"And Isdel={0}", 0);
  48. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  49. {
  50. Regex r = new Regex("And");
  51. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  52. }
  53. string sql = string.Format(@"select i.*,(select CnName from Sys_Users where Id=i.CreateUserId ) as CreateUserName from
  54. Res_InvitationOfficialActivityData i {0} order by CreateTime desc", sqlWhere);
  55. List<InvitationOfficialActivityDataView> _ivitiesViews = _sqlSugar.SqlQueryable<InvitationOfficialActivityDataView>(sql).ToList();
  56. if (_ivitiesViews.Count != 0)
  57. {
  58. List<Grp_DelegationInfo> _DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().ToList();
  59. foreach (var item in _ivitiesViews)
  60. {
  61. string delegationNameList = "";
  62. string[] DelegationName = item.Delegation.Split(',');
  63. for (int i = 0; i < DelegationName.Length; i++)
  64. {
  65. //Grp_DelegationInfo _DelegationInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(a => a.Id ==int.Parse(DelegationName[i]));
  66. //if (_DelegationInfo != null) { delegationNameList += _DelegationInfo.TeamName + ","; }
  67. delegationNameList += _DelegationInfos.Find(it => it.Id == int.Parse(DelegationName[i]))?.TeamName ?? "Unknown" + ",";
  68. }
  69. if (!string.IsNullOrWhiteSpace(delegationNameList))
  70. {
  71. item.DelegationStr = delegationNameList.Substring(0, delegationNameList.Length - 1);
  72. }
  73. }
  74. if (dto.PageSize == 0 && dto.PageIndex == 0)
  75. {
  76. result = new Result() { Code = 0, Msg = "查询成功!", Data = _ivitiesViews };
  77. }
  78. else
  79. {
  80. int count = _ivitiesViews.Count;
  81. float totalPage = (float)count / dto.PageSize;//总页数
  82. if (totalPage == 0) totalPage = 1;
  83. else totalPage = (int)Math.Ceiling((double)totalPage);
  84. List<InvitationOfficialActivityDataView> invitations = new List<InvitationOfficialActivityDataView>();
  85. for (int i = 0; i < dto.PageSize; i++)
  86. {
  87. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  88. if (RowIndex < _ivitiesViews.Count)
  89. {
  90. invitations.Add(_ivitiesViews[RowIndex]);
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. ListViewBase<InvitationOfficialActivityDataView> rst = new ListViewBase<InvitationOfficialActivityDataView>();
  98. rst.DataList = invitations;
  99. rst.DataCount = count;
  100. rst.CurrPageIndex = dto.PageIndex;
  101. rst.CurrPageSize = dto.PageSize;
  102. result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  103. }
  104. }
  105. else
  106. {
  107. result = new Result() { Code = 0, Msg = "暂无数据!", Data = _ivitiesViews };
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. result = new Result() { Code = -2, Msg = ex.Message };
  113. }
  114. return result;
  115. }
  116. /// <summary>
  117. /// 根据Id查询商邀资料信息
  118. /// </summary>
  119. /// <param name="dto"></param>
  120. /// <returns></returns>
  121. /// <exception cref="NotImplementedException"></exception>
  122. public async Task<JsonView> Info(int id)
  123. {
  124. var res = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  125. .Where(x => x.Id == id && x.IsDel == 0)
  126. .FirstAsync();
  127. var _view = _mapper.Map<IOAInfoView>(res);
  128. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功", Data = _view };
  129. }
  130. public async Task<Result> OpInvitationOfficialActivity(OpInvitationOfficialActivityDto dto)
  131. {
  132. Result result = new Result() { Code = -2, Msg = "未知错误" };
  133. try
  134. {
  135. if (dto.Status == 1)//添加
  136. {
  137. string selectSql = string.Format(@"select * from Res_InvitationOfficialActivityData where UnitName='{0}' and IsDel='{1}'", dto.UnitName, 0);
  138. var res_InvitationOfficial = await _sqlSugar.SqlQueryable<Res_InvitationOfficialActivityData>(selectSql).FirstAsync();//查询是否存在
  139. if (res_InvitationOfficial != null)
  140. {
  141. return result = new Result() { Code = -1, Msg = "该信息已存在,请勿重复添加!" };
  142. }
  143. else//不存在,可添加
  144. {
  145. Res_InvitationOfficialActivityData _InvitationOfficialActivityData = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  146. int id = await _sqlSugar.Insertable(_InvitationOfficialActivityData).ExecuteReturnIdentityAsync();
  147. if (id == 0)
  148. {
  149. return result = new Result() { Code = -1, Msg = "添加失败!" };
  150. }
  151. return result = new Result() { Code = 0, Msg = "添加成功!", Data = new { Id = id } };
  152. }
  153. }
  154. else if (dto.Status == 2)//修改
  155. {
  156. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_InvitationOfficialActivityData
  157. {
  158. Country = dto.Country,
  159. City = dto.City,
  160. UnitName = dto.UnitName,
  161. UnitWeb = dto.UnitWeb,
  162. Field = dto.Field,
  163. Address = dto.Address,
  164. UnitInfo = dto.UnitInfo,
  165. Contact = dto.Contact,
  166. Job = dto.Job,
  167. Tel = dto.Tel,
  168. Email = dto.Email,
  169. WeChat = dto.WeChat,
  170. FaceBook = dto.FaceBook,
  171. Ins = dto.Ins,
  172. Delegation = dto.Delegation,
  173. FilePath = dto.FilePath,
  174. SndFilePath = dto.SndFilePath,
  175. Fax = dto.Fax,
  176. CreateUserId = dto.CreateUserId,
  177. Remark = dto.Remark
  178. });
  179. if (!res)
  180. {
  181. return result = new Result() { Code = -1, Msg = "修改失败!" };
  182. }
  183. return result = new Result() { Code = 0, Msg = "修改成功!" };
  184. }
  185. else
  186. {
  187. return result = new Result() { Code = -1, Msg = "请传入Status参数,1添加 2修改!" };
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. return result = new Result() { Code = -2, Msg = "程序错误!" };
  193. }
  194. }
  195. }
  196. }