apply.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package model
  2. import (
  3. "gorm.io/gorm"
  4. "project_management/app/e"
  5. "project_management/global"
  6. "project_management/unity"
  7. "strings"
  8. "time"
  9. )
  10. type Apply struct {
  11. gorm.Model
  12. AppID string `gorm:"type:varchar(50);not null;unique" json:"app_id"` // 应用id
  13. UserId int `gorm:"type:int;" json:"user_id"` // 用户id
  14. UserName string `gorm:"type:varchar(50);" json:"user_name"` // 用户名
  15. AppName string `gorm:"type:varchar(50);index:idx_name,unique" json:"app_name" validate:"required" min:"3" max:"20"` // 应用名称
  16. AppDescription string `gorm:"type:varchar(50);" json:"app_description" validate:"required"` // 应用描述
  17. CertificationTime time.Time `gorm:"type:datetime;" json:"certification_time"` // 认证到期时间
  18. State int `gorm:"type:int;" json:"state"` // 状态 1 正常 2 停用 3 过期 4 禁用
  19. Icon string `gorm:"type:varchar(50);" json:"icon"` // 应用图标
  20. StartupDiagram string `gorm:"type:varchar(50);" json:"startup_diagram"` // 启动图
  21. LoginMode int `gorm:"type:int;" json:"login_mode"` // 登录模式 1 公开注册 2禁止注册
  22. LoginMethod int `gorm:"type:int;" json:"login_method"` // 登录方式 1 短信登录 2 微信登录
  23. BackgroundImage string `gorm:"type:varchar(50);" json:"background_image"` // 背景图
  24. BackgroundImageObscure int `gorm:"type:int;" json:"background_image_obscure"` // 背景图模糊度
  25. }
  26. func (a Apply) QueryApplyByAppName(appName string) ([]Apply, error) {
  27. //TODO implement me
  28. var app []Apply
  29. tx := global.DBLink.
  30. Select("app_name", "icon", "app_description", "app_id", "user_name").
  31. Where("app_name like ?", "%"+appName+"%").
  32. Find(&app)
  33. if tx.Error != nil {
  34. return nil, tx.Error
  35. }
  36. return app, nil
  37. }
  38. func (a Apply) GetApplyById(appid string) (Apply, error) {
  39. //TODO implement me
  40. tx := global.DBLink.
  41. Select("app_name", "icon", "app_description", "app_id", "user_name").
  42. Where("app_id=?", appid).
  43. Where("state=?", 1).
  44. First(&a)
  45. if tx.Error != nil {
  46. return Apply{}, tx.Error
  47. }
  48. if tx.RowsAffected > 0 {
  49. return a, nil
  50. }
  51. return Apply{}, nil
  52. }
  53. func (a Apply) UserUpdateApply(apply Apply) e.Rescode {
  54. //TODO implement me
  55. tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply)
  56. if tx.Error != nil {
  57. return e.ERROR
  58. }
  59. if tx.RowsAffected > 0 {
  60. return e.SUCCESS
  61. }
  62. return e.ERROR
  63. }
  64. func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  65. var count int64
  66. if params.Query != "%%" && params.State != "" {
  67. if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  68. return nil, 0, err
  69. }
  70. // 计算查询的偏移量,并设置每次查询的记录数量
  71. offset := (params.Page - 1) * params.Size
  72. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  73. 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 {
  74. return nil, 0, err
  75. }
  76. } else if params.State != "" && params.Query == "%%" {
  77. if err = global.DBLink.Model(apply).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  78. return nil, 0, err
  79. }
  80. // 计算查询的偏移量,并设置每次查询的记录数量
  81. offset := (params.Page - 1) * params.Size
  82. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  83. 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 {
  84. return nil, 0, err
  85. }
  86. } else if params.State != "" && params.Query != "%%" {
  87. if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  88. return nil, 0, err
  89. }
  90. // 计算查询的偏移量,并设置每次查询的记录数量
  91. offset := (params.Page - 1) * params.Size
  92. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  93. 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 {
  94. return nil, 0, err
  95. }
  96. } else {
  97. if err = global.DBLink.Model(apply).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  98. return nil, 0, err
  99. }
  100. // 计算查询的偏移量,并设置每次查询的记录数量
  101. offset := (params.Page - 1) * params.Size
  102. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  103. if err = global.DBLink.Where("user_id=?", apply.UserId).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  104. return nil, 0, err
  105. }
  106. }
  107. return result, count, nil
  108. }
  109. func (a Apply) AddApply(apply Apply) e.Rescode {
  110. //TODO implement me
  111. //默认每一应用有一年免费时间
  112. apply.CertificationTime = time.Now().Add(time.Hour * 24 * 365)
  113. apply.State = 1
  114. tx := global.DBLink.Create(&apply)
  115. if tx.Error != nil {
  116. errMsg := tx.Error.Error()
  117. if strings.Contains(errMsg, "Duplicate entry") {
  118. return e.Repeat
  119. }
  120. }
  121. if tx.RowsAffected > 0 {
  122. return e.SUCCESS
  123. }
  124. return e.SUCCESS
  125. }
  126. // AppIdISRepeat 检查id是否重复
  127. func AppIdISRepeat(id string) bool {
  128. tx := global.DBLink.Where("app_id = ?", id).First(&Apply{})
  129. if tx.RowsAffected > 0 {
  130. return true
  131. }
  132. return false
  133. }
  134. // IsRegist 判断是否可以注册
  135. func IsRegist(appid string) bool {
  136. tx := global.DBLink.Model(&Apply{}).
  137. Select("login_mode", "app_id").
  138. Where("app_id=?", appid).
  139. Where("state=?", 1).
  140. Where("login_mode=?", 1)
  141. if tx.RowsAffected > 0 {
  142. return true
  143. }
  144. return false
  145. }