InvitationOfficialActivityDataRepository.cs 16 KB

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