apply.go 6.7 KB

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