request->isGet()) { $this->error('请求错误'); } $data = [ 'MN印版' => $this->buildTree('05'), '翌星印版' => $this->buildTree('Y05') ]; $this->success('成功', $data); } //私有方法,构建数据 private function buildTree($prefix) { $list = db('物料_存货结构') ->where('编号', 'like', $prefix . '%') ->order('编号', 'asc') ->select(); $map = []; $tree = []; // 创建编号映射表 foreach ($list as $item) { $map[$item['编号']] = $item['名称']; } // 构建树形结构 foreach ($list as $item) { $code = $item['编号']; $name = $item['名称']; $key = "{$code} {$name}"; $depth = strlen($code) - strlen($prefix); switch ($depth) { case 0: // 根节点 (05/Y05) $tree[$key] = []; break; case 2: // 二级节点 (0501/Y0501) $parentCode = substr($code, 0, strlen($prefix)); $parentKey = "{$parentCode} {$map[$parentCode]}"; $tree[$parentKey][$key] = []; break; case 4: // 三级节点 (050100/Y050101) $parentCode = substr($code, 0, strlen($prefix) + 2); $grandCode = substr($code, 0, strlen($prefix)); $grandKey = "{$grandCode} {$map[$grandCode]}"; $parentKey = "{$parentCode} {$map[$parentCode]}"; if (isset($tree[$grandKey][$parentKey])) { $tree[$grandKey][$parentKey][] = $key; } break; } } return $tree; } //存货编码列表 public function MaterailCodeList() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['code'])){ $this->error('参数错误'); } //截取物料编码,带字母截取前七位,不带字母截取前五位 if (preg_match('/[a-zA-Z]/', $param['code'])) { $code = substr($param['code'], 0, 7); } else { $code = substr($param['code'], 0, 6); } //根据物料编码分类查询 } public function MaterailDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['code'])){ $this->error('参数错误'); } } //印版资料修改 public function MaterailEdit() { if ($this->request->isPost() === false){ $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param) || !isset($param['code'])){ $this->error('参数错误'); } $data = [ '存货编码' => $param['code'], '供方批号' => $param['batch'], '印版名称' => $param['desc'], '制造日期' => $param['Manufactur_date'], '原始印数' => $param['start_num'], '考核印数' => $param['Assessment_num'], '报废日期' => $param['Scrappe_date']?:'1900-01-01 00:00:00', 'Sys_id' => $param['sys_id'] ]; //查询数据是否存在,存在则修改,不存在则增加 $res = db('产品_印版库') ->where('存货编码',$param['code']) ->where('供方批号',$param['batch']) ->find(); if (empty($res)){ $data['Sys_rq'] = date('Y-m-d H:i:s',time()); $sql = db('产品_印版库')->fetchSql(true)->insert($data); }else{ $data['Mod_rq'] = date('Y-m-d H:i:s',time()); $sql = db('产品_印版库')->fetchSql(true)->update($data); } $result = db()->query($sql); if ($result === false){ $this->error('修改失败'); }else{ $this->success('修改成功'); } } }