标签搜索

测试

wehg489
2025-04-19 / 0 评论 / 2 阅读 / 正在检测是否收录...
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Content-Type');

try {
    $input = json_decode(file_get_contents('php://input'), true);

    if (!$input || !isset($input['data']) || empty($input['data'])) {
        throw new Exception('无效数据,请检查输入');
    }

    $rawData = $input['data'];
    $workers = isset($input['workers']) ? intval($input['workers']) : 3;
    $productivity = isset($input['productivity']) ? intval($input['productivity']) : 70;
    $startDateStr = isset($input['startDate']) ? $input['startDate'] : null;

    if (!$startDateStr) {
        throw new Exception('请提供排产开始日期');
    }

    // 数据库连接
    $pdo = new PDO("mysql:host=localhost;dbname=ytpmc;charset=utf8", "ytpmc", "ytpmc321");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 查询 sl 表
    $slMap = [];
    $stmt = $pdo->query("SELECT productId, sl FROM text");
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $slMap[$row['productId']] = intval($row['sl']);
    }

    $dailyCapacity = $workers * $productivity;
    $scheduleData = [];
    $warningData = [];

    usort($rawData, function ($a, $b) {
        $dateDiff = strtotime($a['deliveryDate']) - strtotime($b['deliveryDate']);
        return $dateDiff !== 0 ? $dateDiff : ($a['originalQuantity'] - $b['originalQuantity']);
    });

    $currentDate = new DateTime($startDateStr);
    $dailyRemaining = $dailyCapacity;
    $dailyProductionMap = []; // 每日产品排产汇总
    $dailyProductCount = 0;
    $queue = $rawData;

    while (!empty($queue)) {
        $isAnyOrderScheduled = false;

        foreach ($queue as $key => &$item) {
            $productId = $item['productId'];
            $quantityRemaining = $item['quantity'];

            // 获取 sl,并同步 dailyMax
            if (isset($slMap[$productId])) {
                $sl = $slMap[$productId];
                $item['dailyMax'] = min($item['dailyMax'], $sl);
            } else {
                $sl = $item['dailyMax'];
            }

            $alreadyProducedToday = $dailyProductionMap[$productId]['quantity'] ?? 0;

            // 还可排产量(不能超过 dailyMax,也不能超过日产能剩余)
            $maxAllocatable = min(
                $quantityRemaining,
                $item['dailyMax'] - $alreadyProducedToday,
                $dailyRemaining
            );

            if ($maxAllocatable > 0) {
                // 累加数量
                if (isset($dailyProductionMap[$productId])) {
                    $dailyProductionMap[$productId]['quantity'] += $maxAllocatable;
                } else {
                    $dailyProductionMap[$productId] = [
                        'productId' => $productId,
                        'quantity' => $maxAllocatable,
                        'deliveryDate' => $item['deliveryDate'],
                        'dailyMax' => $item['dailyMax'],
                        'productionDate' => $currentDate->format('Y-m-d'),
                        'isLate' => strtotime($currentDate->format('Y-m-d')) > strtotime($item['deliveryDate']),
                        'sl' => $sl
                    ];
                }

                $item['quantity'] -= $maxAllocatable;
                $dailyRemaining -= $maxAllocatable;
                $dailyProductCount++;
                $isAnyOrderScheduled = true;

                if ($item['quantity'] <= 0) {
                    unset($queue[$key]);
                }
            }

            if ($dailyProductCount >= 80) {
                break;
            }
        }

        // 保存每日排产记录
        if (!empty($dailyProductionMap)) {
            foreach ($dailyProductionMap as $record) {
                $scheduleData[] = $record;
            }
        }

        // 下一天
        $currentDate->modify('+1 day');
        $dailyRemaining = $dailyCapacity;
        $dailyProductCount = 0;
        $dailyProductionMap = [];
    }

    // 预警检查
    foreach ($rawData as $item) {
        $lastProdDate = null;
        foreach ($scheduleData as $schedule) {
            if ($schedule['productId'] === $item['productId'] && $schedule['deliveryDate'] === $item['deliveryDate']) {
                $lastProdDate = new DateTime($schedule['productionDate']);
            }
        }

        if ($lastProdDate && $lastProdDate > new DateTime($item['deliveryDate'])) {
            $lateDays = $lastProdDate->diff(new DateTime($item['deliveryDate']))->days;

            $warningData[] = [
                'productId' => $item['productId'],
                'quantity' => $item['originalQuantity'],
                'deliveryDate' => $item['deliveryDate'],
                'dailyMax' => $item['dailyMax'],
                'productionDate' => $lastProdDate->format('Y-m-d'),
                'lateDays' => $lateDays
            ];
        }
    }

    echo json_encode([
        'success' => true,
        'scheduleData' => $scheduleData,
        'warningData' => $warningData
    ]);

} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?>
0

评论 (0)

取消
歌曲封面
0:00