fix: avoid company writes for employee serials
This commit is contained in:
@@ -79,20 +79,6 @@ func (s *EmployeeSerialsService) generateWithDB(
|
||||
) ([]models.EmployeeSerial, error) {
|
||||
var serials []models.EmployeeSerial
|
||||
|
||||
// 检查公司是否存在,不存在则创建
|
||||
var company models.Company
|
||||
result := db.Where("company_name = ?", companyName).First(&company)
|
||||
if result.Error != nil {
|
||||
company = models.Company{
|
||||
CompanyName: companyName,
|
||||
IsActive: true,
|
||||
}
|
||||
result = db.Create(&company)
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("创建公司失败: %w", result.Error)
|
||||
}
|
||||
}
|
||||
|
||||
// 生成序列号前缀
|
||||
if serialPrefix == "" {
|
||||
serialPrefix = fmt.Sprintf("EMP%d", time.Now().Year()%100)
|
||||
@@ -136,7 +122,7 @@ func (s *EmployeeSerialsService) generateWithDB(
|
||||
}
|
||||
|
||||
// 保存到数据库
|
||||
result = db.Create(&serials)
|
||||
result := db.Create(&serials)
|
||||
if result.Error != nil {
|
||||
return nil, fmt.Errorf("保存员工序列号失败: %w", result.Error)
|
||||
}
|
||||
@@ -197,17 +183,6 @@ func (s *EmployeeSerialsService) Update(serialNumber string, updateData models.U
|
||||
}
|
||||
|
||||
if updateData.CompanyName != "" {
|
||||
// 检查公司是否存在
|
||||
var company models.Company
|
||||
companyResult := database.DB.Where("company_name = ?", updateData.CompanyName).First(&company)
|
||||
if companyResult.Error != nil {
|
||||
company = models.Company{
|
||||
CompanyName: updateData.CompanyName,
|
||||
IsActive: true,
|
||||
}
|
||||
database.DB.Create(&company)
|
||||
}
|
||||
|
||||
serial.CompanyName = updateData.CompanyName
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user