apply.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "log"
  8. "project_management/app/e"
  9. "project_management/global"
  10. "project_management/unity"
  11. "project_management/utils"
  12. "strings"
  13. "time"
  14. )
  15. type Apply struct {
  16. //gorm.Model
  17. utils.BaseModel
  18. AppID string `gorm:"type:varchar(50);not null;unique" json:"app_id"` // 应用id
  19. UserId int `gorm:"type:int;" json:"user_id"` // 用户id
  20. UserName string `gorm:"type:varchar(50);" json:"user_name"` // 用户名
  21. AppName string `gorm:"type:varchar(50);index:idx_name,unique" json:"app_name" validate:"required" min:"3" max:"20"` // 应用名称
  22. AppDescription string `gorm:"type:varchar(255);" json:"app_description" validate:"required"` // 应用描述
  23. CertificationTime utils.Time `gorm:"type:datetime;" json:"certification_time"` // 认证到期时间
  24. State int `gorm:"type:int;" json:"state"` // 状态 1 正常 2 停用 3 过期 4 禁用
  25. Icon string `gorm:"type:varchar(255);" json:"icon"` // 应用图标
  26. StartupDiagramPc *string `gorm:"type:varchar(255);" json:"startup_diagram_pc"` // 启动图pc
  27. StartupDiagramMobile *string `gorm:"type:varchar(255);" json:"startup_diagram_mobile"` // 启动图移动
  28. LoginMode *int `gorm:"type:int;" json:"login_mode"` // 注册模式 1 公开注册 2禁止注册
  29. LoginMethod *LoginMethod `gorm:"type:json" json:"login_method"` // 登录方式 1 短信 2 微信登录
  30. BackgroundImagePc *string `gorm:"type:varchar(255);" json:"background_image_pc"` // 背景图pc
  31. BackgroundImageMobile *string `gorm:"type:varchar(255);" json:"background_image_mobile"` // 背景图移动
  32. BackgroundImageObscure *float32 `gorm:"type:double;" json:"background_image_obscure"` // 背景图模糊度
  33. TopicPC *int `gorm:"type:int;" json:"topic_pc"` // 主题pc
  34. TopicMobile *int `gorm:"type:int;" json:"topic_mobile"` // 主题移动
  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. if a.LoginMethod == nil {
  116. a.LoginMethod = &LoginMethod{}
  117. }
  118. return a, nil
  119. }
  120. return Apply{}, nil
  121. }
  122. func (a Apply) UserUpdateApply(apply Apply) e.Rescode {
  123. //TODO implement me
  124. log.Print(apply)
  125. tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply)
  126. if tx.Error != nil {
  127. return e.ERROR
  128. }
  129. if tx.RowsAffected > 0 {
  130. return e.SUCCESS
  131. }
  132. return e.ERROR
  133. }
  134. // func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  135. // var count int64
  136. // if params.Query != "%%" && params.State != "" {
  137. // if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  138. // return nil, 0, err
  139. // }
  140. // // 计算查询的偏移量,并设置每次查询的记录数量
  141. // offset := (params.Page - 1) * params.Size
  142. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  143. // 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 {
  144. // return nil, 0, err
  145. // }
  146. // } else if params.State != "" && params.Query == "%%" {
  147. // if err = global.DBLink.Model(apply).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  148. // return nil, 0, err
  149. // }
  150. // // 计算查询的偏移量,并设置每次查询的记录数量
  151. // offset := (params.Page - 1) * params.Size
  152. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  153. // 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 {
  154. // return nil, 0, err
  155. // }
  156. // } else if params.State != "" && params.Query != "%%" {
  157. // if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  158. // return nil, 0, err
  159. // }
  160. // // 计算查询的偏移量,并设置每次查询的记录数量
  161. // offset := (params.Page - 1) * params.Size
  162. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  163. // 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 {
  164. // return nil, 0, err
  165. // }
  166. // } else {
  167. // if err = global.DBLink.Model(apply).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  168. // return nil, 0, err
  169. // }
  170. // // 计算查询的偏移量,并设置每次查询的记录数量
  171. // offset := (params.Page - 1) * params.Size
  172. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  173. // if err = global.DBLink.Where("user_id=?", apply.UserId).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  174. // return nil, 0, err
  175. // }
  176. // }
  177. // return result, count, nil
  178. // }
  179. func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  180. var conditions []interface{}
  181. var whereClauses []string
  182. // 基础条件:按用户ID查询
  183. whereClauses = append(whereClauses, "user_id = ?")
  184. conditions = append(conditions, apply.UserId)
  185. // 添加状态查询条件
  186. if params.State != "" {
  187. whereClauses = append(whereClauses, "state = ?")
  188. conditions = append(conditions, params.State)
  189. }
  190. // 添加自定义查询条件,如果提供且不为默认值
  191. if params.Query != "%%" {
  192. whereClauses = append(whereClauses, queryCond)
  193. conditions = append(conditions, params.Query)
  194. }
  195. // 计算总数
  196. if err = global.DBLink.Model(apply).Where(strings.Join(whereClauses, " AND "), conditions...).Count(&total).Error; err != nil {
  197. return nil, 0, err
  198. }
  199. // 分页查询准备
  200. offset := (params.Page - 1) * params.Size
  201. // 执行分页查询
  202. if err = global.DBLink.Model(apply).
  203. Where(strings.Join(whereClauses, " AND "), conditions...).
  204. Offset(offset).
  205. Limit(params.Size).
  206. Order(params.Desc).
  207. Find(&result).Error; err != nil {
  208. return nil, 0, err
  209. }
  210. for i, _ := range result {
  211. if result[i].LoginMethod == nil {
  212. result[i].LoginMethod = &LoginMethod{}
  213. }
  214. }
  215. return result, total, nil
  216. }
  217. func (a Apply) AddApply(apply Apply) e.Rescode {
  218. //TODO implement me
  219. //设置系统默认状态
  220. tiem := time.Now().AddDate(0, 0, 365)
  221. apply.CertificationTime = utils.Time(tiem)
  222. apply.State = 1
  223. apply.Icon = global.IconSetting.IconPath
  224. *apply.BackgroundImageObscure = 0.5
  225. *apply.TopicPC = 1
  226. *apply.TopicMobile = 1
  227. tx := global.DBLink.Create(&apply)
  228. if tx.Error != nil {
  229. errMsg := tx.Error.Error()
  230. if strings.Contains(errMsg, "Duplicate entry") {
  231. return e.Repeat
  232. }
  233. }
  234. if tx.RowsAffected > 0 {
  235. return e.SUCCESS
  236. }
  237. return e.INSERTFAIL
  238. }
  239. // AppIdISRepeat 检查id是否重复
  240. func AppIdISRepeat(id string) bool {
  241. tx := global.DBLink.Where("app_id = ?", id).First(&Apply{})
  242. if tx.RowsAffected > 0 {
  243. return true
  244. }
  245. return false
  246. }
  247. // IsRegist 判断是否可以注册
  248. func IsRegist(appid string) bool {
  249. tx := global.DBLink.Model(&Apply{}).
  250. Select("login_mode", "app_id").
  251. Where("app_id=?", appid).
  252. Where("state=?", 1).
  253. Where("login_mode=?", 1)
  254. if tx.RowsAffected > 0 {
  255. return true
  256. }
  257. return false
  258. }
  259. // ApplyAddCap 添加应用能力
  260. func (a Apply) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
  261. var caps Capabilities
  262. tx := global.DBLink.Begin()
  263. if err := tx.Table(a.TableName()).
  264. Where("app_id = ?", applycap.AppID).
  265. Update("caps", applycap.CapID).Error; err != nil {
  266. tx.Rollback()
  267. return e.INSERTFAIL
  268. }
  269. split := strings.Split(applycap.CapID, ",")
  270. for _, v := range split {
  271. if err := tx.Table(caps.TableName()).Where("cap_id = ?", v).
  272. Update("count", gorm.Expr("count + ?", 1)).
  273. Update("updated_at", utils.Time(time.Now())).Error; err != nil {
  274. tx.Rollback()
  275. return e.INSERTFAIL
  276. }
  277. }
  278. if err := tx.Commit().Error; err != nil {
  279. return e.INSERTFAIL
  280. }
  281. return e.SUCCESS
  282. }
  283. func (a Apply) UpDateApplyCap(applycap ApplyCapabilities) e.Rescode {
  284. //TODO implement me
  285. begin := global.DBLink.Begin()
  286. begin.Table(a.TableName()).Where("app_id = ?", applycap.AppID).First(&a)
  287. if err := begin.Table(a.TableName()).
  288. Where("app_id = ?", applycap.AppID).
  289. Update("caps", applycap.CapID).Error; err != nil {
  290. begin.Rollback()
  291. return e.UPDATEFAIL
  292. }
  293. return e.UPDATEFAIL
  294. }