采购人根据历史上传记录来
This commit is contained in:
@ -5,29 +5,19 @@ import traceback
|
||||
|
||||
inbound_buy_bp = Blueprint('stock_buy', __name__)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 0. 基础物料搜索
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/search-base', methods=['GET'])
|
||||
def search_base():
|
||||
"""
|
||||
供前端下拉框远程搜索使用
|
||||
Query Param: keyword (名称或规格)
|
||||
"""
|
||||
try:
|
||||
keyword = request.args.get('keyword', '')
|
||||
data = BuyInboundService.search_base_material(keyword)
|
||||
return jsonify({
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": data
|
||||
})
|
||||
return jsonify({"code": 200, "msg": "success", "data": data})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 1. 获取列表
|
||||
# ------------------------------------------------------------------
|
||||
@ -37,8 +27,6 @@ def get_list():
|
||||
page = request.args.get('page', 1, type=int)
|
||||
limit = request.args.get('pageSize', 15, type=int)
|
||||
keyword = request.args.get('keyword', '')
|
||||
|
||||
# 获取状态列表参数
|
||||
statuses_str = request.args.get('statuses', '')
|
||||
statuses = statuses_str.split(',') if statuses_str else []
|
||||
|
||||
@ -48,7 +36,6 @@ def get_list():
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 2. 新增入库
|
||||
# ------------------------------------------------------------------
|
||||
@ -58,19 +45,12 @@ def submit():
|
||||
data = request.get_json()
|
||||
if not data:
|
||||
return jsonify({"code": 400, "msg": "No data"}), 400
|
||||
|
||||
new_stock = BuyInboundService.handle_inbound(data)
|
||||
|
||||
return jsonify({
|
||||
"code": 200,
|
||||
"msg": "入库成功",
|
||||
"data": new_stock.to_dict()
|
||||
})
|
||||
return jsonify({"code": 200, "msg": "入库成功", "data": new_stock.to_dict()})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 3. 更新入库
|
||||
# ------------------------------------------------------------------
|
||||
@ -83,7 +63,6 @@ def update_buy(id):
|
||||
except Exception as e:
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 4. 删除
|
||||
# ------------------------------------------------------------------
|
||||
@ -95,26 +74,8 @@ def delete_buy(id):
|
||||
except Exception as e:
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 5. 获取关联的出库历史
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/<int:id>/history', methods=['GET'])
|
||||
def get_history(id):
|
||||
try:
|
||||
history = BuyInboundService.get_outbound_history(id)
|
||||
return jsonify({
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": history
|
||||
})
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 6. 供应商建议
|
||||
# 5. 供应商建议 (基于 base_id)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/suggestions/suppliers', methods=['GET'])
|
||||
def get_supplier_suggestions():
|
||||
@ -124,18 +85,17 @@ def get_supplier_suggestions():
|
||||
data = BuyInboundService.get_history_suppliers(base_id)
|
||||
return jsonify({"code": 200, "msg": "success", "data": data})
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 7. 系统用户建议 (采购人)
|
||||
# 6. 采购人建议 (全局,基于 stock_buy 表)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/suggestions/users', methods=['GET'])
|
||||
def get_user_suggestions():
|
||||
keyword = request.args.get('keyword', '')
|
||||
data = BuyInboundService.search_system_users(keyword)
|
||||
data = BuyInboundService.get_history_purchasers(keyword)
|
||||
return jsonify({"code": 200, "msg": "success", "data": data})
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# 8. [新增] 链接建议
|
||||
# 7. 链接建议 (基于 base_id)
|
||||
# ------------------------------------------------------------------
|
||||
@inbound_buy_bp.route('/suggestions/links', methods=['GET'])
|
||||
def get_link_suggestions():
|
||||
|
||||
@ -1,13 +1,6 @@
|
||||
from app.extensions import db
|
||||
from app.models.inbound.buy import StockBuy
|
||||
from app.models.base import MaterialBase
|
||||
|
||||
# 尝试导入出库模型,如果不存在则忽略
|
||||
try:
|
||||
from app.models.outbound import TransOutbound
|
||||
except ImportError:
|
||||
TransOutbound = None
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from sqlalchemy import or_, func, text, and_
|
||||
import traceback
|
||||
@ -53,15 +46,12 @@ class BuyInboundService:
|
||||
@staticmethod
|
||||
def search_base_material(keyword):
|
||||
try:
|
||||
# 只查询已启用的物料
|
||||
query = MaterialBase.query.filter(MaterialBase.is_enabled == True)
|
||||
|
||||
if keyword:
|
||||
query = query.filter(
|
||||
or_(
|
||||
MaterialBase.name.ilike(f'%{keyword}%'),
|
||||
MaterialBase.spec_model.ilike(f'%{keyword}%'),
|
||||
MaterialBase.pinyin.ilike(f'%{keyword}%')
|
||||
MaterialBase.spec_model.ilike(f'%{keyword}%')
|
||||
)
|
||||
)
|
||||
query = query.order_by(MaterialBase.id.desc()).limit(20)
|
||||
@ -342,50 +332,61 @@ class BuyInboundService:
|
||||
return {"total": 0, "items": []}
|
||||
|
||||
# ============================================================
|
||||
# 6. 供应商历史查询 (根据 base_id)
|
||||
# 6. 供应商历史查询 (基于 base_id)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_history_suppliers(base_id):
|
||||
"""返回该物料关联的供应商列表(去重)"""
|
||||
"""返回该物料在 stock_buy 表中关联过的供应商列表"""
|
||||
try:
|
||||
# 去重查询
|
||||
query = db.session.query(StockBuy.supplier_name).filter(
|
||||
StockBuy.base_id == base_id,
|
||||
StockBuy.supplier_name.isnot(None),
|
||||
StockBuy.supplier_name != ''
|
||||
).distinct().order_by(StockBuy.supplier_name)
|
||||
|
||||
suppliers = [row[0] for row in query.all()]
|
||||
return suppliers
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 7. 系统用户搜索 (全局)
|
||||
# 7. 采购人/邮箱历史查询 (全局,从 stock_buy 获取)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def search_system_users(keyword):
|
||||
"""搜索系统用户(活跃状态)"""
|
||||
from app.models.system import SysUser
|
||||
def get_history_purchasers(keyword):
|
||||
"""
|
||||
从 stock_buy 表中提取历史采购人和邮箱。
|
||||
不绑定 base_id,因为采购人通常是全局的。
|
||||
"""
|
||||
try:
|
||||
query = SysUser.query.filter(SysUser.status == 'active')
|
||||
# 查询 buyer_name 和 buyer_email,并去重
|
||||
query = db.session.query(StockBuy.buyer_name, StockBuy.buyer_email) \
|
||||
.filter(StockBuy.buyer_name.isnot(None), StockBuy.buyer_name != '')
|
||||
|
||||
if keyword:
|
||||
kw = f'%{keyword}%'
|
||||
query = query.filter(db.or_(
|
||||
SysUser.username.ilike(kw),
|
||||
SysUser.email.ilike(kw)
|
||||
query = query.filter(or_(
|
||||
StockBuy.buyer_name.ilike(kw),
|
||||
StockBuy.buyer_email.ilike(kw)
|
||||
))
|
||||
query = query.order_by(SysUser.username)
|
||||
|
||||
# 按名字去重,取最新的记录(这里简单做 distinct,具体业务如果一个人有多个邮箱可能需要更复杂逻辑,这里简化为 distinct 组合)
|
||||
results = query.distinct().limit(20).all()
|
||||
|
||||
users = []
|
||||
for u in query.limit(20).all():
|
||||
for row in results:
|
||||
users.append({
|
||||
'value': u.username,
|
||||
'email': u.email
|
||||
'value': row.buyer_name, # 前端 autocomplete 显示的值
|
||||
'email': row.buyer_email or ''
|
||||
})
|
||||
return users
|
||||
except Exception:
|
||||
traceback.print_exc()
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 8. [新增] 链接建议 (根据 base_id)
|
||||
# 8. 链接建议 (基于 base_id)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_history_links(base_id, link_type='original'):
|
||||
|
||||
Reference in New Issue
Block a user