package service

import (
	"XT_New/enums"
	"XT_New/models"
	"XT_New/utils"
	"time"
)

func RegisterSuperAdmin(mobile string, password string) (*models.AdminUser, *enums.SGJError) {
	tx := writeUserDb.Begin()
	now := time.Now().Unix()
	admin := models.AdminUser{
		Mobile:       mobile,
		Password:     password,
		IsSuperAdmin: true,
		Status:       1,
		CreateTime:   now,
		ModifyTime:   now,
	}
	registerSuccess := false
	if createAdminErr := tx.Create(&admin).Error; createAdminErr != nil {
		utils.ErrorLog("创建 AdminUser 失败：%v", createAdminErr)
		tx.Rollback()
	} else {
		registerSuccess = true
	}
	tx.Commit()
	if registerSuccess == true {
		return &admin, nil
	} else {
		return nil, &enums.SGJError{Code: enums.ErrorCodeRegisterFail}
	}
}
