标签搜索
PHP

分班问题

wehg489
2025-04-16 / 0 评论 / 1 阅读 / 正在检测是否收录...
<?php

header('Content-Type: application/json');

$data = json_decode(file_get_contents('php://input'), true)['data'];



$pdo = new PDO('mysql:host=localhost;dbname=ytpmc;charset=utf8', 'ytpmc', 'ytpmc321');



// 读取共用模具信息

$gongyongData = [];

$sql = "SELECT productId, promj, promjsl FROM gongyong";

foreach ($pdo->query($sql) as $row) {

    $gongyongData[$row['productId']] = [

        'mould' => $row['promj'],

        'limit' => $row['promjsl'] * 6

    ];

}



// 按日期分组

$grouped = [];

foreach ($data as $row) {

    list($productId, $qty, $date,$sx) = $row;

    $grouped[$date][] = ['productId' => $productId, 'quantity' => (int)$qty, 'sx' => $sx];

}



$result = [];



foreach ($grouped as $date => $items) {

    $A = []; $B = [];

    $sumA = 0; $sumB = 0;

    $typesA = []; $typesB = [];

    $mouldA = []; $mouldB = [];

    $warnings = [];

    $mouldProducts = []; // 存储共用模具的产品信息

    $mouldTotalUsage = ['A' => [], 'B' => []]; // 存储各班次模具总使用量



    // 预先建立模具到产品的映射关系

    foreach ($items as $item) {

        $pid = $item['productId'];
        //$sx=$item['sx'];

        if (isset($gongyongData[$pid])) {

            $mould = $gongyongData[$pid]['mould'];

            $mouldProducts[$mould][] = $pid;

        }

    }



    // 拆分任务:将数量 > 10 的产品拆成多个

    $splitItems = [];

    foreach ($items as $item) {
        
      

        if ($item['quantity'] <= 10 && $item['quantity']<=$item['sx'] ) {

            $splitItems[] = $item;
          

        } else {


            $qty = $item['quantity'];

            $half = (int)ceil($qty / 2);

            $splitItems[] = ['productId' => $item['productId'], 'quantity' => $half,'sx' => $item['sx']];

            $splitItems[] = ['productId' => $item['productId'], 'quantity' => $qty - $half,'sx' => $item['sx']];

        }

    }



    // 排序优化(先排大任务)

    usort($splitItems, function ($a, $b) {

        return $b['quantity'] - $a['quantity'];

    });



    // 分配任务

    foreach ($splitItems as $item) {

        $pid = $item['productId'];

        $qty = $item['quantity'];
        
        $sx=$item['sx'];
        
       

        $mould = isset($gongyongData[$pid]) ? $gongyongData[$pid]['mould'] : null;

        $limit = isset($gongyongData[$pid]) ? $gongyongData[$pid]['limit'] : null;



        // 检查产品是否已在班次中

        $inA = isset($typesA[$pid]);

        $inB = isset($typesB[$pid]);

        

        // 计算模具总使用量(同班次所有共用该模具的产品)

        $mouldATotal = ($mould && isset($mouldTotalUsage['A'][$mould])) ? $mouldTotalUsage['A'][$mould] : 0;

        $mouldBTotal = ($mould && isset($mouldTotalUsage['B'][$mould])) ? $mouldTotalUsage['B'][$mould] : 0;



        // 判断分配条件

        $canA = !$inA && ($mould ? $mouldATotal + $qty <= 10000 : true);

        $canB = !$inB && ($mould ? $mouldBTotal + $qty <= 10000 : true);



        if (!$canA && !$canB) {

            $sharedProducts = isset($mouldProducts[$mould]) ? implode(',', array_unique($mouldProducts[$mould])) : '';

            $warnings[] = "模具 {$mould} 已达到上限 | 共用产品: {$sharedProducts} | 上限: {$limit} | A班已用: {$mouldATotal} | B班已用: {$mouldBTotal}";

            continue;

        }



        // 分配逻辑

        if ($sumA <= $sumB+70 && $canA) {

            $A[] = ['productId' => $pid, 'quantity' => $qty, 'sx' => $sx];

            $sumA += $qty;

            $typesA[$pid] = true;

            if ($mould) {

                $mouldTotalUsage['A'][$mould] = ($mouldTotalUsage['A'][$mould] ?? 0) + $qty;

            }

        } elseif ($sumB < $sumA && $canB) {

            $B[] = ['productId' => $pid, 'quantity' => $qty, 'sx' => $sx];

            $sumB += $qty;

            $typesB[$pid] = true;

            if ($mould) {

                $mouldTotalUsage['B'][$mould] = ($mouldTotalUsage['B'][$mould] ?? 0) + $qty;

            }

        } elseif ($canA) {

            $A[] = ['productId' => $pid, 'quantity' => $qty, 'sx' => $sx];

            $sumA += $qty;

            $typesA[$pid] = true;

            if ($mould) {

                $mouldTotalUsage['A'][$mould] = ($mouldTotalUsage['A'][$mould] ?? 0) + $qty;

            }

        } elseif ($canB) {

            $B[] = ['productId' => $pid, 'quantity' => $qty, 'sx' => $sx];

            $sumB += $qty;

            $typesB[$pid] = true;

            if ($mould) {

                $mouldTotalUsage['B'][$mould] = ($mouldTotalUsage['B'][$mould] ?? 0) + $qty;

            }

        }

    }



    // 检查是否有共用模具超限情况

    foreach ($mouldTotalUsage['A'] as $mould => $used) {

        if ($used > $gongyongData[$mouldProducts[$mould][0]]['limit']) {

            $sharedProducts = implode(',', array_unique($mouldProducts[$mould]));

            $limit = $gongyongData[$mouldProducts[$mould][0]]['limit'];

            $warnings[] = "A班模具 {$mould} 共用超限需增加模具数量 | 共用产品: {$sharedProducts} | 上限: {$limit} | 已排产: {$used}";

        }

    }

    

    foreach ($mouldTotalUsage['B'] as $mould => $used) {

        if ($used > $gongyongData[$mouldProducts[$mould][0]]['limit']) {

            $sharedProducts = implode(',', array_unique($mouldProducts[$mould]));

            $limit = $gongyongData[$mouldProducts[$mould][0]]['limit'];

            $warnings[] = "B班模具 {$mould} 共用超限需增加模具数量 | 共用产品: {$sharedProducts} | 上限: {$limit} | 已排产: {$used}";

        }

    }



    $result[$date] = [

        'A' => $A,

        'B' => $B,

        'sumA' => $sumA,

        'sumB' => $sumB,

        'typesA' => count($typesA),

        'typesB' => count($typesB),

        'warnings' => array_unique($warnings), // 去重

        'mouldUsage' => [

            'A' => $mouldTotalUsage['A'],

            'B' => $mouldTotalUsage['B']

        ],

        'mouldInfo' => $mouldProducts

    ];

}



echo json_encode($result, JSON_UNESCAPED_UNICODE);
0

评论 (0)

取消
歌曲封面
0:00