huangyan 9 місяців тому
батько
коміт
cb2b91110b

+ 21 - 0
app/controller/applyCap.go

@@ -68,6 +68,27 @@ func UpDateApplyCap(c *gin.Context) {
 
 }
 
+// UpDateApplyCapSort 拖拽排序
+func UpDateApplyCapSort(c *gin.Context) {
+	var applyCap model.ApplyCap
+	if err := c.ShouldBindJSON(&applyCap); err != nil {
+		e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
+		return
+	}
+	validate := validator.New()
+	if err := validate.Struct(applyCap); err != nil {
+		e.ResponseWithMsg(c, e.PleaseCheckTherRquiredFields, e.PleaseCheckTherRquiredFields.GetMsg())
+		return
+	}
+	dateApplyCap := ApplyCap.UpdateApplyCapSort(applyCap)
+	if dateApplyCap != e.SUCCESS {
+		e.ResponseWithMsg(c, dateApplyCap, dateApplyCap.GetMsg())
+		return
+	}
+	e.ResponseSuccess(c, nil)
+
+}
+
 // DeleteApplyCap 删除应用能力
 func DeleteApplyCap(c *gin.Context) {
 	var applyCap model.ApplyCapabilities

+ 26 - 28
app/model/apply.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"gorm.io/gorm"
+	"log"
 	"project_management/app/e"
 	"project_management/global"
 	"project_management/unity"
@@ -16,24 +17,23 @@ import (
 type Apply struct {
 	//gorm.Model
 	utils.BaseModel
-	AppID                  string      `gorm:"type:varchar(50);not null;unique" json:"app_id"`                                              // 应用id
-	UserId                 int         `gorm:"type:int;" json:"user_id"`                                                                    // 用户id
-	UserName               string      `gorm:"type:varchar(50);" json:"user_name"`                                                          // 用户名
-	AppName                string      `gorm:"type:varchar(50);index:idx_name,unique" json:"app_name" validate:"required" min:"3" max:"20"` // 应用名称
-	AppDescription         string      `gorm:"type:varchar(255);" json:"app_description" validate:"required"`                               // 应用描述
-	CertificationTime      utils.Time  `gorm:"type:datetime;" json:"certification_time"`                                                    // 认证到期时间
-	State                  int         `gorm:"type:int;" json:"state"`                                                                      // 状态 1 正常 2 停用 3 过期 4 禁用
-	Icon                   string      `gorm:"type:varchar(255);" json:"icon"`                                                              // 应用图标
-	StartupDiagramPc       string      `gorm:"type:varchar(255);" json:"startup_diagram_pc"`                                                // 启动图pc
-	StartupDiagramMobile   string      `gorm:"type:varchar(255);" json:"startup_diagram_mobile"`                                            // 启动图移动
-	LoginMode              int         `gorm:"type:int;" json:"login_mode"`                                                                 // 注册模式 1 公开注册 2禁止注册
-	LoginMethod            LoginMethod `gorm:"type:json" json:"login_method"`                                                               // 登录方式 1 短信 2 微信登录
-	BackgroundImagePc      string      `gorm:"type:varchar(255);" json:"background_image_pc"`                                               // 背景图pc
-	BackgroundImageMobile  string      `gorm:"type:varchar(255);" json:"background_image_mobile"`                                           // 背景图移动
-	BackgroundImageObscure float32     `gorm:"type:double;" json:"background_image_obscure"`                                                // 背景图模糊度
-	TopicPC                int         `gorm:"type:int;" json:"topic_pc"`                                                                   // 主题pc
-	TopicMobile            int         `gorm:"type:int;" json:"topic_mobile"`                                                               // 主题移动
-	Caps                   string      `gorm:"type:varchar(255);" json:"caps"`                                                              // 应用能力
+	AppID                  string       `gorm:"type:varchar(50);not null;unique" json:"app_id"`                                              // 应用id
+	UserId                 int          `gorm:"type:int;" json:"user_id"`                                                                    // 用户id
+	UserName               string       `gorm:"type:varchar(50);" json:"user_name"`                                                          // 用户名
+	AppName                string       `gorm:"type:varchar(50);index:idx_name,unique" json:"app_name" validate:"required" min:"3" max:"20"` // 应用名称
+	AppDescription         string       `gorm:"type:varchar(255);" json:"app_description" validate:"required"`                               // 应用描述
+	CertificationTime      utils.Time   `gorm:"type:datetime;" json:"certification_time"`                                                    // 认证到期时间
+	State                  int          `gorm:"type:int;" json:"state"`                                                                      // 状态 1 正常 2 停用 3 过期 4 禁用
+	Icon                   string       `gorm:"type:varchar(255);" json:"icon"`                                                              // 应用图标
+	StartupDiagramPc       *string      `gorm:"type:varchar(255);" json:"startup_diagram_pc"`                                                // 启动图pc
+	StartupDiagramMobile   *string      `gorm:"type:varchar(255);" json:"startup_diagram_mobile"`                                            // 启动图移动
+	LoginMode              *int         `gorm:"type:int;" json:"login_mode"`                                                                 // 注册模式 1 公开注册 2禁止注册
+	LoginMethod            *LoginMethod `gorm:"type:json" json:"login_method"`                                                               // 登录方式 1 短信 2 微信登录
+	BackgroundImagePc      *string      `gorm:"type:varchar(255);" json:"background_image_pc"`                                               // 背景图pc
+	BackgroundImageMobile  *string      `gorm:"type:varchar(255);" json:"background_image_mobile"`                                           // 背景图移动
+	BackgroundImageObscure *float32     `gorm:"type:double;" json:"background_image_obscure"`                                                // 背景图模糊度
+	TopicPC                *int         `gorm:"type:int;" json:"topic_pc"`                                                                   // 主题pc
+	TopicMobile            *int         `gorm:"type:int;" json:"topic_mobile"`                                                               // 主题移动
 }
 
 type LoginMethod []int
@@ -120,7 +120,7 @@ func (a Apply) GetApplyById(appid string) (Apply, error) {
 	}
 	if tx.RowsAffected > 0 {
 		if a.LoginMethod == nil {
-			a.LoginMethod = LoginMethod{}
+			a.LoginMethod = &LoginMethod{}
 		}
 		return a, nil
 	}
@@ -129,6 +129,7 @@ func (a Apply) GetApplyById(appid string) (Apply, error) {
 
 func (a Apply) UserUpdateApply(apply Apply) e.Rescode {
 	//TODO implement me
+	log.Print(apply)
 	tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply)
 	if tx.Error != nil {
 		return e.ERROR
@@ -223,7 +224,7 @@ func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond
 	}
 	for i, _ := range result {
 		if result[i].LoginMethod == nil {
-			result[i].LoginMethod = LoginMethod{}
+			result[i].LoginMethod = &LoginMethod{}
 		}
 	}
 	return result, total, nil
@@ -231,14 +232,14 @@ func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond
 
 func (a Apply) AddApply(apply Apply) e.Rescode {
 	//TODO implement me
-	//默认每一应用有一年免费时间
-	//time.Parse("2006-01-02 15:04:05", apply.CertificationTime)
+	//设置系统默认状态
 	tiem := time.Now().AddDate(0, 0, 365)
 	apply.CertificationTime = utils.Time(tiem)
 	apply.State = 1
-	apply.BackgroundImageObscure = 0.5
-	apply.TopicPC = 1
-	apply.TopicMobile = 1
+	apply.Icon = global.IconSetting.IconPath
+	*apply.BackgroundImageObscure = 0.5
+	*apply.TopicPC = 1
+	*apply.TopicMobile = 1
 	tx := global.DBLink.Create(&apply)
 	if tx.Error != nil {
 		errMsg := tx.Error.Error()
@@ -309,9 +310,6 @@ func (a Apply) UpDateApplyCap(applycap ApplyCapabilities) e.Rescode {
 	begin := global.DBLink.Begin()
 
 	begin.Table(a.TableName()).Where("app_id = ?", applycap.AppID).First(&a)
-	if a.Caps == applycap.CapID {
-
-	}
 	if err := begin.Table(a.TableName()).
 		Where("app_id = ?", applycap.AppID).
 		Update("caps", applycap.CapID).Error; err != nil {

+ 53 - 1
app/model/applyCap.go

@@ -38,7 +38,6 @@ func (a ApplyCap) DeleteApplyCAp(applycap ApplyCapabilities) e.Rescode {
 
 // UpDateApplyCap 更新应用能力
 func (a ApplyCap) UpDateApplyCap(applycap ApplyCap) e.Rescode {
-	//TODO implement me
 	tableName := "appcap_" + applycap.AppId
 	if applycap.CapName != "" {
 		first := global.DBLink.Table(tableName).Where("cap_name = ?", applycap.CapName).First(&a)
@@ -56,6 +55,59 @@ func (a ApplyCap) UpDateApplyCap(applycap ApplyCap) e.Rescode {
 	return e.UPDATEFAIL
 }
 
+// UpdateApplyCapSort 拖拽排序功能
+func (a ApplyCap) UpdateApplyCapSort(updatedApplyCap ApplyCap) e.Rescode {
+	tableName := "appcap_" + updatedApplyCap.AppId
+
+	// 开始事务
+	tx := global.DBLink.Begin()
+	defer func() {
+		if r := recover(); r != nil {
+			tx.Rollback()
+		}
+	}()
+
+	// 尝试在事务中执行更新
+	if err := tx.Table(tableName).Where("cap_id = ?", updatedApplyCap.CapId).First(&a).Error; err != nil {
+		tx.Rollback()
+		return e.UPDATEFAIL
+	}
+
+	// 获取旧的排序位置
+	oldSort := a.Sort
+
+	// 如果新位置小于旧位置,意味着元素向上移动
+	if updatedApplyCap.Sort < oldSort {
+		if err := tx.Table(tableName).Where("sort >= ? AND sort < ?", updatedApplyCap.Sort, oldSort).
+			Update("sort", gorm.Expr("sort + 1")).Error; err != nil {
+			tx.Rollback()
+			return e.UPDATEFAIL
+		}
+	} else if updatedApplyCap.Sort > oldSort { // 否则元素向下移动
+		if err := tx.Table(tableName).Where("sort <= ? AND sort > ?", updatedApplyCap.Sort, oldSort).
+			Update("sort", gorm.Expr("sort - 1")).Error; err != nil {
+			tx.Rollback()
+			return e.UPDATEFAIL
+		}
+	}
+
+	// 更新被拖动元素的排序位置
+	a.Sort = updatedApplyCap.Sort
+
+	// 保存更改
+	if err := tx.Table(tableName).Save(a).Error; err != nil {
+		tx.Rollback()
+		return e.UPDATEFAIL
+	}
+
+	// 提交事务
+	if err := tx.Commit().Error; err != nil {
+		return e.UPDATEFAIL
+	}
+
+	return e.SUCCESS
+}
+
 // ApplyCapList 获取应用能力列表
 func (a ApplyCap) ApplyCapList(appid string) ([]ApplyCap, error) {
 	//TODO implement me

+ 1 - 0
app/routers/applyCap.go

@@ -10,6 +10,7 @@ func ApplyCapRouter(r *gin.Engine) {
 	group.POST("/apply/addcap", controller.ApplyAddCap)
 	group.GET("/apply/cap", controller.GetApplyCapList)
 	group.PUT("/apply/cap", controller.UpDateApplyCap)
+	group.PUT("/apply/sort", controller.UpDateApplyCapSort)
 	group.DELETE("/apply/cap", controller.DeleteApplyCap)
 	group.GET("/apply/cap/detail", controller.GetCapDetailByCapID)
 }

+ 1 - 0
app/services/applyCap.go

@@ -10,4 +10,5 @@ type ApplyCap interface {
 	ApplyCapList(appid string) (applycap []model.ApplyCap, err error)
 	UpDateApplyCap(applycap model.ApplyCap) e.Rescode
 	DeleteApplyCAp(applycap model.ApplyCapabilities) e.Rescode
+	UpdateApplyCapSort(updatedApplyCap model.ApplyCap) e.Rescode
 }

+ 3 - 1
configs/config.yaml

@@ -47,4 +47,6 @@ qiniu:
   endpoint: "https://bzdcdn.baozhida.cn/"
   accessKeyID: "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo"
   accessKeySecret: "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN"
-  bucketName: "bzdcdn"
+  bucketName: "bzdcdn"
+Icon:
+  iconPath: "icon/icon.png"

+ 5 - 0
global/setting.go

@@ -56,6 +56,9 @@ type Qiniu struct {
 	AccessKeySecret string `json:"accessKeySecret"`
 	BucketName      string `json:"bucketName"`
 }
+type ICon struct {
+	IconPath string `json:"iconPath"`
+}
 
 var (
 	DatabaseSetting *DatabaseSettingS
@@ -66,6 +69,7 @@ var (
 	RedisSetting    *Redis
 	SwaggerSetting  *Swagger
 	QiniuSetting    *Qiniu
+	IconSetting     *ICon
 )
 
 // SetupSetting 读取配置到全局变量
@@ -79,6 +83,7 @@ func SetupSetting() error {
 	err = s.ReadSection("Redis", &RedisSetting)
 	err = s.ReadSection("Swagger", &SwaggerSetting)
 	err = s.ReadSection("Qiniu", &QiniuSetting)
+	err = s.ReadSection("ICon", &IconSetting)
 	if err != nil {
 		return err
 	}