修正git管理关系

This commit is contained in:
dxc
2026-01-26 13:47:53 +08:00
commit ee9f4aed3e
49 changed files with 2761 additions and 0 deletions

17
.gitignore vendored Normal file
View File

@ -0,0 +1,17 @@
# --- Python 后端忽略 ---
inventory-backend/venv/
inventory-backend/pgdata/
inventory-backend/__pycache__/
inventory-backend/*.pyc
inventory-backend/.env
# --- Vue 前端忽略 ---
inventory-web/node_modules/
inventory-web/dist/
inventory-web/*.local
# --- 通用忽略 ---
.idea/
.vscode/
.DS_Store
*.log

8
inventory-backend/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
venv/
pgdata/
__pycache__/
*.pyc
.env
.idea/
.vscode/
.DS_Store

View File

@ -0,0 +1,26 @@
from flask import Flask
from config import Config
from app.extensions import db, ma
def create_app():
app = Flask(__name__)
app.config.from_object(Config)
# 初始化插件
db.init_app(app)
ma.init_app(app)
# 【新增关键步骤】: 显式导入 models让 SQLAlchemy 认识所有的表
# 必须放在 db.init_app 之后create_all 或 蓝图注册 之前
from app import models
# 注册路由蓝图
from app.api.v1.stocks import stock_bp
app.register_blueprint(stock_bp, url_prefix='/api/v1')
# 【可选】如果你没有用 Flask-Migrate可以用下面这句话自动建表开发阶段
# with app.app_context():
# db.create_all()
return app

View File

View File

View File

View File

View File

View File

@ -0,0 +1,34 @@
from flask import Blueprint, request, jsonify
from app.services.stock_service import create_inbound_stock
from app.schemas.stock_schema import StockBuySchema
stock_bp = Blueprint('stocks', __name__)
@stock_bp.route('/buy-inbound', methods=['POST'])
def buy_inbound():
"""
采购入库接口
POST /api/v1/buy-inbound
Body: { "material_id": 1, "qty_inbound": 100, "price_unit": 10.5 ... }
"""
# 1. 接收 JSON 数据
json_data = request.get_json()
if not json_data:
return jsonify({"message": "No input data provided"}), 400
# 2. 数据校验
schema = StockBuySchema()
try:
# 这一步只做校验,不直接生成对象,因为我们要在 Service 里手动处理逻辑
data = schema.load(json_data, partial=True)
except Exception as e:
return jsonify({"message": "Validation error", "errors": e.messages}), 422
# 3. 调用业务逻辑
try:
new_stock = create_inbound_stock(data)
# 4. 返回成功结果
result = schema.dump(new_stock)
return jsonify({"message": "Inbound successful", "data": result}), 201
except Exception as e:
return jsonify({"message": "Internal Server Error", "error": str(e)}), 500

View File

@ -0,0 +1,6 @@
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
# 初始化数据库和序列化工具
db = SQLAlchemy()
ma = Marshmallow()

View File

@ -0,0 +1,2 @@
from app.models.material import MaterialBase
from app.models.stock import StockBuy

View File

View File

@ -0,0 +1,11 @@
from app.extensions import db
class MaterialBase(db.Model):
__tablename__ = 'material_base'
id = db.Column(db.Integer, primary_key=True)
sku_code = db.Column(db.String(100), unique=True, nullable=False)
name = db.Column(db.String(255), nullable=False)
spec_model = db.Column(db.String(255))
unit = db.Column(db.String(50))
# 其他字段按需添加,入库时主要是为了外键关联

View File

@ -0,0 +1,25 @@
from app.extensions import db
from datetime import datetime
class StockBuy(db.Model):
__tablename__ = 'stock_buy'
id = db.Column(db.Integer, primary_key=True)
material_id = db.Column(db.Integer, db.ForeignKey('material_base.id'), nullable=False)
inbound_date = db.Column(db.DateTime, default=datetime.now)
barcode = db.Column(db.String(100))
batch_no = db.Column(db.String(100))
# 数量相关 (使用 Numeric 对应数据库的 NUMERIC)
qty_inbound = db.Column(db.Numeric(19, 4), default=0)
qty_current = db.Column(db.Numeric(19, 4), default=0)
qty_available = db.Column(db.Numeric(19, 4), default=0)
price_unit = db.Column(db.Numeric(19, 4), default=0)
price_total = db.Column(db.Numeric(19, 4), default=0)
supplier_name = db.Column(db.String(255))
warehouse_loc = db.Column(db.String(100))
# 建立关联,方便查询物料详情
material = db.relationship('MaterialBase', backref='buy_stocks')

View File

View File

@ -0,0 +1,14 @@
from app.extensions import ma
from app.models.stock import StockBuy
from marshmallow import fields
class StockBuySchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = StockBuy
load_instance = True # 反序列化时自动创建模型实例
include_fk = True # 包含外键 material_id
# 必须字段校验
material_id = fields.Integer(required=True)
qty_inbound = fields.Decimal(required=True, as_string=True)
price_unit = fields.Decimal(missing=0, as_string=True)

View File

@ -0,0 +1,39 @@
from app.extensions import db
from app.models.stock import StockBuy
from sqlalchemy.exc import SQLAlchemyError
def create_inbound_stock(data):
"""
处理采购入库逻辑
"""
try:
# 1. 计算总价
qty = data.get('qty_inbound')
price = data.get('price_unit', 0)
total = float(qty) * float(price)
# 2. 创建库存记录
# 注意:入库时,当前库存(current)和可用库存(available)通常等于入库数量
new_stock = StockBuy(
material_id=data['material_id'],
barcode=data.get('barcode'),
batch_no=data.get('batch_no'),
qty_inbound=qty,
qty_current=qty, # 初始:当前=入库
qty_available=qty, # 初始:可用=入库
price_unit=price,
price_total=total,
supplier_name=data.get('supplier_name'),
warehouse_loc=data.get('warehouse_loc'),
inbound_date=data.get('inbound_date') # 如果前端没传Model会默认用当前时间
)
db.session.add(new_stock)
db.session.commit()
return new_stock
except SQLAlchemyError as e:
db.session.rollback()
raise e

View File

View File

View File

@ -0,0 +1,14 @@
import os
class Config:
# 数据库连接配置
# 请务必将 '你的密码' 替换为你 PostgreSQL 的真实密码
# 如果数据库不在本地,请将 localhost 替换为 IP 地址
SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:1234@localhost:5432/inventory_system'
# 关闭 SQLAlchemy 的事件追踪,减少内存消耗
SQLALCHEMY_TRACK_MODIFICATIONS = False
# Flask 的密钥,用于 Session 加密等,开发环境随便写一个即可
SECRET_KEY = 'dev-secret-key-1234'

View File

@ -0,0 +1,17 @@
version: '3.8'
services:
db:
image: postgres:15-alpine # 使用轻量级的 Alpine 版本
container_name: inventory_db
restart: always
environment:
POSTGRES_USER: test # 自定义用户名
POSTGRES_PASSWORD: 1234 # 自定义密码 (开发环境简单点没事)
POSTGRES_DB: inventory_system # 默认创建的数据库名
ports:
- "5432:5432" # 将容器的5432端口映射到 WSL 的5432端口
volumes:
- ./pgdata:/var/lib/postgresql/data # 【重要】数据持久化!防止重启容器数据丢失
# 这里以后可以加你的 pgadmin 或者 redis 等其他服务

View File

@ -0,0 +1,8 @@
Flask==3.0.0
Flask-SQLAlchemy==3.1.1
Flask-Migrate==4.0.5
Flask-Marshmallow==1.1.0
marshmallow-sqlalchemy==1.0.0
psycopg2-binary==2.9.9
python-dotenv==1.0.0
flask-cors==4.0.0

6
inventory-backend/run.py Normal file
View File

@ -0,0 +1,6 @@
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)

24
inventory-web/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

5
inventory-web/README.md Normal file
View File

@ -0,0 +1,5 @@
# Vue 3 + TypeScript + Vite
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
Learn more about the recommended Project Setup and IDE Support in the [Vue Docs TypeScript Guide](https://vuejs.org/guide/typescript/overview.html#project-setup).

13
inventory-web/index.html Normal file
View File

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>inventory-web</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

2289
inventory-web/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,31 @@
{
"name": "inventory-web",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc -b && vite build",
"preview": "vite preview"
},
"dependencies": {
"@element-plus/icons-vue": "^2.3.2",
"axios": "^1.13.3",
"element-plus": "^2.13.1",
"pinia": "^3.0.4",
"sass": "^1.97.3",
"vue": "^3.5.24",
"vue-router": "^4.6.4"
},
"devDependencies": {
"@types/node": "^24.10.1",
"@vitejs/plugin-vue": "^6.0.1",
"@vue/tsconfig": "^0.8.1",
"typescript": "~5.9.3",
"vite": "npm:rolldown-vite@7.2.5",
"vue-tsc": "^3.1.4"
},
"overrides": {
"vite": "npm:rolldown-vite@7.2.5"
}
}

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

30
inventory-web/src/App.vue Normal file
View File

@ -0,0 +1,30 @@
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
</script>
<template>
<div>
<a href="https://vite.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
</div>
<HelloWorld msg="Vite + Vue" />
</template>
<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
</style>

View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>

After

Width:  |  Height:  |  Size: 496 B

View File

@ -0,0 +1,5 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
createApp(App).mount('#app')

View File

@ -0,0 +1,79 @@
:root {
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
.card {
padding: 2em;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

View File

@ -0,0 +1,16 @@
{
"extends": "@vue/tsconfig/tsconfig.dom.json",
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"types": ["vite/client"],
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
}

View File

@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" }
]
}

View File

@ -0,0 +1,26 @@
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"target": "ES2023",
"lib": ["ES2023"],
"module": "ESNext",
"types": ["node"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
},
"include": ["vite.config.ts"]
}

View File

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vite.dev/config/
export default defineConfig({
plugins: [vue()],
})