request->isGet() === false){ $this->error('请求错误'); } $data = []; $sist = ['印后糊盒车间','精品自动化车间','数字化车间']; $department = \db('设备_基本资料') ->distinct(true) ->where('使用部门','in',$sist) ->where('设备编组','<>','') ->order('设备编组') ->column('rtrim(使用部门) as 使用部门'); if (empty($department)){ $this->success('未获取到机台数据'); } foreach ($department as $value){ $machine = \db('设备_基本资料') ->where('使用部门',$value) ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称') ->order('设备编号') ->select(); foreach ($machine as $k=>$v){ $data[rtrim($value)][] = $v['设备编号'].'-->'.$v['设备名称']; } } $this->success('成功',$data); } /** * 设备角色列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGluingcoleList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $list = db('设备_糊盒班组角色') ->where('jtbh',$param['machine']) ->select(); if (empty($list)){ $this->error('未找到数据'); }else{ $this->success('成功',$list); } } /** * 设备角色资料详情 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGluingcoleDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $list = db('设备_糊盒班组角色') ->where('id',$param['id']) ->find(); if (empty($list)){ $this->error('未找到数据详情'); }else{ $this->success('成功',$list); } } /** * 新增机台班组角色 * @return void */ public function saveGluingcoleDetail(){ if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $param['sys_rq'] = date('Y-m-d H:i:s',time()); $res = db('设备_糊盒班组角色')->insert($param); if (empty($res)){ $this->error('新增失败'); }else{ $this->success('新增成功'); } } /** * 修改设备角色资料 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function getGluingcoleDetaiEdit() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param['id'])){ $this->error('参数错误'); } $id = intval($param['id']); $param['mod_rq'] = date('Y-m-d H:i:s',time()); unset($param['id']); $res = db('设备_糊盒班组角色') ->where('id',$id) ->update($param); if ($res === false){ $this->error('修改失败'); }else{ $this->success('修改成功'); } } /** * 设备当前班组角色 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function UpdateGluingcoleStatus() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param['id']) || empty($param['jtbh'])){ $this->error('参数错误'); } $updateRes = db('设备_糊盒班组角色') ->where('jtbh',$param['jtbh']) ->update(['status' => 0]); $res = db('设备_糊盒班组角色') ->where('id',$param['id']) ->update(['status' => 1]); if ($updateRes === false || $res === false){ $this->error('更新失败'); }else{ $this->success('更新成功'); } } /** * 设备班组资料列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGluingClassLList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $list = db('设备_糊盒班组资料') ->where('jtbh',$param['machine']) ->select(); foreach ($list as $k=>$v){ for ($i = 1; $i <= 15; $i++){ $bh = $v['bh'.$i]; $list[$k]['name'.$i] = ''; if (!empty($bh) && $bh !== ''){ $name = \db('人事_基本资料') ->where('员工编号',$bh) ->value('员工姓名'); $list[$k]['name'.$i] = $name; } } } if (empty($list)){ $this->error('未找到数据'); }else{ $this->success('成功',$list); } } /** * 获取设备角色数据 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGluingcole() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param)){ $this->error('参数错误'); } $list = db('设备_糊盒班组角色') ->where('jtbh',$param['machine']) ->where('status',1) ->find(); if (empty($list)){ $this->error('未找到角色资料'); }else{ $this->success('成功',$list); } } /** * 新增糊盒机台班组资料 * @return void */ public function AddGluingClass() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param)){ $this->error('参数错误'); } $param['sys_rq'] = date('Y-m-d H:i:s',time()); $res = db('设备_糊盒班组资料')->insert($param); if ($res === false){ $this->error('新增失败'); }else{ $this->success('新增成功'); } } /** * 修改设备糊盒班组资料 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function UpdateGluingClass() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param['id'])){ $this->error('参数错误'); } $id = intval($param['id']); $param['mod_rq'] = date('Y-m-d H:i:s',time()); unset($param['id']); $res = db('设备_糊盒班组资料') ->where('id',$id) ->update($param); if ($res === false){ $this->error('修改失败'); }else{ $this->success('修改成功'); } } /** * 糊盒机台报工 * @return void * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function addGluingReportData() { // 1. 请求方式验证(严格限制POST请求) if (!$this->request->isPost()) { $this->error('仅支持POST请求'); } // 2. 获取并验证参数(重点适配含#字符的sczl_jtbh字段) $param = $this->request->post(); // 核心修复:验证规则调整,明确允许#字符,兼容特殊字符 $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]); // 只去空格,保留#等特殊字符 } } // 执行验证 if (!$validate->check($param)) { $this->error($validate->getError()); } if ($this->isEmptyReportPrice($param['price'] ?? null)) { $resolvedPrice = $this->resolveReportPrice($param); if ($resolvedPrice !== null) { $param['price'] = $resolvedPrice; } else { unset($param['price']); } } // 3. 事务处理(确保数据一致性) $currentTime = date('Y-m-d H:i:s'); $tableClass = '糊盒报工班组'; $tableDevice = '设备_糊盒报工资料'; // 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'], 'sczl_bzdh' => $param['sczl_bzdh'], ]; // 处理动态字段(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)->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('报工提交失败'); } } /** * 糊盒机台报工数据菜单 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getTab() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $data = []; $date = date('Y-m-d 00:00:00',time()-3888000); $sist = ['印后糊盒车间','精品自动化车间','精品手工车间','数字化车间']; $department = \db('设备_基本资料') ->distinct(true) ->where('使用部门','in',$sist) ->where('设备编组','<>','') ->order('设备编组') ->column('rtrim(使用部门) as 使用部门'); if (empty($department)){ $this->success('未获取到机台数据'); } $list = \db('设备_糊盒报工资料') ->field([ 'DISTINCT(sczl_rq)' => '时间', 'rtrim(sczl_jtbh)' => '机台编号' ]) ->where('sczl_rq','>',$date) ->order('sczl_rq desc') ->select(); foreach ($department as $value){ $machine = \db('设备_基本资料') ->where('使用部门',$value) ->field('rtrim(设备编号) as 设备编号,rtrim(设备名称) as 设备名称') ->order('设备编号') ->select(); foreach ($machine as $k=>$v){ $data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']] = []; foreach ($list as $kk=>$vv){ if ($v['设备编号'] === $vv['机台编号']){ array_push($data[rtrim($value)][$v['设备编号'].'-->'.$v['设备名称']],date('Y-m-d',strtotime($vv['时间']))); } } } } $this->success('成功',$data); } /** * 获取当前生产工单 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GetProduction() { if (Request::instance()->isGet() == false) { $this->error('非法请求'); } $params = Request::instance()->param(); if (!isset($params['machine']) || empty($params['machine'])) { $this->error('参数错误'); } $machine = $params['machine']; $machineCode = \db('dic_lzde')->where('适用机型',$machine)->value('sys_bh'); $data = \db('设备_糊盒报工采集')->where('sczl_jtbh',$machine)->order('UID desc')->find(); if (empty($data)){ $this->success('未找到数据'); } $list = []; $list['班组ID'] = $data['班组ID']; $row = []; if (!empty($data['sczl_gdbh'])){ $endTime = \db('工单_工艺资料') ->where('Gy0_gdbh',$data['sczl_gdbh']) ->where('Gy0_yjno',$data['sczl_yjno']) ->where('Gy0_gxh',$data['sczl_gxh']) ->find(); $list['工单编号'] = $data['sczl_gdbh']; if (!empty($endTime)){ $list['印件号'] = $data['sczl_yjno']; $name = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品名称'); $code = \db('工单_基本资料')->where('Gd_Gdbh',$data['sczl_gdbh'])->value('成品代号'); $list['产品名称'] = rtrim($name); $list['产品代号'] = rtrim($code); $list['工序名称'] = $data['sczl_gxmc']; $list['联数'] = $endTime['Gy0_ls']; } $list['状态'] = rtrim($data['status']); $list['开工时间'] = $data['sczl_kgsj']; }else{ $list['工单编号'] = ''; $list['印件号'] = 0; $list['产品名称'] = ''; $list['工序名称'] = ''; $list['产品代号'] = ''; $list['状态'] = ''; $list['开工时间'] = ''; $list['联数'] = ''; } $list['班组编号'] = rtrim($data['sczl_bzbh']); $idList = explode(',',$data['班组ID']); foreach ($idList as $k=>$v){ $class = \db('设备_糊盒班组资料') ->where('id',$v) ->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(); $row[] = [ '编号' => $class['bh' . $i], '姓名' => $name['姓名'], '比例' => $class['rate'], '角色' => $class['role'], ]; } } $row = array_values($row); } } $list['班组成员'] = $row; $list['定额代号'] = $machineCode; $this->success('成功',$list); } /** * 设置当前工单和当前班组 * @return void */ public function setMachineTeam() { if (Request::instance()->isPost() == false) { $this->error('非法请求'); } $params = Request::instance()->post(); if (!isset($params['machine']) || empty($params['machine'] )) { $this->error('参数不能为空'); } if (empty($params['team_id']) || empty($params['sczl_bzdh'])){ $this->error('请先选择班组成员'); } $machine = $params['machine'] . '#'; $data = []; $data['status'] = $params['status']; $data['sczl_sj'] = date('Y-m-d H:i:s'); $data['sczl_jtbh'] = $machine; $data['sczl_gdbh'] = empty($params['order']) ? '':$params['order']; $data['sczl_yjno'] = empty($params['yjno']) ? '':$params['yjno']; $data['sczl_gxh'] = empty($params['gy_name']) ? '' : (int)substr($params['gy_name'], 0, 2); $data['sczl_gxmc'] = empty($params['gy_name']) ? '' : $params['gy_name']; $data['sczl_bzbh'] = $params['sczl_bzdh']; $data['班组ID'] = $params['team_id']; // 获取当前时间 $current_time = time(); // 设置时间范围 $start_time1 = strtotime(date('Y-m-d') . ' 08:30:00'); $end_time1 = strtotime(date('Y-m-d') . ' 20:30:00'); $end_time2 = strtotime(date('Y-m-d') . ' 24:00:00'); $start_time3 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 08:30:00'); $start_time4 = strtotime(date('Y-m-d', strtotime('+1 day')) . ' 00:00:00'); // 判断当前时间属于哪个时间范围 if ($current_time >= $start_time1 && $current_time <= $end_time1) { $data['sczl_kgsj'] = date('Y-m-d') . ' 08:30:00'; } elseif ($current_time > $end_time1 && $current_time <= $end_time2) { $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00'; } elseif ($current_time > $end_time1 && $current_time <= $start_time3) { $data['sczl_kgsj'] = date('Y-m-d', strtotime('+1 day')) . ' 08:30:00'; }elseif ($current_time > $start_time4 && $current_time <= $start_time3){ $data['sczl_kgsj'] = date('Y-m-d') . ' 20:30:00'; } if (!empty($params['order']) && !empty($params['yjno'])){ $option['Gy0_gdbh'] = $params['order']; $option['Gy0_yjno'] = $params['yjno']; $option['Gy0_gxh'] = $data['sczl_gxh']; $data['任务ID'] = \db('工单_工艺资料')->where($option)->value('UniqId'); }else{ $data['任务ID'] = ''; } $sql = \db('设备_糊盒报工采集')->fetchSql(true)->insert($data); $res = Db::query($sql); if ($res === false) { $this->error('设置失败'); } else { $this->success('设置成功'); } } /** * 糊盒设备运行跟踪数据页面显示 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGluingReportDataList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $params = $this->request->param(); if (empty($params['machine']) || empty($params['day'])){ $this->error('参数错误'); } $where = [ 'a.sczl_jtbh' => $params['machine'], 'a.sczl_rq' => ['like',$params['day'].'%'] ]; $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.price', 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称','a.sczl_bzdh as 班组','a.来料异常']; $list = \db('设备_糊盒报工资料') ->alias('a') ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left') ->field($field) ->where($where) ->group('a.Uid') ->select(); if (empty($list)){ $this->error('未找到报工数据'); } foreach ($list as $key=>$value) { $list[$key]['class'] = []; $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']); } } $this->success('成功',$list); } /** * 报工数据详情显示 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getGluingReportDataDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $params = $this->request->param(); if(!isset($params['id']) || empty($params['id'])){ $this->error('参数错误'); } $where = ['Uid'=>$params['id']]; $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.price', 'b.Gd_cpdh as 产品代号','b.Gd_cpmc as 产品名称','a.sczl_bzdh as 班组','a.来料异常']; $list = \db('设备_糊盒报工资料') ->alias('a') ->join('工单_基本资料 b','a.sczl_gdbh = b.Gd_gdbh','left') ->where($where) ->field($field) ->find(); $list['class'] = []; $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']); } $this->success('成功',$list); } /** * 修改糊盒班组报工资料 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function getGluingReportDetailUpdate() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if(!isset($param['id']) || empty($param['id'])){ $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'], 'sczl_bzdh' => $param['sczl_bzdh'], ]; // 处理动态字段(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->error('修改失败'); } } /** * 修改报工数据班组 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function UpdateGluingReportClass() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $params = Request::instance()->post(); if(!isset($params['id']) || empty($params['id'])){ $this->error('参数错误'); } $id = $params['id']; $data['role'] = $params['role']; $res = \db('设备_糊盒报工资料')->where('Uid',$id)->update($data); if ($res === false) { $this->error('修改失败'); }else{ $this->success('修改成功'); } } /** * 当班产量 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function GluingReportList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $params = $this->request->param(); if (!isset($params['machine']) || empty($params['machine'])){ $this->error('参数错误'); } $machine = $params['machine']; $sczlTime = date('Y-m-d H:i:s',time()); if ($sczlTime>date('Y-m-d 00:00:00') && $sczlTime $machine, 'sczl_rq' => $time, ]; $field = 'rtrim(sczl_gdbh) as 工单编号,rtrim(sczl_yjno) as yjno,rtrim(sczl_dedh) as dedh,rtrim(sczl_gxmc) as gxmc, rtrim(sczl_cl) as 产量,rtrim(sczl_zcfp) as 制程废品,rtrim(装版工时) as 装版工时,rtrim(保养工时) as 保养工时, rtrim(异常工时) as 异常工时,rtrim(设备运行工时) as 通电工时,startTime,endTime,Uid,rtrim(sczl_ls) as ls,来料数量,来料异常'; $list = \db('设备_糊盒报工资料')->where($where)->field($field)->order('Uid desc')->select(); if (!empty($list)){ foreach ($list as $k=>$v){ $name = \db('工单_印件资料')->where('Yj_Gdbh',$v['工单编号'])->where('yj_Yjno',$v['yjno'])->field('rtrim(yj_yjmc) as cpmc')->find(); if ($v['yjno']<10){ $list[$k]['yjno'] = '0'.$v['yjno']; } $list[$k]['印件及工序'] = $list[$k]['yjno'].'-'.$v['gxmc']; $list[$k]['生产时间段'] = substr($v['startTime'],5,5).' '.substr($v['startTime'],11,5).'<-->'.substr($v['endTime'],5,5).' '.substr($v['endTime'],11,5); $list[$k]['产品名称'] = $name['cpmc']; } } $this->success('成功',$list); } /** * 工单查询 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getWorkOrderList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $params = $this->request->param(); if (!isset($params['search']) || empty($params['search'])){ $this->error('参数错误'); } $search = $params['search']; $list = \db('工单_基本资料') ->where('Gd_gdbh|Gd_cpmc','like','%'.$search.'%') ->field('Gd_gdbh as 工单编号,Gd_cpdh as 产品代号,Gd_cpmc as 产品名称') ->order('Gd_gdbh desc') ->select(); if (empty($list)){ $this->error('未找到数据'); }else{ $this->success('成功',$list); } } /** * 查询机台编号 * @return void */ public function getMachineList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $params = $this->request->param(); if (!isset($params['search']) || empty($params['search'])){ $this->error('参数错误'); } $search = $params['search']; $list = \db('设备_基本资料') ->where('使用部门',$search) ->order('设备编号') ->column('设备名称','设备编号'); if (empty($list)){ $this->error('未找到数据'); }else{ $this->success('成功',$list); } } /** * 查询工艺名称 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function gitProcessList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $params = $this->request->param(); if (!isset($params['search']) || empty($params['search'])){ $this->error('参数错误'); } $search = $params['search']; $list = \db('erp_常用字典') ->where('分类','糊盒工艺') ->where('名称','like','%'.$search.'%') ->field('名称') ->select(); $data = []; if (empty($list)){ $this->error('未找到工艺数据'); } foreach ($list as $k=>$v){ $name = explode('_',$v['名称']); $data[] = $name[2]; } $this->success('成功',$data); } //最新糊盒班组查询 public function classifyList() { // 1. 请求方法验证 if (!$this->request->isGet()) { $this->error('请使用GET请求'); } // 2. 参数验证 $params = $this->request->param(); // 验证必需参数 $requiredParams = ['sczl_jtbh', 'sczl_bzdh']; foreach ($requiredParams as $param) { if (empty($params[$param] ?? null)) { $this->error("缺少必要参数: {$param}"); } } $jtbh = trim($params['sczl_jtbh']); $bzdh = trim($params['sczl_bzdh']); // 3. 查询糊盒报工资料 $role = \db('设备_糊盒报工资料') ->where('sczl_jtbh', $jtbh) ->where('sczl_bzdh', $bzdh) ->order('Uid', 'desc') ->value('role'); if (empty($role)) { $this->error('未找到对应的报工班组信息'); } // 4. 动态生成字段列表 $fields = []; for ($i = 1; $i <= 30; $i++) { $fields[] = "bh{$i}"; $fields[] = "rate{$i}"; } // 5. 查询班组信息 $class = \db('糊盒报工班组') ->where('id', $role) ->field($fields) ->find(); if (empty($class)) { $this->error('班组信息不存在'); } // 6. 处理班组成员信息 $members = []; $employeeIds = []; // 收集所有有效的员工编号 for ($i = 1; $i <= 30; $i++) { $bh = $class["bh{$i}"] ?? ''; $rate = $class["rate{$i}"] ?? 0; // 验证员工编号是否有效 $bh = trim($bh); if (!empty($bh) && $bh !== '000000') { $employeeIds[$bh] = [ 'index' => $i, 'rate' => $rate ]; } } // 如果有有效的员工编号,批量查询员工信息 if (!empty($employeeIds)) { $employeeNames = []; $ids = array_keys($employeeIds); // 批量查询员工信息(减少数据库查询次数) $result = \db('人事_基本资料') ->whereIn('员工编号', $ids) ->field('员工编号, rtrim(员工姓名) as 姓名') ->select(); foreach ($result as $item) { $employeeNames[$item['员工编号']] = $item['姓名']; } // 组装返回数据 foreach ($employeeIds as $employeeId => $info) { $members[] = [ '编号' => $employeeId, '姓名' => $employeeNames[$employeeId] ?? '未知', '比例' => $info['rate'] ]; } } $this->success('获取成功', array_values($members)); } /** * 判断报工单价是否为空 * @param mixed $price * @return bool */ protected function isEmptyReportPrice($price) { return $price === null || $price === ''; } /** * 报工时自动解析单价 * @param array $param * @return float|null */ protected function resolveReportPrice(array $param) { $machine = trim($param['sczl_jtbh']); $gxmc = trim($param['sczl_gxmc']); $count = $this->extractManualProcessCountSilent($param); $machinePrices = $this->collectMachinePriceValues($machine); if (!empty($machinePrices)) { return (float)$machinePrices[0]; } $manualPrice = $this->resolveManualProcessPriceSilent($gxmc, $count); if ($manualPrice !== null) { return $manualPrice; } $aiService = new Aicompute(); $rates = $aiService->fetchWorkshopRatesByMachine($machine); if (empty($rates)) { return null; } $productName = db('工单_基本资料') ->where('Gd_gdbh', trim($param['sczl_gdbh'])) ->value('Gd_cpmc'); $productName = $productName ? trim($productName) : ''; if ($productName !== '') { $matched = $aiService->matchWorkshopRateByProductName($rates, $productName); if (!empty($matched)) { return (float)$matched['piece_price']; } } $aiPrice = $aiService->resolvePiecePriceByAi(trim($param['sczl_gdbh']), $machine); if ($aiPrice !== null) { return $aiPrice; } return (float)$rates[0]['piece_price']; } /** * 收集机台单价列表 * @param string $machine * @return array */ protected function collectMachinePriceValues($machine) { $list = db('machine_price') ->where('machine_id', $machine) ->field('price1,price2,price3,price4,price5,price6,price7,price8,price9,price10') ->select(); $result = []; foreach ($list as $item) { foreach ($item as $value) { if ($value !== '' && $value !== null) { $result[] = $value; } } } return $result; } /** * 静默解析手工工序单价(报工场景,不中断请求) * @param string $gxmc * @param int|null $count * @return float|null */ protected function resolveManualProcessPriceSilent($gxmc, $count) { if (strpos($gxmc, '割围条') !== false) { $row = db('machine_price')->where('machine_id', '手工割围条')->find(); return $row ? (float)$row['price1'] : null; } if (strpos($gxmc, '打孔') !== false) { if ($count === null) { return null; } return $this->resolvePunchHolePrice($count); } if (strpos($gxmc, '拆片') !== false) { $rows = db('machine_price')->where('machine_id', '手工拆片')->select(); if (empty($rows)) { return null; } return $this->resolveManualStripPriceSilent($rows, $gxmc, $count); } if (strpos($gxmc, '贴磁铁') !== false) { return $this->resolveManualCountPriceFromDb('手工贴磁铁', $count, false); } if (strpos($gxmc, '贴片') !== false) { return $this->resolveManualCountPriceFromDb('手工贴片', $count, false); } $row = db('machine_price') ->where('machine_id', $gxmc) ->whereOr('machine_name', 'like', '%' . $gxmc . '%') ->find(); return $row ? (float)$row['price1'] : null; } /** * 静默提取孔数/拼数 * @param array $params * @return int|null */ protected function extractManualProcessCountSilent(array $params) { if (!isset($params['count']) || $params['count'] === '') { return null; } if (!is_numeric($params['count']) || (int)$params['count'] != $params['count'] || (int)$params['count'] <= 0) { return null; } return (int)$params['count']; } /** * 静默解析手工拆片单价 * @param array $rows * @param string $gxmc * @param int|null $count * @return float|null */ protected function resolveManualStripPriceSilent(array $rows, $gxmc, $count) { $gxmc = trim($gxmc); if ($gxmc === '手工拆片' || $gxmc === '拆片') { if ($count === null) { return null; } return $this->resolveDefaultStripPriceByCount($count); } if ($gxmc !== '') { $row = $this->matchManualPriceRow($rows, $gxmc); if (empty($row)) { return null; } return $this->calculateStripPriceByRowSilent($row, $count); } if ($count === null) { return null; } return $this->resolveDefaultStripPriceByCount($count); } /** * 静默计算拆片规格单价 * @param array $row * @param int|null $count * @return float|null */ protected function calculateStripPriceByRowSilent(array $row, $count) { $name = $row['machine_name']; $basePrice = (float)$row['price1']; if (strpos($name, '1-4拼(特种纸') !== false) { if ($count === null) { return null; } if ($count <= 4) { return $basePrice; } return floatval(number_format($basePrice + ($count - 4) * 0.001, 4, '.', '')); } if (strpos($name, '1-4拼(小盒二次拆片') !== false) { if ($count === null) { return null; } if ($count === 8) { return 0.01; } if ($count === 10) { return 0.012; } return $basePrice; } if (strpos($name, '5-6拼(小盒二次拆片') !== false) { return $basePrice; } if (strpos($name, '6拼及以上') !== false) { return $basePrice; } if (preg_match('/^(\d+)拼/u', $name) && $count === null) { return null; } return $basePrice; } /** * 获取机台报工单价数据 * 参数:machine(机台编号)、gxmc(工序名称)、count(孔数/拼数,纯数字) * 优先按机台编号查询;查不到则按手工工序(gxmc + count)匹配单价 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getMachinePriceData() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $params = $this->request->param(); if (!isset($params['machine']) || $params['machine'] === '') { $this->error('参数错误'); } $machine = trim($params['machine']); // 1. 优先按机台编号查询 $list = db('machine_price') ->where('machine_id', $machine) ->field('price1,price2,price3,price4,price5,price6,price7,price8,price9,price10') ->select(); $result = []; foreach ($list as $item) { foreach ($item as $value) { if ($value !== '' && $value !== null) { $result[] = $value; } } } if (!empty($result)) { $this->success('获取成功', $result); } // 2. 机台编号查不到,按手工工序查询 $gxmc = trim((string)($params['gxmc'] ?? '')); if ($gxmc === '') { $this->error('未找到机台单价,请传入工序名称'); } $price = $this->resolveManualProcessPriceByGxmc($gxmc, $params); if ($price === null) { $this->error('未找到匹配的单价'); } $this->success('获取成功', [$price]); } /** * 按工序名称解析手工工序单价 * @param string $gxmc 工序名称 * @param array $params count=孔数/拼数(纯数字) * @return float|null */ protected function resolveManualProcessPriceByGxmc($gxmc, array $params) { $count = $this->extractManualProcessCount($params); if (strpos($gxmc, '割围条') !== false) { $row = db('machine_price')->where('machine_id', '手工割围条')->find(); return $row ? (float)$row['price1'] : null; } if (strpos($gxmc, '打孔') !== false) { if ($count === null) { $this->error('请传入孔数(纯数字)'); } return $this->resolvePunchHolePrice($count); } if (strpos($gxmc, '拆片') !== false) { $rows = db('machine_price')->where('machine_id', '手工拆片')->select(); if (empty($rows)) { return null; } return $this->resolveManualStripPrice($rows, $gxmc, $count); } if (strpos($gxmc, '贴磁铁') !== false) { return $this->resolveManualCountPriceFromDb('手工贴磁铁', $count, true); } if (strpos($gxmc, '贴片') !== false) { return $this->resolveManualCountPriceFromDb('手工贴片', $count, true); } // 其他手工工序:按 machine_id 或 machine_name 匹配 $row = db('machine_price') ->where('machine_id', $gxmc) ->whereOr('machine_name', 'like', '%' . $gxmc . '%') ->find(); return $row ? (float)$row['price1'] : null; } /** * 按拼数查询手工工序单价(machine_name 存拼数) * @param string $machineId * @param int|null $count * @param bool $errorOnMissingCount * @return float|null */ protected function resolveManualCountPriceFromDb($machineId, $count, $errorOnMissingCount = false) { if ($count === null) { if ($errorOnMissingCount) { $this->error('请传入拼数(纯数字)'); } return null; } $row = db('machine_price') ->where('machine_id', $machineId) ->where('machine_name', (string)$count) ->find(); return $row ? (float)$row['price1'] : null; } /** * 提取孔数/拼数(前端传入纯数字) * @param array $params * @return int|null */ protected function extractManualProcessCount(array $params) { if (!isset($params['count']) || $params['count'] === '') { return null; } if (!is_numeric($params['count']) || (int)$params['count'] != $params['count'] || (int)$params['count'] <= 0) { $this->error('孔数/拼数必须为大于0的整数'); } return (int)$params['count']; } /** * 手工打孔单价(按孔数区间) * @param int $holeCount * @return float */ protected function resolvePunchHolePrice($holeCount) { $holeCount = (int)$holeCount; if ($holeCount === 1) { return 0.0055; } if ($holeCount >= 2 && $holeCount <= 4) { return 0.007; } if ($holeCount >= 5 && $holeCount <= 8) { return 0.012; } if ($holeCount >= 9 && $holeCount <= 12) { return 0.016; } if ($holeCount >= 13 && $holeCount <= 18) { return 0.025; } return floatval(number_format(0.025 + ($holeCount - 18) * 0.001, 4, '.', '')); } /** * 手工拆片单价(按规格名称 + 拼数) * @param array $rows * @param string $gxmc 工序规格名称(文字) * @param int|null $count 拼数(纯数字) * @return float|null */ protected function resolveManualStripPrice(array $rows, $gxmc, $count) { $gxmc = trim($gxmc); // 通用拆片工序,仅按拼数数字匹配默认单价 if ($gxmc === '手工拆片' || $gxmc === '拆片') { if ($count === null) { $this->error('请传入拼数(纯数字)'); } return $this->resolveDefaultStripPriceByCount($count); } if ($gxmc !== '') { $row = $this->matchManualPriceRow($rows, $gxmc); if (empty($row)) { return null; } return $this->calculateStripPriceByRow($row, $count); } if ($count === null) { $this->error('请传入拼数(纯数字)'); } return $this->resolveDefaultStripPriceByCount($count); } /** * 默认拆片单价(1.0厚以上灰白板,按拼数数字匹配) * @param int $count * @return float */ protected function resolveDefaultStripPriceByCount($count) { $prices = [ 1 => 0.008, 2 => 0.01, 3 => 0.013, 4 => 0.017, 5 => 0.022, 6 => 0.028, ]; $pieceCount = min((int)$count, 6); return $prices[$pieceCount]; } /** * 按 machine_name 匹配手工单价行 * @param array $rows * @param string $gxmc * @return array|null */ protected function matchManualPriceRow(array $rows, $gxmc) { $gxmc = trim($gxmc); foreach ($rows as $row) { if ($row['machine_name'] === $gxmc) { return $row; } } $best = null; $bestLen = 0; foreach ($rows as $row) { $name = $row['machine_name']; if (strpos($name, $gxmc) !== false || strpos($gxmc, $name) !== false) { $len = mb_strlen($name); if ($len > $bestLen) { $best = $row; $bestLen = $len; } } } return $best; } /** * 根据拆片规格行及拼数计算单价 * @param array $row * @param int|null $count 拼数(纯数字) * @return float|null */ protected function calculateStripPriceByRow(array $row, $count) { $name = $row['machine_name']; $basePrice = (float)$row['price1']; if (strpos($name, '1-4拼(特种纸') !== false) { if ($count === null) { $this->error('请传入拼数(纯数字)'); } if ($count <= 4) { return $basePrice; } return floatval(number_format($basePrice + ($count - 4) * 0.001, 4, '.', '')); } if (strpos($name, '1-4拼(小盒二次拆片') !== false) { if ($count === null) { $this->error('请传入拼数(纯数字)'); } if ($count === 8) { return 0.01; } if ($count === 10) { return 0.012; } return $basePrice; } if (strpos($name, '5-6拼(小盒二次拆片') !== false) { return $basePrice; } if (strpos($name, '6拼及以上') !== false) { return $basePrice; } if (preg_match('/^(\d+)拼/u', $name, $matches)) { if ($count === null) { $this->error('请传入拼数(纯数字)'); } } return $basePrice; } /** * 报工数据删除 * @return void * @throws \Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ public function deleteGluingReportData() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $params = $this->request->param(); if (!isset($params['id']) || empty($params['id'])){ $this->error('参数错误'); } $ids = explode(',', $params['id']); $roleIds = \db('设备_糊盒报工资料') ->where('Uid', 'in', $ids) ->column('role'); $roleIds = array_filter(array_unique($roleIds)); db()->startTrans(); try { $res = \db('设备_糊盒报工资料')->where('Uid', 'in', $ids)->delete(); if ($res === false) { throw new \Exception('删除报工数据失败'); } if (!empty($roleIds)) { $classRes = \db('糊盒报工班组')->where('id', 'in', $roleIds)->delete(); if ($classRes === false) { throw new \Exception('删除班组数据失败'); } } db()->commit(); } catch (\Exception $e) { db()->rollback(); $this->error($e->getMessage()); } $this->success('删除成功'); } }