Initial commit
This commit is contained in:
112
services/companies_service.go
Normal file
112
services/companies_service.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.beifan.cn/trace-system/backend-go/database"
|
||||
"git.beifan.cn/trace-system/backend-go/models"
|
||||
)
|
||||
|
||||
// CompaniesService 企业管理服务
|
||||
type CompaniesService struct{}
|
||||
|
||||
// FindAll 获取所有企业列表
|
||||
func (s *CompaniesService) FindAll(page int, limit int, search string) ([]models.Company, int, int, error) {
|
||||
var companies []models.Company
|
||||
var total int64
|
||||
|
||||
offset := (page - 1) * limit
|
||||
db := database.DB
|
||||
|
||||
// 搜索条件
|
||||
if search != "" {
|
||||
db = db.Where("company_name LIKE ?", "%"+search+"%")
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
db.Count(&total)
|
||||
|
||||
// 分页查询
|
||||
result := db.Order("created_at DESC").Offset(offset).Limit(limit).Find(&companies)
|
||||
if result.Error != nil {
|
||||
return nil, 0, 0, errors.New("查询企业列表失败")
|
||||
}
|
||||
|
||||
totalPages := (int(total) + limit - 1) / limit
|
||||
|
||||
return companies, int(total), totalPages, nil
|
||||
}
|
||||
|
||||
// Create 创建企业
|
||||
func (s *CompaniesService) Create(companyName string) (*models.Company, error) {
|
||||
// 检查企业是否已存在
|
||||
var existingCompany models.Company
|
||||
result := database.DB.Where("company_name = ?", companyName).First(&existingCompany)
|
||||
if result.Error == nil {
|
||||
return nil, errors.New("企业名称已存在")
|
||||
}
|
||||
|
||||
company := models.Company{
|
||||
CompanyName: companyName,
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
result = database.DB.Create(&company)
|
||||
if result.Error != nil {
|
||||
return nil, errors.New("创建企业失败")
|
||||
}
|
||||
|
||||
return &company, nil
|
||||
}
|
||||
|
||||
// Update 更新企业信息
|
||||
func (s *CompaniesService) Update(companyName string, newCompanyName string, isActive bool) (*models.Company, error) {
|
||||
var company models.Company
|
||||
result := database.DB.Where("company_name = ?", companyName).First(&company)
|
||||
if result.Error != nil {
|
||||
return nil, errors.New("企业不存在")
|
||||
}
|
||||
|
||||
// 如果企业名称已变更,检查新名称是否已存在
|
||||
if newCompanyName != companyName {
|
||||
var existingCompany models.Company
|
||||
checkResult := database.DB.Where("company_name = ?", newCompanyName).First(&existingCompany)
|
||||
if checkResult.Error == nil {
|
||||
return nil, errors.New("企业名称已存在")
|
||||
}
|
||||
|
||||
company.CompanyName = newCompanyName
|
||||
}
|
||||
|
||||
company.IsActive = isActive
|
||||
|
||||
result = database.DB.Save(&company)
|
||||
if result.Error != nil {
|
||||
return nil, errors.New("更新企业信息失败")
|
||||
}
|
||||
|
||||
return &company, nil
|
||||
}
|
||||
|
||||
// Delete 删除企业
|
||||
func (s *CompaniesService) Delete(companyName string) error {
|
||||
var company models.Company
|
||||
result := database.DB.Where("company_name = ?", companyName).First(&company)
|
||||
if result.Error != nil {
|
||||
return errors.New("企业不存在")
|
||||
}
|
||||
|
||||
// 检查企业是否有关联的序列号
|
||||
var serialCount int64
|
||||
database.DB.Model(&models.Serial{}).Where("company_name = ?", companyName).Count(&serialCount)
|
||||
if serialCount > 0 {
|
||||
return errors.New("企业下还有序列号,无法删除")
|
||||
}
|
||||
|
||||
result = database.DB.Delete(&company)
|
||||
if result.Error != nil {
|
||||
return errors.New("删除企业失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user