HomeController.cs
7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using HHECS.WorkHourStatistics.Dtos;
using HHECS.WorkHourStatistics.Models;
using HHECS.WorkHourStatistics.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System.Linq.Expressions;
using System.Text;
namespace HHECS.WorkHourStatistics.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
private readonly IFreeSql _freeSql;
/// <summary>
/// 长沙公司部门根节点Id
/// </summary>
private readonly string rootDeptId = "8682240671139790443";
public HomeController(ILogger<HomeController> logger, IFreeSql freeSql)
{
_logger = logger;
_freeSql = freeSql;
}
public IActionResult Index()
{
var depts = _freeSql.Queryable<Department>().ToList();
var root = new TreeDto
{
Id = rootDeptId,
Title = "长沙华恒机器人系统有限公司",
Spread = true
};
BuiderTree(root, depts);
return View(root);
}
[HttpPost]
public IActionResult Index(string person, string deptId, DateTime? startTime, DateTime? endTime, int page, int limit)
{
try
{
var query = _freeSql.Queryable<WorkRecord>().Where(GetFilter(person, deptId, startTime, endTime)).OrderBy(x => x.BaseWorkCard).OrderBy(x => x.Number);
var result = query.Skip((page - 1) * limit).Take(limit).ToList();
var numbers = result.Select(x => x.Number).Distinct().ToList();
var personInfos = _freeSql.Queryable<Person>().Where(x => numbers.Contains(x.Number)).ToList(x => new Person
{
Number = x.Number,
Virtual_departmentPath = x.Virtual_departmentPath,
});
foreach (var item in result)
{
var departmentPath = personInfos.Where(x => x.Number == item.Number).Select(x => x.Virtual_departmentPath).FirstOrDefault();
if (!string.IsNullOrWhiteSpace(departmentPath))
{
item.DeptName = departmentPath.Split('/').LastOrDefault() ?? string.Empty;
}
}
return Ok(new
{
code = 0,
msg = "",
count = query.Count(),
data = result,
});
}
catch (Exception ex)
{
return Ok(new
{
code = 0,
msg = ex.Message,
count = 0,
data = Array.Empty<string>(),
});
}
}
[HttpGet]
public IActionResult Export(string person, string deptId, DateTime? startTime, DateTime? endTime)
{
try
{
var result = _freeSql.Queryable<WorkRecord>().Where(GetFilter(person, deptId, startTime, endTime)).OrderBy(x => x.Number).OrderBy(x => x.BaseWorkCard).ToList();
var numbers = result.Select(x => x.Number).Distinct().ToList();
var personInfos = _freeSql.Queryable<Person>().Where(x => numbers.Contains(x.Number)).ToList(x => new Person
{
Number = x.Number,
Virtual_departmentPath = x.Virtual_departmentPath,
});
foreach (var item in result)
{
var departmentPath = personInfos.Where(x => x.Number == item.Number).Select(x => x.Virtual_departmentPath).FirstOrDefault();
if (!string.IsNullOrWhiteSpace(departmentPath))
{
item.DeptName = departmentPath.Split('/').LastOrDefault() ?? string.Empty;
}
}
var reportData = RepotTool.GetReport(result).Data;
var time1 = startTime ?? result.Min(x => x.BaseWorkCard);
var time2 = endTime ?? result.Max(x => x.BaseOffDutyCard);
var days = RepotTool.GetHeadDays(result.Min(x => x.BaseWorkCard), result.Max(x => x.BaseOffDutyCard));
var workBookResult = NpoiHelper.GetWorkbook(reportData, days);
// 写 WorkBook信息到 内存流中
byte[] buffer;
using (var ms = new MemoryStream())
{
workBookResult.Data.Write(ms);
buffer = ms.ToArray();
}
var fileName = new StringBuilder();
fileName.Append("WT时间统计 - ");
if (!string.IsNullOrWhiteSpace(deptId))
{
if (deptId == rootDeptId)
{
fileName.Append("长沙华恒机器人系统有限公司");
}
else
{
var deptName = _freeSql.Queryable<Department>().Where(x => x.Id == deptId).First(x => x.Name);
fileName.Append($"[{deptName}]");
}
}
if (!string.IsNullOrWhiteSpace(person))
{
fileName.Append($"[{person}]");
}
fileName.Append($"{time1:D}至{time2:D}.xlsx");
// .xlsx文件对应的Mime信息
var mime = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
return File(buffer, mime, fileName.ToString());
}
catch (Exception ex)
{
return Ok(ex.Message);
}
}
private void BuiderTree(TreeDto root, List<Department> depts)
{
var childrens = depts.Where(x => x.ParentId == root.Id).OrderBy(x => x.OrderNum).Select(x => new TreeDto
{
Id = x.Id,
Title = x.Name,
}).ToList();
root.Children = childrens;
foreach (var child in childrens)
{
BuiderTree(child, depts);
}
}
private Expression<Func<WorkRecord, bool>> GetFilter(string person, string deptId, DateTime? startTime, DateTime? endTime)
{
Expression<Func<WorkRecord, bool>> filter = x => true;
if (!string.IsNullOrWhiteSpace(person))
{
filter = filter.And(x => x.Number.Contains(person) || x.PersonName.Contains(person));
}
if (!string.IsNullOrWhiteSpace(deptId) && deptId != rootDeptId)
{
var deptName = _freeSql.Queryable<Department>().Where(x => x.Id == deptId).First(x => x.Name);
var personNumbers = _freeSql.Queryable<Person>().Where(x => x.Virtual_departmentPath.Contains(deptName)).ToList(x => x.Number);
filter = filter.And(x => personNumbers.Contains(x.Number));
}
if (startTime != null)
{
filter = filter.And(x => x.BaseWorkCard >= startTime.Value.Date);
}
if (endTime != null)
{
filter = filter.And(x => x.BaseOffDutyCard < endTime.Value.Date.AddDays(1));
}
return filter;
}
}
}