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

267 lines
12 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Linq;
using static TreeView;
public partial class admin_follower_index : MyWeb.config
{
public int page = 1;
private Model.ezEntities _db = new Model.ezEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BuildKind();
}
else
{
}
}
#region
public void BuildKind()
{
//國籍
s_country.Items.Clear();
s_country.Items.Add(new ListItem("請選擇", ""));
var qry =_db.countries.AsEnumerable().OrderBy(x => x.range).ThenBy(x => x.name_en).ToList();
if (qry.Count > 0)
{
foreach(var x in qry)
s_country.Items.Add(new ListItem(x.name_zh, x.ID));
}
//var qry1 = new TreeView().get_data2(_db.countries.AsEnumerable().Select(o => new TreeItem()
//{
// kind = o.name_zh,
// num = Convert.ToInt32( o.ID),
// root = 0,
// range = o.range,
//}).OrderBy(x => x.range).ThenBy(x => x.kind).ToList(), 0, 0);
//if (qry1.Count() > 0)
// foreach (var qq in qry1)
// s_country.Items.Add(new ListItem(new TreeView().RptDash(qq.Level) + qq.kind, qq.num.ToString()));
}
#endregion
#region Excel
protected void excel_Click(object sender, EventArgs e)
{
var memoryStream = new MemoryStream();
using (var doc = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook))
{
var wb = doc.AddWorkbookPart();
wb.Workbook = new Workbook();
var sheets = wb.Workbook.AppendChild(new Sheets());
//建立第一個頁籤
var ws = wb.AddNewPart<WorksheetPart>();
ws.Worksheet = new Worksheet();
sheets.Append(new Sheet()
{
Id = wb.GetIdOfPart(ws),
SheetId = 1,
Name = "信眾資料"
});
//設定欄寬
var cu = new Columns();
cu.Append(
new Column { Min = 1, Max = 2, Width = 15, CustomWidth = true },
new Column { Min = 3, Max = 3, Width = 10, CustomWidth = true },
new Column { Min = 4, Max = 11, Width = 15, CustomWidth = true },
new Column { Min = 12, Max = 12, Width = 25, CustomWidth = true },
new Column { Min = 13, Max = 13, Width = 8, CustomWidth = true },
new Column { Min = 14, Max = 14, Width = 35, CustomWidth = true },
new Column { Min = 15, Max = 16, Width = 15, CustomWidth = true }
);
ws.Worksheet.Append(cu);
//建立資料頁
var sd = new SheetData();
ws.Worksheet.AppendChild(sd);
//第一列資料
var tr = new Row();
tr.Append(
new Cell() { CellValue = new CellValue("信眾編號"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("信眾姓名"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("性別"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("身分別"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("生日(國曆)"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("聯絡電話"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("手機號碼"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("皈依道場"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("皈依法名"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("皈依日期"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("加入日期"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("緊急連絡人"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("緊急連絡人電話"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("血型"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("國籍"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("收件地址"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("介紹人"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("LINE帳號"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("其它社群帳號"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("建檔日期"), DataType = CellValues.String }
,new Cell() { CellValue = new CellValue("身分證號"), DataType = CellValues.String }
//,new Cell() { CellValue = new CellValue("SHA"), DataType = CellValues.String }
);
sd.AppendChild(tr);
//查詢要匯出的資料
//紀錄匯出條件
string _query = "";
var list = searchData(ref _query);
if (list.Count > 0)
{
MyWeb.encrypt encrypt = new MyWeb.encrypt();
Model.country country = new Model.country();
var tdesc = publicFun.enum_desc<Model.follower.type>();
foreach (var item in list)
{
//新增資料列
tr = new Row();
string s1, s2, sha;
s1= encrypt.DecryptAutoKey(item.phone);
s2= encrypt.DecryptAutoKey(item.id_code);
sha = encrypt.followerHash(s1, s2);
tr.Append(
new Cell() { CellValue = new CellValue(item.f_number), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.u_name), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.sex), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(!isStrNull(item.identity_type)? tdesc[item.identity_type ?? 1] :""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.birthday.HasValue ? ValDate(item.birthday.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.phone)), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.cellphone)), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.refuge_area), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.refuge_name), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.refugedate.HasValue ? ValDate(item.refugedate.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.join_date.HasValue ? ValDate(item.join_date.Value).ToString("yyyy/MM/dd") : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.contactor), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.contactor_phone)), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.blood), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(!isStrNull(item.country) ? item.country1.name_zh : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.address), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.introducer), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.socialid1), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.socialid2), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.reg_time.HasValue ? ValDate(item.reg_time.Value).ToString("yyyy/MM/dd HH:mm:ss") : ""), DataType = CellValues.String }
,new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.id_code)), DataType = CellValues.String }
//, new Cell() { CellValue = new CellValue(sha), DataType = CellValues.String }
);
sd.AppendChild(tr);
}
//空一列
tr = new Row();
sd.AppendChild(tr);
//匯出資訊
string _data = "匯出時間 : " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
_data += " " + admin.info.u_id;
tr = new Row();
tr.Append(
new Cell() { CellValue = new CellValue(_data), DataType = CellValues.String }
);
sd.AppendChild(tr);
_data = "匯出條件 : " + (!isStrNull(_query) ? _query : "-");
tr = new Row();
tr.Append(
new Cell() { CellValue = new CellValue(_data), DataType = CellValues.String }
);
sd.AppendChild(tr);
Model.admin_log admin_log = new Model.admin_log();
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Follower, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => x.f_number + x.u_name).ToList()));
}
else
{
ScriptMsg2("查無資料");
}
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=信眾_data.xlsx");
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.End();
}
protected List<Model.follower> searchData(ref string _query)
{
//查詢要匯出的資料
var qry = _db.followers.AsEnumerable();
//紀錄匯出條件
if (!isStrNull(s_f_number.Value))
{
qry = qry.Where(o => o.f_number.Contains(s_f_number.Value.Trim()));
_query += "信眾編號:" + s_f_number.Value.Trim() + "\n";
}
if (!isStrNull(s_u_name.Value))
{
qry = qry.Where(o => o.u_name.Contains(s_u_name.Value.Trim()));
_query += "信眾姓名:" + s_u_name.Value.Trim() + "\n";
}
if (!isStrNull(s_address.Value))
{
qry = qry.Where(o => o.address.Contains(s_address.Value.Trim()));
_query += "地址:" + s_u_name.Value.Trim() + "\n";
}
if (!isStrNull(s_birthday.Value) && isDate(s_birthday.Value))
{
qry = qry.Where(o => o.birthday >= ValDate(s_birthday.Value));
_query += "生日(起):" + s_birthday.Value.Trim() + "\n";
}
if (!isStrNull(s_birthday2.Value) && isDate(s_birthday2.Value))
{
qry = qry.Where(o => o.birthday < Convert.ToDateTime(s_birthday2.Value).AddDays(1));
_query += "生日(訖):" + s_birthday2.Value.Trim() + "\n";
}
qry = qry.OrderByDescending(o => o.num);
return qry.ToList();
}
#endregion
}