unknown 3 месяцев назад
Родитель
Сommit
242c5c1ad7
1 измененных файлов с 111 добавлено и 124 удалено
  1. 111 124
      application/api/controller/GluingReport.php

+ 111 - 124
application/api/controller/GluingReport.php

@@ -298,71 +298,7 @@ class GluingReport extends Api
      * @throws \think\db\exception\BindParamException
      * @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()
     {
         // 1. 请求方式验证(严格限制POST请求)
@@ -449,9 +385,6 @@ class GluingReport extends Api
         }else{
             $this->error('报工提交失败');
         }
-
-
-
     }
 
 
@@ -673,7 +606,7 @@ class GluingReport extends Api
         $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_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 机台编号','a.price',
             'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称'];
         $list = \db('设备_糊盒报工资料')
             ->alias('a')
@@ -685,30 +618,27 @@ class GluingReport extends Api
         if (empty($list)){
             $this->error('未找到报工数据');
         }
-        foreach ($list as $key=>$value){
+        foreach ($list as $key=>$value) {
             $list[$key]['class'] = [];
-            $idList = explode(',',$value['role']);
-            foreach ($idList as $item){
-                $class = \db('')
-                    ->where('id',$item)
-                    ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
-                    ->find();
-                if (!empty($class)){
-                    for ($i=1;$i<16;$i++) {
-                        if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
-                            $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
-                            $list[$key]['class'][] = [
-                                '编号' => $class['bh' . $i],
-                                '姓名' => $name['姓名'],
-                                '比例' => $class['rate'],
-                                '角色' => $class['role'],
-                            ];
-                        }
+            $class = \db('糊盒报工班组')
+                ->where('id', $value['role'])
+                ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
+                bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
+                rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
+                ->find();
+            if (!empty($class)) {
+                for ($i = 1; $i <=30; $i++) {
+                    if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
+                        $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
+                        $list[$key]['class'][] = [
+                            '编号' => $class['bh' . $i],
+                            '姓名' => $name['姓名'],
+                            '比例' => $class['rate'.$i],
+                        ];
                     }
-                    $list[$key]['class'] = array_values($list[$key]['class']);
                 }
+                $list[$key]['class'] = array_values($list[$key]['class']);
             }
-            unset($list[$key]['role']);
         }
 
         $this->success('成功',$list);
@@ -733,39 +663,37 @@ class GluingReport extends Api
             $this->error('参数错误');
         }
         $where = ['Uid'=>$params['id']];
-        $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_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 机台编号',
-            'b.yj_Yjdh as 产品代号','yj_yjmc as 产品名称'];
+            'a.sys_id as 创建人员','a.sys_rq as 上报时间','a.mod_rq as 修改时间','a.Uid','a.sczl_jtbh as 机台编号','a.price',
+            'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称'];
         $list = \db('设备_糊盒报工资料')
             ->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')
             ->where($where)
             ->field($field)
             ->find();
 
         $list['class'] = [];
-        $idList = explode(',',$list['role']);
-        foreach ($idList as $item){
-            $class = \db('设备_糊盒班组资料')
-                ->where('id',$item)
-                ->field("role,rate,bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15")
-                ->find();
-            if (!empty($class)){
-                for ($i=1;$i<16;$i++) {
-                    if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
-                        $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
-                        $list['class'][] = [
-                            '编号' => $class['bh' . $i],
-                            '姓名' => $name['姓名'],
-                            '比例' => $class['rate'],
-                            '角色' => $class['role'],
-                        ];
-                    }
+        $class = \db('糊盒报工班组')
+            ->where('id', $list['role'])
+            ->field("bh1,bh2,bh3,bh4,bh5,bh6,bh7,bh8,bh9,bh10,bh11,bh12,bh13,bh14,bh15,bh16,bh17,bh18,bh19,bh20,bh21,bh22,bh23,
+                bh24,bh25,bh26,bh27,bh28,bh29,bh30,rate1,rate2,rate3,rate4,rate5,rate6,rate7,rate8,rate9,rate10,rate11,rate12,rate13,rate14,
+                rate15,rate16,rate17,rate18,rate19,rate20,rate21,rate22,rate23,rate24,rate25,rate26,rate27,rate28,rate29,rate30")
+            ->find();
+        if (!empty($class)) {
+            for ($i = 1; $i <=30; $i++) {
+                if ($class['bh' . $i] != '' && $class['bh' . $i] != '000000') {
+                    $name = \db('人事_基本资料')->where('员工编号', $class['bh' . $i])->field('rtrim(员工姓名) as 姓名')->find();
+                    $list['class'][] = [
+                        '编号' => $class['bh' . $i],
+                        '姓名' => $name['姓名'],
+                        '比例' => $class['rate'.$i],
+                    ];
                 }
-                $list['class'] = array_values($list['class']);
             }
+            $list['class'] = array_values($list['class']);
         }
         $this->success('成功',$list);
     }
@@ -783,22 +711,81 @@ class GluingReport extends Api
         if ($this->request->isPost() === false){
             $this->error('请求错误');
         }
-        $params =  Request::instance()->post();
-        if(!isset($params['id']) || empty($params['id'])){
+        $param = Request::instance()->post();
+        if(!isset($param['id']) || empty($param['id'])){
             $this->error('参数错误');
         }
-        $id = $params['id'];
-        unset($params['id']);
-        $params['mod_rq'] = date('Y-m-d H:i:s',time());
-        $sql = \db('设备_糊盒报工资料')
-            ->where('Uid',$id)
-            ->fetchSql(true)
-            ->update($params);
-        $res = \db()->query($sql);
-        if ($res === false) {
-            $this->error('修改成功');
+        $id = $param['id'];
+        $role = $param['role'];
+        unset($param['id']);
+        // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符
+        $validate = new \think\Validate([
+            'sczl_jtbh' => 'require|regex:/^[\w#\x{4e00}-\x{9fa5}]+$/u', // 允许字母、数字、下划线、#、中文
+            'sczl_gdbh' => 'require',
+            'sczl_gxmc' => 'require',
+            'sczl_rq'   => 'require|dateFormat:Y-m-d H:i:s',
+        ], [
+            'sczl_jtbh.require' => '机组编号不能为空',
+            'sczl_jtbh.regex'   => '机组编号仅支持字母、数字、下划线、#号和中文',
+            'sczl_gdbh.require' => '工单号不能为空',
+            'sczl_gxmc.require' => '工序名称不能为空',
+            'sczl_rq.require'   => '报工日期不能为空',
+            'sczl_rq.dateFormat' => '报工日期格式错误(需为Y-m-d H:i:s)',
+        ]);
+
+        // 验证前处理:仅去除前后空格(不影响#字符)
+        foreach (['sczl_jtbh', 'sczl_gdbh', 'sczl_gxmc', 'sczl_rq'] as $field) {
+            if (isset($param[$field]) && is_string($param[$field])) {
+                $param[$field] = trim($param[$field]); // 只去空格,保留#等特殊字符
+            }
+        }
+
+        // 执行验证
+        if (!$validate->check($param)) {
+            $this->error($validate->getError());
+        }
+
+        // 3. 事务处理(确保数据一致性)
+        $currentTime = date('Y-m-d H:i:s');
+        $tableClass = '糊盒报工班组';
+        $tableDevice = '设备_糊盒报工资料';
+
+        // 4. 构建班组数据(保留#字符,无需额外处理)
+        $classData = [
+            'jtbh'    => $param['sczl_jtbh'], // 直接保留含#的原始值
+            'gdbh'    => $param['sczl_gdbh'],
+            'gxmc'    => $param['sczl_gxmc'],
+            'sczl_rq' => $param['sczl_rq'],
+        ];
+
+        // 处理动态字段(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);
+
+        // 5. 保存班组数据(框架会自动处理特殊字符转义,避免SQL注入)
+        $classId = Db::name($tableClass)->where('id',$role)->update($classData);
+
+
+        // 6. 构建设备数据(适配PHP7.2)
+        $filteredParam = array_filter($param, function ($key) {
+            return !preg_match('/^(bh|rate)\d+$/', $key);
+        }, ARRAY_FILTER_USE_KEY);
+
+        $deviceData = array_merge([
+            'mod_rq'  => $currentTime,
+        ], $filteredParam);
+
+        // 7. 保存设备数据(含#字符的字段会被自动转义,安全插入)
+        $sql = Db::name($tableDevice)->where('Uid',$id)->fetchSql(true)->update($deviceData);
+        $saveResult = db()->query($sql);
+        if ($saveResult !== false) {
+            $this->success('修改成功');
         }else{
-            $this->success('修改失败');
+            $this->error('修改失败');
         }
     }