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()) { $this->error('请求错误'); } $params = $this->request->param(); if (empty($params)) { $this->error('参数错误'); } $page = intval($params['page']); $limit = intval($params['limit']); $where = []; if (!empty($params['code'])) { $code = preg_match('/[a-zA-Z]/', $params['code']) ? substr($params['code'], 0, 7) : substr($params['code'], 0, 6); $where['a.存货编码'] = ['like', $code . '%']; } if (!empty($params['search'])) { $where['b.物料名称'] = ['like', '%' . $params['search'] . '%']; } //总数 $total = db('产品_印版库') ->alias('a') ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码') ->join('工单_印版领用记录 c', 'a.存货编码 = c.Yb_存货编码 AND a.供方批号 = c.Yb_供方批号', 'LEFT') ->where($where) ->group('a.存货编码, a.供方批号') ->count(); $list = db('产品_印版库') ->alias('a') ->join('物料_存货编码 b', 'a.存货编码 = b.物料代码') ->join('工单_印版领用记录 c', 'a.存货编码 = c.Yb_存货编码 AND a.供方批号 = c.Yb_供方批号', 'LEFT') ->where($where) ->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', 'rtrim(a.Sys_id) as 创建用户', 'a.Sys_rq as 创建日期', 'a.Mod_rq as 修改时间', 'COUNT(c.Yb_印数) as 累计印数', "MAX(CASE WHEN c.Yb_领用日期 IS NOT NULL AND c.Yb_退还日期 IS NULL THEN c.Yb_工单编号 END) as 工单编号" ]) ->limit(($page - 1) * $limit, $limit) ->select(); if (empty($list)) { $this->error('未找到相关记录'); } $this->success('查询成功', [ 'data' => $list, 'total' => $total, ]); } /** * 印版管理印版修改 * @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'] ]; //查询数据是否存在,存在则修改,不存在则增加 $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{ $UniqID = $param['UniqID']; $data['Mod_rq'] = date('Y-m-d H:i:s',time()); $sql = db('产品_印版库')->where('UniqID',$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); } /** * 印版领用 * @return void * @throws \think\db\exception\BindParamException * @throws \think\exception\PDOException */ public function PrintingPlateReceive() { if (!$this->request->isPost()) { $this->error('请求错误'); } $param = Request::instance()->post(); if (empty($param) || !isset($param['gdbh'])) { $this->error('参数错误'); } $data = [ 'Yb_工单编号' => $param['gdbh'], 'Yb_印件号' => $param['yjno'], 'Yb_存货编码' => $param['code'], 'Yb_供方批号' => $param['batch'], 'Yb_领用日期' => date('Y-m-d',time()), 'Yb_领用机台' => $param['machine'], 'Sys_id' => $param['sys_id'], 'Sys_rq' => date('Y-m-d H:i:s',time()), ]; $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 PrintDetailReceive() { if (!$this->request->isGet()) { $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['code'])) { $this->error('参数错误'); } $list = db('工单_印版领用记录') ->alias('a') ->field('a.Yb_工单编号 as 工单编号,a.Yb_印件号 as 印件号,a.Yb_领用日期 as 领用日期,a.Yb_退还日期 as 退还日期,a.Yb_领用机台 as 领用机台, a.Yb_印数 as 印数,a.Sys_id as 创建用户,a.Sys_rq as 创建时间,a.UniqID,b.成品代号,rtrim(b.成品名称) as 成品名称') ->join('工单_基本资料 b', 'a.Yb_工单编号 = b.Gd_gdbh') ->where('a.Yb_存货编码',$param['code']) ->where('a.Yb_供方批号',$param['batch']) ->order('a.Yb_领用日期 desc') ->group('a.Yb_工单编号') ->select(); if (empty($list)){ $this->error('未找到领用记录'); }else{ $this->success('成功', $list); } } /** * 印版领用记录删除 * @return void * @throws \think\Exception * @throws \think\exception\PDOException */ public function PrintReceiveDelete() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['id'])) { $this->error('参数错误'); } $id = explode(',', $param['id']); if (empty($id)) { $this->error('请先选中要删除数据'); } $res = db('工单_印版领用记录') ->where('UniqID','in',$id) ->delete(); if ($res === false){ $this->error('删除失败'); }else{ $this->success('删除成功'); } } //印版库管理菜单下方客户产品印版管理 public function PrintDetailTab() { if($this->request->isGet() === false){ $this->error('请求错误'); } $list = db('产品_印版资料') ->alias('a') ->field(' rtrim(a.YB_Cpdh) as 产品代号, rtrim(b.产品名称) as 产品名称, rtrim(b.客户编号) as 客户编号, rtrim(b.客户名称) as 客户名称, rtrim(a.存货编码) as 存货编码, rtrim(c.名称) as 名称 ') ->join('产品_基本资料 b', 'a.YB_Cpdh = b.产品编号') ->join('物料_存货结构 c', "(CASE WHEN a.存货编码 REGEXP '[A-Za-z]' THEN LEFT(rtrim(a.存货编码), 5) ELSE LEFT(rtrim(a.存货编码), 4) END) = rtrim(c.编号)" ) ->group('a.YB_Cpdh,名称') ->order('a.YB_Cpdh,a.存货编码') ->select(); if (empty($list)){ $this->error('未找到客户数据'); } foreach ($list as $item) { $customerKey = $item['客户编号'] . '【' . $item['客户名称'] . '】'; $productKey = $item['产品代号'] . '【' . $item['产品名称'] . '】'; if (!isset($data[$customerKey])) { $data[$customerKey] = []; } if (!isset($data[$customerKey][$productKey])) { $data[$customerKey][$productKey] = []; } if (!in_array($item['名称'], $data[$customerKey][$productKey])) { $data[$customerKey][$productKey][] = $item['名称']; } } $this->success('成功', $data); } //客户印版库右侧列表 public function PrintDetail() { if ($this->request->isGet() === false) { $this->error('请求错误'); } $param = $this->request->param(); if (empty($param) || !isset($param['code']) || !isset($param['category'])) { $this->error('请求错误'); } //获取印版库分类编号 $categoryNumber = db('物料_存货'); $where = [ 'a.YB_Cpdh' => $param['code'], 'b.YB_Cpdh' => $param['category'], ]; // $list = db('_') } }