BundlesService.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Microsoft.EntityFrameworkCore;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Microsoft.AspNetCore.Http;
  7. using System.Threading.Tasks;
  8. using Winsoft.GOV.XF.WebApi.WXCore.Data;
  9. using Winsoft.GOV.XF.WebApi.WXCore.Models;
  10. using Winsoft.GOV.XF.WebApi.WXCore.Helpers;
  11. using Newtonsoft.Json;
  12. using Senparc.Weixin.Entities;
  13. using Microsoft.Extensions.Options;
  14. using Microsoft.Extensions.Configuration;
  15. using Microsoft.EntityFrameworkCore.Extensions.Internal;
  16. namespace Winsoft.GOV.XF.WebApi.WXCore.Services
  17. {
  18. public class BundlesService : BaseService
  19. {
  20. public BundlesService(XFDbContext context, ILoggerFactory loggerFactory, IConfigurationRoot config) :base(context, loggerFactory, config)
  21. {
  22. }
  23. public async Task<IEnumerable<Bundle>> GetByUserId(int userId)
  24. {
  25. return await _context.Bundles.Where(e => e.UserId == userId).ToListAsync<Bundle>();
  26. }
  27. public async Task<Bundle> Get(string id)
  28. {
  29. Guid g;
  30. if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id,out g))
  31. return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.Id == g);
  32. else
  33. {
  34. Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid",id));
  35. }
  36. return null;
  37. }
  38. public async Task<Bundle> GetDetail(string id)
  39. {
  40. Guid g;
  41. if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id, out g))
  42. {
  43. Bundle b = await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.Id == g);
  44. if (b != null && b.Assets == null)
  45. b.Assets = await _context.Assets.Where(m => m.BundleId == g).AsNoTracking().ToArrayAsync<Asset>();
  46. Logger.LogInformation("多媒体资源数是{0}", b.Assets.Count());
  47. return b;
  48. }
  49. else
  50. {
  51. Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid", id));
  52. }
  53. return null;
  54. }
  55. public async Task<Bundle> GetBySearchCode(string searchCode)
  56. {
  57. if (!String.IsNullOrWhiteSpace(searchCode))
  58. return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.SearchCode == searchCode);
  59. else
  60. return null;
  61. }
  62. public async Task<string> Add(Bundle b)
  63. {
  64. if (b != null)
  65. {
  66. await _context.Bundles.AddAsync(b);
  67. await _context.SaveChangesAsync();
  68. return b.Id.ToString();
  69. }
  70. return String.Empty;
  71. }
  72. public async Task Delete(Bundle b)
  73. {
  74. if (b != null)
  75. {
  76. _context.Bundles.Remove(b);
  77. await _context.SaveChangesAsync();
  78. }
  79. }
  80. public async Task UpdateSearchCode(Bundle b)
  81. {
  82. if (b != null)
  83. {
  84. Bundle b2 = new Bundle()
  85. {
  86. Id = b.Id,
  87. SearchCode = b.SearchCode
  88. };
  89. _context.Entry(b2).Property(x => x.SearchCode).IsModified = true;
  90. await _context.SaveChangesAsync();
  91. }
  92. }
  93. public async Task UpdateReply(Bundle b)
  94. {
  95. if (b != null)
  96. {
  97. Bundle b2 = new Bundle()
  98. {
  99. Id = b.Id,
  100. LetterId = b.LetterId,
  101. Reply = b.Reply,
  102. ResultFromXF = b.ResultFromXF,
  103. Status = b.Status
  104. };
  105. _context.Entry(b2).Property(x => x.LetterId).IsModified = true;
  106. _context.Entry(b2).Property(x => x.Reply).IsModified = true;
  107. _context.Entry(b2).Property(x => x.ResultFromXF).IsModified = true;
  108. _context.Entry(b2).Property(x => x.Status).IsModified = true;
  109. //_context.Bundles.Update(b);
  110. await _context.SaveChangesAsync();
  111. }
  112. }
  113. public async Task UpdateAssessed(Bundle b)
  114. {
  115. if (b != null)
  116. {
  117. Bundle b2 = new Bundle()
  118. {
  119. Id = b.Id,
  120. IsAssessed = b.IsAssessed
  121. };
  122. _context.Entry(b2).Property(x => x.IsAssessed).IsModified = true;
  123. await _context.SaveChangesAsync();
  124. }
  125. }
  126. public string GenerateBaseURL(Bundle b, string absoluteUri)
  127. {
  128. BundleAccessToken a = new BundleAccessToken()
  129. {
  130. Expire = -1,
  131. BundleId = b.Id
  132. };
  133. //http://ef7eb3cc.ngrok.io/dist/multimedia/index.html?
  134. string multimediaURL = string.Format("{0}m/index.html?id={1}&ciphertext={2}",
  135. absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
  136. return multimediaURL;
  137. }
  138. public string GenerateSampleBaseURL(Bundle b, string absoluteUri)
  139. {
  140. BundleAccessToken a = new BundleAccessToken()
  141. {
  142. Expire = -1,
  143. BundleId = b.Id
  144. };
  145. //http://ef7eb3cc.ngrok.io/dist/multimedia/index.html?
  146. string multimediaURL = string.Format("{0}m/index.html?id={1}",
  147. absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
  148. return multimediaURL;
  149. }
  150. public string GenerateShareURL(Bundle b, BundleAccessToken a, string absoluteUri)
  151. {
  152. if (a.Expire != -1)
  153. {
  154. a.Expire = DateTime.Now.AddDays(1).ToTimeStamp();
  155. string multimediaURL = string.Format("{0}m/index.html?id={1}&ciphertext={2}",
  156. absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
  157. return multimediaURL;
  158. }
  159. return String.Empty;
  160. }
  161. public string GenerateSampleShareURL(Bundle b, string absoluteUri)
  162. {
  163. BundleAccessToken a = new BundleAccessToken()
  164. {
  165. BundleId = b.Id,
  166. Expire = DateTime.Now.AddDays(1).ToTimeStamp()
  167. };
  168. string multimediaURL = string.Format("{0}m/index.html?id={1}&ciphertext={2}",
  169. absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key));
  170. return multimediaURL;
  171. }
  172. }
  173. }