删掉所有文件
This commit is contained in:
@@ -1,128 +0,0 @@
|
||||
// Package db 提供基于PostgreSQL的数据存储功能
|
||||
// 使用GORM作为ORM库来操作数据库
|
||||
// 实现与PostgreSQL数据库的连接和基本操作
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// migrateModels 需要自动迁移的数据库模型列表
|
||||
var migrateModels = []interface{}{
|
||||
&model.User{},
|
||||
&model.OperationHistory{},
|
||||
&model.Device{},
|
||||
&model.DeviceControl{},
|
||||
&model.FeedingPlan{},
|
||||
&model.FeedingPlanStep{},
|
||||
&model.FeedingExecution{},
|
||||
&model.FeedingExecutionStep{},
|
||||
}
|
||||
|
||||
// PostgresStorage 代表基于PostgreSQL的存储实现
|
||||
// 使用GORM作为ORM库
|
||||
type PostgresStorage struct {
|
||||
// db GORM数据库实例
|
||||
db *gorm.DB
|
||||
|
||||
// connectionString 数据库连接字符串
|
||||
connectionString string
|
||||
|
||||
// maxOpenConns 最大开放连接数
|
||||
maxOpenConns int
|
||||
|
||||
// maxIdleConns 最大空闲连接数
|
||||
maxIdleConns int
|
||||
|
||||
// connMaxLifetime 连接最大生命周期(秒)
|
||||
connMaxLifetime int
|
||||
|
||||
// logger 日志记录器
|
||||
logger *logs.Logger
|
||||
}
|
||||
|
||||
// NewPostgresStorage 创建并返回一个新的PostgreSQL存储实例
|
||||
// 初始化数据库连接和相关配置
|
||||
func NewPostgresStorage(connectionString string, maxOpenConns, maxIdleConns, connMaxLifetime int) *PostgresStorage {
|
||||
return &PostgresStorage{
|
||||
connectionString: connectionString,
|
||||
maxOpenConns: maxOpenConns,
|
||||
maxIdleConns: maxIdleConns,
|
||||
connMaxLifetime: connMaxLifetime,
|
||||
logger: logs.NewLogger(),
|
||||
}
|
||||
}
|
||||
|
||||
// Connect 建立与PostgreSQL数据库的连接
|
||||
// 使用GORM建立数据库连接
|
||||
func (ps *PostgresStorage) Connect() error {
|
||||
ps.logger.Info("正在连接PostgreSQL数据库")
|
||||
|
||||
var err error
|
||||
ps.db, err = gorm.Open(postgres.Open(ps.connectionString), &gorm.Config{})
|
||||
if err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("数据库连接失败: %v", err))
|
||||
return fmt.Errorf("数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
// 测试连接
|
||||
sqlDB, err := ps.db.DB()
|
||||
if err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("获取数据库实例失败: %v", err))
|
||||
return fmt.Errorf("获取数据库实例失败: %v", err)
|
||||
}
|
||||
|
||||
if err = sqlDB.Ping(); err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("数据库连接测试失败: %v", err))
|
||||
return fmt.Errorf("数据库连接测试失败: %v", err)
|
||||
}
|
||||
|
||||
// 设置连接池参数
|
||||
sqlDB.SetMaxOpenConns(ps.maxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(ps.maxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(ps.connMaxLifetime) * time.Second)
|
||||
|
||||
// 自动迁移数据库表结构
|
||||
ps.logger.Info("正在自动迁移数据库表结构")
|
||||
if err = ps.db.AutoMigrate(migrateModels...); err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("数据库表结构迁移失败: %v", err))
|
||||
return fmt.Errorf("数据库表结构迁移失败: %v", err)
|
||||
}
|
||||
ps.logger.Info("数据库表结构迁移完成")
|
||||
|
||||
ps.logger.Info("PostgreSQL数据库连接成功")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disconnect 断开与PostgreSQL数据库的连接
|
||||
// 安全地关闭所有数据库连接
|
||||
func (ps *PostgresStorage) Disconnect() error {
|
||||
if ps.db != nil {
|
||||
ps.logger.Info("正在断开PostgreSQL数据库连接")
|
||||
|
||||
sqlDB, err := ps.db.DB()
|
||||
if err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("获取数据库实例失败: %v", err))
|
||||
return fmt.Errorf("获取数据库实例失败: %v", err)
|
||||
}
|
||||
|
||||
if err := sqlDB.Close(); err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("关闭数据库连接失败: %v", err))
|
||||
return fmt.Errorf("关闭数据库连接失败: %v", err)
|
||||
}
|
||||
ps.logger.Info("PostgreSQL数据库连接已断开")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDB 获取GORM数据库实例
|
||||
// 用于执行具体的数据库操作
|
||||
func (ps *PostgresStorage) GetDB() *gorm.DB {
|
||||
return ps.db
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
// Package db 提供统一的数据存储接口
|
||||
// 定义存储接口规范,支持多种存储后端实现
|
||||
// 当前支持PostgreSQL实现
|
||||
package db
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Storage 代表统一的存储接口
|
||||
// 所有存储实现都需要实现此接口定义的方法
|
||||
type Storage interface {
|
||||
// Connect 建立与存储后端的连接
|
||||
Connect() error
|
||||
|
||||
// Disconnect 断开与存储后端的连接
|
||||
Disconnect() error
|
||||
|
||||
// GetDB 获取数据库实例
|
||||
GetDB() *gorm.DB
|
||||
}
|
||||
|
||||
// NewStorage 创建并返回一个存储实例
|
||||
// 根据配置返回相应的存储实现
|
||||
func NewStorage(connectionString string, maxOpenConns, maxIdleConns, connMaxLifetime int) Storage {
|
||||
// 当前默认返回PostgreSQL存储实现
|
||||
s := NewPostgresStorage(connectionString, maxOpenConns, maxIdleConns, connMaxLifetime)
|
||||
return s
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
// Package repository 提供数据访问层实现
|
||||
// 包含设备控制等数据实体的仓库接口和实现
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeviceRepo 设备仓库接口
|
||||
type DeviceRepo interface {
|
||||
// Create 创建设备
|
||||
Create(device *model.Device) error
|
||||
|
||||
// FindByID 根据ID查找设备
|
||||
FindByID(id uint) (*model.Device, error)
|
||||
|
||||
// FindByIDString 根据ID字符串查找设备
|
||||
FindByIDString(id string) (*model.Device, error)
|
||||
|
||||
// FindByParentID 根据上级设备ID查找设备
|
||||
FindByParentID(parentID uint) ([]*model.Device, error)
|
||||
|
||||
// FindByType 根据设备类型查找设备
|
||||
FindByType(deviceType model.DeviceType) ([]*model.Device, error)
|
||||
|
||||
// Update 更新设备信息
|
||||
Update(device *model.Device) error
|
||||
|
||||
// Delete 删除设备
|
||||
Delete(id uint) error
|
||||
|
||||
// ListAll 获取所有设备列表
|
||||
ListAll() ([]model.Device, error)
|
||||
|
||||
// FindRelayDevices 获取所有中继设备
|
||||
FindRelayDevices() ([]*model.Device, error)
|
||||
}
|
||||
|
||||
// DeviceControlRepo 设备控制仓库接口
|
||||
type DeviceControlRepo interface {
|
||||
// Create 创建设备控制记录
|
||||
Create(control *model.DeviceControl) error
|
||||
|
||||
// FindByUserID 根据用户ID查找设备控制记录
|
||||
FindByUserID(userID uint) ([]*model.DeviceControl, error)
|
||||
|
||||
// FindByID 根据ID查找设备控制记录
|
||||
FindByID(id uint) (*model.DeviceControl, error)
|
||||
|
||||
// List 获取设备控制记录列表(分页)
|
||||
List(offset, limit int) ([]*model.DeviceControl, error)
|
||||
}
|
||||
|
||||
// deviceRepo 设备仓库实现
|
||||
type deviceRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// deviceControlRepo 设备控制仓库实现
|
||||
type deviceControlRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewDeviceRepo 创建设备仓库实例
|
||||
func NewDeviceRepo(db *gorm.DB) DeviceRepo {
|
||||
return &deviceRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDeviceControlRepo 创建设备控制仓库实例
|
||||
func NewDeviceControlRepo(db *gorm.DB) DeviceControlRepo {
|
||||
return &deviceControlRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建设备
|
||||
func (r *deviceRepo) Create(device *model.Device) error {
|
||||
result := r.db.Create(device)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找设备
|
||||
func (r *deviceRepo) FindByID(id uint) (*model.Device, error) {
|
||||
var device model.Device
|
||||
result := r.db.First(&device, id)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
// FindByIDString 根据ID字符串查找设备
|
||||
func (r *deviceRepo) FindByIDString(id string) (*model.Device, error) {
|
||||
deviceID, err := strconv.ParseUint(id, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var device model.Device
|
||||
result := r.db.First(&device, deviceID)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
// ListAll 获取所有设备列表
|
||||
func (r *deviceRepo) ListAll() ([]model.Device, error) {
|
||||
var devices []model.Device
|
||||
if err := r.db.Find(&devices).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// FindByParentID 根据上级设备ID查找设备
|
||||
func (r *deviceRepo) FindByParentID(parentID uint) ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
result := r.db.Where("parent_id = ?", parentID).Find(&devices)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// FindByType 根据设备类型查找设备
|
||||
func (r *deviceRepo) FindByType(deviceType model.DeviceType) ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
result := r.db.Where("type = ?", deviceType).Find(&devices)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// FindRelayDevices 获取所有中继设备
|
||||
func (r *deviceRepo) FindRelayDevices() ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
result := r.db.Where("type = ?", model.DeviceTypeRelay).Find(&devices)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// Update 更新设备信息
|
||||
func (r *deviceRepo) Update(device *model.Device) error {
|
||||
result := r.db.Save(device)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Delete 删除设备
|
||||
func (r *deviceRepo) Delete(id uint) error {
|
||||
result := r.db.Delete(&model.Device{}, id)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Create 创建设备控制记录
|
||||
func (r *deviceControlRepo) Create(control *model.DeviceControl) error {
|
||||
result := r.db.Create(control)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// FindByUserID 根据用户ID查找设备控制记录
|
||||
func (r *deviceControlRepo) FindByUserID(userID uint) ([]*model.DeviceControl, error) {
|
||||
var controls []*model.DeviceControl
|
||||
result := r.db.Where("user_id = ?", userID).Order("created_at DESC").Find(&controls)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return controls, nil
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找设备控制记录
|
||||
func (r *deviceControlRepo) FindByID(id uint) (*model.DeviceControl, error) {
|
||||
var control model.DeviceControl
|
||||
result := r.db.First(&control, id)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &control, nil
|
||||
}
|
||||
|
||||
// List 获取设备控制记录列表(分页)
|
||||
func (r *deviceControlRepo) List(offset, limit int) ([]*model.DeviceControl, error) {
|
||||
var controls []*model.DeviceControl
|
||||
result := r.db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&controls)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return controls, nil
|
||||
}
|
||||
@@ -1,205 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// FeedPlanRepo 饲喂管理接口
|
||||
type FeedPlanRepo interface {
|
||||
|
||||
// ListAllPlanIntroduction 获取所有计划简介
|
||||
ListAllPlanIntroduction() ([]*model.FeedingPlan, error)
|
||||
|
||||
// FindFeedingPlanByID 根据ID获取计划详情
|
||||
FindFeedingPlanByID(id uint) (*model.FeedingPlan, error)
|
||||
|
||||
// CreateFeedingPlan 创建饲料计划
|
||||
CreateFeedingPlan(feedingPlan *model.FeedingPlan) error
|
||||
|
||||
// DeleteFeedingPlan 删除饲料计划及其所有子计划和步骤
|
||||
DeleteFeedingPlan(id uint) error
|
||||
|
||||
// UpdateFeedingPlan 更新饲料计划,采用先删除再重新创建的方式
|
||||
UpdateFeedingPlan(feedingPlan *model.FeedingPlan) error
|
||||
}
|
||||
|
||||
type feedPlanRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewFeedPlanRepo(db *gorm.DB) FeedPlanRepo {
|
||||
return &feedPlanRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// ListAllPlanIntroduction 获取所有计划简介
|
||||
func (f *feedPlanRepo) ListAllPlanIntroduction() ([]*model.FeedingPlan, error) {
|
||||
var plans []*model.FeedingPlan
|
||||
err := f.db.Model(&model.FeedingPlan{}).
|
||||
Select("id, name, description, type, enabled, schedule_cron").
|
||||
Find(&plans).Error
|
||||
return plans, err
|
||||
}
|
||||
|
||||
// FindFeedingPlanByID 根据ID获取计划详情
|
||||
func (f *feedPlanRepo) FindFeedingPlanByID(feedingPlanID uint) (*model.FeedingPlan, error) {
|
||||
var plan model.FeedingPlan
|
||||
err := f.db.Where("id = ?", feedingPlanID).
|
||||
Preload("Steps").
|
||||
Preload("SubPlans").
|
||||
First(&plan).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &plan, nil
|
||||
}
|
||||
|
||||
// CreateFeedingPlan 创建饲料计划,包括步骤和子计划
|
||||
func (f *feedPlanRepo) CreateFeedingPlan(feedingPlan *model.FeedingPlan) error {
|
||||
// 清空所有ID,确保创建新记录
|
||||
f.clearAllIDs(feedingPlan)
|
||||
|
||||
return f.db.Transaction(func(tx *gorm.DB) error {
|
||||
return f.createFeedingPlanWithTx(tx, feedingPlan)
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateFeedingPlan 更新饲料计划,采用先删除再重新创建的方式
|
||||
func (f *feedPlanRepo) UpdateFeedingPlan(feedingPlan *model.FeedingPlan) error {
|
||||
// 检查计划是否存在
|
||||
_, err := f.FindFeedingPlanByID(feedingPlan.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 先删除原有的计划
|
||||
if err := f.deleteFeedingPlanWithTx(tx, feedingPlan.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 清空所有ID,包括子计划和步骤的ID
|
||||
f.clearAllIDs(feedingPlan)
|
||||
|
||||
// 再重新创建更新后的计划
|
||||
if err := f.createFeedingPlanWithTx(tx, feedingPlan); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteFeedingPlan 删除饲料计划及其所有子计划和步骤
|
||||
func (f *feedPlanRepo) DeleteFeedingPlan(id uint) error {
|
||||
return f.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 递归删除计划及其所有子计划
|
||||
if err := f.deleteFeedingPlanWithTx(tx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// deleteFeedingPlanWithTx 在事务中递归删除饲料计划
|
||||
func (f *feedPlanRepo) deleteFeedingPlanWithTx(tx *gorm.DB, id uint) error {
|
||||
// 先查找计划及其子计划
|
||||
var plan model.FeedingPlan
|
||||
if err := tx.Where("id = ?", id).Preload("SubPlans").First(&plan).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 递归删除所有子计划
|
||||
for _, subPlan := range plan.SubPlans {
|
||||
if err := f.deleteFeedingPlanWithTx(tx, subPlan.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 删除该计划的所有步骤
|
||||
if err := tx.Where("plan_id = ?", id).Delete(&model.FeedingPlanStep{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 删除计划本身
|
||||
if err := tx.Delete(&model.FeedingPlan{}, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// createFeedingPlanWithTx 在事务中递归创建饲料计划
|
||||
func (f *feedPlanRepo) createFeedingPlanWithTx(tx *gorm.DB, feedingPlan *model.FeedingPlan) error {
|
||||
// 先创建计划主体
|
||||
if err := tx.Create(feedingPlan).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 处理步骤 - 先按现有顺序排序,再重新分配从0开始的连续编号
|
||||
sort.Slice(feedingPlan.Steps, func(i, j int) bool {
|
||||
return feedingPlan.Steps[i].StepOrder < feedingPlan.Steps[j].StepOrder
|
||||
})
|
||||
|
||||
// 重新填充步骤编号
|
||||
for i := range feedingPlan.Steps {
|
||||
feedingPlan.Steps[i].StepOrder = i
|
||||
feedingPlan.Steps[i].PlanID = feedingPlan.ID
|
||||
}
|
||||
|
||||
// 如果有步骤,批量创建步骤
|
||||
if len(feedingPlan.Steps) > 0 {
|
||||
if err := tx.Create(&feedingPlan.Steps).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 处理子计划 - 重新填充子计划编号和父ID
|
||||
sort.Slice(feedingPlan.SubPlans, func(i, j int) bool {
|
||||
// 如果OrderInParent为nil,放在最后
|
||||
if feedingPlan.SubPlans[i].OrderInParent == nil {
|
||||
return false
|
||||
}
|
||||
if feedingPlan.SubPlans[j].OrderInParent == nil {
|
||||
return true
|
||||
}
|
||||
return *feedingPlan.SubPlans[i].OrderInParent < *feedingPlan.SubPlans[j].OrderInParent
|
||||
})
|
||||
|
||||
// 重新填充子计划编号和父ID
|
||||
for i := range feedingPlan.SubPlans {
|
||||
order := i
|
||||
feedingPlan.SubPlans[i].OrderInParent = &order
|
||||
feedingPlan.SubPlans[i].ParentID = &feedingPlan.ID
|
||||
|
||||
// 递归创建子计划
|
||||
if err := f.createFeedingPlanWithTx(tx, &feedingPlan.SubPlans[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// clearAllIDs 清空计划及其子计划和步骤的所有ID
|
||||
func (f *feedPlanRepo) clearAllIDs(plan *model.FeedingPlan) {
|
||||
// 清空计划ID
|
||||
plan.ID = 0
|
||||
|
||||
// 清空所有步骤的ID和关联的计划ID
|
||||
for i := range plan.Steps {
|
||||
plan.Steps[i].ID = 0
|
||||
plan.Steps[i].PlanID = 0
|
||||
}
|
||||
|
||||
// 清空所有子计划的ID和关联的父计划ID,并递归清空子计划的ID
|
||||
for i := range plan.SubPlans {
|
||||
plan.SubPlans[i].ID = 0
|
||||
plan.SubPlans[i].ParentID = nil
|
||||
f.clearAllIDs(&plan.SubPlans[i])
|
||||
}
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
// Package repository 提供数据访问层实现
|
||||
// 包含各种数据实体的仓库接口和实现
|
||||
package repository
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// OperationHistoryRepo 操作历史仓库接口
|
||||
type OperationHistoryRepo interface {
|
||||
// Create 创建操作历史记录
|
||||
Create(history *model.OperationHistory) error
|
||||
|
||||
// FindByUserID 根据用户ID查找操作历史记录
|
||||
FindByUserID(userID uint) ([]*model.OperationHistory, error)
|
||||
|
||||
// FindByID 根据ID查找操作历史记录
|
||||
FindByID(id uint) (*model.OperationHistory, error)
|
||||
|
||||
// List 获取操作历史记录列表(分页)
|
||||
List(offset, limit int) ([]*model.OperationHistory, error)
|
||||
|
||||
// ListByUserID 根据用户ID获取操作历史记录列表(分页)
|
||||
ListByUserID(userID uint, offset, limit int) ([]*model.OperationHistory, error)
|
||||
}
|
||||
|
||||
// operationHistoryRepo 操作历史仓库实现
|
||||
type operationHistoryRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewOperationHistoryRepo 创建操作历史仓库实例
|
||||
func NewOperationHistoryRepo(db *gorm.DB) OperationHistoryRepo {
|
||||
return &operationHistoryRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建操作历史记录
|
||||
func (r *operationHistoryRepo) Create(history *model.OperationHistory) error {
|
||||
result := r.db.Create(history)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// FindByUserID 根据用户ID查找操作历史记录
|
||||
func (r *operationHistoryRepo) FindByUserID(userID uint) ([]*model.OperationHistory, error) {
|
||||
var histories []*model.OperationHistory
|
||||
result := r.db.Where("user_id = ?", userID).Order("created_at DESC").Find(&histories)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return histories, nil
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找操作历史记录
|
||||
func (r *operationHistoryRepo) FindByID(id uint) (*model.OperationHistory, error) {
|
||||
var history model.OperationHistory
|
||||
result := r.db.First(&history, id)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &history, nil
|
||||
}
|
||||
|
||||
// List 获取操作历史记录列表(分页)
|
||||
func (r *operationHistoryRepo) List(offset, limit int) ([]*model.OperationHistory, error) {
|
||||
var histories []*model.OperationHistory
|
||||
result := r.db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&histories)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return histories, nil
|
||||
}
|
||||
|
||||
// ListByUserID 根据用户ID获取操作历史记录列表(分页)
|
||||
func (r *operationHistoryRepo) ListByUserID(userID uint, offset, limit int) ([]*model.OperationHistory, error) {
|
||||
var histories []*model.OperationHistory
|
||||
result := r.db.Where("user_id = ?", userID).Offset(offset).Limit(limit).Order("created_at DESC").Find(&histories)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return histories, nil
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
// Package repository 提供数据访问层实现
|
||||
// 包含各种数据实体的仓库接口和实现
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserRepo 用户仓库接口
|
||||
type UserRepo interface {
|
||||
// CreateUser 创建新用户
|
||||
CreateUser(username, password string) (*model.User, error)
|
||||
|
||||
// FindByUsername 根据用户名查找用户
|
||||
FindByUsername(username string) (*model.User, error)
|
||||
|
||||
// FindByID 根据ID查找用户
|
||||
FindByID(id uint) (*model.User, error)
|
||||
}
|
||||
|
||||
// userRepo 用户仓库实现
|
||||
type userRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewUserRepo 创建用户仓库实例
|
||||
func NewUserRepo(db *gorm.DB) UserRepo {
|
||||
return &userRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateUser 创建新用户
|
||||
func (r *userRepo) CreateUser(username, password string) (*model.User, error) {
|
||||
// 检查用户是否已存在
|
||||
var existingUser model.User
|
||||
result := r.db.Where("username = ?", username).First(&existingUser)
|
||||
if result.Error == nil {
|
||||
return nil, fmt.Errorf("用户已存在")
|
||||
}
|
||||
|
||||
// 对密码进行哈希处理
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("密码加密失败: %v", err)
|
||||
}
|
||||
|
||||
// 创建新用户
|
||||
user := &model.User{
|
||||
Username: username,
|
||||
PasswordHash: string(hashedPassword),
|
||||
}
|
||||
|
||||
result = r.db.Create(user)
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("用户创建失败: %v", result.Error)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// FindByUsername 根据用户名查找用户
|
||||
func (r *userRepo) FindByUsername(username string) (*model.User, error) {
|
||||
var user model.User
|
||||
result := r.db.Where("username = ?", username).First(&user)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找用户
|
||||
func (r *userRepo) FindByID(id uint) (*model.User, error) {
|
||||
var user model.User
|
||||
result := r.db.First(&user, id)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user