package model import ( "project_management/app/e" "project_management/global" "project_management/unity" "project_management/utils" "strings" "time" ) 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(50);" 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(50);" json:"icon"` // 应用图标 StartupDiagram string `gorm:"type:varchar(50);" json:"startup_diagram"` // 启动图 LoginMode int `gorm:"type:int;" json:"login_mode"` // 登录模式 1 公开注册 2禁止注册 LoginMethod int `gorm:"type:int;" json:"login_method"` // 登录方式 1 短信登录 2 微信登录 BackgroundImage string `gorm:"type:varchar(50);" json:"background_image"` // 背景图 BackgroundImageObscure int `gorm:"type:int;" json:"background_image_obscure"` // 背景图模糊度 } func (a Apply) QueryApplyByAppName(appName string) ([]Apply, error) { //TODO implement me var app []Apply tx := global.DBLink. Select("app_name", "icon", "app_description", "app_id", "user_name"). Where("app_name like ?", "%"+appName+"%"). Find(&app) if tx.Error != nil { return nil, tx.Error } return app, nil } func (a Apply) GetApplyById(appid string) (Apply, error) { //TODO implement me tx := global.DBLink. Select("app_name", "icon", "app_description", "app_id", "user_name"). Where("app_id=?", appid). Where("state=?", 1). First(&a) if tx.Error != nil { return Apply{}, tx.Error } if tx.RowsAffected > 0 { return a, nil } return Apply{}, nil } func (a Apply) UserUpdateApply(apply Apply) e.Rescode { //TODO implement me tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply) if tx.Error != nil { return e.ERROR } if tx.RowsAffected > 0 { return e.SUCCESS } return e.ERROR } func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) { var count int64 if params.Query != "%%" && params.State != "" { if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil { return nil, 0, err } // 计算查询的偏移量,并设置每次查询的记录数量 offset := (params.Page - 1) * params.Size // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件 if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Find(&result).Error; err != nil { return nil, 0, err } } else if params.State != "" && params.Query == "%%" { if err = global.DBLink.Model(apply).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil { return nil, 0, err } // 计算查询的偏移量,并设置每次查询的记录数量 offset := (params.Page - 1) * params.Size // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件 if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where("state=?", params.State).Where("user_id=?", apply.UserId).Find(&result).Error; err != nil { return nil, 0, err } } else if params.State != "" && params.Query != "%%" { if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil { return nil, 0, err } // 计算查询的偏移量,并设置每次查询的记录数量 offset := (params.Page - 1) * params.Size // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件 if err = global.DBLink.Offset(offset).Limit(params.Size).Order(params.Desc).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Find(&result).Error; err != nil { return nil, 0, err } } else { if err = global.DBLink.Model(apply).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil { return nil, 0, err } // 计算查询的偏移量,并设置每次查询的记录数量 offset := (params.Page - 1) * params.Size // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件 if err = global.DBLink.Where("user_id=?", apply.UserId).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil { return nil, 0, err } } return result, count, nil } 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 tx := global.DBLink.Create(&apply) if tx.Error != nil { errMsg := tx.Error.Error() if strings.Contains(errMsg, "Duplicate entry") { return e.Repeat } } if tx.RowsAffected > 0 { return e.SUCCESS } return e.SUCCESS } // AppIdISRepeat 检查id是否重复 func AppIdISRepeat(id string) bool { tx := global.DBLink.Where("app_id = ?", id).First(&Apply{}) if tx.RowsAffected > 0 { return true } return false } // IsRegist 判断是否可以注册 func IsRegist(appid string) bool { tx := global.DBLink.Model(&Apply{}). Select("login_mode", "app_id"). Where("app_id=?", appid). Where("state=?", 1). Where("login_mode=?", 1) if tx.RowsAffected > 0 { return true } return false }