删掉所有文件
This commit is contained in:
@@ -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