From 681d5293a63bf13cdb06b105c5f2046a5c2c4573 Mon Sep 17 00:00:00 2001 From: minom Date: Tue, 14 Jul 2026 17:59:45 +0800 Subject: [PATCH] =?UTF-8?q?=E7=96=8F=E6=96=87=E7=94=A2=E7=94=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/App_Code/api/orderdetailController.cs | 346 +++++++++++++++++- web/admin/print/shuwen.aspx | 8 +- web/admin/print/tablet_edit/editorNewOne.html | 8 +- web/packages.config | 6 +- 4 files changed, 346 insertions(+), 22 deletions(-) diff --git a/web/App_Code/api/orderdetailController.cs b/web/App_Code/api/orderdetailController.cs index 6e8f030..012cc2a 100644 --- a/web/App_Code/api/orderdetailController.cs +++ b/web/App_Code/api/orderdetailController.cs @@ -1,18 +1,26 @@ using com.itextpdf.text.pdf; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; using Model; using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using NPOI.POIFS.Properties; using NPOI.Util.Optional; +using NPOI.XWPF.UserModel; using PagedList; using System; using System.Collections; using System.Collections.Generic; using System.Data.Entity; using System.Data.SqlClient; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; +using System.Web; using System.Web.Http; +using System.Windows; using static TreeView; /// @@ -22,6 +30,7 @@ public class orderdetailController:ApiController { private Model.ezEntities _db = new Model.ezEntities(); + [HttpPost] [Route("api/orderdetail/GetList")] public IHttpActionResult GetList([FromBody] Model.ViewModel.pro_order q, int page, int pageSize = 10, string sortBy = "", bool sortDesc = false) @@ -105,25 +114,34 @@ public class orderdetailController:ApiController public IHttpActionResult GetShuwenData([FromBody] dynamic data) { var json = data; + var mode = (json.mode == null ? "" : (string)json.mode); var num = (json.activity_num == null ? 0 : (int)json.activity_num); + + var act = _db.activities.AsQueryable().Where(v=>v.num==num).FirstOrDefault(); + var query = _db.pro_order_detail.AsQueryable(); query=query.Where(x => (_db.act_bom.AsQueryable().Where(y => y.package_num == null).Select(y => y.item_num).ToList()). Contains(x.actItem_num)); query = query.Where(x => (_db.pro_order.AsQueryable(). Where(w => w.activity_num ==num ).Select(w => w.order_no)).ToList().Contains(x.order_no)); + //query = query.Include(x=>x.actItem).Where(x=>x.actItem.subject.StartsWith("消")); + //這些是功德主 var list = query.OrderByDescending(x=>x.price).Select(x => new { x.num,x.order_no,x.actItem_num,x.parent_num,x.print_id,x.f_num_tablet,x.price, - childs=_db.pro_order_detail.Where(z=>z.order_no==x.order_no&&x.parent_num==x.actItem_num).Select(z=>new + subject= _db.actItems.Where(w => w.num == x.actItem_num).Select(w => w.subject).FirstOrDefault(), + childs =_db.pro_order_detail. + Where(z=>z.order_no==x.order_no&&z.parent_num==x.num).Select(z=>new { - x.num, - x.order_no, - x.actItem_num, - x.parent_num, - x.print_id, - x.f_num_tablet, - x.price + z.num, + z.order_no, + z.actItem_num, + z.parent_num, + z.print_id, + z.f_num_tablet, + z.price, + subject=_db.actItems.Where(w=>w.num==z.actItem_num).Select(w=>w.subject).FirstOrDefault(), }).ToList() }).ToList(); @@ -135,8 +153,9 @@ public class orderdetailController:ApiController query1 = query1.Where(x => x.parent_num == null); query1 = query1.Where(x => (_db.pro_order.AsQueryable(). Where(w => w.activity_num == num).Select(w => w.order_no)).ToList().Contains(x.order_no)); + query1 = query1.Include(x => x.actItem).Where(x => x.actItem.subject.StartsWith(mode=="消"?"消":"超")); //這些是非功德主 - var list1 = query1.OrderByDescending(x => x.price).Select(x => new + var list1 = query1.Select(x => new { x.num, x.order_no, @@ -144,10 +163,315 @@ public class orderdetailController:ApiController x.parent_num, x.print_id, x.f_num_tablet, - x.price + x.price, + subject = _db.actItems.Where(w => w.num == x.actItem_num).Select(w => w.subject).FirstOrDefault(), }).ToList(); - return Ok(new {result="Y",data=new { list=list,list1=list1 } }); + //string filePath = "範例文件.docx"; + string folderPath = HttpContext.Current.Server.MapPath("~/App_Data/"); + + // 確保資料夾存在 + if (!Directory.Exists(folderPath)) + { + Directory.CreateDirectory(folderPath); + } + + // 2. 使用 GUID 命名防衝突 + string fileName = Guid.NewGuid().ToString() + ".docx"; + string fullPath = Path.Combine(folderPath, fileName); + + if (mode == "消") + { + // 1. 建立 Word 文件 + using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(fullPath, WordprocessingDocumentType.Document, true)) + { + Body body = new Body(); + var doc = GenerateDocument(wordDocument, body); + string preItemName = string.Empty; + + foreach (var dd in list) + { + if (dd.childs.Count > 0) + { + if (preItemName != dd.subject) + { + body.Append(RenderParagraph(44, $"{act.subject}{dd.subject} 消災祈福名單", true)); + } + if (string.IsNullOrEmpty(preItemName)) + { + preItemName = dd.subject; + } + List preItemTitle = new List(); + foreach (var child in dd.childs) + { + if (!child.subject.StartsWith("消")) + { + continue; + } + //preItemTitle.Append(child) + JObject obj = JObject.Parse(child.f_num_tablet); + JArray arr = (JArray)obj["mid_items"]; + foreach (var item in arr) + { + + preItemTitle.Add((string)item["fam_name"]); + } + } + + body.Append(RenderParagraph(44, $" {string.Join(" ", preItemTitle)}", false)); + } + } + //ChangePage(body); + //if (list1.Count > 0) + //{ + // Hashtable ht = new Hashtable(); + // string preOrderNo = string.Empty; + // body.Append(RenderParagraph(44, $"{act.subject} 消災祈福名單", true)); + // List preItemTitle = new List(); + // foreach (var dd in list1) + // { + // if (preOrderNo != dd.order_no) + // { + + // if (preItemTitle.Count > 0) + // { + // body.Append(RenderParagraph(44, $" {string.Join(" ", preItemTitle)}", false)); + // } + // ht.Clear(); + // preItemTitle.Clear(); + // } + // JObject obj = JObject.Parse(dd.f_num_tablet); + // JArray arr = (JArray)obj["mid_items"]; + // foreach (var item in arr) + // { + // if (ht.ContainsKey((string)item["fam_name"])) + // { + // continue; + // } + // if (string.Join(" ", preItemTitle).Length + ((string)item["fam_name"]).Length > 25) + // { + // body.Append(RenderParagraph(44, $" {string.Join(" ", preItemTitle)}", false)); + // preItemTitle.Clear(); + // } + // preItemTitle.Add((string)item["fam_name"]); + // ht.Add((string)item["fam_name"], (string)item["fam_name"]); + + // } + // preOrderNo = dd.order_no; + // } + // if (preItemTitle.Count > 0) + // { + // body.Append(RenderParagraph(44, $" {string.Join(" ", preItemTitle)}", false)); + // } + //} + SetStraight(body); + } + }else if (mode=="超") + { + List shuwenList= new List(); + Shuwen shuwen = new Shuwen(); + string preOrderNo=string.Empty; + foreach (var dd in list) + { + if (preOrderNo!=dd.order_no) + { + //if(!string.IsNullOrEmpty(shuwen.order_no)&&shuwen.mid_item.Count>0) + // shuwenList.Add(shuwen); + shuwen = new Shuwen(); + shuwen.order_no = dd.order_no; + shuwen.subject = dd.subject; + shuwen.mid_item = new List(); + } + if (dd.childs.Count > 0) + { + foreach (var item in dd.childs) + { + if (!item.subject.StartsWith("超")) + { + continue; + } + + JObject obj = JObject.Parse(item.f_num_tablet); + JArray mid = (JArray)obj["mid_items"]; + JArray left = (JArray)obj["left_items"]; + List leftList=new List(); + List midList = new List(); + foreach (var o in mid) + { + midList.Add((string)o["fam_name"]); + } + + foreach (var o in left) + { + leftList.Add((string)o["fam_name"]); + } + + var wen=shuwenList.Where(z => z.alive == string.Join(" ", leftList)&&z.order_no==dd.order_no).FirstOrDefault(); + if (wen != null&&wen.alive== string.Join(" ", leftList)) + { + wen.mid_item.AddRange(midList); + shuwen = new Shuwen(); + shuwen.order_no = dd.order_no; + shuwen.subject = dd.subject; + shuwen.mid_item = new List(); + } + else + { + shuwen.alive = string.Join(" ", leftList); + shuwen.mid_item.AddRange(midList); + shuwenList.Add(shuwen); + } + } + //if (shuwen.mid_item.Count > 0) + //{ + // shuwenList.Add(shuwen); + //} + + } + preOrderNo = dd.order_no; + } + + using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(fullPath, WordprocessingDocumentType.Document, true)) + { + Body body = new Body(); + var doc = GenerateDocument(wordDocument, body); + string preItem = string.Empty; + foreach(var shu in shuwenList) + { + if (preItem != shu.subject) + { + body.Append(RenderParagraph(44, $"{act.subject}{shu.subject} 超薦名單", true)); + } + body.Append(RenderParagraph(44, "佛力超薦", true)); + //foreach (var wen in shu.mid_item) + //{ + // body.Append(RenderParagraph(44, $" {wen}", false)); + //} + //這裡要改切段落 + List para= new List(); + foreach (var p in shu.mid_item) + { + if (string.Join(" ",para).Length+p.Length>25) + { + body.Append(RenderParagraph(44, $"{string.Join(" ", para)}", false)); + para.Clear(); + } + para.Add(p.ToString()); + } + if (para.Count>0) + { + body.Append(RenderParagraph(44, $"{string.Join(" ", para)}", false)); + para.Clear(); + } + + body.Append(RenderParagraph(44, $"陽上報恩人", true)); + body.Append(RenderParagraph(44, $"{shu.alive}", false)); + + preItem=shu.subject; + } + + SetStraight(body); + } + } + + return Ok(new { result = "Y", data = new { list = list, list1 = list1 } }); } + + public void ChangePage(Body body) + { + Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00A56886", RsidRunAdditionDefault = "00D919D0" }; + + Run pageBreakRun = new Run(); + pageBreakRun.Append(new Break() { Type = BreakValues.Page }); // 指定 Break 類型為 Page (換頁) + paragraph1.Append(pageBreakRun); + + body.Append(paragraph1); + } + + public WordprocessingDocument GenerateDocument(WordprocessingDocument document1,Body body) + { + MainDocumentPart mainPart = document1.AddMainDocumentPart(); + mainPart.Document = new DocumentFormat.OpenXml.Wordprocessing.Document(); + + mainPart.Document.AppendChild(body); + + + return document1; + } + + public void SetStraight(Body body) + { + + SectionProperties sectionProperties1 = new SectionProperties() { RsidR = "00A56886", RsidSect = "00D919D0" }; + PageSize pageSize1 = new PageSize() { Width = 16838U, Height = 11906U, Orient = PageOrientationValues.Landscape }; + PageMargin pageMargin1 = new PageMargin() { Top = 1440, Right = 1440U, Bottom = 1440, Left = 1440U, Header = 720U, Footer = 720U, Gutter = 0U }; + Columns columns1 = new Columns() { Space = "720" }; + TextDirection textDirection3 = new TextDirection() { Val = TextDirectionValues.TopToBottomRightToLeft }; + DocGrid docGrid1 = new DocGrid() { LinePitch = 326 }; + + sectionProperties1.Append(pageSize1); + sectionProperties1.Append(pageMargin1); + sectionProperties1.Append(columns1); + sectionProperties1.Append(textDirection3); + sectionProperties1.Append(docGrid1); + body.Append(sectionProperties1); + } + + + public Paragraph RenderParagraph(int fontSize,string content,bool isBold) + { + Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "00A56886", RsidRunAdditionDefault = "00D919D0" }; + + ParagraphProperties paragraphProperties2 = new ParagraphProperties(); + TextDirection textDirection2 = new TextDirection() { Val = TextDirectionValues.TopToBottomRightToLeft }; + + + // 2. 建立縮排物件 + Indentation indent = new Indentation(); + + // --- 請依需求選擇以下一種縮排方式設定 --- + + // 懸首縮排 (Hanging): 第一行不動,其餘行縮排。 (1公分約為 567 dxa) + indent.Left = "1134"; // 整體左縮排量 + indent.Hanging = "1134"; + indent.FirstLine = "1134"; + paragraphProperties2.Append(indent); + + paragraphProperties2.Append(textDirection2); + + Run run2 = new Run(); + + RunProperties runProperties2 = new RunProperties(); + RunFonts runFonts2 = new RunFonts() { Hint = FontTypeHintValues.EastAsia, Ascii = "微軟正黑體", EastAsia = "微軟正黑體" }; + FontSize fontSize2 = new FontSize() { Val = $"{fontSize}" }; + + runProperties2.Append(runFonts2); + runProperties2.Append(fontSize2); + if (isBold) + { + runProperties2.Append(new Bold()); + } + Text text2 = new Text(); + text2.Space = SpaceProcessingModeValues.Preserve; + text2.Text = $"{content}"; + + run2.Append(runProperties2); + run2.Append(text2); + paragraph2.Append(paragraphProperties2); + paragraph2.Append(run2); + + + return paragraph2; + } + +} + +public class Shuwen +{ + public Shuwen() { } + public string order_no { get; set; } + public List mid_item { get; set; } + public string alive { get; set; } + public string subject { get; set; } } \ No newline at end of file diff --git a/web/admin/print/shuwen.aspx b/web/admin/print/shuwen.aspx index 052aab3..6405f72 100644 --- a/web/admin/print/shuwen.aspx +++ b/web/admin/print/shuwen.aspx @@ -17,8 +17,8 @@
- - + + @@ -169,10 +169,10 @@ } }, methods: { - getShuwen() { + getShuwen(mode) { let curr = this.search_dialog.current; let target = $(`[data-search-control=${curr.id}]`) - let searchData = { activity_num: target.children("input:hidden").val() } + let searchData = { activity_num: target.children("input:hidden").val(),mode:mode } axios .post(HTTP_HOST + 'api/orderdetail/GetShuwenData', searchData) .then(response => { diff --git a/web/admin/print/tablet_edit/editorNewOne.html b/web/admin/print/tablet_edit/editorNewOne.html index b7048e5..3d63ab2 100644 --- a/web/admin/print/tablet_edit/editorNewOne.html +++ b/web/admin/print/tablet_edit/editorNewOne.html @@ -216,8 +216,8 @@ { name: "超薦2", path: "../../../admin/print/html/tablet-1B_new.svg" }, { name: "消災1", path: "../../../admin/print/html/tablet-2.svg" }, { name: "消災2", path: "../../../admin/print/html/tablet-2B.svg" }, - { name: "消災3", path: "../../admin/print/html/tablet-2C.svg" }, - { name: "消災4", path: "../../admin/print/html/tablet-2D.svg" } + { name: "消災3", path: "../../../admin/print/html/tablet-2C.svg" }, + { name: "消災4", path: "../../../admin/print/html/tablet-2D.svg" } ], async init() { // 載入常用片語 @@ -239,6 +239,7 @@ this.defaultStyle = response.data.data.defaultStyle; this.selected.style = response.data.data.defaultStyle; this.selectStyle.style = response.data.data.defaultStyle; + console.log("GetActItem:",this.defaultStyle) $("#styleSel").val(this.defaultStyle); //this.getStyle(); @@ -333,11 +334,12 @@ if (response.status == 200) { self.printData = response.data; self.printData.forEach(x => { + console.log("GetDetailToPrint:",x.style); if (x.style != null && x.style != undefined) { self.selectStyle.style = x.style; self.selected.style = x.style; self.defaultStyle = x.style; - $("#styleSel").val(x.style) + //$("#styleSel").val(x.style) } if (x.f_num_tablet != "") { let tablet = JSON.parse(x.f_num_tablet); diff --git a/web/packages.config b/web/packages.config index 6ce4a15..2f0bec8 100644 --- a/web/packages.config +++ b/web/packages.config @@ -1,18 +1,16 @@  + + - - - -