apply.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. IsCollection int `gorm:"type:int;" json:"is_collection"` // 是否收藏 1 代表已收藏
  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, phone string) ([]Apply, error) {
  95. //TODO implement me
  96. var app []Apply
  97. var user User
  98. tx := global.DBLink.
  99. Where("app_name like ?", "%"+appName+"%").
  100. Find(&app)
  101. first := global.DBLink.Table(user.TableName()).Where("phone = ?", phone).First(&user)
  102. if first.Error != nil {
  103. return nil, first.Error
  104. }
  105. colls := make(map[string]bool)
  106. for _, s := range user.Collection {
  107. colls[s] = true
  108. }
  109. for i, _ := range app {
  110. if colls[app[i].AppID] {
  111. app[i].IsCollection = 1
  112. }
  113. }
  114. if tx.Error != nil {
  115. return nil, tx.Error
  116. }
  117. return app, nil
  118. }
  119. func (a Apply) GetApplyById(appid string) (Apply, error) {
  120. //TODO implement me
  121. tx := global.DBLink.
  122. Where("app_id=?", appid).
  123. First(&a)
  124. if tx.Error != nil {
  125. return Apply{}, tx.Error
  126. }
  127. if tx.RowsAffected > 0 {
  128. if a.LoginMethod == nil {
  129. a.LoginMethod = &LoginMethod{}
  130. }
  131. return a, nil
  132. }
  133. return Apply{}, nil
  134. }
  135. func (a Apply) UserUpdateApply(apply Apply) e.Rescode {
  136. //TODO implement me
  137. tx := global.DBLink.Where("id=?", apply.ID).Where("user_id=?", apply.UserId).Updates(&apply)
  138. if tx.Error != nil {
  139. return e.ERROR
  140. }
  141. if tx.RowsAffected > 0 {
  142. return e.SUCCESS
  143. }
  144. return e.ERROR
  145. }
  146. // func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  147. // var count int64
  148. // if params.Query != "%%" && params.State != "" {
  149. // if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  150. // return nil, 0, err
  151. // }
  152. // // 计算查询的偏移量,并设置每次查询的记录数量
  153. // offset := (params.Page - 1) * params.Size
  154. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  155. // 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 {
  156. // return nil, 0, err
  157. // }
  158. // } else if params.State != "" && params.Query == "%%" {
  159. // if err = global.DBLink.Model(apply).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  160. // return nil, 0, err
  161. // }
  162. // // 计算查询的偏移量,并设置每次查询的记录数量
  163. // offset := (params.Page - 1) * params.Size
  164. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  165. // 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 {
  166. // return nil, 0, err
  167. // }
  168. // } else if params.State != "" && params.Query != "%%" {
  169. // if err = global.DBLink.Model(apply).Where(queryCond, params.Query).Where("state=?", params.State).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  170. // return nil, 0, err
  171. // }
  172. // // 计算查询的偏移量,并设置每次查询的记录数量
  173. // offset := (params.Page - 1) * params.Size
  174. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  175. // 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 {
  176. // return nil, 0, err
  177. // }
  178. // } else {
  179. // if err = global.DBLink.Model(apply).Where("user_id=?", apply.UserId).Count(&count).Error; err != nil {
  180. // return nil, 0, err
  181. // }
  182. // // 计算查询的偏移量,并设置每次查询的记录数量
  183. // offset := (params.Page - 1) * params.Size
  184. // // 执行分页查询,包括偏移量设置、限制查询数量、排序及应用额外查询条件
  185. // if err = global.DBLink.Where("user_id=?", apply.UserId).Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Error; err != nil {
  186. // return nil, 0, err
  187. // }
  188. // }
  189. // return result, count, nil
  190. // }
  191. func (a Apply) GetApplyList(params unity.QueryPageParams, apply Apply, queryCond string) (result []Apply, total int64, err error) {
  192. var conditions []interface{}
  193. var whereClauses []string
  194. // 基础条件:按用户ID查询
  195. whereClauses = append(whereClauses, "user_id = ?")
  196. conditions = append(conditions, apply.UserId)
  197. // 添加状态查询条件
  198. if params.State != "" {
  199. whereClauses = append(whereClauses, "state = ?")
  200. conditions = append(conditions, params.State)
  201. }
  202. // 添加自定义查询条件,如果提供且不为默认值
  203. if params.Query != "%%" {
  204. whereClauses = append(whereClauses, queryCond)
  205. conditions = append(conditions, params.Query)
  206. }
  207. // 计算总数
  208. if err = global.DBLink.Model(apply).Where(strings.Join(whereClauses, " AND "), conditions...).Count(&total).Error; err != nil {
  209. return nil, 0, err
  210. }
  211. // 分页查询准备
  212. offset := (params.Page - 1) * params.Size
  213. // 执行分页查询
  214. if err = global.DBLink.Model(apply).
  215. Where(strings.Join(whereClauses, " AND "), conditions...).
  216. Offset(offset).
  217. Limit(params.Size).
  218. Order(params.Desc).
  219. Find(&result).Error; err != nil {
  220. return nil, 0, err
  221. }
  222. for i, _ := range result {
  223. if result[i].LoginMethod == nil {
  224. result[i].LoginMethod = &LoginMethod{}
  225. }
  226. }
  227. return result, total, nil
  228. }
  229. func (a Apply) AddApply(apply Apply) e.Rescode {
  230. //TODO implement me
  231. //设置系统默认状态
  232. tiem := time.Now().AddDate(0, 0, 365)
  233. apply.CertificationTime = utils.Time(tiem)
  234. apply.State = 1
  235. apply.Icon = global.IconSetting.IconPath
  236. if apply.BackgroundImageObscure == nil {
  237. apply.BackgroundImageObscure = new(float32)
  238. *apply.BackgroundImageObscure = 0.5
  239. }
  240. if apply.BackgroundImagePc == nil {
  241. apply.BackgroundImagePc = new(string)
  242. *apply.BackgroundImagePc = global.IconSetting.BackgroundImagePc
  243. }
  244. if apply.BackgroundImageMobile == nil {
  245. apply.BackgroundImageMobile = new(string)
  246. *apply.BackgroundImageMobile = global.IconSetting.BackgroundImageMobile
  247. }
  248. if apply.StartupDiagramPc == nil {
  249. apply.StartupDiagramPc = new(string)
  250. *apply.StartupDiagramPc = global.IconSetting.StartupDiagramPc
  251. }
  252. if apply.StartupDiagramMobile == nil {
  253. apply.StartupDiagramMobile = new(string)
  254. *apply.StartupDiagramMobile = global.IconSetting.StartupDiagramMobile
  255. }
  256. if apply.TopicPC == nil {
  257. apply.TopicPC = new(int)
  258. *apply.TopicPC = 1
  259. }
  260. if apply.TopicMobile == nil {
  261. apply.TopicMobile = new(int)
  262. *apply.TopicMobile = 1
  263. }
  264. tx := global.DBLink.Create(&apply)
  265. if tx.Error != nil {
  266. errMsg := tx.Error.Error()
  267. if strings.Contains(errMsg, "Duplicate entry") {
  268. return e.Repeat
  269. }
  270. }
  271. if tx.RowsAffected > 0 {
  272. return e.SUCCESS
  273. }
  274. return e.INSERTFAIL
  275. }
  276. // AppIdISRepeat 检查id是否重复
  277. func AppIdISRepeat(id string) bool {
  278. tx := global.DBLink.Where("app_id = ?", id).First(&Apply{})
  279. if tx.RowsAffected > 0 {
  280. return true
  281. }
  282. return false
  283. }
  284. // IsRegist 判断是否可以注册
  285. func IsRegist(appid string) bool {
  286. tx := global.DBLink.Model(&Apply{}).
  287. Select("login_mode", "app_id").
  288. Where("app_id=?", appid).
  289. Where("state=?", 1).
  290. Where("login_mode=?", 1)
  291. if tx.RowsAffected > 0 {
  292. return true
  293. }
  294. return false
  295. }
  296. // ApplyAddCap 添加应用能力
  297. func (a Apply) ApplyAddCap(applycap ApplyCapabilities) e.Rescode {
  298. var caps Capabilities
  299. tx := global.DBLink.Begin()
  300. if err := tx.Table(a.TableName()).
  301. Where("app_id = ?", applycap.AppID).
  302. Update("caps", applycap.CapID).Error; err != nil {
  303. tx.Rollback()
  304. return e.INSERTFAIL
  305. }
  306. split := strings.Split(applycap.CapID, ",")
  307. for _, v := range split {
  308. if err := tx.Table(caps.TableName()).Where("cap_id = ?", v).
  309. Update("count", gorm.Expr("count + ?", 1)).
  310. Update("updated_at", utils.Time(time.Now())).Error; err != nil {
  311. tx.Rollback()
  312. return e.INSERTFAIL
  313. }
  314. }
  315. if err := tx.Commit().Error; err != nil {
  316. return e.INSERTFAIL
  317. }
  318. return e.SUCCESS
  319. }
  320. func (a Apply) UpDateApplyCap(applycap ApplyCapabilities) e.Rescode {
  321. //TODO implement me
  322. begin := global.DBLink.Begin()
  323. begin.Table(a.TableName()).Where("app_id = ?", applycap.AppID).First(&a)
  324. if err := begin.Table(a.TableName()).
  325. Where("app_id = ?", applycap.AppID).
  326. Update("caps", applycap.CapID).Error; err != nil {
  327. begin.Rollback()
  328. return e.UPDATEFAIL
  329. }
  330. return e.UPDATEFAIL
  331. }
  332. func (a Apply) CollectionList(params unity.QueryPageParams, apply Apply, phone string) (result []AppDto, total int64, err error) {
  333. //TODO implement me
  334. var user User
  335. query := global.DBLink.Table(apply.TableName())
  336. if err = query.Count(&total).Where("state = ?", 1).Error; err != nil {
  337. return nil, 0, err
  338. }
  339. offset := (params.Page - 1) * params.Size
  340. if err = query.Offset(offset).Limit(params.Size).Order(params.Desc).Find(&result).Where("state = ?", 1).Error; err != nil {
  341. return nil, 0, err
  342. }
  343. first := global.DBLink.Table(user.TableName()).Where("phone = ?", phone).First(&user)
  344. if first.Error != nil {
  345. return nil, 0, err
  346. }
  347. colls := make(map[string]bool)
  348. for _, s := range user.Collection {
  349. colls[s] = true
  350. }
  351. for i, _ := range result {
  352. if colls[result[i].AppID] {
  353. result[i].IsCollection = 1
  354. }
  355. }
  356. return result, total, nil
  357. }
  358. // Check 检查是否过期
  359. func (a Apply) Check() bool {
  360. //TODO implement me
  361. //检查过期时间大于当前时间的应用并且更新状态为3--过期
  362. var applys []Apply
  363. now := utils.Time(time.Now())
  364. tx := global.DBLink.Table(a.TableName()).Where("certification_time < ?", now).Where("state != ?", 3).Find(&applys)
  365. if tx.Error != nil {
  366. return false
  367. }
  368. for i, _ := range applys {
  369. update := tx.Where("certification_time = ?", applys[i].CertificationTime).Update("state", 3)
  370. if update.Error != nil {
  371. return false
  372. }
  373. if update.RowsAffected > 0 {
  374. return true
  375. }
  376. }
  377. return false
  378. }