diff --git a/application/ZengJieCode/controller/admin/Printaction.php b/application/ZengJieCode/controller/admin/Printaction.php index e54caa1..8ad6b66 100644 --- a/application/ZengJieCode/controller/admin/Printaction.php +++ b/application/ZengJieCode/controller/admin/Printaction.php @@ -22,10 +22,57 @@ class Printaction extends Base protected $fontPath = 'public/tsf/jc.ttf'; // 请替换为你的字体文件路径 // 显示条形码生成页面 - public function print_device_barcode_index() + public function print_device_data_index() { - return $this->fetch(); + + $data = input(); + if(array_key_exists('type',$data)){ + return $this->print_device_data_index_action('down'); + }else{ + $result = $this->print_device_data_index_action('page'); + $this->assign([ + 'data' => $result + ]); + return $this->fetch(); + } + } + + public function print_device_data_index_action($data){ + $zengjie = Db::connect('zengjie_db'); + $pc = $zengjie->table('box_code_all')->order('id desc')->find(); + // dump($pc); + $sql = "SELECT + a.mac_code, + a.sn_code, + b.box_code, + FORMAT(b.box_num, '000') AS box_num + FROM + sn_code_all AS a + LEFT JOIN + box_code_all AS b ON a.batch_id = b.id + WHERE + a.batch_id IS NOT NULL + AND b.box_code = ? + ORDER BY + a.batch_id"; + + $result = $zengjie->query($sql, [$pc['box_code']]); + if($data == 'page'){ + return $result; + }else{ + $result2 = [ + ['Mac码','Sn码','箱号'] + ]; + for ($i=0; $i < count($result); $i++) { + $result2[] = [$result[$i]['mac_code'],$result[$i]['sn_code'],$result[$i]['box_code'].'-'.$result[$i]['box_num']]; + } + $excel = new \app\download\controller\Excel(); + $excel->export($result2, $pc['box_code']); + } + } + + // 显示外箱码生成页面 public function print_outside_box_index() { diff --git a/application/ZengJieCode/controller/app/Savemsg.php b/application/ZengJieCode/controller/app/Savemsg.php index a6676fd..b316b81 100644 --- a/application/ZengJieCode/controller/app/Savemsg.php +++ b/application/ZengJieCode/controller/app/Savemsg.php @@ -18,11 +18,6 @@ class Savemsg extends Base{ ]; protected $name_default = 0; - protected $kitchenscale_db_msg = [ - 'cookbook'=>'app_user_cookbook',//菜谱表 - 'foodlist3'=>'app_z_national_standard_food_type_3',//食材列表3 - 'user'=>'app_user_data',//banner - ]; // 加 bcadd(,,20) // 减 bcsub(,,20) @@ -54,7 +49,7 @@ class Savemsg extends Base{ } - public function save_sn_msg($data = ['sn_code'=>'564654564654654','bl_name'=>'bl_5520']){ + public function save_sn_msg($data = ['sn_code'=>'564654564654654','mac_code'=>'ce:sh:yo:ng:d1','bl_name'=>'bl_5520']){ try { // 你的业务逻辑 if(count(input('post.')) > 0){ @@ -66,12 +61,18 @@ class Savemsg extends Base{ if(!array_key_exists('bl_name', $data)){ return $this->msg(10001); } + if(!array_key_exists('mac_code', $data)){ + return $this->msg(10001); + } if(!$this->verify_data_is_ok($data['sn_code'],'str')){ return $this->msg(10005); } if(!$this->verify_data_is_ok($data['bl_name'],'str')){ return $this->msg(10005); } + if(!$this->verify_data_is_ok($data['mac_code'],'str')){ + return $this->msg(10005); + } $result = $this->save_sn_msg_action($data); return $result; } catch (\Exception $e) { @@ -217,6 +218,7 @@ class Savemsg extends Base{ $result = $zengjie->table('sn_code_all')->insert([ 'sn_code'=>$data['sn_code'], 'bluetooth_name'=>$data['bl_name'], + 'mac_code'=>$data['mac_code'], 'create_time'=>date('Y-m-d H:i:s'), ]); if($result){ @@ -226,47 +228,82 @@ class Savemsg extends Base{ } } - public function save_box_msg_action($data){ + public function save_box_msg_action($data) { $zengjie = Db::connect('zengjie_db'); - - $box_num = $zengjie->table('box_code_all')->where(['box_code'=>$data['box_serial_number']])->count(); - $box_num = $box_num+1; - $result = $zengjie->table('box_code_all')->insert([ - 'box_code'=>$data['box_serial_number'], - 'box_num'=>$box_num, - 'create_time'=>date('Y-m-d H:i:s'), - 'content_str'=>$data['sn_code_all'], - ]); - - if($result){ - return $this->msg([]); - }else{ - return $this->msg(10002,'录入失败'); + + // 1. 检查sn_code_all中是否有重复数据 + $snCodes = explode(',', $data['sn_code_all']); + if (count($snCodes) !== count(array_unique($snCodes))) { + return $this->msg(10003, 'SN码列表中存在重复数据'); } - // try { - // // 使用事务闭包,TP5会自动管理事务 - // $result = $zengjie->transaction(function() use ($zengjie, $data) { - // $sn_code = $zengjie->table('box_code_all')->where(['box_code'=>$data['box_serial_number']])->count(); - // $num = $sn_code+1; + // 2. 检查这些SN码是否都存在于sn_code_all表中 + $existCount = $zengjie->table('sn_code_all') + ->where('sn_code', 'in', $snCodes) + ->count(); + + if ($existCount !== count($snCodes)) { + return $this->msg(10004, '存在未登记的SN码'); + } + Db::startTrans(); + try{ + $count = $zengjie->table('box_code_all') + ->where(['box_code'=>$data['box_serial_number']]) + ->count(); + $num = $count + 1; - // $box_id = $zengjie->table('box_code_all')->insertGetId([ - // 'box_code'=>$data['box_serial_number'], - // 'box_num'=>$num, - // 'create_time'=>date('Y-m-d H:i:s'), - // ]); - - // // 使用安全的参数绑定方式 - // $snCodes = explode(',', $data['sn_code_all']); - // $zengjie->table('sn_code_all') - // ->where('sn_code', 'in', $snCodes) - // ->update(['batch_id' => $box_id]); - - // return $box_id; - // }); + $box_id = $zengjie->table('box_code_all')->insertGetId([ + 'box_code' => $data['box_serial_number'], + 'box_num' => $num, + 'create_time' => date('Y-m-d H:i:s'), + 'content_str' => $data['sn_code_all'], + ]); - // return $this->msg(['id'=>$result]); + $affectedRows = $zengjie->table('sn_code_all') + ->where('sn_code', 'in', $snCodes) + ->update(['batch_id' => $box_id]); + + // 检查更新是否影响了预期的行数 + if ($affectedRows !== count($snCodes)) { + throw new \Exception('更新SN码数量不匹配'); + } + Db::commit(); + return $this->msg(['id'=>$box_id]); + } catch (\Exception $e) { + // 回滚事务 + Db::rollback(); + return $this->msg(10002, '录入失败: ' . $e->getMessage()); + } + // // 显式事务控制 + // $zengjie->startTrans(); + // try { + // $count = $zengjie->table('box_code_all') + // ->where(['box_code'=>$data['box_serial_number']]) + // ->count(); + // $num = $count + 1; + + // $box_id = $zengjie->table('box_code_all')->insertGetId([ + // 'box_code' => $data['box_serial_number'], + // 'box_num' => $num, + // 'create_time' => date('Y-m-d H:i:s'), + // 'content_str' => $data['sn_code_all'], + // ]); + + // $affectedRows = $zengjie->table('sn_code_all') + // ->where('sn_code', 'in', $snCodes) + // ->update(['batch_id' => $box_id]); + + // // 检查更新是否影响了预期的行数 + // if ($affectedRows !== count($snCodes)) { + // throw new \Exception('更新SN码数量不匹配'); + // } + + // $zengjie->commit(); + // return $this->msg(['id'=>$box_id]); // } catch (\Exception $e) { + // if ($zengjie->getPdo()->inTransaction()) { + // $zengjie->rollback(); + // } // trace('保存盒信息失败: ' . $e->getMessage(), 'error'); // return $this->msg(10002, '录入失败: ' . $e->getMessage()); // } @@ -301,5 +338,61 @@ class Savemsg extends Base{ return $this->msg(10002,'打印失败'); } } + + ######################################################测试######################################################### + ######################################################测试######################################################### + ######################################################测试######################################################### + + public function ceshiyong(){ + // // 添加测试一维码数据 + // $data = []; + // for ($i = 0; $i < 500; $i++) { + // // 生成15位随机数字作为sn_code + // $sn_code = ''; + // for ($j = 0; $j < 15; $j++) { + // $sn_code .= mt_rand(0, 9); + // } + + // // 生成随机的MAC地址 + // $mac_parts = []; + // for ($k = 0; $k < 6; $k++) { + // $mac_parts[] = sprintf('%02x', mt_rand(0, 255)); + // } + // $mac_code = implode(':', $mac_parts); + + // // 构建数据数组 + // $data[] = [ + // 'sn_code' => $sn_code, + // 'mac_code' => $mac_code, + // 'create_time' => date('Y-m-d H:i:s'), + // 'is_print' => 1, + // 'bluetooth_name' => 'bl_5520' + // ]; + // } + // $zengjie = Db::connect('zengjie_db'); + // $result = $zengjie->table('sn_code_all')->insertAll($data); + // dump($result); + // die; + // // 添加测试一维码数据 + + // 添加测试打包数据 + $zengjie = Db::connect('zengjie_db'); + $records = $zengjie->table('sn_code_all') + ->where('batch_id', 'null') // 或者使用 ->whereNull('batch_id') + ->orderRaw('NEWID()') // SQL Server的随机排序函数 + ->limit(10) + ->select(); + $data['sn_code_all'] = []; + $data['box_serial_number'] = '3ST011527003'; + for ($i=0; $i < count($records); $i++) { + $data['sn_code_all'][] = $records[$i]['sn_code']; + } + $data['sn_code_all'] = implode(',',$data['sn_code_all']); + $result = $this->save_box_msg_action($data); + dump($result); + dump($data); + // 添加测试打包数据 + // return $data; + } } \ No newline at end of file diff --git a/application/ZengJieCode/view/admin/printaction/print_device_barcode_index.html b/application/ZengJieCode/view/admin/printaction/print_device_barcode_index.html deleted file mode 100644 index 42144b0..0000000 --- a/application/ZengJieCode/view/admin/printaction/print_device_barcode_index.html +++ /dev/null @@ -1,122 +0,0 @@ - - -
- - -| MAC地址 | +SN码 | +箱号 | +
|---|---|---|
| {$item.mac_code|default='未分配'} | +{$item.sn_code} | +{$item.box_code}-{$item.box_num} | +