Browse Source

first commit

liuhairui 3 months ago
parent
commit
8af2cb1b31
2 changed files with 309 additions and 74 deletions
  1. 32 8
      application/admin/controller/QcodeAdd.php
  2. 277 66
      application/admin/controller/QcodeBach.php

+ 32 - 8
application/admin/controller/QcodeAdd.php

@@ -70,8 +70,17 @@ class QcodeAdd extends Backend
         $product_name = [];
         foreach ($product_id as $v){
             $list = $product->where('_id',$v)->where('delete_time','')->find();
-            $product_name[$list['product_code']] =  $list['product_name'];
+//            $product_name[$list['product_code']] =  $list['product_name'];
+            // 添加空值检查
+            if (!empty($list) && isset($list['product_code']) && isset($list['product_name'])) {
+                $product_name[$list['product_code']] = $list['product_name'];
+            } else {
+                // 记录日志或跳过
+                error_log("产品ID {$v} 未找到或数据不完整");
+                continue;
+            }
         }
+
         $this->view->assign('row',$data);
         $this->view->assign('product',$product_name);
         return $this->view->fetch();
@@ -191,7 +200,9 @@ class QcodeAdd extends Backend
             'large_endnum' => $data['big_liushui'] + $data['box_num'] -1,
             'create_time' => time(),
         ];
-
+//echo "<pre>";
+//print_r($batchList);
+//echo "<pre>";
         $res = $bach->save($batchList);
         if ($res === 0){
             $this->error('添加失败');
@@ -218,8 +229,20 @@ class QcodeAdd extends Backend
 //            $fixed_code .= $this->intTochar($batchList['supplier_code'], 21);//21位  供应商编码
             $fixed_code .= $this->intTochar($batchList['kes'], 12);//12位  供应商编码
 //            $fixed_code .= '9'; // 补0一位
-            $fixed_code .= '0000000000'; // 补0一位
-            $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位  辅料编码
+
+            // 判断物料编码是否包含'AB'(新物料编码)
+            if (strpos($batchList['matter_no'], 'AB') !== false) {
+                // 新物料编码处理逻辑
+                $fixed_code .= '0000000000'; // 新编码补10个0
+                $fixed_code .= $this->intTochar($batchList['matter_no'], 10);  // 转换为10位辅料编码
+            } else {
+                // 老物料编码处理逻辑
+                $fixed_code .= '000';       // 老编码补
+                $fixed_code .= $this->intTochar($batchList['matter_no'], 17);  // 转换为16位辅料编码
+            }
+
+//            $fixed_code .= '0000000000'; // 补0一位
+//            $fixed_code.=$this->intTochar($batchList['matter_no'],10);//10位  辅料编码
             $fixed_code.=$batchList['manufacture_date'];//6位  生产日期
             $print_code=$batchList['print_date'];//6位  打码日期
 
@@ -238,7 +261,7 @@ class QcodeAdd extends Backend
             ];
 //echo "<pre>";
 //print_r($whereSmall);
-//echo "<pre>";
+//echo "<pre>";die;
             if ($liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find()){
                 //小件二维码存在,更新小件二维码最后流水号
                 $lastNum = $liushui->name($userinfo['company'].'_'.'qcode_liushui')->where($whereSmall)->find();
@@ -254,6 +277,8 @@ class QcodeAdd extends Backend
                 $l_flow = $this->intTochar($batchList['l_flow']+$i,6);
                 $l_weight = $this->intTochar($batchList['l_weight']*100,6);
                 $l_reservation = $this->intTochar($batchList['bach_num'],10);
+
+
                 $l_reservation = $l_reservation.'0000000000';
                 $remainder = $batchList['small_num'] - $batchList['tray_num'] * $i;//最后一托盘小件数量
                 if ($remainder < $batchList['tray_num']){
@@ -268,9 +293,7 @@ class QcodeAdd extends Backend
                 }
                 //大件二维码数据
                 $code_data = $this->CodeData('AB92',$fixed_code,$small_n,$print_code,$l_flow,$l_weight,'2',$l_reservation);
-//                echo "<pre>";
-//                print_r($code_data);
-//                echo "<pre>";
+
                 //大码数据信息
                 $l_data = [
                     'bach_id' => $last_id,
@@ -320,6 +343,7 @@ class QcodeAdd extends Backend
                             's_weight'=>$batchList['s_weight'],//单个小件重量
                             'status' => 0,
                         ];
+
                         $s_res = $small->save($s_data);
                         if ($s_res === 0){
                             $this->error('小件码插入失败');

+ 277 - 66
application/admin/controller/QcodeBach.php

@@ -243,6 +243,106 @@ class QcodeBach extends Backend
     /**
      * 小件列表
      */
+//    public function small()
+//    {
+//        //设置过滤方法
+//        $this->request->filter(['strip_tags', 'trim']);
+//        $req = $this->request->param();
+//        if ($this->request->isAjax()) {
+//            //如果发送的来源是Selectpage,则转发到Selectpage
+//            if ($this->request->request('keyField')) {
+//                return $this->selectpage();
+//            }
+//            $userInfo = Session::get('admin');
+//            $company_id = (int)$userInfo['company'];
+//            $where = [
+//                'delete_time'=> '',
+//                'large_id'=> $req['large_id']
+//            ];
+//
+//            $req = input();
+//            $req['sort'] == 'id' ? $sort = '_id' : $sort = $req['sort'];
+//            $order = $req['order'];
+//            $offset = $req['offset'];
+//            $limit = $req['limit'];
+//
+//            $filter = json_decode($req['filter'], true);
+//            foreach ($filter as $k => $v){
+//                $where[$k] = new \MongoDB\BSON\Regex($v);
+//            }
+//
+//            $list = [];
+//            $total = 0;
+//
+//            $qcodeSmall = new QcodeSmall();
+//
+//            // 超级管理员查询所有公司
+//            if (isSuperAdmin()) {
+//                $companies = Db::name('admin')
+//                    ->field('company')
+//                    ->where('company', '<>', '')
+//                    ->where('kes', '<>', '')
+//                    ->distinct(true)
+//                    ->select();
+//
+//
+//                foreach ($companies as $row) {
+//                    $cid = $row['company'];
+//                    $table = $cid . '_qcode_small';
+//
+//                    // 获取数据(不分页,后面统一分页)
+//                    $rows = $qcodeSmall->name($table)->where($where)->select();
+//
+//                    foreach ($rows as &$v) {
+//                        $oid = $v['_id']->jsonSerialize();
+//                        $v['id'] = $oid['$oid'];
+//                        $v['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
+//                        if (strpos($v['l_flow'], '-') !== false) {
+//                            $parts = explode('-', $v['l_flow']);
+//                            $v['l_flows'] = end($parts);
+//                        } else {
+//                            $v['l_flows'] = '';
+//                        }
+//                    }
+//
+//                    $list = array_merge($list, $rows);
+//                }
+//
+//                // 全部公司数据合并后,统一排序
+//                usort($list, function ($a, $b) use ($sort, $order) {
+//                    $valA = $a[$sort] ?? '';
+//                    $valB = $b[$sort] ?? '';
+//                    return $order === 'asc' ? ($valA <=> $valB) : ($valB <=> $valA);
+//                });
+//
+//                // 总条数
+//                $total = count($list);
+//                // 分页截取
+//                $list = array_slice($list, $offset, $limit);
+//            } else {
+//                // 普通用户仅查本公司
+//                $table = $company_id . '_qcode_small';
+//
+//                $total = $qcodeSmall->name($table)->where($where)->count();
+//
+//                $list = $qcodeSmall->name($table)->where($where)
+//                    ->order([$sort => $order])
+//                    ->limit($limit)
+//                    ->skip($offset)
+//                    ->select();
+//
+//                foreach ($list as $k => $v) {
+//                    $oid = $v['_id']->jsonSerialize();
+//                    $list[$k]['id'] = $oid['$oid'];
+//                    $list[$k]['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
+//                }
+//            }
+//
+//            return json(["total" => $total, "rows" => $list]);
+//        }
+//        return $this->view->fetch();
+//    }
+
     public function small()
     {
         //设置过滤方法
@@ -275,79 +375,33 @@ class QcodeBach extends Backend
                 $where[$k] = new \MongoDB\BSON\Regex($v);
             }
 
-            $list = [];
-            $total = 0;
-
             $qcodeSmall = new QcodeSmall();
+            $total = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)->count();
+            $list = $qcodeSmall->name($company_id.'_'."qcode_small")->where($where)
+                ->order($sort,$order)
+                ->limit($limit)
+                ->skip($offset)
+                ->select();
 
-            // 超级管理员查询所有公司
-            if (isSuperAdmin()) {
-                $companies = Db::name('admin')
-                    ->field('company')
-                    ->where('company', '<>', '')
-                    ->where('kes', '<>', '')
-                    ->distinct(true)
-                    ->select();
-
-                foreach ($companies as $row) {
-                    $cid = $row['company'];
-                    $table = $cid . '_qcode_small';
-
-                    // 获取数据(不分页,后面统一分页)
-                    $rows = $qcodeSmall->name($table)->where($where)->select();
-
-                    foreach ($rows as &$v) {
-                        $oid = $v['_id']->jsonSerialize();
-                        $v['id'] = $oid['$oid'];
-                        $v['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
-                        if (strpos($v['l_flow'], '-') !== false) {
-                            $parts = explode('-', $v['l_flow']);
-                            $v['l_flows'] = end($parts);
-                        } else {
-                            $v['l_flows'] = '';
-                        }
-                    }
-
-                    $list = array_merge($list, $rows);
-                }
-
-                // 全部公司数据合并后,统一排序
-                usort($list, function ($a, $b) use ($sort, $order) {
-                    $valA = $a[$sort] ?? '';
-                    $valB = $b[$sort] ?? '';
-                    return $order === 'asc' ? ($valA <=> $valB) : ($valB <=> $valA);
-                });
-
-                // 总条数
-                $total = count($list);
-
-                // 分页截取
-                $list = array_slice($list, $offset, $limit);
-            } else {
-                // 普通用户仅查本公司
-                $table = $company_id . '_qcode_small';
-
-                $total = $qcodeSmall->name($table)->where($where)->count();
-
-                $list = $qcodeSmall->name($table)->where($where)
-                    ->order([$sort => $order])
-                    ->limit($limit)
-                    ->skip($offset)
-                    ->select();
-
-                foreach ($list as $k => $v) {
-                    $oid = $v['_id']->jsonSerialize();
-                    $list[$k]['id'] = $oid['$oid'];
-                    $list[$k]['l_flow'] = ($req['l_flow'] ?? '') . '-' . $v['l_flow'];
+            foreach ($list as $k=>$v) {
+                $oid = $v['_id']->jsonSerialize();
+                $list[$k]['id'] = $oid['$oid'];
+                $list[$k]['l_flow'] = $req['l_flow'].'-'.$v['l_flow'];
+                if (strpos($v['l_flow'], '-') !== false) {
+                    $parts = explode('-', $v['l_flow']);
+                    $v['l_flows'] = end($parts);
+                } else {
+                    $v['l_flows'] = '';
                 }
             }
 
-            return json(["total" => $total, "rows" => $list]);
+            $result = array("total" => $total, "rows" => $list);
+
+            return json($result);
         }
         return $this->view->fetch();
     }
 
-
     /**
      * 小件导出状态修改(支持超级管理员操作单个公司表)
      * 支持处理单个ID和多个ID(逗号分隔)的情况
@@ -568,7 +622,7 @@ class QcodeBach extends Backend
                         if ($rowData['l_num'] === null) {
                             $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
                         } elseif ($rowData['l_num'] == 0) {
-                            $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] : (
+                            $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
                             $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
                             );
                         } else {
@@ -629,7 +683,7 @@ class QcodeBach extends Backend
                 if ($rowData['l_num'] === null) {
                     $rows[$key]['num'] = $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : '';
                 } elseif ($rowData['l_num'] == 0) {
-                    $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] : (
+                    $rows[$key]['num'] = ($main_unit == '公斤') ? $rowData['l_weight'] /100 : (
                     $proportion ? floor($rows[$key]['sqrcd'] * $proportion * 100) / 100 : ''
                     );
                 } else {
@@ -1209,4 +1263,161 @@ class QcodeBach extends Backend
         return 'data:image/png;base64,'.$imageString;
     }
 
+
+//    public function bacch(){
+//        $mongo = \think\Db::connect('mongodb');
+//
+//        // 表1数据
+//        $qcode_product = $mongo->name('qcode_product')->select();
+//        // 表2数据
+//        $qr_qcode_product_copy = $mongo->name('qr_qcode_product_copy')->select();
+//
+//        // 把表2按 product_name 索引
+//        $map2 = [];
+//        foreach ($qr_qcode_product_copy as $row) {
+//            $map2[$row['product_name']] = $row['product_code'];
+//        }
+//
+//        $same = [];   // 相同 product_name 的记录
+//        $diff = [];   // 不相同的记录
+//
+//        // 先处理表1,找出匹配和不匹配
+//        foreach ($qcode_product as $row1) {
+//            if (isset($map2[$row1['product_name']])) {
+//                $same[] = [
+//                    'product_code_1' => $row1['product_code'],
+//                    'product_code_2' => $map2[$row1['product_name']],
+//                    'product_name'   => $row1['product_name']
+//                ];
+//                unset($map2[$row1['product_name']]); // 已匹配,避免后面重复
+//            } else {
+//                $diff[] = [
+//                    'product_code' => $row1['product_code'],
+//                    'product_name' => $row1['product_name']
+//                ];
+//            }
+//        }
+//
+//        // 剩下 map2 中的就是表2中独有的
+//        foreach ($map2 as $name => $code) {
+//            $diff[] = [
+//                'product_code' => $code,
+//                'product_name' => $name
+//            ];
+//        }
+//
+////        // 存到新表
+//        $mongo->name('same_products')->insertAll($same);
+//        $mongo->name('diff_products')->insertAll($diff);
+//    }
+
+
+//    public function bacch() {
+//        $mongo = \think\Db::connect('mongodb');
+//
+//        // 获取三个表的数据
+//        $table1 = $mongo->name('qcode_product')->select();
+//        $table2 = $mongo->name('qr_qcode_product_copy')->select();
+//
+//        // 预加载qr_qcode_unit_copy表数据并建立索引
+//        $unitData = $mongo->name('qr_qcode_unit_copy')->select();
+//        $unitIndex = [];
+//        foreach ($unitData as $unit) {
+//            $unitIndex[$unit['code']] = [
+//                'main_unit' => $unit['main_unit'] ?? null,
+//                'sec_unit' => $unit['sec_unit'] ?? null,
+//                'proportion' => $unit['proportion'] ?? null
+//            ];
+//        }
+//
+//        // 索引表2数据,包含product_name和temple
+//        $table2Index = [];
+//        foreach ($table2 as $row) {
+//            $table2Index[$row['product_name']] = [
+//                'product_code' => $row['product_code'],
+//                'temple' => $row['temple'] ?? null
+//            ];
+//        }
+//
+//        $differences = [];
+//
+//        // 处理表1中的记录
+//        foreach ($table1 as $row) {
+//            if (!isset($table2Index[$row['product_name']]) &&
+//                strpos($row['product_code'], 'AB') !== 0) {
+//
+//                // 获取单位信息
+//                $unitInfo = $unitIndex[$row['product_code']] ?? [
+//                        'main_unit' => null,
+//                        'sec_unit' => null,
+//                        'proportion' => null
+//                    ];
+//
+//                $differences[] = [
+//                    'product_code' => $row['product_code'],
+//                    'product_name' => $row['product_name'],
+//                    'temple' => null,
+//                    'main_unit' => $unitInfo['main_unit'],
+//                    'sec_unit' => $unitInfo['sec_unit'],
+//                    'proportion' => $unitInfo['proportion']
+//                ];
+//            }
+//        }
+//
+//        // 处理表2中的记录
+//        foreach ($table2Index as $name => $data) {
+//            $existsInTable1 = false;
+//            foreach ($table1 as $row) {
+//                if ($row['product_name'] === $name) {
+//                    $existsInTable1 = true;
+//                    break;
+//                }
+//            }
+//
+//            if (!$existsInTable1 && strpos($data['product_code'], 'AB') !== 0) {
+//                // 获取单位信息
+//                $unitInfo = $unitIndex[$data['product_code']] ?? [
+//                        'main_unit' => null,
+//                        'sec_unit' => null,
+//                        'proportion' => null
+//                    ];
+//
+//                $differences[] = [
+//                    'product_name' => $name,
+//                    'product_code' => $data['product_code'],
+//                    'temple' => $data['temple'],
+//                    'main_unit' => $unitInfo['main_unit'],
+//                    'sec_unit' => $unitInfo['sec_unit'],
+//                    'proportion' => $unitInfo['proportion'],
+//                    'delete_time' => '',
+//                    'sync_flag' => 0,
+//                    'code' => substr($data['product_code'], 0, 4)
+//                ];
+//            }
+//        }
+//
+//        // 获取当前最大的oid_id值
+//        $maxOid = $mongo->name('qcode_product')->order('oid_id','desc')->find();
+//
+//        $nextOid = $maxOid['oid_id'] ? $maxOid['oid_id'] + 1 : 1; // 如果没有记录则从1开始
+//        // 插入数据到qcode_product表
+//        foreach ($differences as $diff) {
+//            $insertData = [
+//                'oid_id' => $nextOid++,
+//                'product_code' => $diff['product_code'],
+//                'product_name' => $diff['product_name'],
+//                'temple' => $diff['temple'],
+//                'main_unit' => $diff['main_unit'],
+//                'sec_unit' => $diff['sec_unit'],
+//                'proportion' => $diff['proportion'],
+//                'delete_time' => $diff['delete_time'] ?? '',
+//                'sync_flag' => $diff['sync_flag'] ?? 0,
+//                'code' => $diff['code'] ?? substr($diff['product_code'], 0, 4)
+//            ];
+//            $mongo->name('qcode_product')->insert($insertData);
+//        }
+//
+//        echo "成功插入 " . count($differences) . " 条记录";
+//    }
+
 }