import { Request, Response } from 'express' import prisma from '../utils/prisma' import { successResponse, errorResponse } from '../utils/response' const baseSelect = { id: true, name: true, code: true, status: true, description: true, created_at: true, updated_at: true } export const getStorageLocationOptions = async (_req: Request, res: Response) => { try { const locations = await prisma.storageLocation.findMany({ where: { status: 'active' }, orderBy: { name: 'asc' }, select: baseSelect }) successResponse(res, locations, '获取成功') } catch (error: any) { errorResponse(res, error.message || '获取地址列表失败') } } export const getStorageLocationList = async (req: Request, res: Response) => { try { const { page = 1, pageSize = 10, keyword, status } = req.query const where: any = {} if (status) { where.status = status } if (keyword) { where.OR = [ { name: { contains: keyword as string } }, { code: { contains: keyword as string } }, { description: { contains: keyword as string } } ] } const skip = (Number(page) - 1) * Number(pageSize) const [list, total] = await Promise.all([ prisma.storageLocation.findMany({ where, skip, take: Number(pageSize), orderBy: { updated_at: 'desc' }, select: { ...baseSelect, _count: { select: { equipments: true } } } }), prisma.storageLocation.count({ where }) ]) successResponse( res, { list, total, page: Number(page), pageSize: Number(pageSize) }, '获取成功' ) } catch (error: any) { errorResponse(res, error.message || '获取地址列表失败') } } export const createStorageLocation = async (req: Request, res: Response) => { try { const { name, code, description } = req.body as { name?: string code?: string description?: string } if (!name || !name.trim()) { return errorResponse(res, '地址名称不能为空', 422) } const location = await prisma.storageLocation.create({ data: { name: name.trim(), code: code?.trim() || null, description: description?.trim() || null }, select: baseSelect }) successResponse(res, location, '新增地址成功') } catch (error: any) { if (error.code === 'P2002') { return errorResponse(res, '地址编码已存在') } errorResponse(res, error.message || '新增地址失败') } } export const updateStorageLocation = async (req: Request, res: Response) => { try { const { id } = req.params const { name, code, description, status } = req.body as { name?: string code?: string description?: string status?: string } const existing = await prisma.storageLocation.findUnique({ where: { id: Number(id) } }) if (!existing) { return errorResponse(res, '地址不存在', 404) } const updated = await prisma.storageLocation.update({ where: { id: Number(id) }, data: { name: name?.trim() || existing.name, code: code?.trim() || null, description: description?.trim() || null, status: status || existing.status }, select: baseSelect }) successResponse(res, updated, '更新地址成功') } catch (error: any) { if (error.code === 'P2002') { return errorResponse(res, '地址编码已存在') } errorResponse(res, error.message || '更新地址失败') } } export const updateStorageLocationStatus = async (req: Request, res: Response) => { try { const { id } = req.params const { status } = req.body as { status?: string } if (!status) { return errorResponse(res, '状态不能为空', 422) } const location = await prisma.storageLocation.update({ where: { id: Number(id) }, data: { status }, select: baseSelect }) successResponse(res, location, '更新状态成功') } catch (error: any) { errorResponse(res, error.message || '更新状态失败') } }