|
|
@@ -21,37 +21,74 @@ class StatisticService extends StatisticCommonService
|
|
|
{
|
|
|
public function employeeDayHourStatistic($data, $user)
|
|
|
{
|
|
|
+ //传参月份、项目编码、项目名称 不允许跨年查询月份
|
|
|
+ //项目编码、项目名称、人员名称、工时、日期
|
|
|
//传参月份、项目编码、项目名称 不允许跨年查询月份
|
|
|
//项目编码、项目名称、人员名称、工时、日期
|
|
|
list($status, $month_start, $month_end) = $this->commonRule($data);
|
|
|
if (!$status) return [false, $month_start];
|
|
|
- $day_employee_list = $this->getItemEmployeeDayWorkList($user,$data,$month_start,$month_end);
|
|
|
- $dataCollection = collect($day_employee_list);
|
|
|
+ $month_employee = DailyPwOrderDetails::Clear($user, $data);
|
|
|
+ $month_employee_list = $month_employee->where("order_time", ">=", $month_start)->where("order_time", "<", $month_end)
|
|
|
+ ->where('del_time', 0)
|
|
|
+ ->select(
|
|
|
+ "item_id",
|
|
|
+ "employee_id",
|
|
|
+ // 将时间戳转为 Y-m-d 格式并起别名
|
|
|
+ DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d') as order_date"),
|
|
|
+ // 聚合求和
|
|
|
+ DB::raw("SUM(total_work_min) as total_work")
|
|
|
+ )
|
|
|
+ ->groupBy(DB::raw("FROM_UNIXTIME(order_time, '%Y-%m-%d')"), "item_id", "employee_id")
|
|
|
+ ->orderby("order_date", "asc")->get()->toArray();
|
|
|
+// $month_employee_list = $this->limit($month_employee_list, ['*'], $data);
|
|
|
+ $dataCollection = collect($month_employee_list);
|
|
|
$item_ids = $dataCollection->pluck('item_id')->unique()->values()->all();
|
|
|
$employee_ids = $dataCollection->pluck('employee_id')->unique()->values()->all();
|
|
|
- $employee_key_list = Employee::wherein('id', $employee_ids)->pluck("title", "id")->toArray();
|
|
|
- $item_title_key_list = Item::wherein('id', $item_ids)->pluck("title", "id")->toArray();
|
|
|
- $item_code_key_list = Item::wherein('id', $item_ids)->pluck("code", "id")->toArray();
|
|
|
- $keys = ["employee_id","order_date"];
|
|
|
- list($employee_count,$employee_work_count) = $this->calculateCount($day_employee_list,$keys,"total_work");
|
|
|
- $collection = collect($day_employee_list);
|
|
|
- $month_employee_list = $collection->transform(function ($item) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$employee_work_count, &$employee_count,$keys) {
|
|
|
+
|
|
|
+ $employee = Employee::TopClear($user, $data);
|
|
|
+ $employee_key_list = $employee->wherein('id', $employee_ids)->pluck("title", "id")->toArray();
|
|
|
+
|
|
|
+ $item = Item::TopClear($user, $data);
|
|
|
+ $item_title_key_list = $item->wherein('id', $item_ids)->pluck("title", "id")->toArray();
|
|
|
+ $item_code_key_list = $item->wherein('id', $item_ids)->pluck("code", "id")->toArray();
|
|
|
+ $collect = collect($month_employee_list);
|
|
|
+
|
|
|
+ $employee_count = $collect->groupBy(function ($item) {
|
|
|
+ // 这里的 $item 是集合中的每一行数据
|
|
|
+ return $item['employee_id'] . '_' . $item['order_date'];
|
|
|
+ })->map(function ($group) {
|
|
|
+ return $group->count();
|
|
|
+ })->toArray();
|
|
|
+ $employee_work_count = $collect->groupBy(fn($item) => $item['employee_id'] . '_' . $item['order_date'])
|
|
|
+ ->map(function ($group) {
|
|
|
+ // 1. 算出小时并保留两位小数(例如 26.08)
|
|
|
+ $hours = round($group->sum('total_work') / 60, 2);
|
|
|
+
|
|
|
+ // 2. 乘以 100 并强转为整数(例如 2608)
|
|
|
+ // 使用 round 是为了再次修正 26.08 * 100 可能产生的 2607.9999 误差
|
|
|
+ return (int)round($hours * 100);
|
|
|
+ })
|
|
|
+ ->toArray();
|
|
|
+ $collection = collect($month_employee_list);
|
|
|
+ // 注意:这里使用 transform,参数名建议写准确
|
|
|
+ // 关键点:use 后面加了 & 符号
|
|
|
+ $month_employee_list = $collection->transform(function ($item, $index) use ($employee_key_list, $item_title_key_list, $item_code_key_list, &$employee_work_count, &$employee_count) {
|
|
|
+
|
|
|
$item['employee_name'] = $employee_key_list[$item['employee_id']] ?? "未知员工({$item['employee_id']})";
|
|
|
$item['item_title'] = $item_title_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
|
|
|
$item['item_code'] = $item_code_key_list[$item['item_id']] ?? "未知项目({$item['item_id']})";
|
|
|
+
|
|
|
// 如果不是最后一条
|
|
|
- $key = collect($keys)->map(fn($k) => $item[$k] ?? '')->implode('_');
|
|
|
- $counts = ["employee_work_count"=>$employee_count];
|
|
|
- $sums = ["employee_work_count"=>$employee_work_count];
|
|
|
- $word_keys = [
|
|
|
- "employee_work_count" =>
|
|
|
- [
|
|
|
- "key" => "total_work",
|
|
|
- "value" => "total_work_hours",
|
|
|
- "type" => "hour",
|
|
|
- ]
|
|
|
- ];
|
|
|
- $this->calculateClosure($key,$item,$counts,$sums,$word_keys);
|
|
|
+ $key = $item['employee_id'] . '_' . $item['order_date'];
|
|
|
+ if (--$employee_count[$key] > 0) {
|
|
|
+ $current_hours = round($item['total_work'] / 60, 2);
|
|
|
+ $item['total_work_hours'] = $current_hours;
|
|
|
+ // 关键:递减外部的 $absolute_total_hours
|
|
|
+ $employee_work_count[$key] -= $current_hours * 100;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ $item['total_work_hours'] = round($employee_work_count[$key] / 100, 2);
|
|
|
+ }
|
|
|
return $item;
|
|
|
})->all();
|
|
|
return [true, $month_employee_list];
|