apply.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "project_management/app/e"
  8. "project_management/global"
  9. "project_management/unity"
  10. "project_management/utils"
  11. "strings"
  12. "time"
  13. )
  14. type Apply struct {
  15. //gorm.Model
  16. utils.BaseModel
  17. AppID string `gorm:"type:varchar(50);not null;unique" json:"app_id"` // 应用id
  18. UserId int `gorm:"type:int;" json:"user_id"` // 用户id
  19. UserName string `gorm:"type:varchar(50);" json:"user_name"` // 用户名
  20. AppName string `gorm:"type:varchar(50);index:idx_name,unique" json:"app_name" validate:"required" min:"3" max:"20"` // 应用名称
  21. AppDescription string `gorm:"type:varchar(255);" json:"app_description" validate:"required"` // 应用描述
  22. CertificationTime utils.Time `gorm:"type:datetime;" json:"certification_time"` // 认证到期时间
  23. State int `gorm:"type:int;" json:"state"` // 状态 1 正常 2 停用 3 过期 4 禁用
  24. Icon string `gorm:"type:varchar(255);" json:"icon"` // 应用图标
  25. StartupDiagramPc string `gorm:"type:varchar(255);" json:"startup_diagram_pc"` // 启动图pc
  26. StartupDiagramMobile string `gorm:"type:varchar(255);" json:"startup_diagram_mobile"` // 启动图移动
  27. LoginMode int `gorm:"type:int;" json:"login_mode"` // 注册模式 1 公开注册 2禁止注册
  28. LoginMethod LoginMethod `gorm:"type:json" json:"login_method"` // 登录方式 1 短信 2 微信登录
  29. BackgroundImagePc string `gorm:"type:varchar(255);" json:"background_image_pc"` // 背景图pc
  30. BackgroundImageMobile string `gorm:"type:varchar(255);" json:"background_image_mobile"` // 背景图移动
  31. BackgroundImageObscure float32 `gorm:"type:double;" json:"background_image_obscure"` // 背景图模糊度
  32. TopicPC int `gorm:"type:int;" json:"topic_pc"` // 主题pc
  33. TopicMobile int `gorm:"type:int;" json:"topic_mobile"` // 主题移动
  34. Caps string `gorm:"type:varchar(255);" json:"caps"` // 应用能力
  35. }
  36. type LoginMethod []int
  37. type AppDto struct {
  38. Apply
  39. AppUserCount int64 `json:"app_user_count"`
  40. }
  41. type ApplyCapabilities struct {
  42. AppID string `json:"app_id" validate:"required"`
  43. CapID string `json:"cap_id" validate:"required"`
  44. }
  45. func (lo LoginMethod) Value() (driver.Value, error) {
  46. return json.Marshal(lo)
  47. }
  48. func (fn *LoginMethod) Scan(src interface{}) error {
  49. if src == nil {
  50. *fn = make(LoginMethod, 0)
  51. return nil
  52. }
  53. var u []byte
  54. switch v := src.(type) {
  55. case string:
  56. u = []byte(v)
  57. case []byte:
  58. u = v
  59. default:
  60. return fmt.Errorf("unsupported type: %T", src)
  61. }
  62. return json.Unmarshal(u, (*[]int)(fn))
  63. }
  64. func (a Apply) GetApplyAminList(params unity.QueryPageParams, apply Apply, queryCond string) (result []AppDto, total int64, err error) {
  65. //TODO implement me
  66. query := global.DBLink.Table(apply.TableName())
  67. if params.Query != "%%" {
  68. query = query.Where(queryCond, params.Query)
  69. }
  70. if params.State != "" {
  71. query = query.Where("state=?", params.State)
  72. }
  73. if err = query.Count(&total).Error; err != nil {
  74. return nil, 0, err
  75. }
  76. offset := (params.Page - 1) * params.Size
  77. if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  78. return nil, 0, err
  79. }
  80. for i, _ := range result {
  81. tableName := "appuser_" + result[i].AppID
  82. recode, count := GetAllUserCount(tableName)
  83. if recode == e.SUCCESS {
  84. result[i].AppUserCount = count
  85. } else {
  86. result[i].AppUserCount = 0
  87. }
  88. }
  89. return result, total, nil
  90. }
  91. func (a Apply) TableName() string {
  92. return "applies"
  93. }
  94. func (a Apply) QueryApplyByAppName(appName string) ([]Apply, error) {
  95. //TODO implement me
  96. var app []Apply
  97. tx := global.DBLink.
  98. Select("app_name", "icon", "app_description", "app_id", "user_name").
  99. Where("app_name like ?", "%"+appName+"%").
  100. Find(&app)
  101. if tx.Error != nil {
  102. return nil, tx.Error
  103. }
  104. return app, nil
  105. }
  106. func (a Apply) GetApplyById(appid string) (Apply, error) {
  107. //TODO implement me
  108. tx := global.DBLink.
  109. Where("app_id=?", appid).
  110. First(&a)
  111. if tx.Error != nil {
  112. return Apply{}, tx.Error
  113. }
  114. if tx.RowsAffected > 0 {
  115. return a, nil
  116. }
  117. return Apply{}, nil
  118. }
  119. func (a Apply) UserUpdateApply(apply Apply) e.Rescode {
  120. //TODO implement me
  121. tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply)
  122. if tx.Error != nil {
  123. return e.ERROR
  124. }
  125. if tx.RowsAffected > 0 {
  126. return e.SUCCESS
  127. }
  128. return e.ERROR
  129. }
  130. // func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  131. // var count int64
  132. // if params.Query != "%%" && params.State != "" {
  133. // if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  134. // return nil, 0, err
  135. // }
  136. // // 计算查询的偏移量,并设置每次查询的记录数量
  137. // offset := (params.Page - 1) * params.Size
  138. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  139. // 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 {
  140. // return nil, 0, err
  141. // }
  142. // } else if params.State != "" && params.Query == "%%" {
  143. // if err = global.DBLink.Model(apply).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  144. // return nil, 0, err
  145. // }
  146. // // 计算查询的偏移量,并设置每次查询的记录数量
  147. // offset := (params.Page - 1) * params.Size
  148. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  149. // 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 {
  150. // return nil, 0, err
  151. // }
  152. // } else if params.State != "" && params.Query != "%%" {
  153. // if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  154. // return nil, 0, err
  155. // }
  156. // // 计算查询的偏移量,并设置每次查询的记录数量
  157. // offset := (params.Page - 1) * params.Size
  158. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  159. // 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 {
  160. // return nil, 0, err
  161. // }
  162. // } else {
  163. // if err = global.DBLink.Model(apply).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  164. // return nil, 0, err
  165. // }
  166. // // 计算查询的偏移量,并设置每次查询的记录数量
  167. // offset := (params.Page - 1) * params.Size
  168. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  169. // if err = global.DBLink.Where("user_id=?", apply.UserId).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  170. // return nil, 0, err
  171. // }
  172. // }
  173. // return result, count, nil
  174. // }
  175. func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  176. var conditions []interface{}
  177. var whereClauses []string
  178. // 基础条件:按用户ID查询
  179. whereClauses = append(whereClauses, "user_id = ?")
  180. conditions = append(conditions, apply.UserId)
  181. // 添加状态查询条件
  182. if params.State != "" {
  183. whereClauses = append(whereClauses, "state = ?")
  184. conditions = append(conditions, params.State)
  185. }
  186. // 添加自定义查询条件,如果提供且不为默认值
  187. if params.Query != "%%" {
  188. whereClauses = append(whereClauses, queryCond)
  189. conditions = append(conditions, params.Query)
  190. }
  191. // 计算总数
  192. if err = global.DBLink.Model(apply).Where(strings.Join(whereClauses, " AND "), conditions...).Count(&total).Error; err != nil {
  193. return nil, 0, err
  194. }
  195. // 分页查询准备
  196. offset := (params.Page - 1) * params.Size
  197. // 执行分页查询
  198. if err = global.DBLink.Model(apply).
  199. Where(strings.Join(whereClauses, " AND "), conditions...).
  200. Offset(offset).
  201. Limit(params.Size).
  202. Order(params.Desc).
  203. Find(&result).Error; err != nil {
  204. return nil, 0, err
  205. }
  206. return result, total, nil
  207. }
  208. func (a Apply) AddApply(apply Apply) e.Rescode {
  209. //TODO implement me
  210. //默认每一应用有一年免费时间
  211. //time.Parse("2006-01-02 15:04:05", apply.CertificationTime)
  212. tiem := time.Now().AddDate(0, 0, 365)
  213. apply.CertificationTime = utils.Time(tiem)
  214. apply.State = 1
  215. apply.BackgroundImageObscure = 0.5
  216. apply.TopicPC = 1
  217. apply.TopicMobile = 1
  218. tx := global.DBLink.Create(&apply)
  219. if tx.Error != nil {
  220. errMsg := tx.Error.Error()
  221. if strings.Contains(errMsg, "Duplicate entry") {
  222. return e.Repeat
  223. }
  224. }
  225. if tx.RowsAffected > 0 {
  226. return e.SUCCESS
  227. }
  228. return e.INSERTFAIL
  229. }
  230. // AppIdISRepeat 检查id是否重复
  231. func AppIdISRepeat(id string) bool {
  232. tx := global.DBLink.Where("app_id = ?", id).First(&Apply{})
  233. if tx.RowsAffected > 0 {
  234. return true
  235. }
  236. return false
  237. }
  238. // IsRegist 判断是否可以注册
  239. func IsRegist(appid string) bool {
  240. tx := global.DBLink.Model(&Apply{}).
  241. Select("login_mode", "app_id").
  242. Where("app_id=?", appid).
  243. Where("state=?", 1).
  244. Where("login_mode=?", 1)
  245. if tx.RowsAffected > 0 {
  246. return true
  247. }
  248. return false
  249. }
  250. // ApplyAddCap 添加应用能力
  251. func (a Apply) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
  252. var caps Capabilities
  253. tx := global.DBLink.Begin()
  254. if err := tx.Table(a.TableName()).
  255. Where("app_id = ?", applycap.AppID).
  256. Update("caps", applycap.CapID).Error; err != nil {
  257. tx.Rollback()
  258. return e.INSERTFAIL
  259. }
  260. split := strings.Split(applycap.CapID, ",")
  261. for _, v := range split {
  262. if err := tx.Table(caps.TableName()).Where("cap_id = ?", v).
  263. Update("count", gorm.Expr("count + ?", 1)).
  264. Update("updated_at", utils.Time(time.Now())).Error; err != nil {
  265. tx.Rollback()
  266. return e.INSERTFAIL
  267. }
  268. }
  269. if err := tx.Commit().Error; err != nil {
  270. return e.INSERTFAIL
  271. }
  272. return e.SUCCESS
  273. }
  274. func (a Apply) UpDateApplyCap(applycap ApplyCapabilities) e.Rescode {
  275. //TODO implement me
  276. begin := global.DBLink.Begin()
  277. begin.Table(a.TableName()).Where("app_id = ?", applycap.AppID).First(&a)
  278. if a.Caps == applycap.CapID {
  279. }
  280. if err := begin.Table(a.TableName()).
  281. Where("app_id = ?", applycap.AppID).
  282. Update("caps", applycap.CapID).Error; err != nil {
  283. begin.Rollback()
  284. return e.UPDATEFAIL
  285. }
  286. return e.UPDATEFAIL
  287. }