Files
17168ERP/web/admin/printpw/index.aspx.cs
2025-08-29 01:27:25 +08:00

413 lines
16 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DocumentFormat.OpenXml.ExtendedProperties;
using System.Data;
using Model;
public partial class admin_printpw_index : MyWeb.function
{
private Model.ezEntities _db = new Model.ezEntities();
private string previousOrderno = "";
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write("item:"+Request["item"]+"<br>");
//Response.Write("file:"+Request["file"]+"<br>");
//Response.Write("list:"+Request["list"]+"<br>");
//if (!IsPostBack)
//{
this.Title = "預覽牌位";
if (Request.HttpMethod == "POST")
{
/*if (!String.IsNullOrEmpty(Request["item"]) &&
!String.IsNullOrEmpty(Request["file"]) &&
!String.IsNullOrEmpty(Request["list"])
)*/
if (!String.IsNullOrEmpty(Request["order_no"]))
{
var order_no = Request["order_no"];
var oderList = _db.pro_order_detail
.Where(u => u.order_no == order_no && u.printed_files != null)
.Select(u => new { detail_num = u.num, actItem_num = u.actItem_num }).ToArray();
Repeater1.DataSource = oderList;
Repeater1.DataBind();
}
else if (!String.IsNullOrEmpty(Request["activity_num"]))
{
//var _details = Newtonsoft.Json.JsonConvert.DeserializeObject<int[]>(Request["list"]);
//string json = "";
//using (System.IO.StreamReader oSR = new System.IO.StreamReader(Request.InputStream))
// json = oSR.ReadToEnd();
int activity_num = Convert.ToInt32(Request["activity_num"]);
//Repeater1.DataSource = _details;
var orderList = _db.pro_order.Where(u => u.activity_num == activity_num).Select(u => u.order_no).ToList();
var gdzOrderList = _db.pro_order_detail.Where(u => orderList.Contains(u.order_no) && u.print_id.Contains("主") ).Select(u=>u.order_no).Distinct().ToArray();
var datalist = _db.pro_order_detail
.Where(u => gdzOrderList.Contains(u.order_no) && u.print_id.Contains("主") && u.parent_num != null && u.printed_files != null)
.OrderBy(o => o.order_no)
.ThenBy(o => o.actItem_num)
.ThenBy(o=>o.print_id)
.Select(u => new {detail_num=u.num ,actItem_num=u.actItem_num})
.ToArray();
//List<int> ints = new List<int>();
//ints.Add(12133);
Repeater1.DataSource = datalist;
Repeater1.DataBind();
if (!String.IsNullOrEmpty(Request["title"]))
{
this.Title += " - " + Request["title"];
}
}
else
{
Response.Clear();
Response.StatusCode = 404;
Response.End();
}
}
//}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//int _detail = (int)e.Item.DataItem;
dynamic dn = e.Item.DataItem;
int _detail = dn.detail_num;
int actItem_num = dn.actItem_num;
//var orderItem = _db.pro_order_detail.AsEnumerable().Where(q => q.num == _detail).FirstOrDefault();
var orderItem = _db.pro_order_detail.Include("follower").Where(q => q.num == _detail).FirstOrDefault();
var actItem = _db.actItems.Where(q => q.num == actItem_num).FirstOrDefault()?.subject;
int _num = Convert.ToInt32(Request["file"]);
var follower = _db.pro_order.Where(u => u.order_no == orderItem.order_no).FirstOrDefault().follower;
//_num = 27;
_num = 30;
if (orderItem != null) {
if (actItem.Contains("超"))
{
//_num = 35;
_num = 38;
}
}
var qry = _db.files.AsEnumerable();
var prod = qry.Where(q => q.num == _num).FirstOrDefault();
if (prod != null && orderItem != null)
{
string _word = prod.word;
string file_cuz_column = prod.customize_data ?? "";
//格式->品項->活動->信眾->報名->報名明細
//上至下層依序讀入各自定義欄位:名稱及值
List<string> newStrList = new List<string>(); //參數集合((名稱及值​
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);
}
if (!string.IsNullOrEmpty(orderItem.print_id))
{
//proCuzData(newStrList, orderItem.print_id);
}
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 (!String.IsNullOrEmpty(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 : "");
_word = _word.Replace("個",follower.u_name);
}
catch (Exception ex)
{
}
_word = ReplacePlaceholders(_word);
/*if(e.Item.ItemIndex > 0 && e.Item.ItemIndex %4 == 0)
{
_word += "<div class=\"page-break\"></div>";
}*/
if(previousOrderno == "")
{
previousOrderno = orderItem.order_no;
}
else
{
if(previousOrderno != orderItem.order_no)
{
_word = "<div class=\"page-break\"></div>" + _word;
previousOrderno = orderItem.order_no;
}
}
((Literal)e.Item.FindControl("word")).Text = _word;
}
}
protected string[] getTabletJson(string tablet_json)
{
string[] ret = new string[2];
try
{
dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(tablet_json);
// 檢查 mid_items 是否為空
ret[0] = (jsonObj.mid_items != null && jsonObj.mid_items.Count > 0)
? JoinText(jsonObj.mid_items)
: "";
// 檢查 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)
{
if (items == null || items.Count == 0)
return string.Empty;
StringBuilder result = new StringBuilder();
for (int i = 0; i < items.Count; i++)
{
result.Append(items[i]["fam_name"]);
// Add break if option_break is true and not the last item
if (items[i]["option_break"].Value<bool>() && i < items.Count - 1)
{
result.Append("<br>");
}
// Add space between names if not the last item and no break
else if (i < items.Count - 1)
{
result.Append(" ");
}
}
return result.ToString();
}
protected void proCuzData(List<string> _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, "");
}
#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<string> sb = new List<string>();
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
}