Forráskód Böngészése

糊盒报工优化

unknown 3 hónapja
szülő
commit
284e6987cd
1 módosított fájl, 144 hozzáadás és 50 törlés
  1. 144 50
      application/api/controller/GluingReport.php

+ 144 - 50
application/api/controller/GluingReport.php

@@ -298,66 +298,160 @@ class GluingReport extends Api
      * @throws \think\db\exception\BindParamException
      * @throws \think\db\exception\BindParamException
      * @throws \think\exception\PDOException
      * @throws \think\exception\PDOException
      */
      */
+//    public function addGluingReportData()
+//    {
+//        // 1. 请求验证
+//        if (!$this->request->isPost()) {
+//            $this->error('请求错误');
+//        }
+//
+//        // 2. 参数验证
+//        $param = $this->request->post();
+//        if (empty($param)) {
+//            $this->error('参数错误');
+//        }
+//
+//        // 3. 使用事务确保数据一致性
+//        Db::startTrans();
+//        try {
+//            // 准备数据
+//            $currentTime = date('Y-m-d H:i:s');
+//
+//            // 构建班组数据
+//            $classData = [
+//                'sys_rq'  => $currentTime,
+//                'jtbh'    => $param['sczl_jtbh'] ?? '',
+//                'gdbh'    => $param['sczl_gdbh'] ?? '',
+//                'gxmc'    => $param['sczl_gxmc'] ?? '',
+//                'sys_id'  => $param['sys_id'] ?? '',
+//                'sczl_rq' => $param['sczl_rq'] ?? '',
+//            ];
+//
+//            // 处理动态字段
+//            for ($i = 1; $i <= 30; $i++) {
+//                $classData['bh'.$i] = $param['bh'.$i] ?? '';
+//                $classData['rate'.$i] = $param['rate'.$i] ?? '';
+//                unset($param['bh'.$i], $param['rate'.$i]);
+//            }
+//
+//            // 保存班组数据
+//            $classId = \db('糊盒报工班组')->insertGetId($classData);
+//            if (!$classId) {
+//                throw new \Exception('保存班组数据失败');
+//            }
+//
+//            // 保存设备数据
+//            $param['role'] = $classId;
+//            $param['sys_rq'] = $currentTime;
+//
+//            $sql = \db('设备_糊盒报工资料')->fetchSql(true)->insert($param);
+//            $result = db()->query($sql);
+//            if ($result === false) {
+//                throw new \Exception('保存设备数据失败');
+//            }
+//
+//            Db::commit();
+//            $this->success('报工成功');
+//
+//        } catch (\Exception $e) {
+//            Db::rollback();
+//            $this->error($e->getMessage() ?: '报工失败');
+//        }
+//    }
+    /**
+     * 新增糊盒报工数据
+     * 说明:适配PHP 7.2、中文字段名,支持sczl_jtbh字段含#字符
+     * @return void
+     */
     public function addGluingReportData()
     public function addGluingReportData()
     {
     {
-        // 1. 请求验证
+        // 1. 请求方式验证(严格限制POST请求)
         if (!$this->request->isPost()) {
         if (!$this->request->isPost()) {
-            $this->error('请求错误');
+            $this->error('仅支持POST请求');
         }
         }
 
 
-        // 2. 参数验证
+        // 2. 获取并验证参数(重点适配含#字符的sczl_jtbh字段)
         $param = $this->request->post();
         $param = $this->request->post();
-        if (empty($param)) {
-            $this->error('参数错误');
-        }
 
 
-        // 3. 使用事务确保数据一致性
-        Db::startTrans();
-        try {
-            // 准备数据
-            $currentTime = date('Y-m-d H:i:s');
-
-            // 构建班组数据
-            $classData = [
-                'sys_rq'  => $currentTime,
-                'jtbh'    => $param['sczl_jtbh'] ?? '',
-                'gdbh'    => $param['sczl_gdbh'] ?? '',
-                'gxmc'    => $param['sczl_gxmc'] ?? '',
-                'sys_id'  => $param['sys_id'] ?? '',
-                'sczl_rq' => $param['sczl_rq'] ?? '',
-            ];
-
-            // 处理动态字段
-            for ($i = 1; $i <= 30; $i++) {
-                $classData['bh'.$i] = $param['bh'.$i] ?? '';
-                $classData['rate'.$i] = $param['rate'.$i] ?? '';
-                unset($param['bh'.$i], $param['rate'.$i]);
+        // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
+        $validate = new \think\Validate([
+            'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
+            'sczl_gdbh' => 'require',
+            'sczl_gxmc' => 'require',
+            'sys_id'    => 'require',
+            'sczl_rq'   => 'require|dateFormat:Y-m-d H:i:s',
+        ], [
+            'sczl_jtbh.require' => '机组编号不能为空',
+            'sczl_jtbh.regex'   => '机组编号仅支持字母、数字、下划线、#号和中文',
+            'sczl_gdbh.require' => '工单号不能为空',
+            'sczl_gxmc.require' => '工序名称不能为空',
+            'sys_id.require'    => '系统ID不能为空',
+            'sczl_rq.require'   => '报工日期不能为空',
+            'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
+        ]);
+
+        // 验证前处理:仅去除前后空格(不影响#字符)
+        foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sys_id', 'sczl_rq'] as $field) {
+            if (isset($param[$field]) && is_string($param[$field])) {
+                $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
             }
             }
+        }
 
 
-            // 保存班组数据
-            $classId = \db('糊盒报工班组')->insertGetId($classData);
-            if (!$classId) {
-                throw new \Exception('保存班组数据失败');
-            }
+        // 执行验证
+        if (!$validate->check($param)) {
+            $this->error($validate->getError());
+        }
 
 
-            // 保存设备数据
-            unset($param['sczl_jtbh'], $param['sczl_gdbh'], $param['sczl_gxmc']);
-            $param['role'] = $classId;
-            $param['sys_rq'] = $currentTime;
+        // 3. 事务处理(确保数据一致性)
+        $currentTime = date('Y-m-d H:i:s');
+        $tableClass = '糊盒报工班组';
+        $tableDevice = '设备_糊盒报工资料';
 
 
-            $sql = \db('设备_糊盒报工资料')->fetchSql(true)->insert($param);
-            $result = db()->query($sql);
-            if (!$result) {
-                throw new \Exception('保存设备数据失败');
-            }
+        // 4. 构建班组数据(保留#字符,无需额外处理)
+        $classData = [
+            'sys_rq'  => $currentTime,
+            'jtbh'    => $param['sczl_jtbh'], // 直接保留含#的原始值
+            'gdbh'    => $param['sczl_gdbh'],
+            'gxmc'    => $param['sczl_gxmc'],
+            'sys_id'  => $param['sys_id'],
+            'sczl_rq' => $param['sczl_rq'],
+        ];
 
 
-            Db::commit();
-            $this->success('报工成功');
+        // 处理动态字段(1-30组bh/rate)
+        $dynamicFields = [];
+        for ($i = 1; $i <= 30; $i++) {
+            $dynamicFields['bh' . $i] = isset($param['bh' . $i]) ? $param['bh' . $i] : '';
+            $dynamicFields['rate' . $i] = isset($param['rate' . $i]) ? $param['rate' . $i] : '';
+        }
+        $classData = array_merge($classData, $dynamicFields);
 
 
-        } catch (\Exception $e) {
-            Db::rollback();
-            $this->error($e->getMessage() ?: '报工失败');
+        // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
+        $classId = Db::name($tableClass)->insertGetId($classData);
+        if (empty($classId)) {
+            throw new \Exception("{$tableClass}数据保存失败");
         }
         }
+
+        // 6. 构建设备数据(适配PHP7.2)
+        $filteredParam = array_filter($param, function ($key) {
+            return !preg_match('/^(bh|rate)\d+$/', $key);
+        }, ARRAY_FILTER_USE_KEY);
+
+        $deviceData = array_merge([
+            'role'    => $classId,
+            'sys_rq'  => $currentTime,
+        ], $filteredParam);
+
+        // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
+        $sql = Db::name($tableDevice)->fetchSql(true)->insert($deviceData);
+        $saveResult = db()->query($sql);
+        if ($saveResult !== false) {
+            $this->success('报工提交成功');
+        }else{
+            $this->error('报工提交失败');
+        }
+
+
+
     }
     }
 
 
 
 
@@ -576,14 +670,14 @@ class GluingReport extends Api
             'a.sczl_jtbh' => $params['machine'],
             'a.sczl_jtbh' => $params['machine'],
             'a.sczl_rq' => ['like',$params['day'].'%']
             'a.sczl_rq' => ['like',$params['day'].'%']
         ];
         ];
-        $field = ['a.sczl_gdbh as 工单编号','a.sczl_yjno as 印件号','a.sczl_gxh as 工序号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
+        $field = ['a.sczl_gdbh as 工单编号','a.sczl_gxmc as 工序名称','a.来料数量','a.sczl_cl as 产量',
             'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
             'a.sczl_zcfp as 制程废品','a.startTime as 开始时间','a.endTime as 结束时间','a.sczl_ls as 联数','a.sczl_rq as 日期',
             'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
             'a.sczl_dedh as 定额代号','a.工价系数','a.保养工时','a.装版工时','a.异常工时','a.异常类型','a.设备运行工时','a.role',
             'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号',
             'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号',
-            'b.yj_Yjdh as 产品代号','yj_yjmc as 产品名称'];
+            'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称'];
         $list = \db('设备_糊盒报工资料')
         $list = \db('设备_糊盒报工资料')
             ->alias('a')
             ->alias('a')
-            ->join('工单_印件资料 b','a.sczl_gdbh = b.Yj_Gdbh and a.sczl_yjno = b.yj_Yjno','left')
+            ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left')
             ->field($field)
             ->field($field)
             ->where($where)
             ->where($where)
             ->group('a.Uid')
             ->group('a.Uid')
@@ -595,7 +689,7 @@ class GluingReport extends Api
             $list[$key]['class'] = [];
             $list[$key]['class'] = [];
             $idList = explode(',',$value['role']);
             $idList = explode(',',$value['role']);
             foreach ($idList as $item){
             foreach ($idList as $item){
-                $class = \db('设备_糊盒班组资料')
+                $class = \db('')
                     ->where('id',$item)
                     ->where('id',$item)
                     ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
                     ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
                     ->find();
                     ->find();