InvitationOfficialActivityDataRepository.cs 16 KB

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