123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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"` // 应用图标
- StartupDiagramPc string `gorm:"type:varchar(50);" json:"startup_diagram_pc"` // 启动图pc
- StartupDiagramMobile string `gorm:"type:varchar(50);" json:"startup_diagram_mobile"` // 启动图移动
- 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"` // 背景图模糊度
- }
- type AppDto struct {
- Apply
- AppUserCount int64 `json:"app_user_count"`
- }
- func (a Apply) GetApplyAminList(params unity.QueryPageParams, apply Apply, queryCond string) (result []AppDto, total int64, err error) {
- //TODO implement me
- query := global.DBLink.Table(apply.TableName())
- if params.Query != "%%" {
- query = query.Where(queryCond, params.Query)
- }
- if params.State != "" {
- query = query.Where("state=?", params.State)
- }
- if err = query.Count(&total).Error; err != nil {
- return nil, 0, err
- }
- offset := (params.Page - 1) * params.Size
- if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
- return nil, 0, err
- }
- for i, _ := range result {
- tableName := "appuser_" + result[i].AppID
- recode, count := GetAllUserCount(tableName)
- if recode == e.SUCCESS {
- result[i].AppUserCount = count
- } else {
- result[i].AppUserCount = 0
- }
- }
- return result, total, nil
- }
- func (a Apply) TableName() string {
- return "applies"
- }
- 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.
- Where("app_id=?", appid).
- 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
- }
|