123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- 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<IActionResult> 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<Join>(joinStr);
- foreach (var wz in wzData)
- {
- var wzNew = context.SqlQuery<Join>(string.Format(wzStr, wz.DocID)).First();
- wz.Covert(wzNew);
- }
- CreateXml(path,wzData);
- }
- /// <summary>
- /// 元数据
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- 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<Join>(joinStr);
- foreach (var wz in wzData)
- {
- var wzNew = context.SqlQuery<Join>(string.Format(wzStr, wz.DocID)).First();
- wz.Covert(wzNew);
- }
- var dics = await context.Query<Dictionary>().ToListAsync();
- var pzDics = dics.Where(dic => dic.Type == 1);
- var jdDics = dics.Where(dic => dic.Type == 2);
- Action<int,Join,XmlDocument,XmlElement> 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 $"<![CDATA[{info}]]>";
- }
- }
- private void CreateXml(string path,List<Join> wzData,Action<int,Join,XmlDocument,XmlElement> 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 $"<![CDATA[{info}]]>";
- }
- 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);
- }
-
- }
- }
- }
|