|
@@ -417,45 +417,56 @@ class RuleSetService extends Service
|
|
|
$data['top_depart_id'] = $user['top_depart_id'];
|
|
$data['top_depart_id'] = $user['top_depart_id'];
|
|
|
if(empty($data['month'])) return [false, '月份不能为空'];
|
|
if(empty($data['month'])) return [false, '月份不能为空'];
|
|
|
|
|
|
|
|
- // 1. 获取当月 1 号 00:00:00 的时间戳
|
|
|
|
|
$monthStart = $this->changeDateToDate($data['month']);
|
|
$monthStart = $this->changeDateToDate($data['month']);
|
|
|
$data['month'] = $monthStart;
|
|
$data['month'] = $monthStart;
|
|
|
-
|
|
|
|
|
- // 2. 计算当月结束时间(下个月 1 号减 1 秒)
|
|
|
|
|
$monthEnd = strtotime('+1 month', $monthStart) - 1;
|
|
$monthEnd = strtotime('+1 month', $monthStart) - 1;
|
|
|
|
|
|
|
|
- // 3. 查找在该月份有效期内的项目 (项目开始 <= 月末 且 项目结束 >= 月初)
|
|
|
|
|
- $itemIds = DB::table('item')
|
|
|
|
|
|
|
+ // 1. 获取在该月份有效期内的项目及其名称
|
|
|
|
|
+ $itemsData = DB::table('item')
|
|
|
->where('top_depart_id', $data['top_depart_id'])
|
|
->where('top_depart_id', $data['top_depart_id'])
|
|
|
->where('del_time', 0)
|
|
->where('del_time', 0)
|
|
|
->where('start_time', '<=', $monthEnd)
|
|
->where('start_time', '<=', $monthEnd)
|
|
|
->where('end_time', '>=', $monthStart)
|
|
->where('end_time', '>=', $monthStart)
|
|
|
- ->pluck('id')
|
|
|
|
|
|
|
+ ->pluck('title', 'id') // 获取 [id => title] 映射
|
|
|
->toArray();
|
|
->toArray();
|
|
|
|
|
|
|
|
- if (empty($itemIds)) return [false, "该月份下无项目信息"];
|
|
|
|
|
|
|
+ if (empty($itemsData)) return [false, "该月份下无项目信息"];
|
|
|
|
|
+ $itemIds = array_keys($itemsData);
|
|
|
|
|
|
|
|
- // 4. 获取这些项目下绑定的所有人员和设备
|
|
|
|
|
|
|
+ // 2. 获取项目明细
|
|
|
$details = DB::table('item_details')
|
|
$details = DB::table('item_details')
|
|
|
->whereIn('item_id', $itemIds)
|
|
->whereIn('item_id', $itemIds)
|
|
|
->where('top_depart_id', $data['top_depart_id'])
|
|
->where('top_depart_id', $data['top_depart_id'])
|
|
|
->where('del_time', 0)
|
|
->where('del_time', 0)
|
|
|
->get();
|
|
->get();
|
|
|
|
|
|
|
|
- $manMap = []; // [人员ID => [项目ID1, 项目ID2...]]
|
|
|
|
|
- $deviceMap = []; // [设备ID => [项目ID1, 项目ID2...]]
|
|
|
|
|
|
|
+ $manIds = [];
|
|
|
|
|
+ $devIds = [];
|
|
|
|
|
+ $manMap = [];
|
|
|
|
|
+ $deviceMap = [];
|
|
|
|
|
|
|
|
foreach ($details as $row) {
|
|
foreach ($details as $row) {
|
|
|
if ($row->type == ItemDetails::type_one) {
|
|
if ($row->type == ItemDetails::type_one) {
|
|
|
$manMap[$row->data_id][] = $row->item_id;
|
|
$manMap[$row->data_id][] = $row->item_id;
|
|
|
|
|
+ $manIds[] = $row->data_id;
|
|
|
} elseif ($row->type == ItemDetails::type_two) {
|
|
} elseif ($row->type == ItemDetails::type_two) {
|
|
|
$deviceMap[$row->data_id][] = $row->item_id;
|
|
$deviceMap[$row->data_id][] = $row->item_id;
|
|
|
|
|
+ $devIds[] = $row->data_id;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 5. 生成随机比例分配 (复用之前定义的分配函数)
|
|
|
|
|
- $man_list = $this->distributeRates($manMap, RuleSetDetails::type_one);
|
|
|
|
|
- $device_list = $this->distributeRates($deviceMap, RuleSetDetails::type_two);
|
|
|
|
|
|
|
+ // 3. 批量获取人员和设备名称映射
|
|
|
|
|
+ $manNames = DB::table('employee')
|
|
|
|
|
+ ->whereIn('id', array_unique($manIds))
|
|
|
|
|
+ ->pluck('title', 'id')->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ $devNames = DB::table('device')
|
|
|
|
|
+ ->whereIn('id', array_unique($devIds))
|
|
|
|
|
+ ->pluck('title', 'id')->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ // 4. 生成随机比例分配,传入名称映射
|
|
|
|
|
+ $man_list = $this->distributeRates($manMap, RuleSetDetails::type_one, $manNames, $itemsData);
|
|
|
|
|
+ $device_list = $this->distributeRates($deviceMap, RuleSetDetails::type_two, $devNames, $itemsData);
|
|
|
|
|
|
|
|
return [true, [
|
|
return [true, [
|
|
|
'month' => date('Y-m', $monthStart),
|
|
'month' => date('Y-m', $monthStart),
|
|
@@ -465,9 +476,9 @@ class RuleSetService extends Service
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 核心算法:按对象随机分配整数比例,确保总和 100
|
|
|
|
|
|
|
+ * 核心算法:增加了名称字段的映射
|
|
|
*/
|
|
*/
|
|
|
- private function distributeRates($map, $type)
|
|
|
|
|
|
|
+ private function distributeRates($map, $type, $nameMap, $itemNames)
|
|
|
{
|
|
{
|
|
|
$result = [];
|
|
$result = [];
|
|
|
foreach ($map as $dataId => $items) {
|
|
foreach ($map as $dataId => $items) {
|
|
@@ -491,7 +502,7 @@ class RuleSetService extends Service
|
|
|
$rates[] = 100 - $currentTotal;
|
|
$rates[] = 100 - $currentTotal;
|
|
|
} else {
|
|
} else {
|
|
|
$val = (int)round(($weight / $sum) * 100);
|
|
$val = (int)round(($weight / $sum) * 100);
|
|
|
- $val = $val <= 0 ? 1 : $val; // 确保不为 0
|
|
|
|
|
|
|
+ $val = $val <= 0 ? 1 : $val;
|
|
|
$rates[] = $val;
|
|
$rates[] = $val;
|
|
|
$currentTotal += $val;
|
|
$currentTotal += $val;
|
|
|
}
|
|
}
|
|
@@ -502,7 +513,9 @@ class RuleSetService extends Service
|
|
|
$result[] = [
|
|
$result[] = [
|
|
|
'type' => $type,
|
|
'type' => $type,
|
|
|
'data_id' => $dataId,
|
|
'data_id' => $dataId,
|
|
|
|
|
+ 'data_title' => $nameMap[$dataId] ?? '', // 人员姓名或设备名称
|
|
|
'item_id' => $itemId,
|
|
'item_id' => $itemId,
|
|
|
|
|
+ 'item_title' => $itemNames[$itemId] ?? '', // 项目名称
|
|
|
'rate' => $rates[$index]
|
|
'rate' => $rates[$index]
|
|
|
];
|
|
];
|
|
|
}
|
|
}
|