using FamilyTreeAPI.Entities; using FamilyTreeAPI.Interface; using FamilyTreeAPI.Models; using SpreadsheetLight; using System.Linq; using DocumentFormat.OpenXml.Spreadsheet; namespace FamilyTreeAPI.Repository; public class ReportRepository : IReport { private enum EnumStaffWork { Id = 0, FirstName, LastName, StartDate, startTime, stopTime, Task, Job, Hour }; private readonly FamilyTreeDBContext _context; //private readonly ILookup _lookup; const string DateTimeFormat = "dd/MM/yyyy HH:mm"; const string TimeFormat = "HH:mm"; const string DateFormat = "dd/MM/yyyy"; private readonly IStaff _staff; private Dictionary _dstaff; private readonly ILookup _lookup; private string DisplayTime(DateTime date) { string result = ""; if (date != DateTime.MinValue) result = date.ToString(TimeFormat); return result; } private string DisplayDateTime(DateTime? date) { string result = ""; if (date != DateTime.MinValue) result = date!.Value.ToString(DateFormat); return result; } private string FormatTime(int hour) { string result; if (hour < 10) result = "0" + hour.ToString() + ":00"; else result = hour.ToString() + ":00"; return result; } public ReportRepository(FamilyTreeDBContext context, ILookup lookup,IStaff staff) { _context = context; _lookup = lookup; _staff = staff; } // public async Task> GetStaffWorkReportAsync(StaffWorkCriteria criteria) // { // //var data = await _lookup.GetLookupDicAsync("TypeOfUse"); // //if (data != null) // //{ // // this._ServiceTypeDic = data.Data; // //} // var rstaff = await _staff.GetDicStaffs(); // if (rstaff.StatusCode == 1) // _dstaff = rstaff.Data; // //data = await _lookup.GetLookupDicAsync("Status"); // //if (data != null) // //{ // // this._StatusDic = data.Data; // //} // int statusCode = 1; // int col = 1; // int row = 1; // SLDocument sl = new SLDocument(); // SLStyle styleRed = sl.CreateStyle(); // styleRed.SetFontColor(System.Drawing.Color.Red); // //first sheetName // sl.RenameWorksheet(SLDocument.DefaultFirstSheetName, "Staff Work Report"); // SLStyle style = sl.CreateStyle(); // style.SetFontUnderline(DocumentFormat.OpenXml.Spreadsheet.UnderlineValues.Single); // style.SetFontBold(true); // style.SetFont("Calibri", 14); // sl.SetCellStyle(1, 3, style); // sl.SetRowHeight(1, 1, 25); // sl.SetCellValue(row++, 3, "Staff Work"); // // sl.SetCellValue(row, 3, "From Date: " + fromDate.ToString(DateFormat) + " - " + toDate.ToString(DateFormat)); // sl.SetCellValue(1, 6, " " + DateTime.Now.ToString("ddd dd/MM/yyyy HH:mm")); // // sl.SetCellValue(row, 6, "Hospital: " + facility); // int startTitleRow = 3; // row = startTitleRow; // //make the title bold // style = sl.CreateStyle(); // style.Font.Bold = true; // sl.SetRowStyle(startTitleRow, style); // //set it height // sl.SetRowHeight(startTitleRow, startTitleRow, 25); // // add the title first // sl.SetCellValue(row, (int)EnumStaffWork.Id, "Id"); // sl.SetCellValue(row, (int) EnumStaffWork.FirstName, "FirstName"); // sl.SetCellValue(row, (int) EnumStaffWork.LastName, "SurName"); // sl.SetCellValue(row, (int) EnumStaffWork.StartDate, "StartDate"); // sl.SetCellValue(row, (int)EnumStaffWork.startTime, "Start Time"); // sl.SetCellValue(row, (int)EnumStaffWork.stopTime, "Stop Time"); // sl.SetCellValue(row, (int)EnumStaffWork.Hour, "Hour"); // /* // style = sl.CreateStyle(); // style.SetFontColor(System.Drawing.Color.Blue); // sl.SetRowStyle(row, style); // */ // sl.FreezePanes(row, 0); //frozen the row. // //wrap text on is concession Expiry // style = sl.CreateStyle(); // style.SetWrapText(true); // sl.SetColumnStyle(4, style); // //format date // // style = sl.CreateStyle(); // //style.FormatCode = DateTimeFormat; // // sl.SetColumnStyle(1, style); // //make column width // sl.SetColumnWidth((int)EnumStaffWork.Id, 10); // sl.SetColumnWidth((int)EnumStaffWork.FirstName, 30); // sl.SetColumnWidth((int)EnumStaffWork.LastName, 30); // sl.SetColumnWidth((int)EnumStaffWork.StartDate, 25); // sl.SetColumnWidth((int)EnumStaffWork.startTime, 10); // sl.SetColumnWidth((int)EnumStaffWork.stopTime, 10); // sl.SetColumnWidth((int)EnumStaffWork.Hour, 20); // style = sl.CreateStyle(); // style.FormatCode = DateFormat; // sl.SetColumnStyle(5, style); // //style = sl.CreateStyle(); // //style.FormatCode = DateTimeFormat; // //sl.SetColumnStyle(1, style); // sl.SetColumnWidth(10, 20); // int startRowFrom = startTitleRow + 1; //// DateTime ttdate = toDate.ToLocalTime(); //add 1 days so if to inclusive todate 02/09 // var container = await _context.LoadStaffWorkAsync(criteria); //fromDate.ToLocalTime(), ttdate.AddDays(1), typeOfCall); // var list = container; // list.Sort((x, y) => x.StartDate.Value.CompareTo(y.StartDate.Value)); // StaffWorkViewDto item; // DateTime startTime, stopTime; // double hour =0; // string fname, lname; // StaffWorkDetailDto detail; // for (int i = 0; i < list.Count; i++) // { // item = list[i]; // row = i + startRowFrom; // (startTime, stopTime ) = GetSSTime(item); // if (item.Details != null) // for (int j = 0; j < item.Details.Count; j++) // { // detail = item.Details[j]; // hour += detail.TotalMinuts(); // } // (fname, lname) = GetStaffName(item.StaffId.Value); // sl.SetCellValue(row, (int)EnumStaffWork.StartDate, item.StartDate.Value); // sl.SetCellValue(row, (int)EnumStaffWork.FirstName, fname); // sl.SetCellValue(row, (int)EnumStaffWork.LastName, lname); // sl.SetCellValue(row, (int)EnumStaffWork.startTime, startTime); // sl.SetCellValue(row, (int)EnumStaffWork.stopTime, stopTime); // sl.SetCellValue(row, (int)EnumStaffWork.Hour, hour/60); // } // byte[] array; // // sl.SaveAs("C:\\Temp\\Report\\ConcessionValidationDate" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xlsx"); // using (MemoryStream ws = new MemoryStream()) // { // sl.SaveAs(ws); // array = ws.ToArray(); // /* // using (FileStream fs = new FileStream("c:\\temp\\Report\\conReport.xlsx",FileMode.Create,FileAccess.Write)) // { // ws.WriteTo(fs); // fs.Close(); // } // */ // ws.Close(); // } // var result = new FileContent // { // Content = array, // FileName = "StaffWork_Report" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm") + ".xlsx", // }; // return new ResultModel // { // Data = result, // StatusCode = statusCode // }; // } private (string, string) GetStaffName(int staffId) { StaffDto staff; if (_dstaff.ContainsKey(staffId)) { staff = _dstaff[staffId]; return ( staff.Firstname ?? "" , staff.Lastname ?? ""); } return ("",""); } //private (DateTime firstTime, DateTime lastTime) GetSSTime(StaffWorkViewDto item) //{ // DateTime sTime, lTime; // DateTime? value; // int count = 0; // sTime = lTime = DateTime.Now; // List detail = item.Details ?? new(); // count = detail.Count; // if (count > 0) // { // value = detail[0].StartTime; // if (value.HasValue) // { // sTime = value.Value; // } // value = detail[count - 1].StopTime; // if (value.HasValue) // { // lTime = value.Value; // } // } // return (sTime, lTime); //} private string GetValue(string item, string text) { string result = item; if (!string.IsNullOrEmpty(text)) result = text; return result; } //private string GetPools(List poolIds) //{ // string result = ""; // string id; // for (int i = 0; i < poolIds.Count; i++) // { // id = poolIds[i]; // if (this._PoolDic.ContainsKey(id)) // result += _PoolDic[id].Description + ","; // } // result = result.Remove(result.Length-1, 1); // return result; //} }