文章目录
显示
数据库设计
菜品分类数据表设计
菜品数据表
菜品库存变更历史数据表
菜品分类功能的开发
数据库创建
show databases;
use food_db
CREATE TABLE `food` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`cat_id` int(11) NOT NULL DEFAULT '0' COMMENT '分类id',
`name` varchar(100) NOT NULL DEFAULT '' COMMENT '书籍名称',
`price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '售卖金额',
`main_image` varchar(100) NOT NULL DEFAULT '' COMMENT '主图',
`summary` varchar(10000) NOT NULL DEFAULT '' COMMENT '描述',
`stock` int(11) NOT NULL DEFAULT '0' COMMENT '库存量',
`tags` varchar(200) NOT NULL DEFAULT '' COMMENT 'tag关键字,以","连接',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1:有效 0:无效',
`month_count` int(11) NOT NULL DEFAULT '0' COMMENT '月销售数量',
`total_count` int(11) NOT NULL DEFAULT '0' COMMENT '总销售量',
`view_count` int(11) NOT NULL DEFAULT '0' COMMENT '总浏览次数',
`comment_count` int(11) NOT NULL DEFAULT '0' COMMENT '总评论量',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后更新时间',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后插入时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='食品表';
CREATE TABLE `food_cat` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '类别名称',
`weight` tinyint(4) NOT NULL DEFAULT '1' COMMENT '权重',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态 1:有效 0:无效',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '最后一次更新时间',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_name` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='食品分类';
CREATE TABLE `food_sale_change_log` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`food_id` int(11) NOT NULL DEFAULT '0' COMMENT '商品id',
`quantity` int(11) NOT NULL DEFAULT '0' COMMENT '售卖数量',
`price` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '售卖金额',
`member_id` int(11) NOT NULL DEFAULT '0' COMMENT '会员id',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '售卖时间',
PRIMARY KEY (`id`),
KEY `idx_food_id_id` (`food_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品销售情况';
CREATE TABLE `food_stock_change_log` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`food_id` int(11) NOT NULL COMMENT '商品id',
`unit` int(11) NOT NULL DEFAULT '0' COMMENT '变更多少',
`total_stock` int(11) NOT NULL DEFAULT '0' COMMENT '变更之后总量',
`note` varchar(100) NOT NULL DEFAULT '' COMMENT '备注字段',
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '插入时间',
PRIMARY KEY (`id`),
KEY `idx_food_id` (`food_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='数据库存变更表';
model创建
flask-sqlacodegen "mysql://root:123456@127.0.0.1/food_db" --tables food --outfile "order/common/models/food/Food.py" --flask
flask-sqlacodegen "mysql://root:123456@127.0.0.1/food_db" --tables food_cat --outfile "order/common/models/food/FoodCat.py" --flask
flask-sqlacodegen "mysql://root:123456@127.0.0.1/food_db" --tables food_sale_change_log --outfile "order/common/models/food/FoodSaleChangeLog.py" --flask
flask-sqlacodegen "mysql://root:123456@127.0.0.1/food_db" --tables food_stock_change_log --outfile "order/common/models/food/FoodStockChangeLog.py" --flask
修改自动生成的model中的db变量
# coding: utf-8
from application import db
分类添加和编辑功能的实现
Food.py添加/cat-set接口
from flask import Blueprint,request
from common.models.food.FoodCat import FoodCat
@route_food.route( "/cat-set" , methods = ["GET", "POST"] )
def catSet():
if request.method == "GET":
resp_data={}
req = request.args
id = int(req.get("id", 0))
info = None
if id:
info = FoodCat.query.filter_by(id = id).first()
resp_data['info'] = info
resp_data['current'] = cat
return ops_render( "food/cat_set.html" , resp_data)
cat_set.html中动态响应数据
<div class="form-group">
<label class="col-lg-2 control-label">分类名称:</label>
<div class="col-lg-10">
<input type="text" name="name" class="form-control" placeholder="请输入分类名称~~" value="{{info.name}}">
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label class="col-lg-2 control-label">权重:</label>
<div class="col-lg-10">
<input type="text" name="weight" class="form-control" placeholder="请输入分类名称~~" value="{{info.weight}}">
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<div class="col-lg-4 col-lg-offset-2">
<input type="hidden" name="id" value="{{info.id}}">
<button class="btn btn-w-m btn-outline btn-primary save">保存</button>
</div>
</div>
cat_set.js
发起分类的添加请求(参考account/set.js
)
;
var food_cat_set_ops = {
init:function () {
this.eventBind();
},
eventBind:function () {
$(".wrap_cat_set .save").click(function () {
var btn_target = $(this);
if (btn_target.hasClass("disabled")){
common_ops.alert("正在处理,请不要重复提交")
return;
}
var name = $(".wrap_cat_set input[name=name]").val()
var weight = $(".wrap_cat_set input[name=weight]").val()
if (!name || name.length<1){
common_ops.alert("请输入正确的姓名")
return false
}
if (parseInt(weight)<1){
common_ops.alert("请输入符合规范的权重")
return false
}
btn_target.addClass("disabled")
var data = {
name:name,
weight:weight,
id:$(".wrap_cat_set input[name=id]").val()
}
$.ajax({
url:common_ops.buildUrl("/food/cat-set"),
type:"POST",
data:data,
dataType:'json',
success:function (res) {
btn_target.removeClass("disabled")
var callback = null;
if(res.code == 200){
callback = function () {
window.location.href = common_ops.buildUrl("/food/cat");
}
}
common_ops.alert(res.msg, callback)
}
})
})
}
}
$(document).ready(function () {
food_cat_set_ops.init()
})
cat_set.html引入js
{%block js %}
<script src="{{ buildStaticUrl('/js/food/cat_set.js') }}"></script>
{% endblock %}
cat-set接口POST处理(参考account/set接口)
from flask import Blueprint,request,jsonify
from common.models.food.FoodCat import FoodCat
from common.libs.Helper import ops_render,getCurrentDate
from application import app,db
@route_food.route( "/cat-set" , methods = ["GET", "POST"] )
def catSet():
if request.method == "GET":
resp_data={}
req = request.args
id = int(req.get("id", 0))
info = None
if id:
info = FoodCat.query.filter_by(id = id).first()
resp_data['info'] = info
resp_data['current'] = cat
return ops_render( "food/cat_set.html" , resp_data)
resp = {'code': 200, 'msg': '账户添加成功', 'data': {}}
req = request.values
name = req['name'] if 'name' in req else ''
weight = int(req['weight']) if ('weight' in req and int(req['weight'])>0) else 1
id = req['id'] if 'id' in req else 0
if name is None or len(name) < 1:
resp['code'] = -1
resp['msg'] = "请输入正确的用户名"
return jsonify(resp)
food_cat_info = FoodCat.query.filter_by(id = id).first()
if food_cat_info:
model_food_cat = food_cat_info
else:
model_food_cat = FoodCat()
model_food_cat.created_time = getCurrentDate()
model_food_cat.name = name
model_food_cat.weight = weight
model_food_cat.updated_time = getCurrentDate()
db.session.add(model_food_cat)
db.session.commit()
return jsonify(resp)
修改也是可以成功修改的