using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Http; using System.Threading.Tasks; using Winsoft.GOV.XF.WebApi.WXCore.Data; using Winsoft.GOV.XF.WebApi.WXCore.Models; using Winsoft.GOV.XF.WebApi.WXCore.Helpers; using Newtonsoft.Json; using Senparc.Weixin.Entities; using Microsoft.Extensions.Options; using Microsoft.Extensions.Configuration; using Microsoft.EntityFrameworkCore.Extensions.Internal; namespace Winsoft.GOV.XF.WebApi.WXCore.Services { public class BundlesService : BaseService { public BundlesService(XFDbContext context, ILoggerFactory loggerFactory, IConfigurationRoot config) :base(context, loggerFactory, config) { } public async Task> GetByUserId(int userId) { return await _context.Bundles.Where(e => e.UserId == userId).ToListAsync(); } public async Task Get(string id) { Guid g; if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id,out g)) return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync(m => m.Id == g); else { Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid",id)); } return null; } public async Task GetDetail(string id) { Guid g; if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id, out g)) { Bundle b = await _context.Bundles.AsNoTracking().SingleOrDefaultAsync(m => m.Id == g); if (b != null && b.Assets == null) b.Assets = await _context.Assets.Where(m => m.BundleId == g).AsNoTracking().ToArrayAsync(); Logger.LogInformation("多媒体资源数是{0}", b.Assets.Count()); return b; } else { Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid", id)); } return null; } public async Task GetBySearchCode(string searchCode) { if (!String.IsNullOrWhiteSpace(searchCode)) return await _context.Bundles.SingleOrDefaultAsync(m => m.SearchCode == searchCode); else return null; } public async Task Add(Bundle b) { if (b != null) { await _context.Bundles.AddAsync(b); await _context.SaveChangesAsync(); return b.Id.ToString(); } return String.Empty; } public async Task Delete(Bundle b) { if (b != null) { _context.Bundles.Remove(b); await _context.SaveChangesAsync(); } } public async Task UpdateSearchCode(Bundle b) { if (b != null) { Bundle b2 = new Bundle() { Id = b.Id, SearchCode = b.SearchCode }; _context.Entry(b2).Property(x => x.SearchCode).IsModified = true; await _context.SaveChangesAsync(); } } public async Task UpdateReply(Bundle b) { if (b != null) { Bundle b2 = new Bundle() { Id = b.Id, LetterId = b.LetterId, Reply = b.Reply, ResultFromXF = b.ResultFromXF, Status = b.Status }; _context.Entry(b2).Property(x => x.LetterId).IsModified = true; _context.Entry(b2).Property(x => x.Reply).IsModified = true; _context.Entry(b2).Property(x => x.ResultFromXF).IsModified = true; _context.Entry(b2).Property(x => x.Status).IsModified = true; //_context.Bundles.Update(b); await _context.SaveChangesAsync(); } } public async Task UpdateAssessed(Bundle b) { if (b != null) { Bundle b2 = new Bundle() { Id = b.Id, IsAssessed = b.IsAssessed }; _context.Entry(b2).Property(x => x.IsAssessed).IsModified = true; await _context.SaveChangesAsync(); } } public string GenerateBaseURL(Bundle b, string absoluteUri) { BundleAccessToken a = new BundleAccessToken() { Expire = -1, BundleId = b.Id }; //http://ef7eb3cc.ngrok.io/dist/multimedia/index.html? string multimediaURL = string.Format("{0}m/index.html?id={1}&ciphertext={2}", absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key)); return multimediaURL; } public string GenerateSampleBaseURL(Bundle b, string absoluteUri) { BundleAccessToken a = new BundleAccessToken() { Expire = -1, BundleId = b.Id }; //http://ef7eb3cc.ngrok.io/dist/multimedia/index.html? string multimediaURL = string.Format("{0}m/index.html?id={1}", absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key)); return multimediaURL; } public string GenerateShareURL(Bundle b, BundleAccessToken a, string absoluteUri) { if (a.Expire != -1) { a.Expire = DateTime.Now.AddDays(1).ToTimeStamp(); string multimediaURL = string.Format("{0}m/index.html?id={1}&ciphertext={2}", absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key)); return multimediaURL; } return String.Empty; } public string GenerateSampleShareURL(Bundle b, string absoluteUri) { BundleAccessToken a = new BundleAccessToken() { BundleId = b.Id, Expire = DateTime.Now.AddDays(1).ToTimeStamp() }; string multimediaURL = string.Format("{0}m/index.html?id={1}&ciphertext={2}", absoluteUri, b.Id, JsonConvert.SerializeObject(a).AESEncrypt(b.Key)); return multimediaURL; } } }