model = new \app\admin\model\Feeding; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 添加 * * @return string * @throws \think\Exception */ public function add() { if (false === $this->request->isPost()) { //车间=>操作人员 $cjcz = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjcz")->order('name desc')->select(); //车间=>检验人员 $cjjy = Db::name('personnel')->where('bid',"=",5)->where('position','=',"cjjy")->order('name desc')->select(); $this->assign('cjcz',$cjcz); $this->assign('cjjy',$cjjy); //通过账号自动获取 操作人员 检验人员 固定机台名称 $user_info = Session::get('admin'); $jinayan = []; if($user_info['username'] === '111'){ $jinayan = '丙类一号机'; }else if($user_info['username'] === '222'){ $jinayan = '丙类二号机'; }else if($user_info['username'] === '333'){ $jinayan = '甲类一号机'; }else if($user_info['username'] === '444'){ $jinayan = '甲类二号机'; }else{ $jinayan = '甲类三号机'; } $this->assign('jinayan',$jinayan); return $this->view->fetch(); } $tid = Db::name('task')->where('bach',$_POST['bach'])->order('id desc')->find(); if(empty($_POST['weight'])){ $this->error('投料数据不能为空'); } $feeding_no = Db::name('feeding')->where('bach',$_POST['bach'])->order('id desc')->find(); if (isset($feeding_no['no'])) { // 确保 $feeding_no 是一个数组且 $feeding_no['no'] 已设置 $param['no'] = $feeding_no['no'] + 1; } else { // $feeding_no 不存在,或者 $feeding_no['no'] 未设置 $param['no'] = 1; } // $param['no'] = $fno['no'] +1;//操作顺序 $param['tid'] = $tid['id'];//关联批次idx $param['fid'] = $_POST['fid'];//关联批次idx $param['bach'] = $_POST['bach'];//批次号 $param['date'] = $_POST['date'];//日期 $param['operator'] = $_POST['operator'];//操作人员 $param['inspector'] = $_POST['inspector'];//检验人员 $param['material'] = $_POST['material'];//原材料 $param['nweight'] = $_POST['nweight'];//应投重量 $param['gy_num'] = $_POST['gy_num'];//工艺序号 $param['weight'] = $_POST['weight'];//投料重量 $param['material_bach'] = $_POST['material_bach'];//原材料批次号 $param['remark'] = $_POST['gy_name'];//操作记录 $param['create'] = date('Y-m-d H:i:s');//创建时间 $feeding = Db::name('feeding')->insert($param); if($feeding){return "添加成功";}else{return "添加失败";} // $params = $this->request->post('row/a'); // $params = $this->preExcludeFields($params); // $tid = Db::name('task')->where('bach',$params['bach'])->order('id desc')->find(); // $arr = []; // if(empty($params['weight'])){ // $this->error('投料数据不能为空'); // } // foreach($params['weight'] as $k=>$v){ // if($v){ // $param['bach'] = $params['bach']; // $param['date'] = $params['date']; // $param['operator'] = $params['operator']; // $param['inspector'] = $params['inspector']; // $param['weight'] = $params['weight'][$k]; // $param['nweight'] = $params['nweight'][$k]; // $param['material'] = $params['material'][$k]; // $param['gy_num'] = $params['gy_num'][$k]; // $param['tid'] = $tid['id']; // $param['create'] = date('Y-m-d H:i:s'); // $arr[] = $param; // } // } // //去掉已经存入数据库的工艺数据 // $list = array(); // foreach ($arr as $key=>$value){ // $map=[]; // $map['bach'] = $value['bach']; // $map['material'] = $value['material']; // $map['gy_num'] = $value['gy_num']; // $is_feeding_gy = Db::name('feeding')->where($map)->order('id desc')->find(); // if (!$is_feeding_gy){ // $list[$key] = $value; // } // } // $result = false; // Db::startTrans(); // try { // //是否采用模型验证 // if ($this->modelValidate) { // $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); // $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; // $this->model->validateFailException()->validate($validate); // } // // $result = $this->model->allowField(true)->saveAll($list); // Db::commit(); // } catch (ValidateException|PDOException|Exception $e) { // Db::rollback(); // $this->error($e->getMessage()); // } // if ($result === false) { // $this->error(__('No rows were inserted')); // } // $this->success(); } //投料添加页面=>修改应投重量 // public function submits(){ // $param = $this->request->post(); // $where = []; // $where['bach'] = $param['bach']; // $id = Db::name('feeding')->where($where)->select(); // echo "
";
//        print_r($id);
//        echo "
";die; // $arr['weight'] = $param['weight']; // $feeding = Db::name('feeding')->where('id',$id['id'])->select(); // echo "
";
//        print_r($feeding);
//        echo "
";die; // if(!$feeding){ // return '修改失败'; // }else{ // return '修改成功'; // } // } //获取作业票信息 public function get_task(){ $bach = $this->request->post('bach'); $year = date('Y'); // 获取当前年份 $row = Db::name('feeding')->where('bach',$bach)->select(); $res = Db::name('task')->where('bach',$bach)->order('create','desc')->select(); //已有过工序 if($row){ $result['inspector'] = $row[0]['inspector']; $result['operator'] = $row[0]['operator']; }else{ $result['inspector'] = ''; $result['operator'] = ''; } $result['data'] = $res; return json($result); } //获取配方信息 原始代码 public function get_formulas(){ $bach = $this->request->post('bach');//批次号 $num = $this->request->post('num');//生产量 $process = Db::name('feeding')->where('bach',$bach)->select(); if($num){//如果有,批次号重复,需精确查找 $res = Db::name('task')->alias('t') ->join('formula_detail f','f.pid=t.fid','left') ->field('f.id as fid,f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan') ->where('t.bach',$bach)->where('t.number',$num)->select(); }else{//如果没有,,批次号未重复,直接差出数据 $res = Db::name('task')->alias('t') ->join('formula_detail f','t.fid = f.pid','left') ->field('f.id as fid,f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan') ->where('t.bach',$bach)->select(); } $pro = 0; //按照百分比计算出应投重量 foreach($res as &$v){ $v['weight']=0; $v['material_bach'] = ''; if($process){ //已有工序,接上一次工序 foreach ($process as $val){ if($val['fid']==$v['fid'] && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){ $v['weight']=$val['weight']; $v['material'] = $val['material']; $v['material_bach']=$val['material_bach']; $pro = $val['gy_num']; } } } if($v['gy_name'] == null){ $v['gy_name'] = ''; } if($v['gy_num'] == null){ $v['gy_num'] = ''; } if($v['material'] == null){ $v['material'] = ''; } if($v['material_bach'] == null){ $v['material_bach'] = ''; } $total = array_column($res,'percentage'); foreach ($total as $key=>$value){ $total[$key] = decode($value); } $num = array_sum($total); if($v['kuodan']){ $kd_sum = $v['number']+$v['kuodan']; if($v['percentage']){ $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000); $v['nweight'] = number_format($number/1000,3); }else{ $v['nweight']=''; } }else{ if($v['percentage']){ $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000); $v['nweight'] = number_format($number/1000,3); }else{ $v['nweight']=''; } } } $row['total']=$num; $row['data'] = $res; $row['process'] = $pro+1; return json($row); } //获取配方信息 public function get_formula(){ // $bach = $this->request->post('bach');//批次号 // $num = $this->request->post('num');//生产量 // $process = Db::name('feeding')->where('bach',$bach)->select(); // if($num){//如果有,批次号重复,需精确查找 // $res = Db::name('task')->alias('t') // ->join('formula_detail f','f.pid=t.fid','left') // ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan') // ->where('t.bach',$bach)->where('t.number',$num)->select(); // }else{//如果没有,,批次号未重复,直接差出数据 // $res = Db::name('task')->alias('t') // ->join('formula_detail f','t.fid = f.pid','left') // ->field('f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan') // ->where('t.bach',$bach)->select(); // } // $pro = 0; // //按照百分比计算出应投重量 // foreach($res as &$v){ // $v['weight']=0; // $v['material_bach'] = ''; // if($process){ //已有工序,接上一次工序 // foreach ($process as $val){ // if(($val['material']==$v['material'] || in_array($val['material'],explode('/',$v['material']))) && $val['tid']==$v['id'] && $val['gy_num'] == $v['gy_num']){ // $v['weight']=$val['weight']; // $v['material_bach']=$val['material_bach']; // $pro = $val['gy_num']; // } // } // } // if($v['gy_name'] == null){ // $v['gy_name'] = ''; // } // if($v['gy_num'] == null){ // $v['gy_num'] = ''; // } // if($v['material'] == null){ // $v['material'] = ''; // } // if($v['material_bach'] == null){ // $v['material_bach'] = ''; // } // $total = array_column($res,'percentage'); // foreach ($total as $key=>$value){ // $total[$key] = decode($value); // } // $num = array_sum($total); // if($v['kuodan']){ // $kd_sum = $v['number']+$v['kuodan']; // if($v['percentage']){ // $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000); // $v['nweight'] = number_format($number/1000,3); // }else{ // $v['nweight']=''; // } // }else{ // if($v['percentage']){ // $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000); // $v['nweight'] = number_format($number/1000,3); // }else{ // $v['nweight']=''; // } // } // } // $row['total']=$num; // $row['data'] = $res; // $row['process'] = $pro+1; // return json($row); //按照工序显示,第一个工序做完,才能显示下一个工序 $bach = $this->request->post('bach');//批次号 $num = $this->request->post('num');//生产量 $process = Db::name('feeding')->where('bach',$bach)->select(); if($num){//如果有,批次号重复,需精确查找 $res = Db::name('task')->alias('t') ->join('formula_detail f','f.pid=t.fid','left') ->field('f.id as fid, f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan') ->where('t.bach',$bach)->where('t.number',$num)->select(); }else{//如果没有,,批次号未重复,直接差出数据 $res = Db::name('task')->alias('t') ->join('formula_detail f','t.fid = f.pid','left') ->field('f.id as fid, f.material,f.percentage,f.gy_name,f.gy_num,t.number,t.id,t.kuodan') ->where('t.bach',$bach)->select(); } $pro = 0; //按照百分比计算出应投重量 foreach($res as &$v){ $v['weight']=0; $v['material_bach'] = ''; if($process){ //已有工序,接上一次工序 foreach ($process as $val){ if( $val['fid'] == $v['fid'] && $val['tid'] == $v['id'] && $val['gy_num'] == $v['gy_num']){ $v['weight']= $val['weight']; // $v['material'] = $val['material']; $v['material_bach']=$val['material_bach']; $pro = $val['gy_num']; } } } if($v['gy_name'] == null){ $v['gy_name'] = ''; } if($v['gy_num'] == null){ $v['gy_num'] = ''; } if($v['material'] == null){ $v['material'] = ''; } if($v['material_bach'] == null){ $v['material_bach'] = ''; } $total = array_column($res,'percentage'); foreach ($total as $key=>$value){ $total[$key] = decode($value); } $num = array_sum($total); if($v['kuodan']){ $kd_sum = $v['number']+$v['kuodan']; if($v['percentage']){ $number = ceil(decode($v['percentage']) / $num * $kd_sum *1000); $v['nweight'] = number_format($number/1000,3); }else{ $v['nweight']=''; } }else{ if($v['percentage']){ $number = ceil(decode($v['percentage']) / $num * $v['number'] *1000); $v['nweight'] = number_format($number/1000,3); }else{ $v['nweight']=''; } } } // 计算每个 gy_num 的 material 数量 $gy_num_counts = []; $originalRes = $res; foreach ($res as $item) { if (!empty(trim($item['material'])) && (strpos($item['material'], '*') === false)) { $gy_num = $item['gy_num']; if (!isset($gy_num_counts[$gy_num])) { $gy_num_counts[$gy_num] = 0; } $gy_num_counts[$gy_num]++; } } if (empty($process)) { // 如果 feeding 表为空,则显示当前工序 gy_num = 1 $current_gy_num = 1; } else { // 初始化一个数组来存储各个工序的完成情况 $gy_num_status = []; // 遍历已经做的数据 foreach ($process as $item) { $gy_num = $item['gy_num']; if (!isset($gy_num_status[$gy_num])) { $gy_num_status[$gy_num] = 0; } $gy_num_status[$gy_num]++; } // 遍历原始数据,检查每个工序是否完成 $current_gy_num = 1; $all_completed = true; foreach ($gy_num_counts as $gy_num => $count) { if (isset($gy_num_status[$gy_num]) && $gy_num_status[$gy_num] === $count) { $current_gy_num++; } else { break; } // if (! isset($gy_num_status[$gy_num]) || $gy_num_status[$gy_num] < $count) { // $all_completed = false; // $current_gy_num = $gy_num; // break; // } else { // $current_gy_num = $gy_num; // } // if ($gy_num > $current_gy_num) { // $current_gy_num = $gy_num; // } } // 如果所有工序都完成,则显示下一工序 // if ($all_completed) { // $current_gy_num++; // } } // 筛选出当前工序的 $res 数据 $filtered_res = array_filter($res, function ($item) use ($current_gy_num) { return $item['gy_num'] == $current_gy_num; }); $filtered_res = array_values($filtered_res); // 重置数组索引 // echo "当前工序: " . $current_gy_num; // echo "
";print_r($filtered_res);echo "
"; $row['total']=$num; $row['data'] = $filtered_res; $row['process'] = $pro+1; return json($row); //die; // $originalRes = $res; // if (!$process) { // // 如果 $process 为空,只保留 gy_num 为 1 的数据 // $filtered = array_filter($res, function($item) { // return $item['gy_num'] == 1; // }); // } else { // // 确定 $process 中最高的 gy_num // $maxGyNumInProcess = max(array_column($process, 'gy_num')); // // [][][][][][][][][][][][][][][][][][][] // // [][][][][][][][][][][][][][][][][][][] // // else maxGyNumInProcess = max(array_column($process, 'gy_num')); 出现了问题 // // [][][][][][][][][][][][][][][][][][][] // $nextGyNum = $maxGyNumInProcess ; //// echo "
";
////            print_r($nextGyNum);
////            echo "
";
//            // 检查并跳过带有特定标记(如 '*****')的工序
//            do {
//                $nextGyNumItems = array_filter($res, function($item) use ($nextGyNum) {
//                    return $item['gy_num'] == $nextGyNum;
//                });
//                // 如果找到的工序是默认完成的(即 material 包含 '*****'),则跳到下一个工序
//                if (!empty($nextGyNumItems) && array_values($nextGyNumItems)[0]['material'] === '*****') {
//                    $nextGyNum++;
//                } else {
//                    break; // 找到了需要处理的下一个工序或者没有更多工序可以处理
//                }
//            } while (true);
//            // 重新过滤,确保处理正确的工序(这一步可能是多余的,根据您的逻辑调整)
//            $filtered = array_filter($res, function($item) use ($nextGyNum) {
//                return $item['gy_num'] == $nextGyNum;
//            });
//        }
//        $res = array_values($filtered); // 重置数组索引
//        if (empty($res)) {
//            $res = $originalRes; // 将所有工序的数据赋值给 $res,进行后续处理
//        }
//
//
//        $row['total']=$num;
//        $row['data'] = $res;
//        $row['process'] = $pro+1;
//        return json($row);
    }

    public function get_material(){
        $bach = $this->request->post('bach');//批次号
        $material = $this->request->post('material');//原材料
        if (!$bach || !$material){
            $this->error('扫码数据不能为空');
        }
        $task = Db::name('task')->where('bach',$bach)->find();
        $map = [];
        $map['f_name'] = $task['name'];
        $map['name'] = $material;
        $data = Db::name('formula_material')->where($map)->select();

        $result['data'] = '';
        if ($data){
            $result['data'] = $data;
            if (count($data)>1){
                $str = [];
                foreach ($data as $key=>$value){
                    $str[$key] = $value['m_name'];
                }
                $result['string'] = implode('或 ',$str);
            }else{
                $result['string'] = $data[0]['m_name'];
            }
            $result['total'] = count($data);
        }else{
            $result['total'] = 0;
        }
        return json($result);
    }

    //查找替代料
    /*public function replace(){
        $bach = $this->request->post('bach');
        $wuliao = $this->request->post('wuliao');
        $res = Db::name('task')->alias('t')
            ->join('formula f','f.id = t.fid','left')
            ->join('formula_detail fd','fd.pid = f.id','left')
            ->join('formula_replace fr','fr.fid = fd.id','left')
            ->where('t.bach',$bach)->where('is_replace=1')->where('fr.material',$wuliao)
            ->field('fd.material,fd.id')->find();
        $res['yuan'] = explode('/',$res['material']);
        $key = array_search($wuliao, $res['yuan']);
        if ($key !== false) array_splice($res['yuan'], $key, 1);
        return json($res);
    }*/

    public function cheng(){
        $user_info = Session::get('');
        return json($user_info);
    }


    //获取机台
//    public function matchCode() {
//        $code = $this->request->post('code');
//        preg_match('/^(M-\d{2,3})/', $code, $matches);
//        $res = Db::name('formula_detail')->where('material', 'like', $matches[0].'%')->select();
//        halt($res);
//        return json(Db::name('formula_detail')->where('material', 'like', $matches[0].'%')->select());
//    }


    public function matchCode() {
        $code = $this->request->post('code');
        preg_match('/^(M-\d{2,3})/', $code, $matches);
        $res = Db::name('formula_detail')->field('distinct material')->where('material', 'like', $matches[0].'%')->select();
        // 初始化结果数组
        $formattedMaterials = [];

        // 遍历查询结果
        foreach ($res as $item) {
            $material = $item['material'];
            // 去除括号和括号内内容
            $material = preg_replace('/\s*$.*?$\s*/', '', $material);

            // 分割材料字符串
            $parts = explode('/', $material);
            foreach ($parts as $part) {
                // 清除空格
                $part = trim($part);
                if (preg_match('/^(M-\d{2,3})$/', $part)) {
                    // 纯基本代码,无需添加
                    $formattedMaterials[] = $part;
                } elseif (preg_match('/^(M-\d{2,3})([A-Z]+)$/', $part, $partMatches)) {
                    // 代码与后缀的组合
                    $formattedMaterials[] = $part;
                } elseif (preg_match('/^[A-Z]+$/i', $part)) {
                    // 只有后缀,需要与基本代码拼接
                    $suffixes = str_split($part);
                    foreach ($suffixes as $suffix) {
                        $formattedMaterials[] = $matches[0] . $suffix;
                    }
                }
            }
        }

        // 去除重复项并重新排序
        $formattedMaterials = array_unique($formattedMaterials);

        if (array_search($code, $formattedMaterials) === false) {
            // todo.. 写入一个临时物料数据  materials name
            array_push($formattedMaterials, $code);
        }
        sort($formattedMaterials);

        // 返回JSON格式的数据
        return json($formattedMaterials);
    }



}