前輩彬哥
處理的真漂亮
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AVSModelLib.ViewModel.BSAModel;
using KD.DBExtension;
using static KD.DBExtension.Utilities;
using AVSModelLib;
using AVSModelLib.EFModels;
using System.Text;
using System.IO;
namespace AVSAdminSystem.Controllers.BS.BSA002
{
public class BSA002Controller : Controller
{
public BSA002Controller()
{
ViewBag.Path1 = "統計分析模組(BSA)";
ViewBag.Path2 = "統計報表作業";
}
//[Authorize(Roles = "BSA002")]
[HttpGet]
public ActionResult Index()
{
var model = new BSA002ConditionModel();
return View(model);
}
[HttpPost]
public ActionResult ExportFile(BSA002ConditionModel model)
{
if (!ModelState.IsValid)
return HttpNotFound();
var cache = System.Runtime.Caching.MemoryCache.Default;
var key = Guid.NewGuid().ToString();
cache.Add(key, model, new System.Runtime.Caching.CacheItemPolicy { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddMinutes(5)) });
return Json(new { success = true, key = key });
}
[HttpGet]
public ActionResult ExportFile(string id)
{
var cache = System.Runtime.Caching.MemoryCache.Default;
if (!cache.Contains(id))
return HttpNotFound();
var model = (BSA002ConditionModel)cache.GetCacheItem(id).Value;
var criteria = model.CreateCondition();
StringBuilder sb = new StringBuilder();
using (var db = DBFactory.CreateConnection())
{
var querySys_Org_Guid = criteria.Item2.Where(i => i.Key == "@querySys_Org_Guid").Any() ? Guid.Parse(criteria.Item2.Where(i => i.Key == "@querySys_Org_Guid").Select(i => i.Value).FirstOrDefault().ToString()) : new Guid?();
var BeginCreate_Date = criteria.Item2.Where(i => i.Key == "@beginCreate_Date").Any() ? DateTime.Parse(criteria.Item2.Where(i => i.Key == "@beginCreate_Date").Select(i => i.Value).FirstOrDefault().ToString()) : new DateTime?();
var EndCreate_Date = criteria.Item2.Where(i => i.Key == "@endCreate_Date").Any() ? DateTime.Parse(criteria.Item2.Where(i => i.Key == "@endCreate_Date").Select(i => i.Value).FirstOrDefault().ToString()) : new DateTime?();
#region 取資料
var orgList = querySys_Org_Guid.HasValue ? db.GetDataList<SYS_ORG>("select * from SYS_ORG where Guid = ?", querySys_Org_Guid) : db.GetDataList<SYS_ORG>("select * from SYS_ORG order by Sort_Seq");
var codeList = db.GetDataList<SYS_Code>("select * from SYS_CODE where Parent_Code = 'C008'");
var GenderList = db.GetDataList<GenderStatistics>(string.Format(@"
select
a.Sys_Org_Guid,
a.Victim_Legal_Type,
cast(b.Gender as varchar) Gender,
count(1) C
from BS_Case_Main a
inner join BS_Case_Person b on a.Victim_Guid = b.Guid
where a.Victim_Legal_Type is not null {0}
group by a.Sys_Org_Guid, a.Victim_Legal_Type, b.Gender ", criteria.Item1), criteria.Item2);
var AgeList = db.GetDataList<AgeStatistics>(string.Format(@"
select
a.Sys_Org_Guid,
a.Victim_Legal_Type,
cast(FLOOR(DATEDIFF(DY, b.Birthday, GETDATE()) / 365.25) as integer) Age
from BS_Case_Main a
inner join BS_Case_Person b on a.Victim_Guid = b.Guid
where a.Victim_Legal_Type is not null and b.Birthday is not null {0}", criteria.Item1), criteria.Item2);
var JobList = db.GetDataList<JobStatistics>(string.Format(@"
select
a.Sys_Org_Guid,
a.Victim_Legal_Type,
dbo.getCodeName(Job_Type, 0) Job
from BS_Case_Main a
inner join BS_Case_Person b on a.Victim_Guid = b.Guid
where a.Victim_Legal_Type is not null and b.Job_Type is not null {0}", criteria.Item1), criteria.Item2);
#endregion
sb.AppendLine("<div align='center'><font size='5'><b>財團法人犯罪被害人保護協會</b></font></div>");
sb.AppendLine("<div align='center'><font size='5'><b>個案資料統計表</b></font></div>");
sb.AppendLine($"<div align='right'>製表日:{DateTime.Now.ToString("yyyy/MM/dd")}</div>");
sb.AppendLine($"<div align='right'>製表人:{AVSModelLib.Security.User.Current.Organ_Name}/{AVSModelLib.Security.User.Current.Name}</div>");
sb.AppendLine($"<div align='right'>統計期間:{(BeginCreate_Date.HasValue ? BeginCreate_Date.Value.ToString("yyyy/MM/dd") : "") + "~"}{(EndCreate_Date.HasValue ? EndCreate_Date.Value.ToString("yyyy/MM/dd") : "")}</div>");
sb.AppendLine("<table width='100%' border=1 cellpadding=0 cellspacing=0>");
#region 分會
sb.AppendLine("<tr>");
sb.AppendLine("<td colspan='2'></td>");
orgList.ForEach(o =>
{
sb.AppendLine($"<td align='center' colspan='{codeList.Count}'>{o.Orgcode_Name}</td>");
});
sb.AppendLine("</tr>");
#endregion
#region 案件類型
sb.AppendLine("<tr>");
sb.AppendLine("<td colspan='2'></td>");
orgList.ForEach(o =>
{
codeList.ForEach(c =>
{
sb.AppendLine($"<td align='center'>{c.Code_Name}</td>");
});
});
sb.AppendLine("</tr>");
#endregion
#region 性別
sb.AppendLine("<tr>");
sb.AppendLine("<td rowspan='2'>性別</td>");
for (int i = 0; i <= 1; i++)
{
if (i != 0) sb.AppendLine("<tr>");
sb.AppendLine("<td>" + (i == 0 ? "女性" : "男性") + "</td>");
orgList.ForEach(o =>
{
codeList.ForEach(c =>
{
var Genger = GenderList.Where(g => g.Sys_Org_Guid == o.Guid && g.Victim_Legal_Type == c.Code && g.Gender == i.ToString());
sb.AppendLine($"<td>{(Genger.Any() ? Genger.FirstOrDefault().C : 0)}</td>");
});
});
sb.AppendLine("</tr>");
}
#endregion
#region 年齡
sb.AppendLine("<tr>");
sb.AppendLine("<td rowspan='10'>年齡</td>");
for (int i = 0; i < 100; i += 10)
{
var Sage = (i == 0 ? i : i + 1);
if (i != 0) sb.AppendLine("<tr>");
sb.AppendLine($"<td>{Sage}~{i + 10}</td>");
orgList.ForEach(o =>
{
codeList.ForEach(c =>
{
var Age = AgeList.Where(a => a.Sys_Org_Guid == o.Guid && a.Victim_Legal_Type == c.Code && a.Age >= Sage && a.Age <= i + 10);
sb.AppendLine($"<td>{(Age.Any() ? Age.Count() : 0)}</td>");
});
});
sb.AppendLine("</tr>");
}
#endregion
#region 職業
if (JobList.Any())
{
var JobNameList = (from d in JobList group d by d.Job into g select g.FirstOrDefault().Job).ToList();
sb.AppendLine("<tr>");
sb.AppendLine($"<td rowspan='{JobNameList.Count()}'>職業</td>");
for (int i = 0; i < JobNameList.Count(); i++)
{
if (i != 0) sb.AppendLine("<tr>");
sb.AppendLine("<td>" + JobNameList[i] + "</td>");
orgList.ForEach(o =>
{
codeList.ForEach(c =>
{
var Job = JobList.Where(g => g.Sys_Org_Guid == o.Guid && g.Victim_Legal_Type == c.Code && g.Job == JobNameList[i].ToString());
sb.AppendLine($"<td>{(Job.Any() ? Job.Count() : 0)}</td>");
});
});
sb.AppendLine("</tr>");
}
}
#endregion
sb.AppendLine("</table>");
}
byte[] result;
using (var ms = new MemoryStream())
{
using (var sw = new StreamWriter(ms, Encoding.UTF8))
{
sw.Write(sb.ToString());
}
result = ms.ToArray();
}
return File(result, "application/xls", "BSA002.xls");
}
public class GenderStatistics
{
public Guid Sys_Org_Guid { get; set; }
public string Victim_Legal_Type { get; set; }
public string Gender { get; set; }
public int C { get; set; }
}
public class AgeStatistics
{
public Guid Sys_Org_Guid { get; set; }
public string Victim_Legal_Type { get; set; }
public int Age { get; set; }
}
public class JobStatistics
{
public Guid Sys_Org_Guid { get; set; }
public string Victim_Legal_Type { get; set; }
public string Job { get; set; }
}
}
}
歡迎高手低手來看看 指教指教 切磋切磋 教學香腸