using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using WZExport.Data; using WZExport.Models; using System.IO; using System.IO.Compression; using System.Xml; using System.Text; using WZExport.Utilitys; namespace WZExport.Pages.Wzs { public class ExportModel : PageModel { private readonly WZContext _context; public ExportModel(WZContext context) { _context = context; } public IActionResult OnGet() { return Page(); } [BindProperty] public string Type { get; set; } public readonly string _type1 = "导出文章"; public readonly string _type2 = "导出元数据文章"; static readonly string[] _image = { "jpg", "gif", "png", "bmp","jpeg" }; static readonly string[] _flash = { "swf","avi","flash","mp4" }; static readonly string[] _attach = { "ceb","doc","xls","rar","docx","pdf","zip","txt","xlsx","ppt","htm","cebx","pptx","dot" }; public async Task OnPostAsync() { string basePath = $"/temp/{nameof(Ptwd)}/{Type}/"; string floderPath = $"{basePath}{DateTime.Now:yyyyMMdd}"; if (Directory.Exists(floderPath) is false) { Directory.CreateDirectory(floderPath); if(Type==_type1) await CreateDefault(floderPath); else if (Type == _type2) await CreateYSJ(floderPath); DeleteEmptyFloder(floderPath); Replace(floderPath); } string zipfileName = $"{Type}-{DateTime.Now:yyyyMMddHHmmss}.zip"; string zipFilePath = $"{basePath}{zipfileName}"; ZipFile.CreateFromDirectory(floderPath, zipFilePath); return File(System.IO.File.OpenRead(zipFilePath), "application/x-zip-compressed", zipfileName); } public async Task CreateDefault(string path) { string joinStr = "SELECT lm.Channelid,wz.DocID, " + " fj.Srcfile,fj.Fileext " + " from lm inner JOIN ptwd as wz ON lm.CHANNELID=wz.DOCCHANNEL LEFT JOIN fj on wz.DOCID=fj.APPDOCID"; string wzStr = "SELECT Doctitle, Subdoctitle,Docauthor,Docsourcename,Doceditor," + " Dockeywords,Docabstract,Dochtmlcon,Crtime,Docpubtime " + " from ptwd where DOCID={0} "; using var context = DatabaseService.Current.GetDbContext(); var wzData = await context.SqlQueryAsync(joinStr); foreach (var wz in wzData) { var wzNew = context.SqlQuery(string.Format(wzStr, wz.DocID)).First(); wz.Covert(wzNew); } CreateXml(path,wzData); } /// /// 元数据 /// /// /// public async Task CreateYSJ(string path) { string joinStr = "SELECT lm.Channelid,wz.WCMMetaTablegovInfoID as DocID, " + " fj.Srcfile,fj.Fileext " + " from lm inner JOIN stqy as wz ON lm.CHANNELID=wz.ChannelId LEFT JOIN fj on wz.WCMMetaTablegovInfoID=fj.APPENDIXID"; string wzStr = "SELECT Doctitle, title2 as Subdoctitle,CrUser as Docauthor,docsourcename as Docsourcename,CrUser as Doceditor," + " Dockeywords,CONTENT_GS as Docabstract,dochtmlcon as Dochtmlcon,Crtime,DATE_SC as Docpubtime,fbjg " + " from stqy where WCMMetaTablegovInfoID={0} "; using var context = DatabaseService.Current.GetDbContext(); var wzData = await context.SqlQueryAsync(joinStr); foreach (var wz in wzData) { var wzNew = context.SqlQuery(string.Format(wzStr, wz.DocID)).First(); wz.Covert(wzNew); } var dics = await context.Query().ToListAsync(); var pzDics = dics.Where(dic => dic.Type == 1); var jdDics = dics.Where(dic => dic.Type == 2); Action action = (channelid, wz,doc,article) => { var b_xxgk = doc.CreateElement("b_xxgk"); b_xxgk.InnerText = UseTemp("1"); var vc_number = doc.CreateElement("vc_number"); vc_number.InnerText = UseTemp(pzDics.FirstOrDefault(dic=>dic.Key== channelid)?.Value??""); var vc_openmodel = doc.CreateElement("vc_openmodel"); vc_openmodel.InnerText = UseTemp("主动公开"); var vc_filenumber = doc.CreateElement("vc_filenumber"); vc_filenumber.InnerText = UseTemp(); var c_complatedate = doc.CreateElement("c_complatedate"); c_complatedate.InnerText = UseTemp(wz.Docpubtime?.ToString("yyyy-MM-dd HH:mm:ss")); var vc_opentimelimit = doc.CreateElement("vc_opentimelimit"); vc_opentimelimit.InnerText = UseTemp("长期公开"); var vc_auditprogram = doc.CreateElement("vc_auditprogram"); vc_auditprogram.InnerText = UseTemp("单位审核公开"); var vc_ztflid = doc.CreateElement("vc_ztflid"); vc_ztflid.InnerText = UseTemp(); var vc_ztflname = doc.CreateElement("vc_ztflname"); vc_ztflname.InnerText = UseTemp(); var vc_serviceid = doc.CreateElement("vc_serviceid"); vc_serviceid.InnerText = UseTemp(); var vc_servicename = doc.CreateElement("vc_servicename"); vc_servicename.InnerText = UseTemp(); var vc_fbjg = doc.CreateElement("vc_fbjg"); vc_fbjg.InnerText = UseTemp(wz.fbjg??""); var vc_service = doc.CreateElement("vc_service"); vc_service.InnerText = UseTemp(); var vc_area = doc.CreateElement("vc_area"); vc_area.InnerText = UseTemp(jdDics.FirstOrDefault(dic => dic.Key == channelid)?.Value ?? ""); var validend = doc.CreateElement("validend"); validend.InnerText = UseTemp(); article.AppendChild(b_xxgk); article.AppendChild(vc_number); article.AppendChild(vc_openmodel); article.AppendChild(vc_filenumber); article.AppendChild(c_complatedate); article.AppendChild(vc_opentimelimit); article.AppendChild(vc_auditprogram); article.AppendChild(vc_ztflid); article.AppendChild(vc_ztflname); article.AppendChild(vc_serviceid); article.AppendChild(vc_servicename); article.AppendChild(vc_fbjg); article.AppendChild(vc_service); article.AppendChild(vc_area); article.AppendChild(validend); }; CreateXml(path, wzData, action); string UseTemp(string info = "") { return $""; } } private void CreateXml(string path,List wzData,Action action=null) { var wzGroupData = wzData.GroupBy(data => data.Channelid).Select(gpData => new { Channelid = gpData.Key, wzs = gpData.AsEnumerable() }); var lms = _context.Lm.AsEnumerable(); var parentLms = lms.Where(lm => lm.Parentid == 0).OrderByDescending(lm => lm.Chnlorder); foreach (var parentLm in parentLms) { FindSon(parentLm, path); } void FindSon(Lm parentLm, string lmPath) { var sonLms = lms.Where(lm => lm.Parentid == parentLm.Channelid).OrderByDescending(lm => lm.Chnlorder); var currentFilePath = $"{lmPath}/{parentLm.Chnldesc}"; WriteWZXml(parentLm, currentFilePath); if (sonLms.Count() > 0) { // parentLm.Childrens = sonLms; if (Directory.Exists(currentFilePath) is false) Directory.CreateDirectory(currentFilePath); foreach (var sonLm in sonLms) { FindSon(sonLm, currentFilePath); } } } void WriteWZXml(Lm lm, string lmPath) { var wzs = wzGroupData.FirstOrDefault(wzGp => wzGp.Channelid == lm.Channelid)?.wzs?.Reverse(); if (wzs is null) return; if (Directory.Exists(lmPath) is false) Directory.CreateDirectory(lmPath); const int onceNumber = 100; GetNumberOfCycle(out int cs, out int ys); for (int i = 1; i <= cs; i++) { XmlDocument doc = new XmlDocument(); var docDec = doc.CreateXmlDeclaration("1.0", "UTF-8", null); var book = doc.CreateElement("book"); doc.AppendChild(docDec); doc.AppendChild(book); var edition = doc.CreateElement("edition"); edition.InnerText = "1.0"; var edition1 = doc.CreateElement("edition"); edition1.InnerText = "1.0"; var copyright = doc.CreateElement("copyright"); copyright.InnerText = "大汉网络"; book.AppendChild(edition); book.AppendChild(edition1); book.AppendChild(copyright); var wzOfCycle = (i == cs ? wzs.TakeLast(ys) : wzs.Skip((i - 1) * onceNumber).Take(onceNumber)); foreach (var wz in wzOfCycle) { var article = doc.CreateElement("article"); book.AppendChild(article); var title = doc.CreateElement("title"); title.InnerText = UseTemp(wz.Doctitle); var caption = doc.CreateElement("caption"); caption.InnerText = UseTemp(wz.Subdoctitle); var leadtitle = doc.CreateElement("leadtitle"); leadtitle.InnerText = UseTemp(); var linktitle = doc.CreateElement("linktitle"); linktitle.InnerText = UseTemp(); var href = doc.CreateElement("href"); href.InnerText = UseTemp(); var author = doc.CreateElement("author"); author.InnerText = UseTemp(wz.Docauthor); var source = doc.CreateElement("source"); source.InnerText = UseTemp(wz.Docsourcename); var userid = doc.CreateElement("userid"); userid.InnerText = UseTemp(); var editor = doc.CreateElement("editor"); editor.InnerText = UseTemp(wz.Doceditor); var keyword = doc.CreateElement("keyword"); keyword.InnerText = UseTemp(wz.Dockeywords); var classname = doc.CreateElement("classname"); classname.InnerText = UseTemp(); var describe = doc.CreateElement("describe"); describe.InnerText = UseTemp(wz.Docabstract); var text = doc.CreateElement("text"); text.InnerText = UseTemp(wz.Dochtmlcon); var datetime = doc.CreateElement("datetime"); datetime.InnerText = UseTemp(wz.Crtime?.ToString("yyyy-MM-dd HH:mm:ss")); var deploytime = doc.CreateElement("deploytime"); deploytime.InnerText = UseTemp(wz.Docpubtime?.ToString("yyyy-MM-dd HH:mm:ss")); var image = doc.CreateElement("image"); image.InnerText = UseTemp(IdentifyFileExt(wz.Srcfile, wz.Fileext, nameof(_image))); var flash = doc.CreateElement("flash"); flash.InnerText = UseTemp(IdentifyFileExt(wz.Srcfile, wz.Fileext, nameof(_flash))); var media = doc.CreateElement("media"); media.InnerText = UseTemp(); var attach = doc.CreateElement("attach"); attach.InnerText = UseTemp(IdentifyFileExt(wz.Srcfile, wz.Fileext, nameof(_attach))); article.AppendChild(title); article.AppendChild(caption); article.AppendChild(leadtitle); article.AppendChild(linktitle); article.AppendChild(href); article.AppendChild(author); article.AppendChild(source); article.AppendChild(userid); article.AppendChild(edition); article.AppendChild(keyword); article.AppendChild(classname); article.AppendChild(describe); article.AppendChild(text); article.AppendChild(datetime); article.AppendChild(deploytime); article.AppendChild(image); article.AppendChild(flash); article.AppendChild(media); article.AppendChild(attach); action?.Invoke(lm.Channelid,wz,doc,article); } doc.Save($"{lmPath}/{lm.Chnldesc}-文章-{i}.xml"); } string UseTemp(string info = "") { return $""; } string IdentifyFileExt(string file, string ext, string type) { file = $"/picture/old/{file}"; if (_image.Contains(ext) && type == nameof(_image)) return file; else if (_flash.Contains(ext) && type == nameof(_flash)) return file; else if (_attach.Contains(ext) && type == nameof(_attach)) return file; //else if (type == nameof(_attach)) return file; else return ""; } void GetNumberOfCycle(out int cs, out int ys) { int count = wzs.Count(); if (count == 0) { cs = 0; ys = 0; } else if (count <= onceNumber) { cs = 1; ys = count; } else { ys = count % onceNumber; cs = ((count - ys) / onceNumber) + 1; } } } } private void DeleteEmptyFloder(string path) { var floders = Directory.GetDirectories(path); foreach (string floder in floders) { var hasFile = Directory.GetFileSystemEntries(floder, "*.xml", SearchOption.AllDirectories).Count()>0; if (hasFile is false) Directory.Delete(floder,true); else DeleteEmptyFloder(floder); } } private void Replace(string path) { var files = Directory.GetFileSystemEntries(path, "*.xml", SearchOption.AllDirectories); foreach (var filePath in files) { String xmlStr = ""; using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8)) { xmlStr = reader.ReadToEnd(); } xmlStr = xmlStr.Replace("<", "<") .Replace(">", ">") .Replace("&", "&") .Replace("src=\"W", "src=\"/picture/old/W"); using StreamWriter readTxt = new StreamWriter(filePath, false, Encoding.UTF8); readTxt.Write(xmlStr); } } } }