354 lines
18 KiB
C#
354 lines
18 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;
|
||
|
||
public partial class admin_order_index : MyWeb.config
|
||
{
|
||
private Model.ezEntities _db = new Model.ezEntities();
|
||
public Dictionary<int, string> _keyin1Item = null;
|
||
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack)
|
||
{
|
||
|
||
Model.pro_order order = new Model.pro_order();
|
||
ArrayList options = order.keyin1_list();
|
||
foreach (Model.pro_order.keyin optionKey in options)
|
||
{
|
||
ListItem item = new ListItem(optionKey.Text, optionKey.Value);
|
||
item.Attributes.Add("style", "color:" + optionKey.Color);
|
||
s_keyin1.Items.Add(item);
|
||
}
|
||
|
||
_keyin1Item = publicFun.enum_desc<Model.activity_check.keyin1>(); //狀態
|
||
BuildKind();
|
||
|
||
}
|
||
|
||
}
|
||
#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));
|
||
}
|
||
|
||
}
|
||
#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 = 1, Width = 15, CustomWidth = true },
|
||
new Column { Min = 2, Max = 4, Width = 10, CustomWidth = true },
|
||
new Column { Min = 5, Max = 5, Width = 15, CustomWidth = true },
|
||
new Column { Min = 6, Max = 8, Width = 25, CustomWidth = true },
|
||
new Column { Min = 9, Max = 9, Width = 15, CustomWidth = true },
|
||
new Column { Min = 10, Max = 10, Width = 10, CustomWidth = true },
|
||
new Column { Min = 11, Max = 11, Width = 25, CustomWidth = true },
|
||
new Column { Min = 12, Max = 16, Width = 10, CustomWidth = true },
|
||
new Column { Min = 17, Max = 20, Width = 10, 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("數量"), 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 }
|
||
);
|
||
sd.AppendChild(tr);
|
||
|
||
//查詢要匯出的資料
|
||
var aIDt = _db.actItems.AsEnumerable().Where(f => f.subject.Contains(s_actItemTxt.Value.Trim())).Select(f => f.num.ToString());//品項
|
||
var qry = _db.pro_order.AsEnumerable();
|
||
|
||
if (!isStrNull(s_order_no.Value))
|
||
qry = qry.Where(o => o.order_no.Contains(s_order_no.Value.Trim()));
|
||
if (!isStrNull(s_u_name.Value))
|
||
qry = qry.Where(o => o.f_num.HasValue && o.follower.u_name.Contains(s_u_name.Value.Trim()));
|
||
if (!isStrNull(s_introducerTxt.Value))
|
||
qry = qry.Where(o => o.f_num.HasValue && o.follower1.u_name.Contains(s_introducerTxt.Value.Trim()));
|
||
if (!isStrNull(s_subject.Value))
|
||
qry = qry.Where(o => o.activity_num.HasValue && o.activity.subject.Contains(s_subject.Value.Trim()));
|
||
if (!isStrNull(s_actItemTxt.Value))
|
||
qry = qry.Where(o => o.pro_order_detail.Where(f2 => f2.order_no == o.order_no && aIDt.ToArray().Contains(f2.actItem_num.ToString())).Count() > 0);
|
||
if (!isStrNull(s_keyin1.SelectedValue))
|
||
qry = qry.Where(o => o.keyin1 == s_keyin1.SelectedValue);
|
||
|
||
if (!isStrNull(s_up_time1.Value) && isDate(s_up_time1.Value))
|
||
qry = qry.Where(o => o.up_time >= ValDate(s_up_time1.Value));
|
||
if (!isStrNull(s_up_time2.Value) && isDate(s_up_time2.Value))
|
||
qry = qry.Where(o => o.up_time < Convert.ToDateTime(s_up_time2.Value).AddDays(1));
|
||
|
||
qry = qry.OrderByDescending(o => o.reg_time);
|
||
|
||
MyWeb.encrypt encrypt = new MyWeb.encrypt();
|
||
var tdesc = publicFun.enum_desc<Model.pro_order.detailKeyin1>();
|
||
|
||
var bedDt = _db.bed_order_detail.AsEnumerable();//掛單明細
|
||
|
||
|
||
//left join 使用 GroupJoin
|
||
//var list = qry.Join
|
||
var list = qry.GroupJoin(
|
||
_db.pro_order_detail, o => o.order_no, p => p.order_no, (o, c) =>
|
||
new
|
||
{
|
||
//訂單資料
|
||
order_no = o.order_no,
|
||
up_time = o.up_time,
|
||
keyin1 = o.keyin1,
|
||
f_num = o.follower?.u_name, //姓名/名稱
|
||
phone = o.phone,
|
||
activity_num = o.activity_num.HasValue ? o.activity.subject : "",
|
||
address = o.address,
|
||
demo = o.demo,
|
||
c
|
||
}).SelectMany(o => o.c.DefaultIfEmpty(), (o, d) => //SelectMany 展開
|
||
new
|
||
{
|
||
//訂單資料
|
||
order_no = o.order_no,
|
||
up_time = o.up_time,
|
||
keyin1 = o.keyin1,
|
||
f_num = o.f_num, //姓名/名稱
|
||
phone = o.phone,
|
||
activity_num = o.activity_num,
|
||
address = o.address,
|
||
demo = o.demo,
|
||
|
||
//訂單明細
|
||
//使用DefaultIfEmpty 因匿名型別無法輸出NULL(無法轉換成強型別),需特別注意Null的處理
|
||
d_actItem_num = d == null ? "" : (d.actItem_num.HasValue ? d.actItem.subject : ""), //項目名稱
|
||
d_category = d == null ? "" : (d.actItem_num.HasValue ? d.actItem.category.ToString() : ""),
|
||
d_f_num = d == null ? "" : (d.f_num.HasValue ? d.follower.u_name : ""), //姓名
|
||
d_address = d == null ? "" : d.address,
|
||
d_from_id = d == null ? "" : (d.from_id.HasValue ? d.follower1.u_name : ""), //陽上/報恩者
|
||
d_start_date = d == null ? "" : (d.start_date.HasValue ? d.start_date.Value.ToString("yyyy/MM/dd") : ""), //開始日期
|
||
d_due_date = d == null ? "" : (d.due_date.HasValue ? d.due_date.Value.ToString("yyyy/MM/dd") : ""), //期滿日期
|
||
d_extend_date = d == null ? "" : (d.extend_date.HasValue ? d.extend_date.Value.ToString("yyyy/MM/dd") : ""), //應續約日
|
||
d_price = d == null ? "" : (d.price.HasValue ? d.price.Value.ToString() : "0"), //預設金額
|
||
d_qty = d == null ? "" : (d.qty.HasValue ? d.qty.Value.ToString() : "0"), //數量
|
||
d_writeBedQty = d == null ? 0 : bedDt.Where(b =>( b.bed_order.o_detail_id.Value == d.num) && b.checkIn_date.HasValue && b.bed_kind_detail_id.HasValue).Count(), //已劃數量
|
||
d_notBedQty = d == null ? 0 : bedDt.Where(b => b.bed_order.o_detail_id.Value == d.num && (!b.checkIn_date.HasValue || !b.bed_kind_detail_id.HasValue)).Count(), //未劃數量
|
||
d_pay = d == null ? "" : (d.pay.HasValue ? d.pay.Value.ToString() : "0"), //已收金額
|
||
d_pay_date = d == null ? "" : (d.pay_date.HasValue ? d.pay_date.Value.ToString("yyyy/MM/dd") : ""), //付款期限
|
||
d_keyin1 = d == null ? "" : (d.keyin1.HasValue&& d.keyin1.Value>0 ? tdesc[d.keyin1 ?? 1] : ""), //報名狀態
|
||
d_demo = d == null ? "" : d.demo, //狀態備註
|
||
}).ToList();
|
||
|
||
|
||
|
||
|
||
//也可使用查詢式
|
||
//try
|
||
////{
|
||
// var listQ = from o in qry
|
||
// join p in _db.pro_order_detail
|
||
// on new
|
||
// {
|
||
// order_no = o.order_no,
|
||
// } equals
|
||
// new { p.order_no }
|
||
// into subGrp //into ==
|
||
// from d in subGrp.DefaultIfEmpty()
|
||
// select new
|
||
// {
|
||
// //訂單資料
|
||
// order_no = o.order_no,
|
||
// up_time = o.up_time,
|
||
// f_num = o.follower.u_name, //姓名/名稱
|
||
// ...
|
||
|
||
// //訂單明細
|
||
// //使用DefaultIfEmpty 因匿名型別無法輸出NULL(無法轉換成強型別),需特別注意Null的處理
|
||
// d_actItem_num = d == null? "" :(d.actItem_num.HasValue ? d.actItem.subject : ""), //項目名稱
|
||
// d_f_num = d == null ? "" :( d.f_num.HasValue ? d.follower.u_name : ""), //姓名
|
||
// ...
|
||
// };
|
||
// var list = listQ.ToList();
|
||
|
||
|
||
|
||
//}
|
||
//catch (Exception ex)
|
||
//{
|
||
// Response.Write(ex.Message);
|
||
//}
|
||
|
||
|
||
if (list.Count > 0)
|
||
{
|
||
foreach (var item in list)
|
||
{
|
||
//新增資料列
|
||
tr = new Row();
|
||
tr.Append(
|
||
new Cell() { CellValue = new CellValue(item.order_no), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.up_time.Value.ToString("yyyy/MM/dd")), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(Model.pro_order.keyin1_value_to_text(item.keyin1)), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.f_num), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(encrypt.DecryptAutoKey(item.phone)), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.activity_num), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.address), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.demo), DataType = CellValues.String },
|
||
|
||
new Cell() { CellValue = new CellValue(item.d_actItem_num), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_f_num), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_address), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_from_id), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_start_date), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_due_date), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_extend_date), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue((Val(item.d_category) ==(int)Model.activity.category.Order) ?( item.d_notBedQty +"/" + item.d_writeBedQty):""), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue("$" + ValMoney(item.d_price)), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_qty), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue("$" + ValMoney(ValFloat(item.d_price) * Val(item.d_qty))), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue("$" + ValMoney(item.d_pay)), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue("$" + ValMoney(ValFloat(item.d_price) * Val(item.d_qty) - ValFloat(item.d_pay))), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_pay_date), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_keyin1), DataType = CellValues.String },
|
||
new Cell() { CellValue = new CellValue(item.d_demo), 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.Order, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => x.order_no).ToList()));
|
||
|
||
|
||
// 合并具有相同单号的行
|
||
//string orderNumber = "";
|
||
//int startRow = 1;
|
||
//int endRow = 1;
|
||
//int nowRow = 1;
|
||
//foreach (var kvp in list)
|
||
//{
|
||
// if(orderNumber == "" || orderNumber != kvp.order_no)
|
||
// {
|
||
// orderNumber = kvp.order_no;
|
||
// int _count = list.Where(x => x.order_no == orderNumber).Count();
|
||
// if (_count > 1)
|
||
// {
|
||
// endRow += _count;
|
||
|
||
// var mergeCells = new MergeCells();
|
||
// string startCellReference = GetCellReference(1, startRow);
|
||
// string endCellReference = GetCellReference(1, endRow);
|
||
// if (startCellReference != endCellReference)
|
||
// mergeCells.Append(new MergeCell() { Reference = new StringValue($"{startCellReference}:{endCellReference}") });
|
||
|
||
// }
|
||
// startRow += _count;
|
||
// endRow = startRow;
|
||
// }
|
||
// else
|
||
// {
|
||
// startRow ++;
|
||
// endRow = startRow;
|
||
// }
|
||
// nowRow++;
|
||
//}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
HttpContext.Current.Response.Clear();
|
||
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=報名.xlsx");
|
||
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
|
||
HttpContext.Current.Response.End();
|
||
|
||
|
||
}
|
||
|
||
private string GetCellReference(int column, int row)
|
||
{
|
||
int dividend = column;
|
||
string cellReference = string.Empty;
|
||
|
||
while (dividend > 0)
|
||
{
|
||
int modulo = (dividend - 1) % 26;
|
||
char columnChar = (char)('A' + modulo);
|
||
cellReference = columnChar + cellReference;
|
||
dividend = (dividend - modulo) / 26;
|
||
}
|
||
|
||
return cellReference + row.ToString();
|
||
}
|
||
#endregion
|
||
|
||
|
||
} |