123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- <?php
- namespace App\Service;
- use App\Model\ApplyOrder;
- use App\Model\ApplyOrderDetail;
- use App\Model\DispatchSub;
- use App\Model\Equipment;
- use App\Model\OrdersProduct;
- use App\Model\Process;
- use App\Model\SaleOrdersProduct;
- use App\Model\Scrapp;
- use App\Model\ScrappCount;
- use Illuminate\Support\Facades\DB;
- class LargeScreenService extends Service
- {
- public function integratedDashboard($data){
- //总产值 ------------------------------------------------
- $total = SaleOrdersProduct::where('del_time',0)
- ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
- ->select(DB::raw("sum(finished_num) as total"))
- ->value('total');
- //总产值 ------------------------------------------------
- //当日产量 当月产量 ---------------------------------------------
- $data_one = SaleOrdersProduct::where('del_time',0)
- ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
- ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
- ->select('finished_num','order_quantity','crt_time')
- ->get()->toArray();
- //当日产量 当月产量
- $today_total = $month_total = $order_total = 0;
- $timeStamp = strtotime(date("Y-m-d 00:00:00"));
- $timeStampE = strtotime(date("Y-m-d 23:59:59"));
- foreach ($data_one as $value){
- $order_total = bcadd($order_total, $value['order_quantity'],3);
- $month_total = bcadd($month_total, $value['finished_num'],3);
- if($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['finished_num'],3);
- }
- //当日产量 当月产量 ---------------------------------------------
- //月计划完成率--------------------------------------------------------------------
- $month_rate = floatval($order_total) > 0 ? bcdiv($month_total, $order_total,4) : 0;
- $month_rate = bcmul($month_rate,100,2) . "%";
- //月计划完成率--------------------------------------------------------------------
- //月注塑合格率----------------------------------------------------------------------
- $data_two = ScrappCount::where('del_time',0)
- ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
- ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
- ->where('process_id', 14)
- ->select(DB::raw("sum(quantity) as quantity"), DB::raw("sum(scrapp_num) as waste"))
- ->get()->toArray();
- $total_zj = $total_blp = 0;
- foreach ($data_two as $value){
- $total_zj = bcadd($total_zj, $value['quantity'],3);
- $total_zj = bcadd($total_zj, $value['waste'],3);
- $total_blp = bcadd($total_blp, $value['waste'],3);
- }
- $process_14_total = bcadd($total_zj, $total_blp,3);
- $process_14_rate = floatval($process_14_total) > 0 ? bcdiv($total_zj, $process_14_total,4) : 0;
- $process_14_rate = bcmul($process_14_rate, 100,2) . "%";
- //月注塑合格率----------------------------------------------------------------------
- //设备监控状态----------------------------------------------------------------------
- $deviceList = Equipment::where('del_time',0)
- ->where('model','<>',1)
- ->select('id','title','code','status')
- ->get()->toArray();
- foreach ($deviceList as $key => $value){
- $status_title = Equipment::$status_title[$value['status']] ?? "";
- $deviceList[$key]['status_title'] = $status_title;
- }
- //设备监控状态----------------------------------------------------------------------
- //质量管理--------------------------------------------------------------------------
- $dates = [];
- for ($i = 7; $i >= 0; $i--) {
- $timestamp = strtotime("-{$i} days");
- $date = date('m-d', $timestamp);
- $dates[] = $date;
- }
- $zl_data = [];
- $data_three = ScrappCount::where('del_time',0)
- ->where('crt_time', "<=", time())
- ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00",time() - 7 * 24 * 60 * 60)))
- ->where('process_id', 14)
- ->select('quantity', "scrapp_num as waste", "crt_time")
- ->get()->toArray();
- foreach ($data_three as $value){
- $day = date("m-d", $value['crt_time']);
- $tmp = bcadd($value['quantity'], $value['waste'], 3);
- if(isset($zl_data[$day])){
- $zl_data[$day]['quantity'] = bcadd($zl_data[$day]['quantity'], $value['quantity'],3);
- $zl_data[$day]['total'] = bcadd($zl_data[$day]['total'], $tmp,3);
- }else{
- $zl_data[$day] = [
- 'quantity' => $value['quantity'],
- 'total' => $tmp
- ];
- }
- }
- $zj_data_final = [];
- foreach ($dates as $value){
- $tmp = [
- "day" => $value,
- "total" => 0,
- "rate" => 0,
- ];
- if(isset($zl_data[$value])){
- $tmp['total'] = $zl_data[$value]['total'];
- $tmp['rate'] = bcmul(bcdiv($zl_data[$value]['quantity'], $zl_data[$value]['total'],4),100,2);
- }
- $zj_data_final[] = $tmp;
- }
- //质量管理--------------------------------------------------------------------------
- //物料管理--------------------------------------------------------------------------
- $thing = [
- ["name" => '物料一', 'rate' => 50],
- ["name" => '物料二', 'rate' => 60],
- ["name" => '物料三', 'rate' => 34],
- ["name" => '物料四', 'rate' => 22],
- ["name" => '物料五', 'rate' => 11],
- ["name" => '物料六', 'rate' => 22],
- ["name" => '物料七', 'rate' => 44],
- ["name" => '物料八', 'rate' => 32],
- ["name" => '物料九', 'rate' => 34],
- ["name" => '物料十', 'rate' => 51],
- ];
- //物料管理--------------------------------------------------------------------------
- //销售管理--------------------------------------------------------------------------
- $xs = SaleOrdersProduct::where('del_time',0)
- ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
- ->select(DB::raw("sum(finished_num) as total"),'technology_name as color')
- ->groupBy('technology_name')
- ->orderBy('total','desc')
- ->get()->toArray();
- //销售管理--------------------------------------------------------------------------
- //总装各线体目标/完成量---------------------------------------------------------------
- $zx_12_dispatch_quantity = $zx_12_finished_num = 0;
- $zx_14_dispatch_quantity_8 = $zx_14_finished_num_8 = 0;
- $zx_14_dispatch_quantity_9 = $zx_14_finished_num_9 = 0;
- $zx_14_dispatch_quantity_12 = $zx_14_finished_num_12 = 0;
- $zx_14_dispatch_quantity_13 = $zx_14_finished_num_13 = 0;
- $dispatch = DispatchSub::where('del_time',0)
- ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
- ->whereIn('process_id',[12,14])
- ->select('dispatch_quantity', 'finished_num', 'process_id','device_id')
- ->get()->toArray();
- foreach ($dispatch as $value){
- if($value['process_id'] == 12){
- $zx_12_dispatch_quantity = bcadd($zx_12_dispatch_quantity, $value['dispatch_quantity'],3);
- $zx_12_finished_num = bcadd($zx_12_finished_num, $value['finished_num'],3);
- }else{
- if($value['device_id'] == 8){
- $zx_14_dispatch_quantity_8 = bcadd($zx_14_dispatch_quantity_8, $value['dispatch_quantity'],3);
- $zx_14_finished_num_8 = bcadd($zx_14_finished_num_8, $value['finished_num'],3);
- }elseif ($value['device_id'] == 9){
- $zx_14_dispatch_quantity_9 = bcadd($zx_14_dispatch_quantity_9, $value['dispatch_quantity'],3);
- $zx_14_finished_num_9 = bcadd($zx_14_finished_num_9, $value['finished_num'],3);
- }elseif ($value['device_id'] == 12){
- $zx_14_dispatch_quantity_12 = bcadd($zx_14_dispatch_quantity_12, $value['dispatch_quantity'],3);
- $zx_14_finished_num_12 = bcadd($zx_14_finished_num_12, $value['finished_num'],3);
- }elseif ($value['device_id'] == 13){
- $zx_14_dispatch_quantity_13 = bcadd($zx_14_dispatch_quantity_13, $value['dispatch_quantity'],3);
- $zx_14_finished_num_13 = bcadd($zx_14_finished_num_13, $value['finished_num'],3);
- }
- }
- }
- $diff_num = bcsub($zx_12_dispatch_quantity, $zx_12_finished_num,3);
- $diff_num_8 = bcsub($zx_14_dispatch_quantity_8, $zx_14_finished_num_8,3);
- $diff_num_9 = bcsub($zx_14_dispatch_quantity_9, $zx_14_finished_num_9,3);
- $diff_num_12 = bcsub($zx_14_dispatch_quantity_12, $zx_14_finished_num_12,3);
- $diff_num_13 = bcsub($zx_14_dispatch_quantity_13, $zx_14_finished_num_13,3);
- $xt = [
- [
- "name" => "清洗脱水产线", "sj_num" => $zx_12_finished_num, "jh_num" => $zx_12_dispatch_quantity, "diff_num" => $diff_num
- ],[
- "name" => "注塑一号产线", "sj_num" => $zx_14_finished_num_8, "jh_num" => $zx_14_dispatch_quantity_8, "diff_num" => $diff_num_8
- ],[
- "name" => "注塑二号产线", "sj_num" => $zx_14_finished_num_9, "jh_num" => $zx_14_dispatch_quantity_9, "diff_num" => $diff_num_9
- ],[
- "name" => "注塑三号产线", "sj_num" => $zx_14_finished_num_12, "jh_num" => $zx_14_dispatch_quantity_12, "diff_num" => $diff_num_12
- ],[
- "name" => "注塑四号产线", "sj_num" => $zx_14_finished_num_13, "jh_num" => $zx_14_dispatch_quantity_13, "diff_num" => $diff_num_13
- ],
- ];
- //总装各线体目标/完成量---------------------------------------------------------------
- //订单完成进度-----------------------------------------------------------------
- $orderBy = [11 => "破碎分离", 12 => "清洗脱水" ,15 => "混合搅拌",13 => "挤出造粒",14 => "注塑成型"];
- $order = OrdersProduct::where('del_time',0)
- ->select('production_no as order_number','id','technology_name as color','production_quantity','dispatch_complete_quantity','finished_num')
- ->orderBy('id','desc')
- ->limit(10)
- ->get()->toArray();
- $dispatch_num = DispatchSub::where('del_time',0)
- ->whereIn('order_product_id',array_column($order,'id'))
- ->select('order_product_id','process_id','finished_num','dispatch_quantity','waste_num')
- ->get()->toArray();
- $details = [];
- foreach ($dispatch_num as $value){
- $new = $value['order_product_id'] . $value['process_id'];
- $finished_num = bcadd($value['finished_num'], $value['waste_num'],3);
- if(isset($details[$new])){
- $details[$new]['dispatch_quantity'] = bcadd($details[$new]['dispatch_quantity'], $value['dispatch_quantity'],3);
- $details[$new]['finished_num'] = bcadd($details[$new]['finished_num'], $finished_num,3);
- }else{
- $details[$new] = [
- "dispatch_quantity" => $value['dispatch_quantity'],
- "finished_num" => $finished_num,
- ];
- }
- }
- foreach ($order as $key => $value){
- $d_tmp = [];
- foreach ($orderBy as $k => $v){
- $new = $value['id'] . $k;
- $tmpS = $details[$new] ?? [];
- $rate = 0;
- if(! empty($tmpS)) {
- $rate = bcdiv($tmpS['finished_num'], $tmpS['dispatch_quantity'],4);
- $rate = bcmul($rate, 100,2);
- }
- $d_tmp[] = [
- "name" => $v,
- "rate" => $rate
- ];
- }
- $order[$key]['detail'] = $d_tmp;
- if($value['production_quantity'] <= $value['finished_num']){
- $state = "已完成";
- }else{
- $state = "进行中";
- }
- $order[$key]['state_title'] = $state;
- }
- //订单完成进度-----------------------------------------------------------------
- // dd($total, $today_total, $month_total, $month_rate, $process_14_rate, $deviceList, $zj_data_final, $thing, $xs, $xt,$order);
- $return = [
- 'total' => $total, //年总产量
- 'today_total' => $today_total, //当日产量
- 'month_total' => $month_total, //当月产量
- 'month_rate' => $month_rate, // 月计划完成比例
- 'process_14_rate' => $process_14_rate, // 注塑产线合格率
- 'device' => $deviceList, // 设备监控状态
- 'zj_result' => $zj_data_final, //质量管理
- 'thing' => $thing, //物料管理
- 'xs' => $xs, //销售管理
- 'xt' => $xt, //总装各线体目标/完成量
- 'order' => $order, // 订单完成进度
- ];
- return [true, $return];
- }
- public function productionExecution($data){
- //生产进度实时监控-----------------------------------------------------
- $order = OrdersProduct::where('del_time',0)
- ->limit(15)
- ->orderBy('id','desc')
- ->pluck('id')
- ->toArray();
- $dispatch = $this->getDispatch($order);
- $process_map = Process::whereIn('id',array_column($dispatch,'process_id'))
- ->pluck('title','id')
- ->toArray();
- foreach ($dispatch as $key => $value){
- if(in_array($value['process_id'], [11,12])){
- $cx = "清洗脱水产线";
- }else{
- if($value['device_id'] == 8){
- $cx = "注塑一号产线";
- }elseif ($value['device_id'] == 9){
- $cx = "注塑二号产线";
- }elseif ($value['device_id'] == 12){
- $cx = "注塑三号产线";
- }else {
- $cx = "注塑四号产线";
- }
- }
- $dispatch[$key]['cx'] = $cx;
- $dispatch[$key]['process_name'] = $process_map[$value['process_id']] ?? '';
- }
- //生产进度实时监控-----------------------------------------------------
- //订单看板-----------------------------------------------------
- $sales = SaleOrdersProduct::where('del_time',0)
- ->select('out_order_no as order_number','customer_name','product_title','technology_name as color','order_quantity', 'pre_shipment_time','finished_num','production_quantity')
- ->limit(10)
- ->orderBy('id','desc')
- ->get()->toArray();
- foreach ($sales as $key => $value){
- $sales[$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
- if(floatval($value['production_quantity']) <= 0.0){
- $state_title = "未排产";
- }elseif ($value['finished_num'] < $value['order_quantity']){
- $state_title = "进行中";
- }else{
- $state_title = "已完成";
- }
- $sales[$key]['pre_shipment_time'] = $state_title;
- }
- //订单看板-----------------------------------------------------
- //工序负荷全览-----------------------------------------------------
- $process = Process::where('del_time',0)->get()->toArray();
- $return = ApplyOrderDetail::selectRaw('
- DATE(FROM_UNIXTIME(crt_time)) as date,
- ROUND(SUM(quantity), 3) as dispatch_quantity
- ')
- ->where('del_time', 0)
- ->where('type', ApplyOrder::type_one)
- ->groupBy('date')
- ->orderBy('date', 'desc') // 可选:按日期倒序
- ->get()
- ->map(function ($item) {
- $item->date = str_replace('-', '', $item->date); // 转为 Ymd 格式字符串
- return $item;
- })
- ->pluck('dispatch_quantity', 'date')
- ->toArray();
- $maxValue = empty($return) ? 0 : max($return);
- $today = $return[date("Ymd")] ?? 0;
- $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
- $array_p = [];
- foreach ($process as $value){
- $array_p[] = [
- 'title' => $value['title'],
- 'rate' => $rate
- ];
- }
- //工序负荷全览-----------------------------------------------------
- //双周生产计划达成率-----------------------------------------------------
- $dates = [];
- $first = $end = 0;
- for ($i = 14; $i >= 0; $i--) {
- $timestamp = strtotime("-{$i} days");
- if($i == 14) $first = strtotime(date("Y-m-d 00:00:00",$timestamp));
- if($i == 0) $end = strtotime(date("Y-m-d 00:00:00",$timestamp));
- $dates[] = strtotime(date("Y-m-d 00:00:00",$timestamp));
- }
- $results = DispatchSub::where('del_time', 0)
- ->where('dispatch_time_start', '>=', $first) // 计划开始 < 给定结束
- ->where('dispatch_time_end', '<=', $end) // 计划结束 > 给定开始
- ->where('process_id',14)
- ->select('dispatch_time_start as crt_time',DB::raw("sum(dispatch_quantity) as quantity"))
- ->groupBy('dispatch_time_start')
- ->get()->toArray();
- $map = array_column($results,'quantity','crt_time');
- $jd = [];
- foreach ($dates as $value){
- if(isset($map[$value])){
- $jd[$value] = [
- 'plan_num' => $map[$value],
- 'sj_num' => 0,
- 'rate' => 0,
- 'day' => date("m-d", $value)
- ];
- }else{
- $jd[$value] = [
- 'plan_num' => 0,
- 'sj_num' => 0,
- 'rate' => 0,
- 'day' => date("m-d", $value)
- ];
- }
- }
- $pending = 0; // 待完成的任务量(滚存)
- foreach ($jd as $timestamp => &$item) {
- $totalWork = $pending + $item['plan_num']; // 可用任务总量(含滚存)
- // 当天最多完成
- $item['sj_num'] = min(42, $totalWork);
- // 剩余未完成任务,滚入下一天
- $pending = bcsub($totalWork, $item['sj_num'], 3);
- // 计算 rate
- if ($item['plan_num'] == 0) {
- // 没有计划 → 完成率 0%
- $item['rate'] = 0;
- } else {
- // 有计划 → 完成率 = 实际 / 计划
- $item['rate'] = round(($item['sj_num'] / $item['plan_num']) * 100, 2);
- }
- }
- //双周生产计划达成率-----------------------------------------------------
- list($status, $return) = (new ApplyOrderService())->reportList(['page_size' => 3, 'page_index' => 1,'not_need' =>1]);
- $cs = $return['list'] ?? [];
- $cs = $cs['data'] ?? [];
- $cs = array_column($cs,'message');
- $return = [
- 'dispatch' => $dispatch, //生产进度实时监控
- 'sales' => $sales, //订单看板
- 'array_p' => $array_p, //工序负荷全览
- 'jd' => array_values($jd), // 生产进度实时监控
- 'cs' => $cs
- ];
- return [true, $return];
- }
- private function getDispatch($order){
- $customOrder = [11, 12, 15, 13, 14];
- $priorityMap = array_flip($customOrder); // [11=>0, 12=>1, 15=>2, 13=>3, 14=>4]
- // 先从数据库取出数据(只需按 id 倒序即可)
- $dispatch = DispatchSub::where('del_time', 0)
- ->whereIn('order_product_id', $order)
- ->where('dispatch_quantity', '>', 1)
- ->select(
- 'process_id',
- 'dispatch_quantity',
- 'finished_num',
- DB::raw('finished_num as bg_num'),
- DB::raw('finished_num as hg_num'),
- 'waste_num',
- 'device_id',
- 'order_product_id',
- 'id',
- 'technology_name as color',
- 'product_title'
- )
- ->orderBy('id', 'desc')
- ->get()
- ->toArray();
- // === 在 PHP 中分组并排序 ===
- $grouped = [];
- foreach ($dispatch as $item) {
- $grouped[$item['order_product_id']][] = $item;
- }
- // 对每一组按 process_id 自定义顺序排序
- foreach ($grouped as &$group) {
- usort($group, function ($a, $b) use ($priorityMap) {
- $posA = $priorityMap[$a['process_id']] ?? 999;
- $posB = $priorityMap[$b['process_id']] ?? 999;
- return $posA <=> $posB; // 升序:11(0), 12(1), 15(2), 13(3), 14(4)
- });
- }
- unset($group);
- // 合并成扁平数组(保持 order_product_id 之间的顺序为 id 倒序)
- $sortedDispatch = [];
- $seenOrderIds = [];
- // 按原始 id 倒序决定 order_product_id 的出现顺序
- foreach ($dispatch as $item) {
- $orderId = $item['order_product_id'];
- if (!in_array($orderId, $seenOrderIds)) {
- $seenOrderIds[] = $orderId;
- }
- }
- // 按首次出现的 order_product_id 顺序(即 id 倒序)拼接每组数据
- foreach ($seenOrderIds as $orderId) {
- if (isset($grouped[$orderId])) {
- foreach ($grouped[$orderId] as $item) {
- $sortedDispatch[] = $item;
- }
- }
- }
- return $sortedDispatch;
- }
- public function qualityDashboard($data)
- {
- //质量趋势图 以及 合格率----------------------------------------------------
- // 近15天日期戳
- $dates = [];
- for ($i = 14; $i >= 0; $i--) {
- $timestamp = strtotime("-{$i} days");
- $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
- }
- $dispatch = ScrappCount::where('del_time', 0)
- ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
- ->whereIn('process_id', [12, 14])
- ->select('scrapp_num as dispatch_quantity', 'quantity as finished_num', 'process_id', 'equipment_id as device_id', 'crt_time')
- ->get()
- ->toArray();
- // 数据聚合
- $time_map = []; // 清洗脱水线
- $time_map_2 = []; // 注塑多条产线
- $total_map = []; // 所有产线总汇总
- foreach ($dispatch as $value) {
- $time_tmp = strtotime(date("Ymd 00:00:00", $value['crt_time']));
- $total = bcadd($value['finished_num'], $value['dispatch_quantity'], 3);
- // ✅ 所有产线的汇总
- if (!isset($total_map[$time_tmp])) {
- $total_map[$time_tmp] = ['total' => 0, 'finished' => 0];
- }
- $total_map[$time_tmp]['total'] = bcadd($total_map[$time_tmp]['total'], $total, 3);
- $total_map[$time_tmp]['finished'] = bcadd($total_map[$time_tmp]['finished'], $value['finished_num'], 3);
- if ($value['process_id'] == 12) {
- // 清洗脱水产线
- if (!isset($time_map[$time_tmp])) {
- $time_map[$time_tmp] = ['total' => 0, 'finished' => 0];
- }
- $time_map[$time_tmp]['total'] = bcadd($time_map[$time_tmp]['total'], $total, 3);
- $time_map[$time_tmp]['finished'] = bcadd($time_map[$time_tmp]['finished'], $value['finished_num'], 3);
- } else{
- // 注塑产线
- $key = "{$value['device_id']}|{$time_tmp}";
- if (!isset($time_map_2[$key])) {
- $time_map_2[$key] = ['total' => 0, 'finished' => 0];
- }
- $time_map_2[$key]['total'] = bcadd($time_map_2[$key]['total'], $total, 3);
- $time_map_2[$key]['finished'] = bcadd($time_map_2[$key]['finished'], $value['finished_num'], 3);
- }
- }
- // 各产线配置
- $lines = [
- '清洗脱水产线' => ['map' => &$time_map, 'key_prefix' => null],
- '注塑一号产线' => ['map' => &$time_map_2, 'key_prefix' => '8|'],
- '注塑二号产线' => ['map' => &$time_map_2, 'key_prefix' => '9|'],
- '注塑三号产线' => ['map' => &$time_map_2, 'key_prefix' => '12|'],
- '注塑四号产线' => ['map' => &$time_map_2, 'key_prefix' => '13|'],
- '总合格率' => ['map' => &$total_map, 'key_prefix' => null, 'is_total' => true],
- ];
- $result = [];
- foreach ($lines as $name => $cfg) {
- $map = $cfg['map'];
- $prefix = $cfg['key_prefix'] ?? null;
- $arr = [];
- foreach ($dates as $date_ts) {
- $key = $prefix ? "{$prefix}{$date_ts}" : $date_ts;
- $a = $map[$key]['total'] ?? 0;
- $b = $map[$key]['finished'] ?? 0;
- $rate = 0.00;
- if (floatval($a) > 0.0) {
- $rate = bcmul(bcdiv($b, $a, 4), 100, 2);
- }
- $arr[date("m-d", $date_ts)] = $rate;
- }
- $result[] = [
- 'name' => $name,
- 'array' => $arr,
- ];
- }
- $end = array_pop($result);
- $end = $end["array"];
- //质量趋势图 end 以及 合格率 $result ----------------------------------------------------
- //不合格原因分析--------------------------------------------------------------
- $s = ScrappCount::where('del_time', 0)
- ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
- ->where('scrapp_id', '>', 0)
- ->select(
- 'scrapp_id',
- DB::raw('SUM(scrapp_num) as total'),
- )
- ->groupBy('scrapp_id')
- ->orderBy('total','desc')
- ->get()
- ->toArray();
- $sum = ! empty($s) ? array_sum(array_column($s,'total')) : 0;
- $map1 = Scrapp::where('del_time',0)
- ->pluck('title', 'id')
- ->toArray();
- foreach ($s as $key => $value){
- $s[$key]['rate'] = ! empty($sum) ? bcmul(bcdiv($value['total'], $sum, 4),100,2): 0;
- $s[$key]['title'] = $map1[$value['scrapp_id']] ?? "";
- }
- //不合格原因分析--------------------------------------------------------------
- //明细派工单对应质检完成率-----------------------------------------------------
- $cx_map = [
- 11 => "清洗脱水产线",
- 12 => "清洗脱水产线" ,
- 15 => "注塑",
- 13 => "注塑",
- 14 => "注塑"
- ];
- $dispatch_2 = DispatchSub::where('del_time', 0)
- ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
- ->select('finished_num', 'process_id', 'crt_time','dispatch_quantity','device_id','product_title','technology_name as color','waste_num')
- ->get()
- ->toArray();
- $pro_map = Process::where('del_time',0)
- ->pluck('title','id')
- ->toArray();
- foreach ($dispatch_2 as $key => $value) {
- $dispatch_2[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
- $cx = $cx_map[$value['process_id']] ?? "";
- if(in_array($value['process_id'], [15,13,14])){
- if($value['device_id'] == 8){
- $cx .= "一号产线";
- }elseif ($value['device_id'] == 9){
- $cx .= "二号产线";
- }elseif ($value['device_id'] == 12){
- $cx .= "三号产线";
- }else {
- $cx .= "四号产线";
- }
- }
- $dispatch_2[$key]['cx'] = $cx;
- $num = bcadd($value['finished_num'], $value['waste_num'],3);
- $dispatch_2[$key]['rate'] = bcmul(bcdiv($num, $value['dispatch_quantity'],4),100,2);
- }
- //明细派工单对应质检完成率-----------------------------------------------------
- //过程质量监控------------------------------------------------------
- $s_3 = DispatchSub::from('dispatch_sub as a')
- ->join('zj_plan as b','b.id','a.zj_plan_id')
- ->join('zj as c',DB::raw('FIND_IN_SET(c.id, b.zj_id)'), '>', DB::raw('0'))
- ->where('a.del_time', 0)
- // ->where('a.waste_num', '>',0)
- ->where('a.crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
- ->whereIn('a.process_id',[12,14])
- ->select("a.process_id","a.product_title",'a.technology_name as color','a.waste_num','c.title as c_title','a.crt_time','a.order_product_id')
- ->orderBy('a.id','desc')
- ->get()
- ->toArray();
- foreach ($s_3 as $key => $value){
- $s_3[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
- if($value['waste_num'] > 0){
- $title = (rand(1, 100) <= 60) ? '合格' : '不合格';
- }else{
- $title = "合格";
- }
- $s_3[$key]['hg_title'] = $title;
- }
- //过程质量监控------------------------------------------------------
- $return = [
- 'zl' => $end, //质量趋势图
- 'zl_rate' => $result, //质量合格率
- 'reason' => $s, //不合格原因分析
- 'detail' => $dispatch_2, // 明细派工单对应质检完成率
- 'procedure' => $s_3, //过程质量监控
- ];
- return [true, $return];
- }
- }
|