using System; using System.Web.UI.WebControls; using System.Data; using System.Linq; using System.Collections.Generic; using DocumentFormat.OpenXml.Spreadsheet; using System.Text; using System.Globalization; using Model; using System.Text.RegularExpressions; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public partial class admin_order_print : MyWeb.function { private Model.ezEntities _db = new Model.ezEntities(); public string body_class = "tablet-l"; protected void Page_Load(object sender, EventArgs e) { string bclass = Request["class"]; if (!String.IsNullOrEmpty(bclass)) { body_class= bclass; } if (!IsPostBack) { if (!isStrNull(Request["num"]) && !isStrNull(Request["detail"])) { int _detail = Val(Request["detail"]); var orderItem = _db.pro_order_detail.AsEnumerable().Where(q => q.num == _detail).FirstOrDefault(); int _num = Val(Request["num"]); var qry = _db.files.AsEnumerable(); var prod = qry.Where(q => q.num == _num).FirstOrDefault(); if (prod != null && orderItem !=null) { string _word = prod.word; //格式_列印檔html if (_word == null) _word = ""; string file_cuz_column = prod.customize_data ?? ""; //格式->品項->活動->信眾->報名->報名明細 //上至下層依序讀入各自定義欄位:名稱及值 List newStrList = new List(); //參數集合((名稱及值​ if (!string.IsNullOrEmpty(file_cuz_column)) { proCuzData(newStrList, file_cuz_column); } if (orderItem.actItem_num.HasValue && !string.IsNullOrEmpty(orderItem.actItem.customize_data)) { proCuzData(newStrList, orderItem.actItem.customize_data); } if (orderItem.pro_order.activity_num.HasValue && !string.IsNullOrEmpty(orderItem.pro_order.activity.customize_data)) { proCuzData(newStrList, orderItem.pro_order.activity.customize_data); } //if (orderItem.f_num.HasValue && !string.IsNullOrEmpty(orderItem.follower.customize_data)) if (orderItem.pro_order.f_num.HasValue && !string.IsNullOrEmpty(orderItem.pro_order.follower.customize_data))//訂單主黨的姓名/名稱? { proCuzData(newStrList, orderItem.pro_order.follower.customize_data); } if (!string.IsNullOrEmpty(orderItem.pro_order.customize_data)) { proCuzData(newStrList, orderItem.pro_order.customize_data); } if (!string.IsNullOrEmpty(orderItem.customize_data)) { proCuzData(newStrList, orderItem.customize_data); } //string detail_data = orderItem.customize_data ?? ""; //訂單明細的自訂欄位 string detail_data = newStrList.Count > 0 ? string.Join("\r\n", newStrList) : "";//參數集合((名稱及值​ string[] k = detail_data.Split('$'); //取代 for (int i=0; i < k.Length; i++) { try{ if (!isStrNull(k[i])) { //string[] _v = k[i].Split(':'); string[] _v = k[i].Split(new char[] { ':' }, 2); // 只分割第一次出現的冒號 _word = _word.Replace("{$"+ _v [0]+ "}", _v[1].Trim()); } } catch (Exception ex) { } } //加入固定欄位變數定義 //日期 string[] _type = { "zh", "min", "lunar" }; _word = _word.Replace("{pro_order.up_time}", !isStrNull(orderItem.pro_order.up_time)? proDateStr(orderItem.pro_order.up_time.Value,""):""); foreach (var tt in _type) { _word = _word.Replace("{pro_order.up_time." + tt + "}", !isStrNull(orderItem.pro_order.up_time) ? proDateStr(orderItem.pro_order.up_time.Value, tt) : ""); } if (orderItem.pro_order.activity!=null) { _word = _word.Replace("{activity.startDate_solar}", !isStrNull(orderItem.pro_order.activity.startDate_solar) ? proDateStr(orderItem.pro_order.activity.startDate_solar.Value, "") : ""); foreach (var tt in _type) { _word = _word.Replace("{activity.startDate_solar." + tt + "}", !isStrNull(orderItem.pro_order.activity.startDate_solar) ? proDateStr(orderItem.pro_order.activity.startDate_solar.Value, tt) : ""); } _word = _word.Replace("{activity.endDate_solar}", !isStrNull(orderItem.pro_order.activity.endDate_solar) ? proDateStr(orderItem.pro_order.activity.endDate_solar.Value, "") : ""); foreach (var tt in _type) { _word = _word.Replace("{activity.endDate_solar." + tt + "}", !isStrNull(orderItem.pro_order.activity.endDate_solar) ? proDateStr(orderItem.pro_order.activity.endDate_solar.Value, tt) : ""); } } //金額 _word = _word.Replace("{​​​​​​​pro_order_detail.price}", !isStrNull(orderItem.price) ? ValMoneyCh(orderItem.price.Value) : ""); try { //訂單編號 _word = _word.Replace("{pro_order.order_no}", !isStrNull(orderItem.order_no) ? orderItem.order_no : ""); //信眾編號 _word = _word.Replace("{follower.f_number}", !isStrNull(orderItem.pro_order.follower.f_number) ? orderItem.pro_order.follower.f_number : ""); string tablet_json = orderItem.f_num_tablet; var tablet_json_obj = getTabletJson(tablet_json); //牌位中間標題(消災/超薦) //_word = _word.Replace("{orderItem.f_num_tablet}", !isStrNull(orderItem.f_num_tablet) ? orderItem.f_num_tablet : "");//牌位標題, 不去除全型空白 _word = _word.Replace("{orderItem.f_num_tablet}", tablet_json_obj[0]); //牌位左側標題(陽上/報恩) //_word = _word.Replace("{orderItem.from_id_tablet}", !isStrNull(orderItem.from_id_tablet) ? orderItem.from_id_tablet : ""); _word = _word.Replace("{orderItem.from_id_tablet}", tablet_json_obj[1]); //牌位編號 _word = _word.Replace("{orderItem.print_id}", !isStrNull(orderItem.print_id) ? orderItem.print_id : ""); } catch (Exception ex) { } _word=ReplacePlaceholders(_word); word.Text = _word; } } else { Response.Clear(); Response.StatusCode = 404; Response.End(); } } } protected string[] getTabletJson(string tablet_json) { string[] ret = new string[2]; try { dynamic jsonObj = JsonConvert.DeserializeObject(tablet_json); // 檢查 mid_items 是否為空 ret[0] = (jsonObj.mid_items != null && jsonObj.mid_items.Count > 0) ? JoinText(jsonObj.mid_items, 4) : ""; // 檢查 left_items 是否為空 ret[1] = (jsonObj.left_items != null && jsonObj.left_items.Count > 0) ? JoinText(jsonObj.left_items) : ""; } catch (Exception ex) { var msg = ex.Message; ret[0] = "??:" + msg; ret[1] = "??"; } return ret; } private string JoinText(JArray items, int addFullSpace = 0) { if (items == null || items.Count == 0) return string.Empty; StringBuilder result = new StringBuilder(); for (int i = 0; i < items.Count; i++) { try { string fam_name = items[i]["fam_name"].ToString().Trim(); if (addFullSpace > 0 && items.Count == 1 && fam_name.Length <= addFullSpace) { // Add full-width space ( ) before each character // fam_name = string.Join("", fam_name.Select(c => " " + c)); // 先改用JS處理 } result.Append(fam_name); // Add break if option_break is true and not the last item if (items[i]["option_break"].Value() && i < items.Count - 1) { result.Append("
"); } // Add space between names if not the last item and no break else if (i < items.Count - 1) { result.Append(" "); } } catch (Exception ex) { result.Append("??" + ex.Message); } } result = result.Replace("|", "
"); return result.ToString(); } #region 自訂參數 protected void proCuzData(List _column, string this_data) { if (!string.IsNullOrWhiteSpace(this_data)) { string[] _list = this_data.Split('$'); for (int i = 0; i < _list.Length; i++) { if (!string.IsNullOrWhiteSpace(_list[i])) { string[] _v = _list[i].Split(':'); //比對 bool isExist = false; //參數是否已存在 for (int j = 0; j < _column.Count; j++) { string[] _y = _column[j].Split(':'); if ("$" + _v[0] == _y[0]) { isExist = true; if (!string.IsNullOrWhiteSpace(_v[1])) _column[j] = "$" + _list[i]; break; } } if (!isExist) { //加入參數 _column.Add("$" + _list[i]); } } } } } private static string ReplacePlaceholders(string input) { string pattern = @"\{\$[^\}]+\}"; //return Regex.Replace(input, pattern, ""); return input; } #endregion #region 日期文字處理 protected string proDateStr(DateTime date, string type ) { string txt = ""; if (type == "") { txt = date.ToString("yyyy/MM/dd"); } else if(type == "zh")//西元中文日期 { //将日期转换成中文显示 string year = date.Year.ToString(); string month = date.Month.ToString("00"); //if (int.Parse(month) < 10) { month = "0" + month; } string days = date.Day.ToString("00"); //if (int.Parse(days) < 10) { days = "0" + days; } txt = GetChineseDateNumber2(year, month, days); } else if (type == "min")//民國中文日期 { //轉成民國年 CultureInfo culture = new CultureInfo("zh-TW"); culture.DateTimeFormat.Calendar = new TaiwanCalendar(); string _cul= date.ToString("yyy/MM/dd", culture); string[] k = _cul.Split('/'); //System.Globalization.TaiwanCalendar tc = new System.Globalization.TaiwanCalendar(); //tc.GetYear(date), tc.GetMonth(date), tc.GetDayOfMonth(date) if (k.Length == 3) { txt = GetChineseDateNumber2(k[0], k[1], k[2]); } } else if (type == "lunar")//民國年農曆日期 { //轉成農曆年 ChineseLunisolarCalendar chineseDate = new ChineseLunisolarCalendar(); int yy = chineseDate.GetYear(date) - 1911; int mm = chineseDate.GetMonth(date); int dd = chineseDate.GetDayOfMonth(date); txt = GetChineseDateNumber2(yy.ToString(), mm.ToString("00"), dd.ToString("00")); } return txt; } static string GetChineseDateNumber2(string year, string month, string days) { //StringBuilder sb = new StringBuilder(); List sb = new List(); for (int i = 0; i < year.Length; i++)//年转换 { //sb.Append(GetChineseDateNumber(int.Parse(year[i].ToString()))); sb.Add(GetChineseDateNumber(int.Parse(year[i].ToString()))); } sb.Add("年"); for (int r = 0; r < month.Length; r++)//月转换 { sb.Add(GetChineseDateNumber(int.Parse(month[r].ToString()))); } sb.Add("月"); for (int t = 0; t < days.Length; t++)//日转换 { sb.Add(GetChineseDateNumber(int.Parse(days[t].ToString()))); } sb.Add("日"); //return sb.ToString(); return string.Join("", sb); } static string GetChineseDateNumber(int dateStr) { string[] cns = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十", "二十一", "二十二", "二十三", "二十四", "二十五", "二十六", "二十七", "二十八", "二十九", "三十", "三十一" }; return cns[dateStr]; } #endregion }