using Attribute;
using Dto;
using Vo;
using Model;
using Model.Base;
using Repository;
using Service;
using Microsoft.AspNetCore.Mvc;
using Aliyun.OSS;
using Common;
using System.Text.RegularExpressions;
using Infrastructure.Model;
using Base;
using Mapster;
namespace Services
{
///
/// 页面模板更新信息Service业务层处理
///
[AppService(ServiceType = typeof(IPageUpdateInfoService), ServiceLifetime = LifeTime.Transient)]
public class PageUpdateInfoService : BaseService, IPageUpdateInfoService
{
///
/// 页面模板更新信息-列表
///
/// 请求参数
/// 页面模板更新信息列表
public PagedInfo List([FromQuery] PageUpdateInfoListDto parm, [FromQuery] PagerInfo page)
{
//开始拼装查询条件
var predicate = Expressionable.Create();
predicate = predicate.And(m => m.appVersion == parm.appVersion);
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.kind), m => m.kind.Contains(parm.kind));
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.title), m => m.title.Contains(parm.title));
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.modulePath), m => m.modulePath.Contains(parm.modulePath));
var response = Queryable()
.Where(predicate.ToExpression())
.OrderBy(m => m.id)
.ToPage(page);
return response;
}
///
/// 页面模板更新信息-刷新文件
///
/// 请求参数
/// 页面模板更新信息刷新文件
public void UpdateTemplate([FromQuery] PageUpdateInfoUpdateTemplateDto parm)
{
OssConfigs ossConfigs = new();
AppSettings.Bind("OssConfigs", ossConfigs);
string kind = parm.kind;
if(!string.IsNullOrEmpty(parm.appVersion))
{
kind += "/" + parm.appVersion;
}
GetSystemFiles(ossConfigs, kind);
foreach (string filename in SystemFiles)
{
string template = filename.Substring(filename.LastIndexOf("/") + 1);
if (template.EndsWith(".html"))
{
PageUpdateInfo check = GetFirst(m => m.modulePath == template && m.kind == parm.kind && m.appVersion == parm.appVersion);
if (check == null)
{
string ModuleContent = Function.GetNetFileContent(ossConfigs.Host + "template/app/" + kind + "/" + template);
if (!string.IsNullOrEmpty(ModuleContent))
{
string admintitle = "";
string title = "";
Match match = Regex.Match(ModuleContent, ".*?", RegexOptions.IgnoreCase);
if (match.Success)
{
admintitle = match.Value.Replace("", "").Replace("", "");
// if (admintitle.Contains("-"))
// {
// title = admintitle.Substring(admintitle.LastIndexOf("-") + 1);
// }
// else
// {
title = admintitle;
// }
}
var item = GetFirst(m => m.modulePath == template && m.appVersion == parm.appVersion);
if(item == null)
{
Add(new PageUpdateInfo()
{
createDate = DateTime.Now,
updateDate = DateTime.Now,
kind = parm.kind,
leftAction1 = "{\"Url\":\"GoBack#{\\\"Level\\\":\\\"1\\\"}\",\"Jump\":\"1\",\"Kind\":\"2\"}",
leftBtn1 = "static/images/left.png",
skidFlag = true,
showScrollBar = false,
title = title,
showTitle = true,
statusBarStyle = "default",
textColor = "333333",
backgroudColor = "FFFFFF",
modulePath = template,
moduleVersion = 1,
mustUpdate = true,
appVersion = Function.CheckNull(parm.appVersion),
});
}
else
{
item.updateDate = DateTime.Now;
item.title = title;
Update(item, true);
}
}
}
}
}
}
List SystemFiles;
private void GetSystemFiles(OssConfigs ossConfigs, string Kind = "default")
{
SystemFiles = new List();
var client = new OssClient(ossConfigs.Endpoint, ossConfigs.Key, ossConfigs.Secret);
var listObjectsRequest = new ListObjectsRequest(ossConfigs.BucketName);
listObjectsRequest.MaxKeys = 1000;
listObjectsRequest.Prefix = "template/app/" + Kind + "/";
var result = client.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummaries)
{
SystemFiles.Add(summary.Key);
}
}
///
/// 生成APP配置文件
///
/// 请求参数
/// 生成APP配置文件
public string makeAppInitData(string kind, string appVersion = "")
{
OssConfigs ossConfigs = new();
AppSettings.Bind("OssConfigs", ossConfigs);
string GotoPages = "{";
string PageInfoList = "{";
string LibFile = "{";
List newpages = GetList(m => m.kind == kind && m.appVersion == appVersion);
if(!string.IsNullOrEmpty(appVersion))
{
kind += "/" + appVersion;
}
foreach (PageUpdateInfo page in newpages)
{
string FileName = page.modulePath.Split('.')[0];
var pageinfo = page.Adapt();
pageinfo.dataId = Function.MD5_16(pageinfo.id.ToString() + "123890");
PageInfoList += "\"page" + FileName + "\":" + Newtonsoft.Json.JsonConvert.SerializeObject(pageinfo) + ",";
string PagePath = "template/app/" + kind + "/" + page.modulePath;
string pageContent = Function.GetNetFileContent(ossConfigs.Host + PagePath);
pageContent = Dbconn.Encrypt3DES(pageContent, "*ga34|^7");
LibFile += "\"page" + FileName + "\":\"" + pageContent + "\",";
}
PageInfoList = PageInfoList.TrimEnd(',');
PageInfoList += "}";
GotoPages = GotoPages.TrimEnd(',');
GotoPages += "}";
LibFile = LibFile.TrimEnd(',');
LibFile += "}";
return Dbconn.Encrypt3DES(PageInfoList, "*ga34|^7") + "#cut#" + GotoPages + "#cut#" + Dbconn.Encrypt3DES(LibFile, "*ga34|^7");
}
}
}