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; } /** * 存货编码列表 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ 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); } //根据物料编码分类查询 $list = db('产品_印版库') ->alias('a') ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码') ->join('工单_印版领用记录 c', 'a.存货编码 = c.Yb_存货编码 AND a.供方批号 = c.Yb_供方批号', 'left') ->where('a.存货编码', 'like', $code . '%') ->group('a.存货编码, a.供方批号') ->order('报废日期, a.存货编码') ->field(' rtrim(a.存货编码) as 存货编码, rtrim(b.物料名称) as 物料名称, rtrim(a.印版名称) as 印版名称, rtrim(a.供方批号) as 供方批号, DATE(a.制造日期) as 制造日期, CASE WHEN a.报废日期 = "1900-01-01 00:00:00" THEN NULL ELSE DATE(a.报废日期) END as 报废日期, a.原始印数, a.考核印数, a.UniqID, a.Sys_id as 创建用户, a.Sys_rq as 创建日期, a.Mod_rq as 修改时间, count(c.Yb_印数) as 累计印数 ') ->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 MaterailDetail() { if ($this->request->isGet() === false){ $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['UniqID'])){ $this->error('参数错误'); } $result = db('产品_印版库') ->alias('a') ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码') ->where('a.UniqID', $param['UniqID']) ->order('报废日期, a.存货编码') ->field(' rtrim(a.存货编码) as 存货编码, rtrim(b.物料名称) as 物料名称, rtrim(a.印版名称) as 印版备注, rtrim(a.供方批号) as 供方批号, DATE(a.制造日期) as 制造日期, CASE WHEN a.报废日期 = "1900-01-01 00:00:00" THEN NULL ELSE DATE(a.报废日期) END as 报废日期, a.原始印数, a.考核印数, a.UniqID ') ->find(); if (empty($result)) { $this->error('未找到数据'); }else{ $this->success('成功', $result); } } /** * 印版资料新增修改 * @return void * @throws \think\Exception * @throws \think\db\exception\BindParamException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\exception\PDOException */ 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'] ]; $UniqID = $param['UniqID']; //查询数据是否存在,存在则修改,不存在则增加 $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('产品_印版库')->where($UniqID)->fetchSql(true)->update($data); } $result = db()->query($sql); if ($result === false){ $this->error('修改失败'); }else{ $this->success('修改成功'); } } /** * 印版新增->存货编码获取 * @return void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getInventoryCode() { if (!$this->request->isGet()) { $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['code'])) { $this->error('参数错误'); } // 提取前缀 $codeLength = preg_match('/[a-zA-Z]/', $param['code']) ? 5 : 4; $code = substr($param['code'], 0, $codeLength); $where['物料代码'] = ['like', $code . '%']; if (!empty($param['search'])) { $where['物料名称'] = ['like', '%' . $param['search'] . '%']; } // 查询物料存货编码 $list = db('物料_存货编码') ->where($where) ->field('rtrim(物料代码) as 物料代码, rtrim(物料名称) as 物料名称, rtrim(规格) as 规格') ->select(); // 数据处理 $data = []; foreach ($list as $item) { // 提取物料代码的前缀 $prefixLength = preg_match('/[a-zA-Z]/', $item['物料代码']) ? [3, 5, 7] : [2, 4, 6]; // 使用普通的数组映射替代箭头函数 $num1 = substr($item['物料代码'], 0, $prefixLength[0]); $num2 = substr($item['物料代码'], 0, $prefixLength[1]); $num3 = substr($item['物料代码'], 0, $prefixLength[2]); // 批量查询物料结构名称 $names = db('物料_存货结构') ->whereIn('编号', [$num1, $num2, $num3]) ->column('名称', '编号'); // 将数据组织进数组 $name1 = isset($names[$num1]) ? $names[$num1] : ''; $name2 = isset($names[$num2]) ? $names[$num2] : ''; $name3 = isset($names[$num3]) ? $names[$num3] : ''; if (!isset($data["$num1/$name1"][$num2][$num3.'/'.$name3])) { $data["$num1/$name1"][$num2][$num3.'/'.$name3] = []; } $data["$num1/$name1"][$num2][$num3.'/'.$name3][] = "{$item['物料代码']}/{$item['物料名称']}/{$item['规格']}"; } $this->success('成功', $data); } }