package services import ( "fmt" "strings" "xugu_license/internal/api" "xugu_license/internal/global" "xugu_license/internal/models" "xugu_license/internal/module/email" "xugu_license/internal/utils" "github.com/pkg/errors" ) func DistributeOALicenseServices(OperatorUniqueID, OaRequestId string, UserUniqueIDs, UserAccounts, UserNames, EmailArray []string) (error, []string) { //检测OARequest是否以及分发过给用户 existUsers, err := checkDistributeToUser(OaRequestId, UserUniqueIDs, UserNames) if err != nil { global.Logger.Errorln("license分发请求检测是否已发放给用户失败 %s", err.Error()) return err, nil } //返回以及分发过的用户列表 if len(existUsers) != 0 { return nil, existUsers } fmt.Println("DistributeOALicenseServices : ") //获取一个申请单的多个license信息 LicInfos, _, err := models.GetOALicenseInfo(1, 1, "", "", "", OaRequestId) if err != nil { return err, nil } //将分发的lic信息插入lic分发用户表中 for _, licInfo := range *LicInfos { for userIdx, userUID := range UserUniqueIDs { //如果lic1为空则报错 if licInfo.GenrateInfo.Lic1.String == "" { global.Logger.Errorln("DistributeOALicenseServices : license生成信息为空 ") return errors.New("license生成信息为空"), nil } err = models.InsertlicenseRecordByUserRow(OaRequestId, licInfo.LicInfo.UniqueID.String, userUID, UserAccounts[userIdx], OperatorUniqueID) if err != nil { global.Logger.Errorln("DistributeOALicenseServices: 数据库插入失败: ", err.Error()) return err, nil } } } //发送邮件 //邮箱不空则发送 //将一个申请单的多个license信息用邮箱发送 fmt.Println("len(EmailArray) == ") if len(EmailArray) == 0 { return nil, nil } fmt.Println("EmailArray : ", EmailArray) emails := strings.Join(EmailArray, ",") if !strings.Contains(emails, "@") { return nil, nil } for _, lic := range *LicInfos { if lic.GenrateInfo.Lic1.String == "" { global.Logger.Errorln("DistributeOALicenseServices : license生成信息为空 ") return errors.New("license生成信息为空"), nil } //组装邮件 em, err := email.BuildEmail(&lic, EmailArray, lic.GenrateInfo.Lic1.String, lic.GenrateInfo.Lic2.String) if err != nil { global.Logger.Errorln("邮件生成失败", err.Error()) return err, nil } //发送邮件 err = email.SendEmail(em) if err != nil { global.Logger.Errorln("邮件发送失败", err.Error()) return err, nil } //将分发的lic信息插入lic分发邮箱表中 err = models.InsertlicenseRecordByEmailRow(OaRequestId, lic.LicInfo.UniqueID.String, emails, OperatorUniqueID) if err != nil { global.Logger.Errorln("数据库插入失败: ", err.Error()) return err, nil } } return nil, nil } func GenerateOALicenseStrServices(request api.GenerateLicenseStrRequest) error { //生成单独的license if request.UniqueID != "" { if err, isCheck := generateLicenseStrSub(request.UniqueID); err != nil { global.Logger.Errorf("请联系管理员, LicenseInfo%v生成失败: %v\n", request.UniqueID, err.Error()) return errors.Errorf("请联系管理员, LicenseInfo%v生成失败: %v", err.Error()) } else if isCheck { return errors.Errorf("该申请信息与OA库不一致,请刷新页面") } } else if request.OARequestId != "" { //一个OARequestId会有多条数据UniqueID OALics, _, err := models.GetOALicenseInfo(0, 0, "", "", "", request.OARequestId) if err != nil || OALics == nil { global.Logger.Errorln("LicenseInfo数据查询失败: ", err.Error()) return errors.Errorf("LicenseInfo数据查询失败 : %v", err.Error()) } //生成license,已生成的不管 for _, v := range *OALics { if v.GenrateInfo.LicenseFlage.String == "未生成" { err, isCheck := generateLicenseStrSub(utils.ToString(v.LicInfo.UniqueID)) if err != nil { global.Logger.Errorf("请联系管理员, LicenseInfo%v生成失败: %v\n", v.LicInfo.UniqueID, err.Error()) return errors.Errorf("请联系管理员, LicenseInfo%v生成失败: %v\n", v.LicInfo.UniqueID, err.Error()) } else if isCheck { return errors.Errorf("该申请信息与OA库不一致,请刷新页面") } } } } else { global.Logger.Errorln("未指定明确License行生成 ") return errors.Errorf("未指定明确License行生成\n") } return nil } func generateLicenseStrSub(UniqueID string) (error, bool) { var applications *[]models.OALicenseInfo var err error if UniqueID != "" { applications, _, err = models.GetOALicenseInfo(0, 1, "", UniqueID, "", "") if err != nil { global.Logger.Errorln("LicenseInfo数据查询失败: ", err.Error()) // c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("数据查询失败: ", err.Error())}) return errors.Errorf("LicenseInfo数据查询失败: %v", err.Error()), false } } // (*applications)[0].LicInfo.UniqueID.String = "" // (*applications)[0].LicInfo.UniqueID.Valid = false // (*applications)[0].GenrateInfo.LicenseUniqueID.String = "" // (*applications)[0].GenrateInfo.LicenseUniqueID.Valid = false fmt.Printf("applications%#v\n", applications) //检测该数据是否与oa库数据库一致 //todo // isCheck, err := models.CheckLicenseInfoInOADB(&(*applications)[0].LicInfo) // if err != nil { // global.Logger.Errorln("LicenseInfo数据对比oa库 查询失败: ", err.Error()) // // c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("数据查询失败: ", err.Error())}) // return errors.Errorf("LicenseInfo数据对比oa库 查询失败 %v", err.Error()), false // } // if isCheck { // global.Logger.Errorln("LicenseInfo数据对比oa库 不一致") // err := models.UpdatelicenseInfoRow(models.OALicenseInfo{ // LicInfo: (*applications)[0].LicInfo, // }) // if err != nil { // return err, false // } // return nil, true // } //生成副主节点license if applications == nil { global.Logger.Errorln("LicenseInfo数据生成失败 ,数据查询为空 ") //c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("LicenseInfo数据生成失败 ,数据查询为空: ", err.Error())}) return errors.Errorf("LicenseInfo数据生成失败 ,数据查询为空 "), false } if (*applications)[0].LicInfo.OASecondMAC.String != "" { // var licStr2 []byte // ei.MacAddr = applications[0].SecondaryMasterMacAddress // licStr2 = license.GenerateLicense(pj, ei, lI) err = models.UpdateOALicenseStr(utils.ToString((*applications)[0].LicInfo.UniqueID), []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9")) if err != nil { global.Logger.Errorln("LicenseInfo生成插入失败: ", err.Error()) // c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("数据查询失败: ", err.Error())}) return errors.Errorf("LicenseInfo生成插入失败:: ", err.Error()), false } } else { //fmt.Println("licStr licStr2", licStr, licStr2) //插入到数据库 err = models.UpdateOALicenseStr(utils.ToString((*applications)[0].LicInfo.UniqueID), []byte("b4j6z4rE2IfG1av0wIPT7YnvyGZFHxwIBikMGjgCLQILR0xsT1NHiuzoi+Dqq+bmiNDEiuPyitDVgdvlRmYbFAk+MAAGASlPTkdMbE9"), nil) if err != nil { global.Logger.Errorln("LicenseInfo2生成插入失败: ", err.Error()) // c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("数据查询失败: ", err.Error())}) return errors.Errorf("LicenseInfo2生成插入失败:: ", err.Error()), false } } return nil, false }