355 lines
10 KiB
Go
355 lines
10 KiB
Go
package outgiving
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
err1 "errors"
|
||
"fmt"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/controller"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/controller/v1/app_manage"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/middleware"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/node"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/out_giving"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/project_info"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/user"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/errors"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/request"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/utils"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/service/app_manage/common"
|
||
"github.com/gin-gonic/gin"
|
||
"regexp"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type OutGivgingPageBody struct {
|
||
PageInfo *request.PageInfo `json:"pageInfo" binding:"required"`
|
||
Group string `json:"group"`
|
||
OutGivingProject string `json:"outGivingProject"`
|
||
Status string `json:"status"`
|
||
}
|
||
|
||
func DispatchList(c *gin.Context) {
|
||
var pageBody OutGivgingPageBody
|
||
err := c.ShouldBindJSON(&pageBody)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
||
return
|
||
}
|
||
|
||
fields := map[string]interface{}{}
|
||
if pageBody.Group != "" {
|
||
fields["\"group\""] = pageBody.Group
|
||
}
|
||
if pageBody.OutGivingProject != "" {
|
||
// 将字符串转化为布尔值
|
||
toBool := utils.StringToBool(pageBody.OutGivingProject)
|
||
fields["outGivingProject"] = toBool
|
||
}
|
||
if pageBody.Status != "" {
|
||
fields["status"] = pageBody.Status
|
||
}
|
||
|
||
workspaceId := app_manage.GetWorkspaceId(c)
|
||
fields["workspace_id"] = workspaceId
|
||
|
||
page := &model.Page[out_giving.OutGivingModel]{
|
||
CurrentPage: pageBody.PageInfo.CurrentPage,
|
||
PageSize: pageBody.PageInfo.PageSize,
|
||
Order: pageBody.PageInfo.Order,
|
||
}
|
||
|
||
model := out_giving.NewOutGivingModel()
|
||
err = model.Page(page, fields)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "查询分发列表失败!")
|
||
return
|
||
}
|
||
|
||
controller.Success(c, page)
|
||
}
|
||
|
||
func DispatchListAll(c *gin.Context) {
|
||
workspaceId := app_manage.GetWorkspaceId(c)
|
||
models := out_giving.ListOutGivingModelByWorkspace(workspaceId)
|
||
controller.Success(c, models)
|
||
}
|
||
|
||
// 关联分发项目
|
||
func SaveConnected(c *gin.Context) {
|
||
bodyToMap := request.GetBodyToMap(c)
|
||
typeStr, _ := bodyToMap["type"].(string)
|
||
id, _ := bodyToMap["id"].(string)
|
||
|
||
workspaceId := app_manage.GetWorkspaceId(c)
|
||
bodyToMap["workspace_id"] = workspaceId
|
||
|
||
userObject, _ := c.Get(middleware.LoginUserKey)
|
||
userModel := userObject.(*user.UserObj)
|
||
|
||
if "add" == typeStr {
|
||
checkId := strings.ReplaceAll(id, "-", "")
|
||
checkId = strings.ReplaceAll(checkId, "_", "")
|
||
err := validateID(checkId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, err.Error())
|
||
return
|
||
}
|
||
addOutGivingConnected(c, id, bodyToMap, userModel)
|
||
} else {
|
||
updateGivingConnected(c, id, bodyToMap, userModel)
|
||
}
|
||
|
||
}
|
||
|
||
// validateID 验证项目ID
|
||
func validateID(id string) error {
|
||
re := regexp.MustCompile(`^[a-zA-Z0-9_]{2,50}$`)
|
||
matchString := re.MatchString(id)
|
||
if !matchString {
|
||
return fmt.Errorf("项目id 长度范围2-%d(英文字母 、数字和下划线)", 20)
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func addOutGivingConnected(c *gin.Context, id string, body map[string]interface{}, userObj *user.UserObj) {
|
||
byId := out_giving.GetOutGivingModelById(id)
|
||
if byId.Id != "" {
|
||
controller.FailCode(c, errors.ServerError, nil, "项目id已存在")
|
||
return
|
||
}
|
||
outGivingModel := out_giving.NewOutGivingModel()
|
||
outGivingModel.Id = id
|
||
outGivingModel.CreateUser = userObj.UserID
|
||
outGivingModel.ModifyUser = userObj.UserID
|
||
|
||
err := doDataConnected(outGivingModel, body)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, err.Error())
|
||
return
|
||
}
|
||
|
||
_, err = outGivingModel.Create(outGivingModel)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "项目id已存在")
|
||
return
|
||
}
|
||
controller.Success(c, "添加成功")
|
||
}
|
||
|
||
func updateGivingConnected(c *gin.Context, id string, body map[string]interface{}, userObj *user.UserObj) {
|
||
outGivingModel := out_giving.GetOutGivingModelById(id)
|
||
if outGivingModel == nil {
|
||
controller.Fail(c, errors.ServerError, "没有找到对应的分发id")
|
||
return
|
||
}
|
||
|
||
err := doDataConnected(outGivingModel, body)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, err.Error())
|
||
return
|
||
}
|
||
|
||
outGivingModel.ModifyUser = userObj.UserID
|
||
outGivingModel.ModifyTimeMillis = time.Now().UnixMilli()
|
||
err = out_giving.UpdateOutGivingModel(outGivingModel, []string{"name", "group", "workspace_id", "after_opt", "secondary_directory",
|
||
"out_giving_node_project_list", "clear_old", "upload_close_first", "secondary_directory", "modify_user", "modify_time_millis"})
|
||
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "修改失败")
|
||
return
|
||
}
|
||
controller.Success(c, "修改成功")
|
||
}
|
||
|
||
// {
|
||
// "type": "add",
|
||
// "id": "0K1GRY",
|
||
// "name": "sdafas",
|
||
// "projectId": "",
|
||
// "group": "gg",
|
||
// "afterOpt": "0",
|
||
// "secondaryDirectory": "tssss",
|
||
// "clearOld": "true",
|
||
// "uploadCloseFirst": "true",
|
||
// "node_56e192c8e76742fa85c72342212ef466_0": "27G909",
|
||
// "node_5722cd7a4e7a475cac61da2507088a9d_1": "T88633"
|
||
// }
|
||
func doDataConnected(outGivingModel *out_giving.OutGivingModel, body map[string]interface{}) error {
|
||
outGivingModel.Name = body["name"].(string)
|
||
outGivingModel.WorkspaceId = body["workspace_id"].(string)
|
||
|
||
if outGivingModel.Name == "" {
|
||
return fmt.Errorf("分发名称不能为空")
|
||
}
|
||
|
||
if groupStr, ok := body["group"]; ok {
|
||
if groupStr != nil {
|
||
s := groupStr.(string)
|
||
outGivingModel.Group = &s
|
||
}
|
||
}
|
||
|
||
outGivingModels := out_giving.ListOutGivingModel()
|
||
|
||
paramMap := make(map[string]string)
|
||
for k, v := range body {
|
||
if strings.HasPrefix(k, "node_") {
|
||
paramMap[k] = v.(string)
|
||
}
|
||
}
|
||
|
||
outGivingNodeProjects := make([]out_giving.OutGivingNodeProject, 0)
|
||
for key, projectId := range paramMap {
|
||
// "node_5722cd7a4e7a475cac61da2507088a9d_1": "T88633"
|
||
if strings.HasPrefix(key, "node_") {
|
||
nodeId := strings.TrimPrefix(key, "node_")
|
||
nodeId = strings.Split(nodeId, "_")[0]
|
||
|
||
nodeModel := node.GetNodeById(nodeId)
|
||
if nodeModel.Id == "" {
|
||
return fmt.Errorf("不存在对应的节点: %s", nodeId)
|
||
}
|
||
|
||
exists, _ := project_info.Exists(nodeModel.WorkspaceId, nodeModel.Id, projectId)
|
||
if !exists {
|
||
return fmt.Errorf("没有找到对应的项目id: %s", projectId)
|
||
}
|
||
|
||
outGivingNodeProjectList := outGivingModel.GetOutGivingNodeProjectList()
|
||
outGivingNodeProject := outGivingModel.GetNodeProject(outGivingNodeProjectList, nodeModel.Id, projectId)
|
||
if outGivingNodeProject == nil {
|
||
outGivingNodeProject = &out_giving.OutGivingNodeProject{}
|
||
}
|
||
outGivingNodeProject.NodeId = nodeModel.Id
|
||
outGivingNodeProject.ProjectId = projectId
|
||
|
||
// 检查项目是否已被使用
|
||
for _, model := range outGivingModels {
|
||
if strings.EqualFold(model.Id, outGivingModel.Id) {
|
||
continue
|
||
}
|
||
contains := model.CheckContains(outGivingNodeProject.NodeId, outGivingNodeProject.ProjectId)
|
||
if contains {
|
||
return fmt.Errorf("已经存在相同的分发项目: %s", outGivingNodeProject.ProjectId)
|
||
}
|
||
}
|
||
|
||
outGivingNodeProjects = append(outGivingNodeProjects, *outGivingNodeProject)
|
||
}
|
||
}
|
||
|
||
if len(outGivingNodeProjects) == 0 {
|
||
return fmt.Errorf("至少选择1个节点项目")
|
||
}
|
||
|
||
outGivingModel.SetOutGivingNodeProjectList(outGivingNodeProjects)
|
||
|
||
if afterOpt, ok := body["afterOpt"]; ok {
|
||
afterOptInt := int(afterOpt.(float64))
|
||
outGivingModel.AfterOpt = afterOptInt
|
||
} else {
|
||
return fmt.Errorf("请选择分发后的操作")
|
||
}
|
||
|
||
outGivingModel.IntervalTime = 10
|
||
|
||
if clearOld, ok := body["clearOld"]; ok {
|
||
outGivingModel.ClearOld = clearOld.(bool)
|
||
}
|
||
|
||
if secondaryDirectory, ok := body["secondaryDirectory"]; ok {
|
||
if secondaryDirectory != nil {
|
||
s := secondaryDirectory.(string)
|
||
outGivingModel.SecondaryDirectory = &s
|
||
}
|
||
}
|
||
|
||
if uploadCloseFirst, ok := body["uploadCloseFirst"]; ok {
|
||
outGivingModel.UploadCloseFirst = uploadCloseFirst.(bool)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// 释放关联分发项目
|
||
func ReleaseDel(c *gin.Context) {
|
||
bodyToMap := request.GetBodyToMap(c)
|
||
id, ok := bodyToMap["id"]
|
||
if !ok {
|
||
controller.Fail(c, errors.ServerError, "id不能为空")
|
||
return
|
||
}
|
||
|
||
// todo 验证是否需要workspaceId
|
||
outGivingModel := out_giving.GetOutGivingModelById(id.(string))
|
||
if outGivingModel == nil {
|
||
controller.Fail(c, errors.ServerError, "没有找到对应的分发id")
|
||
return
|
||
}
|
||
|
||
outGivingNodeProjectList := outGivingModel.GetOutGivingNodeProjectList()
|
||
|
||
for _, outGivingNodeProject := range outGivingNodeProjectList {
|
||
nodeModel := node.GetNodeById(outGivingNodeProject.NodeId)
|
||
if nodeModel.Id == "" {
|
||
controller.Fail(c, errors.ServerError, "没有找到对应的节点")
|
||
return
|
||
}
|
||
|
||
body := map[string]string{
|
||
"id": outGivingNodeProject.ProjectId,
|
||
}
|
||
body1, _ := json.Marshal(body)
|
||
resp, err := common.Request5(nodeModel, common.Manage_ReleaseOutGiving, bytes.NewReader(body1))
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "释放节点项目失败")
|
||
return
|
||
}
|
||
if resp.Result.Code != errors.SUCCESS {
|
||
controller.FailCode(c, errors.ServerError, err1.New(resp.Result.Msg), "释放节点项目失败: "+resp.Result.Msg)
|
||
return
|
||
}
|
||
}
|
||
|
||
rows, err := out_giving.DeleteOutGivingModelById(id.(string))
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "删除分发失败")
|
||
return
|
||
}
|
||
if rows >= 0 {
|
||
out_giving.DeleteOutGivingLogByOutGivingId(id.(string))
|
||
}
|
||
|
||
controller.Success(c, "释放成功")
|
||
}
|
||
|
||
// 解绑关联分发项目
|
||
func Unbind(c *gin.Context) {
|
||
bodyToMap := request.GetBodyToMap(c)
|
||
id, ok := bodyToMap["id"]
|
||
if !ok {
|
||
controller.Fail(c, errors.ServerError, "id不能为空")
|
||
return
|
||
}
|
||
|
||
outGivingModel := out_giving.GetOutGivingModelById(id.(string))
|
||
if outGivingModel == nil {
|
||
controller.Fail(c, errors.ServerError, "没有找到对应的分发id")
|
||
return
|
||
}
|
||
|
||
rows, err := out_giving.DeleteOutGivingModelById(id.(string))
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "删除分发失败")
|
||
return
|
||
}
|
||
if rows >= 0 {
|
||
out_giving.DeleteOutGivingLogByOutGivingId(id.(string))
|
||
}
|
||
|
||
// todo 删除日志
|
||
controller.Success(c, "解绑成功")
|
||
}
|