InvitationOfficialActivityDataRepository.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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<JsonView> QueryInvitationOfficialActivityData(QueryInvitationOfficialActivityDataDto dto)
  33. {
  34. var result = new JsonView() { Msg = "操作失败!" };
  35. //验证日期格式
  36. DateTime _beginDt, _endDt;
  37. var _beginDtBool = DateTime.TryParse(dto.StartCreateTime+" 00:00:00", out _beginDt);
  38. var _endDtBool = DateTime.TryParse(dto.EndCreateTime + " 23:59:59", out _endDt);
  39. RefAsync<int> total = 0;
  40. var _view = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  41. .LeftJoin<Sys_Users>((ioa, u) => ioa.CreateUserId == u.Id)
  42. .Where((ioa,u) => ioa.IsDel == 0)
  43. .WhereIF(!string.IsNullOrEmpty(dto.Country), (ioa, u) => ioa.Country.Contains(dto.Country))
  44. .WhereIF(!string.IsNullOrEmpty(dto.UnitName), (ioa, u) => ioa.UnitName.Contains(dto.UnitName))
  45. .WhereIF(!string.IsNullOrEmpty(dto.Contact), (ioa, u) => ioa.Contact.Contains(dto.Contact))
  46. .WhereIF(!string.IsNullOrEmpty(dto.Delegation), (ioa, u) => ioa.Delegation.Contains(dto.Delegation))
  47. .WhereIF(!string.IsNullOrEmpty(dto.Field), (ioa, u) => ioa.Field.Contains(dto.Field))
  48. .WhereIF(_beginDtBool && _endDtBool, (ioa, u) => ioa.CreateTime >= _beginDt && ioa.CreateTime <= _endDt)
  49. .WhereIF(dto.CreateUserId > 0, (ioa, u) => ioa.CreateUserId == dto.CreateUserId)
  50. .Select((ioa, u) => new {
  51. Row_Number = ioa.RowIndex,
  52. ioa.Id,
  53. ioa.Country,
  54. ioa.City,
  55. ioa.UnitName,
  56. ioa.Field,
  57. ioa.Contact,
  58. ioa.Job,
  59. ioa.Tel,
  60. ioa.CreateTime,
  61. CreateUserName = u.CnName,
  62. DelegationStr = ioa.Delegation
  63. })
  64. .OrderByDescending((ioa) => ioa.CreateTime)
  65. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  66. foreach (var item in _view)
  67. {
  68. string groupNameStr = "";
  69. int[] groupIds = item.DelegationStr.Split(',').Select(x =>
  70. {
  71. int id;
  72. if (int.TryParse(x, out id)) return id;
  73. else return id;
  74. }).ToArray();
  75. var _DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().Where(x => groupIds.Contains(x.Id)).ToList();
  76. foreach (var group in _DelegationInfos)
  77. {
  78. groupNameStr += $"{group.TeamName},";
  79. }
  80. if (groupNameStr.Length > 1)
  81. {
  82. groupNameStr= groupNameStr.Substring(0, groupNameStr.Length - 1);
  83. }
  84. }
  85. result.Code = StatusCodes.Status200OK;
  86. result.Msg = $"操作成功!";
  87. result.Data = _view;
  88. result.Count = _view.Count;
  89. return result;
  90. //try
  91. //{
  92. // string sqlWhere = string.Empty;
  93. // if (!string.IsNullOrWhiteSpace(dto.Country)) { sqlWhere += string.Format(@" And i.Country like '%{0}%'", dto.Country); }
  94. // if (!string.IsNullOrWhiteSpace(dto.UnitName)) { sqlWhere += string.Format(@" And i.UnitName like '%{0}%'", dto.UnitName); }
  95. // if (!string.IsNullOrWhiteSpace(dto.Contact)) { sqlWhere += string.Format(@" And i.Contact like '%{0}%'", dto.Contact); }
  96. // if (!string.IsNullOrWhiteSpace(dto.Delegation)) { sqlWhere += string.Format(@" And ','+i.Delegation+',' like '%,{0},%'", dto.Delegation); }
  97. // if (!string.IsNullOrWhiteSpace(dto.Field)) { sqlWhere += string.Format(@" And i.Field like '%{0}%'", dto.Field); }
  98. // if (dto.CreateUserId != 0 && !string.IsNullOrWhiteSpace(dto.CreateUserId.ToString())) { sqlWhere += string.Format(@" And i.CreateUserId={0}", dto.CreateUserId); }
  99. // if (!string.IsNullOrWhiteSpace(dto.StartCreateTime) && !string.IsNullOrWhiteSpace(dto.EndCreateTime))
  100. // {
  101. // sqlWhere += string.Format(@" And i.CreateTime between '{0}' and '{1}'", dto.StartCreateTime, dto.EndCreateTime);
  102. // }
  103. // sqlWhere += string.Format(@"And Isdel={0}", 0);
  104. // if (!string.IsNullOrEmpty(sqlWhere.Trim()))
  105. // {
  106. // Regex r = new Regex("And");
  107. // sqlWhere = r.Replace(sqlWhere, "Where", 1);
  108. // }
  109. // string sql = string.Format(@"select i.*,(select CnName from Sys_Users where Id=i.CreateUserId ) as CreateUserName from
  110. // Res_InvitationOfficialActivityData i {0} order by CreateTime desc", sqlWhere);
  111. // List<InvitationOfficialActivityDataView> _ivitiesViews = _sqlSugar.SqlQueryable<InvitationOfficialActivityDataView>(sql).ToList();
  112. // if (_ivitiesViews.Count != 0)
  113. // {
  114. // List<Grp_DelegationInfo> _DelegationInfos = _sqlSugar.Queryable<Grp_DelegationInfo>().ToList();
  115. // foreach (var item in _ivitiesViews)
  116. // {
  117. // string delegationNameList = "";
  118. // string[] DelegationName = item.Delegation.Split(',');
  119. // for (int i = 0; i < DelegationName.Length; i++)
  120. // {
  121. // //Grp_DelegationInfo _DelegationInfo = await _sqlSugar.Queryable<Grp_DelegationInfo>().FirstAsync(a => a.Id ==int.Parse(DelegationName[i]));
  122. // //if (_DelegationInfo != null) { delegationNameList += _DelegationInfo.TeamName + ","; }
  123. // delegationNameList += _DelegationInfos.Find(it => it.Id == int.Parse(DelegationName[i]))?.TeamName ?? "Unknown" + ",";
  124. // }
  125. // if (!string.IsNullOrWhiteSpace(delegationNameList))
  126. // {
  127. // item.DelegationStr = delegationNameList.Substring(0, delegationNameList.Length - 1);
  128. // }
  129. // }
  130. // if (dto.PageSize == 0 && dto.PageIndex == 0)
  131. // {
  132. // result = new Result() { Code = 0, Msg = "查询成功!", Data = _ivitiesViews };
  133. // }
  134. // else
  135. // {
  136. // int count = _ivitiesViews.Count;
  137. // float totalPage = (float)count / dto.PageSize;//总页数
  138. // if (totalPage == 0) totalPage = 1;
  139. // else totalPage = (int)Math.Ceiling((double)totalPage);
  140. // List<InvitationOfficialActivityDataView> invitations = new List<InvitationOfficialActivityDataView>();
  141. // for (int i = 0; i < dto.PageSize; i++)
  142. // {
  143. // var RowIndex = i + (dto.PageIndex - 1) * dto.PageSize;
  144. // if (RowIndex < _ivitiesViews.Count)
  145. // {
  146. // invitations.Add(_ivitiesViews[RowIndex]);
  147. // }
  148. // else
  149. // {
  150. // break;
  151. // }
  152. // }
  153. // ListViewBase<InvitationOfficialActivityDataView> rst = new ListViewBase<InvitationOfficialActivityDataView>();
  154. // rst.DataList = invitations;
  155. // rst.DataCount = count;
  156. // rst.CurrPageIndex = dto.PageIndex;
  157. // rst.CurrPageSize = dto.PageSize;
  158. // result = new Result() { Code = 0, Msg = "查询成功!", Data = rst };
  159. // }
  160. // }
  161. // else
  162. // {
  163. // result = new Result() { Code = 0, Msg = "暂无数据!", Data = _ivitiesViews };
  164. // }
  165. //}
  166. //catch (Exception ex)
  167. //{
  168. // result = new Result() { Code = -2, Msg = ex.Message };
  169. //}
  170. //return result;
  171. }
  172. /// <summary>
  173. /// 根据Id查询商邀资料信息
  174. /// </summary>
  175. /// <param name="dto"></param>
  176. /// <returns></returns>
  177. /// <exception cref="NotImplementedException"></exception>
  178. public async Task<JsonView> Info(int id)
  179. {
  180. var res = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>()
  181. .Where(x => x.Id == id && x.IsDel == 0)
  182. .FirstAsync();
  183. var _view = _mapper.Map<IOAInfoView>(res);
  184. return new JsonView() { Code = StatusCodes.Status200OK, Msg = "操作成功", Data = _view };
  185. }
  186. public async Task<JsonView> IOA_OP(OpInvitationOfficialActivityDto dto)
  187. {
  188. JsonView result = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  189. var _info = _mapper.Map<Res_InvitationOfficialActivityData>(dto);
  190. if (dto.Status == 1)//添加
  191. {
  192. string selectSql = string.Format(@"select * from Res_InvitationOfficialActivityData where UnitName='{0}' and IsDel='{1}'", dto.UnitName, 0);
  193. var res_InvitationOfficial = await _sqlSugar.SqlQueryable<Res_InvitationOfficialActivityData>(selectSql).FirstAsync();//查询是否存在
  194. if (res_InvitationOfficial != null)
  195. {
  196. result.Msg = $"该信息已存在,请勿重复添加!";
  197. return result;
  198. }
  199. #region 处理上传文件
  200. var fileNames = await Upload(dto.Files);
  201. if (fileNames.Count > 0)
  202. {
  203. _info.SndFileName = JsonConvert.SerializeObject(fileNames);
  204. }
  205. #endregion
  206. var id = await _sqlSugar.Insertable(_info).ExecuteReturnIdentityAsync();
  207. if (id < 1) return result;
  208. }
  209. else if (dto.Status == 2)//修改
  210. {
  211. var fileNameJsonStr = string.Empty;
  212. var fileNames = await Upload(dto.Files);
  213. if (fileNames.Count > 0)
  214. {
  215. var ioaInfo = await _sqlSugar.Queryable<Res_InvitationOfficialActivityData>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.Id);
  216. if (ioaInfo != null) {
  217. var fileName = ioaInfo.SndFileName;
  218. if (!string.IsNullOrEmpty(fileName))
  219. {
  220. try
  221. {
  222. var ioaFiles = JsonConvert.DeserializeObject<List<string>>(fileName);
  223. fileNames.AddRange(ioaFiles);
  224. }
  225. catch (Exception)
  226. {
  227. fileNames.Add(fileName);
  228. }
  229. }
  230. }
  231. fileNameJsonStr = JsonConvert.SerializeObject(fileNames);
  232. }
  233. bool res = await UpdateAsync(a => a.Id == dto.Id, a => new Res_InvitationOfficialActivityData
  234. {
  235. Country = dto.Country,
  236. City = dto.City,
  237. UnitName = dto.UnitName,
  238. UnitWeb = dto.UnitWeb,
  239. Field = dto.Field,
  240. Address = dto.Address,
  241. UnitInfo = dto.UnitInfo,
  242. Contact = dto.Contact,
  243. Job = dto.Job,
  244. Tel = dto.Tel,
  245. Email = dto.Email,
  246. WeChat = dto.WeChat,
  247. FaceBook = dto.FaceBook,
  248. Ins = dto.Ins,
  249. Delegation = dto.Delegation,
  250. FilePath = dto.FilePath,
  251. SndFileName = fileNameJsonStr,
  252. Fax = dto.Fax,
  253. CreateUserId = dto.CreateUserId,
  254. Remark = dto.Remark
  255. });
  256. if (!res) return result;
  257. }
  258. else
  259. {
  260. result.Msg = $"请传入Status参数,1添加 2修改!";
  261. return result;
  262. }
  263. result.Msg = $"操作成功!";
  264. result.Code = StatusCodes.Status200OK;
  265. return result;
  266. }
  267. public async Task< List<string>> Upload(IFormFile[] formFiles)
  268. {
  269. var fileNames = new List<string>();
  270. if (formFiles != null && formFiles.Length > 0)
  271. {
  272. var filePath = $@"{AppSettingsHelper.Get("GrpFileBasePath")}/商邀相关文件";
  273. if (!Directory.Exists(filePath))
  274. {
  275. Directory.CreateDirectory(filePath);
  276. }
  277. foreach (var file in formFiles)
  278. {
  279. //filePath = @$"{filePath}/{file.FileName}";
  280. var path = Path.Combine(filePath, file.FileName);
  281. using (var stream = new FileStream(path, FileMode.Create))
  282. {
  283. await file.CopyToAsync(stream);
  284. }
  285. fileNames.Add(file.FileName);
  286. }
  287. }
  288. return fileNames;
  289. }
  290. }
  291. }