123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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<IEnumerable<Bundle>> GetByUserId(int userId)
- {
- return await _context.Bundles.Where(e => e.UserId == userId).ToListAsync<Bundle>();
- }
- public async Task<Bundle> Get(string id)
- {
- Guid g;
- if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id,out g))
- return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.Id == g);
- else
- {
- Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid",id));
- }
- return null;
- }
- public async Task<Bundle> GetDetail(string id)
- {
- Guid g;
- if (!String.IsNullOrWhiteSpace(id) && Guid.TryParse(id, out g))
- {
- Bundle b = await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.Id == g);
- if (b != null && b.Assets == null)
- b.Assets = await _context.Assets.Where(m => m.BundleId == g).AsNoTracking().ToArrayAsync<Asset>();
- Logger.LogInformation("多媒体资源数是{0}", b.Assets.Count());
- return b;
- }
- else
- {
- Logger.LogError(String.Format("id:{0};id不存在或不能转换为guid", id));
- }
- return null;
- }
- public async Task<Bundle> GetBySearchCode(string searchCode)
- {
- if (!String.IsNullOrWhiteSpace(searchCode))
- return await _context.Bundles.AsNoTracking().SingleOrDefaultAsync<Bundle>(m => m.SearchCode == searchCode);
- else
- return null;
- }
- public async Task<string> 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;
- }
- }
- }
|