InvitationOfficialActivityDataRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using AutoMapper;
  2. using Newtonsoft.Json;
  3. using OASystem.Domain;
  4. using OASystem.Domain.Dtos.Groups;
  5. using OASystem.Domain.Dtos.Resource;
  6. using OASystem.Domain.Entities.Groups;
  7. using OASystem.Domain.Entities.Resource;
  8. using OASystem.Domain.ViewModels;
  9. using OASystem.Domain.ViewModels.Groups;
  10. using OASystem.Domain.ViewModels.Resource;
  11. using OASystem.Infrastructure.Tools;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Security.Cryptography;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace OASystem.Infrastructure.Repositories.Resource
  19. {
  20. public class InvitationOfficialActivityDataRepository : BaseRepository<Res_InvitationOfficialActivityData, InvitationOfficialActivitiesByIdDto>
  21. {
  22. private readonly IMapper _mapper;
  23. public InvitationOfficialActivityDataRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  24. {
  25. _mapper = mapper;
  26. }
  27. /// <summary>
  28. /// 查询商邀资料列表
  29. /// </summary>
  30. /// <param name="dto"></param>
  31. /// <returns></returns>
  32. public async Task<Result> QueryInvitationOfficialActivityData(QueryInvitationOfficialActivityDataDto dto)
  33. {
  34. Result result = new Result() { Code = -2, Msg = "未知错误" };
  35. try
  36. {
  37. string sqlWhere = string.Empty;
  38. if (!string.IsNullOrWhiteSpace(dto.Country)) { sqlWhere += string.Format(@" And i.Country like '%{0}%'", dto.Country); }
  39. if (!string.IsNullOrWhiteSpace(dto.UnitName)) { sqlWhere += string.Format(@" And i.UnitName like '%{0}%'", dto.UnitName); }
  40. if (!string.IsNullOrWhiteSpace(dto.Contact)) { sqlWhere += string.Format(@" And i.Contact like '%{0}%'", dto.Contact); }
  41. if (!string.IsNullOrWhiteSpace(dto.Delegation)) { sqlWhere += string.Format(@" And ','+i.Delegation+',' like '%,{0},%'", dto.Delegation); }
  42. if (!string.IsNullOrWhiteSpace(dto.Field)) { sqlWhere += string.Format(@" And i.Field like '%{0}%'", dto.Field); }
  43. if (dto.CreateUserId != 0 && !string.IsNullOrWhiteSpace(dto.CreateUserId.ToString())) { sqlWhere += string.Format(@" And i.CreateUserId={0}", dto.CreateUserId); }
  44. if (!string.IsNullOrWhiteSpace(dto.StartCreateTime) && !string.IsNullOrWhiteSpace(dto.EndCreateTime))
  45. {
  46. sqlWhere += string.Format(@" And i.CreateTime between '{0}' and '{1}'", dto.StartCreateTime, dto.EndCreateTime);
  47. }
  48. sqlWhere += string.Format(@"And Isdel={0}", 0);
  49. if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  50. {
  51. Regex r = new Regex("And");
  52. sqlWhere = r.Replace(sqlWhere, "Where", 1);
  53. }
  54. string sql = string.Format(@"select i.*,(select CnName from Sys_Users where Id=i.CreateUserId ) as CreateUserName from
  55. Res_InvitationOfficialActivityData i {0} order by CreateTime desc", sqlWhere);
  56. List<InvitationOfficialActivityDataView> _ivitiesViews = _sqlSugar.SqlQueryable<InvitationOfficialActivityDataView>(sql).ToList();
  57. if (_ivitiesViews.Count != 0)
  58. {
  59. List<Grp_DelegationInfo> _DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().ToList();
  60. foreach (var item in _ivitiesViews)
  61. {
  62. string delegationNameList = "";
  63. string[] DelegationName = item.Delegation.Split(',');
  64. for (int i = 0; i < DelegationName.Length; i++)
  65. {
  66. //Grp_DelegationInfo _DelegationInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(a => a.Id ==int.Parse(DelegationName[i]));
  67. //if (_DelegationInfo != null) { delegationNameList += _DelegationInfo.TeamName + ","; }
  68. delegationNameList += _DelegationInfos.Find(it => it.Id == int.Parse(DelegationName[i]))?.TeamName ?? "Unknown" + ",";
  69. }
  70. if (!string.IsNullOrWhiteSpace(delegationNameList))
  71. {
  72. item.DelegationStr = delegationNameList.Substring(0, delegationNameList.Length - 1);
  73. }
  74. }
  75. if (dto.PageSize == 0 && dto.PageIndex == 0)
  76. {
  77. result = new Result() { Code = 0, Msg = "查询成功!", Data = _ivitiesViews };
  78. }
  79. else
  80. {
  81. int count = _ivitiesViews.Count;
  82. float totalPage = (float)count / dto.PageSize;//总页数
  83. if (totalPage == 0) totalPage = 1;
  84. else totalPage = (int)Math.Ceiling((double)totalPage);
  85. List<InvitationOfficialActivityDataView> invitations = new List<InvitationOfficialActivityDataView>();
  86. for (int i = 0; i < dto.PageSize; i++)
  87. {
  88. var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  89. if (RowIndex < _ivitiesViews.Count)
  90. {
  91. invitations.Add(_ivitiesViews[RowIndex]);
  92. }
  93. else
  94. {
  95. break;
  96. }
  97. }
  98. ListViewBase<InvitationOfficialActivityDataView> rst = new ListViewBase<InvitationOfficialActivityDataView>();
  99. rst.DataList = invitations;
  100. rst.DataCount = count;
  101. rst.CurrPageIndex = dto.PageIndex;
  102. rst.CurrPageSize = dto.PageSize;
  103. result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  104. }
  105. }
  106. else
  107. {
  108. result = new Result() { Code = 0, Msg = "暂无数据!", Data = _ivitiesViews };
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. result = new Result() { Code = -2, Msg = ex.Message };
  114. }
  115. return result;
  116. }
  117. /// <summary>
  118. /// 根据Id查询商邀资料信息
  119. /// </summary>
  120. /// <param name="dto"></param>
  121. /// <returns></returns>
  122. /// <exception cref="NotImplementedException"></exception>
  123. public async Task<JsonView> Info(int id)
  124. {
  125. var res = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  126. .Where(x => x.Id == id && x.IsDel == 0)
  127. .FirstAsync();
  128. var _view = _mapper.Map<IOAInfoView>(res);
  129. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功", Data = _view };
  130. }
  131. public async Task<JsonView> IOA_OP(OpInvitationOfficialActivityDto dto)
  132. {
  133. JsonView result = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败1" };
  134. var _info = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  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. result.Msg = $"该信息已存在,请勿重复添加!";
  142. return result;
  143. }
  144. #region 处理上传文件
  145. var fileNames = await Upload(dto.Files);
  146. if (fileNames.Count > 0)
  147. {
  148. _info.SndFileName = JsonConvert.SerializeObject(fileNames);
  149. }
  150. #endregion
  151. var id = await _sqlSugar.Insertable(_info).ExecuteReturnIdentityAsync();
  152. if (id < 1) return result;
  153. }
  154. else if (dto.Status == 2)//修改
  155. {
  156. var fileNameJsonStr = string.Empty;
  157. var fileNames = await Upload(dto.Files);
  158. if (fileNames.Count > 0)
  159. {
  160. var ioaInfo = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.Id);
  161. if (ioaInfo != null) {
  162. var fileName = ioaInfo.SndFileName;
  163. if (!string.IsNullOrEmpty(fileName))
  164. {
  165. try
  166. {
  167. var ioaFiles = JsonConvert.DeserializeObject<List<string>>(fileName);
  168. fileNames.AddRange(ioaFiles);
  169. }
  170. catch (Exception)
  171. {
  172. fileNames.Add(fileName);
  173. }
  174. }
  175. }
  176. fileNameJsonStr = JsonConvert.SerializeObject(fileNames);
  177. }
  178. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_InvitationOfficialActivityData
  179. {
  180. Country = dto.Country,
  181. City = dto.City,
  182. UnitName = dto.UnitName,
  183. UnitWeb = dto.UnitWeb,
  184. Field = dto.Field,
  185. Address = dto.Address,
  186. UnitInfo = dto.UnitInfo,
  187. Contact = dto.Contact,
  188. Job = dto.Job,
  189. Tel = dto.Tel,
  190. Email = dto.Email,
  191. WeChat = dto.WeChat,
  192. FaceBook = dto.FaceBook,
  193. Ins = dto.Ins,
  194. Delegation = dto.Delegation,
  195. FilePath = dto.FilePath,
  196. SndFileName = fileNameJsonStr,
  197. Fax = dto.Fax,
  198. CreateUserId = dto.CreateUserId,
  199. Remark = dto.Remark
  200. });
  201. if (!res) return result;
  202. }
  203. else
  204. {
  205. result.Msg = $"请传入Status参数,1添加 2修改!";
  206. return result;
  207. }
  208. result.Msg = $"操作成功!";
  209. result.Code = StatusCodes.Status200OK;
  210. return result;
  211. }
  212. public async Task< List<string>> Upload(IFormFile[] formFiles)
  213. {
  214. var fileNames = new List<string>();
  215. if (formFiles != null && formFiles.Length > 0)
  216. {
  217. var filePath = $@"{AppSettingsHelper.Get("GrpFileBasePath")}/商邀相关文件";
  218. if (!Directory.Exists(filePath))
  219. {
  220. Directory.CreateDirectory(filePath);
  221. }
  222. foreach (var file in formFiles)
  223. {
  224. //filePath = @$"{filePath}/{file.FileName}";
  225. var path = Path.Combine(filePath, file.FileName);
  226. using (var stream = new FileStream(path, FileMode.Create))
  227. {
  228. await file.CopyToAsync(stream);
  229. }
  230. fileNames.Add(file.FileName);
  231. }
  232. }
  233. return fileNames;
  234. }
  235. }
  236. }