随着生活水平的提高,人们对于饮食的需求也越来越高。越来越多的人开始尝试自己动手做饭,然而也有不少人因为工作繁忙或者懒惰心态无法实现。因此,上门做菜服务应运而生。
而现在的上门做菜服务,一般都是通过网络平台进行预约和下单。顾客通过平台选择需要的菜品和数量,支付相应的费用后,便可以等待上门服务。而在这些服务中,用户消费记录功能显得尤为重要。对于服务提供商来讲,消费记录可以帮助他们更好的管理账务,从而提高运营效率;对于用户来讲,消费记录可以查看自己近期的消费情况,以便于更好的预估自己的消费能力。
那么,如何实现上门做菜系统的用户消费记录功能呢?下面我们来一起看看。
一、设计数据表
在思考消费记录功能的实现之前,我们需要先设计相应的数据表。在这个案例中,我们需要设计菜品表、订单表、订单详情表以及消费记录表。
菜品表设计如下:create table if not exists `dishes` ( `id` int(10) unsigned not null auto_increment comment '菜品 id', `name` varchar(50) not null comment '菜名', `image` varchar(100) not null comment '图片地址', `category_id` int(10) unsigned not null comment '分类 id', `price` float(10,2) unsigned not null comment '价格', `created_at` timestamp not null default current_timestamp comment '创建时间', `updated_at` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (`id`)) engine=innodb charset=utf8mb4 collate=utf8mb4_general_ci comment='菜品表';
订单表设计如下:create table if not exists `orders` ( `id` int(10) unsigned not null auto_increment comment '订单 id', `user_id` int(10) unsigned not null comment '用户 id', `total_price` float(10,2) unsigned not null comment '订单总价', `status` tinyint(1) unsigned not null default '0' comment '订单状态,0:未支付,1:已支付,2:已完成,3:已取消', `created_at` timestamp not null default current_timestamp comment '创建时间', `updated_at` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (`id`)) engine=innodb charset=utf8mb4 collate=utf8mb4_general_ci comment='订单表';
订单详情表设计如下:create table if not exists `order_items` ( `id` int(10) unsigned not null auto_increment comment '订单详情 id', `order_id` int(10) unsigned not null comment '订单 id', `dish_id` int(10) unsigned not null comment '菜品 id', `quantity` smallint(5) unsigned not null comment '数量', `price` float(10,2) unsigned not null comment '单价', `created_at` timestamp not null default current_timestamp comment '创建时间', `updated_at` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (`id`)) engine=innodb charset=utf8mb4 collate=utf8mb4_general_ci comment='订单详情表';
消费记录表设计如下:create table if not exists `consumption_records` ( `id` int(10) unsigned not null auto_increment comment '消费记录 id', `user_id` int(10) unsigned not null comment '用户 id', `order_id` int(10) unsigned not null comment '订单 id', `money` float(10,2) unsigned not null comment '消费金额', `created_at` timestamp not null default current_timestamp comment '创建时间', `updated_at` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (`id`)) engine=innodb charset=utf8mb4 collate=utf8mb4_general_ci comment='消费记录表';
二、实现代码
在完成数据表的设计后,我们需要使用 go 语言来实现相应的业务逻辑。下面是相应代码:
定义结构体:type consumptionrecord struct { id uint32 `db:"id" json:"id"` userid uint32 `db:"user_id" json:"user_id"` orderid uint32 `db:"order_id" json:"order_id"` money float32 `db:"money" json:"money"` createdat time.time `db:"created_at" json:"created_at"` updatedat time.time `db:"updated_at" json:"updated_at"`}type orderdetail struct { id uint32 `db:"id" json:"id"` orderid uint32 `db:"order_id" json:"order_id"` dishid uint32 `db:"dish_id" json:"dish_id"` quantity uint16 `db:"quantity" json:"quantity"` price float32 `db:"price" json:"price"` createdat time.time `db:"created_at" json:"created_at"` updatedat time.time `db:"updated_at" json:"updated_at"` dish *dish `db:"-" json:"dish"`}type order struct { id uint32 `db:"id" json:"id"` userid uint32 `db:"user_id" json:"user_id"` totalprice float32 `db:"total_price" json:"total_price"` status orderstatus `db:"status" json:"status"` createdat time.time `db:"created_at" json:"created_at"` updatedat time.time `db:"updated_at" json:"updated_at"` items []*orderdetail `db:"-" json:"items"`}
查询订单详情:// getorderdetailsbyorderids 根据订单 id 列表查询订单详情func getorderdetailsbyorderids(db *sql.db, orderids []uint32) ([]*orderdetail, error) { details := make([]*orderdetail, 0) if len(orderids) == 0 { return details, nil } // 拼接查询 sql var placeholders strings.builder var args []interface{} for i, id := range orderids { if i != 0 { placeholders.writestring(", ") } placeholders.writestring("?") args = append(args, id) } query := fmt.sprintf(` select id, order_id, dish_id, quantity, price, created_at, updated_at from order_items where order_id in (%s) `, placeholders.string()) rows, err := db.query(query, args...) if err != nil { return nil, err } defer rows.close() // 遍历查询结果,并填充菜品信息到订单详情结构体 for rows.next() { detail := &orderdetail{} err := rows.scan( &detail.id, &detail.orderid, &detail.dishid, &detail.quantity, &detail.price, &detail.createdat, &detail.updatedat) if err != nil { return nil, err } dish, err := getdishbyid(db, detail.dishid) if err != nil { return nil, err } detail.dish = dish details = append(details, detail) } return details, nil}
添加消费记录:// addconsumptionrecord 添加消费记录func addconsumptionrecord( db *sql.db, userid uint32, orderid uint32, money float32) error { insertquery := ` insert into consumption_records (user_id, order_id, money) values (?, ?, ?) ` _, err := db.exec(insertquery, userid, orderid, money) if err != nil { return err } return nil}
三、总结
上面是一个简单的上门做菜系统中,如何使用 go 语言实现用户消费记录功能的案例。通过该案例,我们可以学到拼接查询 sql 的方法、批量查询的方法、遍历查询结果的方法以及插入数据的方法。
总体来看,go 语言具有简单、高效、安全等优点,并得到了广大开发人员的喜爱。相信通过阅读这个案例,你也能对 go 语言有更深的了解,同时也希望能够对你在实现用户消费记录功能时有所帮助。
以上就是上门做菜系统的go语言开发:如何实现用户消费记录功能?的详细内容。
