123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- package model
- import (
- "database/sql/driver"
- "encoding/json"
- "fmt"
- "gorm.io/gorm"
- "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(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"` // 主题移动
- IsCollection int `gorm:"type:int;" json:"is_collection"` // 是否收藏 1 代表已收藏
- }
- type LoginMethod []int
- type AppDto struct {
- Apply
- AppUserCount int64 `json:"app_user_count"`
- }
- type ApplyCapabilities struct {
- AppID string `json:"app_id" validate:"required"`
- CapID string `json:"cap_id" validate:"required"`
- }
- func (lo LoginMethod) Value() (driver.Value, error) {
- return json.Marshal(lo)
- }
- func (fn *LoginMethod) Scan(src interface{}) error {
- if src == nil {
- *fn = make(LoginMethod, 0)
- return nil
- }
- var u []byte
- switch v := src.(type) {
- case string:
- u = []byte(v)
- case []byte:
- u = v
- default:
- return fmt.Errorf("unsupported type: %T", src)
- }
- return json.Unmarshal(u, (*[]int)(fn))
- }
- 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, phone string) ([]Apply, error) {
- //TODO implement me
- var app []Apply
- var user User
- tx := global.DBLink.
- Where("app_name like ?", "%"+appName+"%").
- Find(&app)
- first := global.DBLink.Table(user.TableName()).Where("phone = ?", phone).First(&user)
- if first.Error != nil {
- return nil, first.Error
- }
- colls := make(map[string]bool)
- for _, s := range user.Collection {
- colls[s] = true
- }
- for i, _ := range app {
- if colls[app[i].AppID] {
- app[i].IsCollection = 1
- }
- }
- 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 {
- if a.LoginMethod == nil {
- a.LoginMethod = &LoginMethod{}
- }
- 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) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
- var conditions []interface{}
- var whereClauses []string
- // 基础条件:按用户ID查询
- whereClauses = append(whereClauses, "user_id = ?")
- conditions = append(conditions, apply.UserId)
- // 添加状态查询条件
- if params.State != "" {
- whereClauses = append(whereClauses, "state = ?")
- conditions = append(conditions, params.State)
- }
- // 添加自定义查询条件,如果提供且不为默认值
- if params.Query != "%%" {
- whereClauses = append(whereClauses, queryCond)
- conditions = append(conditions, params.Query)
- }
- // 计算总数
- if err = global.DBLink.Model(apply).Where(strings.Join(whereClauses, " AND "), conditions...).Count(&total).Error; err != nil {
- return nil, 0, err
- }
- // 分页查询准备
- offset := (params.Page - 1) * params.Size
- // 执行分页查询
- if err = global.DBLink.Model(apply).
- Where(strings.Join(whereClauses, " AND "), conditions...).
- Offset(offset).
- Limit(params.Size).
- Order(params.Desc).
- Find(&result).Error; err != nil {
- return nil, 0, err
- }
- for i, _ := range result {
- if result[i].LoginMethod == nil {
- result[i].LoginMethod = &LoginMethod{}
- }
- }
- return result, total, nil
- }
- func (a Apply) AddApply(apply Apply) e.Rescode {
- //TODO implement me
- //设置系统默认状态
- tiem := time.Now().AddDate(0, 0, 365)
- apply.CertificationTime = utils.Time(tiem)
- apply.State = 1
- apply.Icon = global.IconSetting.IconPath
- if apply.BackgroundImageObscure == nil {
- apply.BackgroundImageObscure = new(float32)
- *apply.BackgroundImageObscure = 0.5
- }
- if apply.BackgroundImagePc == nil {
- apply.BackgroundImagePc = new(string)
- *apply.BackgroundImagePc = global.IconSetting.BackgroundImagePc
- }
- if apply.BackgroundImageMobile == nil {
- apply.BackgroundImageMobile = new(string)
- *apply.BackgroundImageMobile = global.IconSetting.BackgroundImageMobile
- }
- if apply.StartupDiagramPc == nil {
- apply.StartupDiagramPc = new(string)
- *apply.StartupDiagramPc = global.IconSetting.StartupDiagramPc
- }
- if apply.StartupDiagramMobile == nil {
- apply.StartupDiagramMobile = new(string)
- *apply.StartupDiagramMobile = global.IconSetting.StartupDiagramMobile
- }
- if apply.TopicPC == nil {
- apply.TopicPC = new(int)
- *apply.TopicPC = 1
- }
- if apply.TopicMobile == nil {
- apply.TopicMobile = new(int)
- *apply.TopicMobile = 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.INSERTFAIL
- }
- // 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
- }
- // ApplyAddCap 添加应用能力
- func (a Apply) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
- var caps Capabilities
- tx := global.DBLink.Begin()
- if err := tx.Table(a.TableName()).
- Where("app_id = ?", applycap.AppID).
- Update("caps", applycap.CapID).Error; err != nil {
- tx.Rollback()
- return e.INSERTFAIL
- }
- split := strings.Split(applycap.CapID, ",")
- for _, v := range split {
- if err := tx.Table(caps.TableName()).Where("cap_id = ?", v).
- Update("count", gorm.Expr("count + ?", 1)).
- Update("updated_at", utils.Time(time.Now())).Error; err != nil {
- tx.Rollback()
- return e.INSERTFAIL
- }
- }
- if err := tx.Commit().Error; err != nil {
- return e.INSERTFAIL
- }
- return e.SUCCESS
- }
- func (a Apply) UpDateApplyCap(applycap ApplyCapabilities) e.Rescode {
- //TODO implement me
- begin := global.DBLink.Begin()
- begin.Table(a.TableName()).Where("app_id = ?", applycap.AppID).First(&a)
- if err := begin.Table(a.TableName()).
- Where("app_id = ?", applycap.AppID).
- Update("caps", applycap.CapID).Error; err != nil {
- begin.Rollback()
- return e.UPDATEFAIL
- }
- return e.UPDATEFAIL
- }
- func (a Apply) CollectionList(params unity.QueryPageParams, apply Apply, phone string) (result []AppDto, total int64, err error) {
- //TODO implement me
- var user User
- query := global.DBLink.Table(apply.TableName())
- if err = query.Count(&total).Where("state = ?", 1).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).Where("state = ?", 1).Error; err != nil {
- return nil, 0, err
- }
- first := global.DBLink.Table(user.TableName()).Where("phone = ?", phone).First(&user)
- if first.Error != nil {
- return nil, 0, err
- }
- colls := make(map[string]bool)
- for _, s := range user.Collection {
- colls[s] = true
- }
- for i, _ := range result {
- if colls[result[i].AppID] {
- result[i].IsCollection = 1
- }
- }
- return result, total, nil
- }
- // Check 检查是否过期
- func (a Apply) Check() bool {
- //TODO implement me
- //检查过期时间大于当前时间的应用并且更新状态为3--过期
- var applys []Apply
- now := utils.Time(time.Now())
- tx := global.DBLink.Table(a.TableName()).Where("certification_time < ?", now).Where("state != ?", 3).Find(&applys)
- if tx.Error != nil {
- return false
- }
- for i, _ := range applys {
- update := tx.Where("certification_time = ?", applys[i].CertificationTime).Update("state", 3)
- if update.Error != nil {
- return false
- }
- if update.RowsAffected > 0 {
- return true
- }
- }
- return false
- }
|