apply.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. StartupDiagramPc string `gorm:"type:varchar(50);" json:"startup_diagram_pc"` // 启动图pc
  22. StartupDiagramMobile string `gorm:"type:varchar(50);" json:"startup_diagram_mobile"` // 启动图移动
  23. LoginMode int `gorm:"type:int;" json:"login_mode"` // 登录模式 1 公开注册 2禁止注册
  24. LoginMethod int `gorm:"type:int;" json:"login_method"` // 登录方式 1 短信登录 2 微信登录
  25. BackgroundImage string `gorm:"type:varchar(50);" json:"background_image"` // 背景图
  26. BackgroundImageObscure int `gorm:"type:int;" json:"background_image_obscure"` // 背景图模糊度
  27. }
  28. type AppDto struct {
  29. Apply
  30. AppUserCount int64 `json:"app_user_count"`
  31. }
  32. func (a Apply) GetApplyAminList(params unity.QueryPageParams, apply Apply, queryCond string) (result []AppDto, total int64, err error) {
  33. //TODO implement me
  34. query := global.DBLink.Table(apply.TableName())
  35. if params.Query != "%%" {
  36. query = query.Where(queryCond, params.Query)
  37. }
  38. if params.State != "" {
  39. query = query.Where("state=?", params.State)
  40. }
  41. if err = query.Count(&total).Error; err != nil {
  42. return nil, 0, err
  43. }
  44. offset := (params.Page - 1) * params.Size
  45. if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  46. return nil, 0, err
  47. }
  48. for i, _ := range result {
  49. tableName := "appuser_" + result[i].AppID
  50. recode, count := GetAllUserCount(tableName)
  51. if recode == e.SUCCESS {
  52. result[i].AppUserCount = count
  53. } else {
  54. result[i].AppUserCount = 0
  55. }
  56. }
  57. return result, total, nil
  58. }
  59. func (a Apply) TableName() string {
  60. return "applies"
  61. }
  62. func (a Apply) QueryApplyByAppName(appName string) ([]Apply, error) {
  63. //TODO implement me
  64. var app []Apply
  65. tx := global.DBLink.
  66. Select("app_name", "icon", "app_description", "app_id", "user_name").
  67. Where("app_name like ?", "%"+appName+"%").
  68. Find(&app)
  69. if tx.Error != nil {
  70. return nil, tx.Error
  71. }
  72. return app, nil
  73. }
  74. func (a Apply) GetApplyById(appid string) (Apply, error) {
  75. //TODO implement me
  76. tx := global.DBLink.
  77. Where("app_id=?", appid).
  78. First(&a)
  79. if tx.Error != nil {
  80. return Apply{}, tx.Error
  81. }
  82. if tx.RowsAffected > 0 {
  83. return a, nil
  84. }
  85. return Apply{}, nil
  86. }
  87. func (a Apply) UserUpdateApply(apply Apply) e.Rescode {
  88. //TODO implement me
  89. tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply)
  90. if tx.Error != nil {
  91. return e.ERROR
  92. }
  93. if tx.RowsAffected > 0 {
  94. return e.SUCCESS
  95. }
  96. return e.ERROR
  97. }
  98. func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  99. var count int64
  100. if params.Query != "%%" && params.State != "" {
  101. if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  102. return nil, 0, err
  103. }
  104. // 计算查询的偏移量,并设置每次查询的记录数量
  105. offset := (params.Page - 1) * params.Size
  106. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  107. 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 {
  108. return nil, 0, err
  109. }
  110. } else if params.State != "" && params.Query == "%%" {
  111. if err = global.DBLink.Model(apply).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  112. return nil, 0, err
  113. }
  114. // 计算查询的偏移量,并设置每次查询的记录数量
  115. offset := (params.Page - 1) * params.Size
  116. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  117. 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 {
  118. return nil, 0, err
  119. }
  120. } else if params.State != "" && params.Query != "%%" {
  121. if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  122. return nil, 0, err
  123. }
  124. // 计算查询的偏移量,并设置每次查询的记录数量
  125. offset := (params.Page - 1) * params.Size
  126. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  127. 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 {
  128. return nil, 0, err
  129. }
  130. } else {
  131. if err = global.DBLink.Model(apply).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  132. return nil, 0, err
  133. }
  134. // 计算查询的偏移量,并设置每次查询的记录数量
  135. offset := (params.Page - 1) * params.Size
  136. // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  137. if err = global.DBLink.Where("user_id=?", apply.UserId).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  138. return nil, 0, err
  139. }
  140. }
  141. return result, count, nil
  142. }
  143. func (a Apply) AddApply(apply Apply) e.Rescode {
  144. //TODO implement me
  145. //默认每一应用有一年免费时间
  146. //time.Parse("2006-01-02 15:04:05", apply.CertificationTime)
  147. tiem := time.Now().AddDate(0, 0, 365)
  148. apply.CertificationTime = utils.Time(tiem)
  149. apply.State = 1
  150. tx := global.DBLink.Create(&apply)
  151. if tx.Error != nil {
  152. errMsg := tx.Error.Error()
  153. if strings.Contains(errMsg, "Duplicate entry") {
  154. return e.Repeat
  155. }
  156. }
  157. if tx.RowsAffected > 0 {
  158. return e.SUCCESS
  159. }
  160. return e.SUCCESS
  161. }
  162. // AppIdISRepeat 检查id是否重复
  163. func AppIdISRepeat(id string) bool {
  164. tx := global.DBLink.Where("app_id = ?", id).First(&Apply{})
  165. if tx.RowsAffected > 0 {
  166. return true
  167. }
  168. return false
  169. }
  170. // IsRegist 判断是否可以注册
  171. func IsRegist(appid string) bool {
  172. tx := global.DBLink.Model(&Apply{}).
  173. Select("login_mode", "app_id").
  174. Where("app_id=?", appid).
  175. Where("state=?", 1).
  176. Where("login_mode=?", 1)
  177. if tx.RowsAffected > 0 {
  178. return true
  179. }
  180. return false
  181. }