api_detail.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**********************************************
  2. ** @Des: This file ...
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-16 15:42:43
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-10-09 11:48:17
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "github.com/astaxie/beego/orm"
  11. "wuyebaoxiuapi/models"
  12. )
  13. type ApiDetail struct {
  14. Id int
  15. SourceId int
  16. Method int
  17. ApiName string
  18. ApiUrl string
  19. Detail string
  20. Status int
  21. CreateId int
  22. AuditId int
  23. UpdateId int
  24. CreateTime int64
  25. UpdateTime int64
  26. AuditTime int64
  27. }
  28. type ApiDetails struct {
  29. Id int
  30. SourceId int
  31. Method int
  32. ApiName string
  33. ApiUrl string
  34. Detail string
  35. Status int
  36. CreateId int
  37. AuditId int
  38. UpdateId int
  39. CreateTime int64
  40. UpdateTime int64
  41. AuditTime int64
  42. CreateName string
  43. UpdateName string
  44. AuditName string
  45. }
  46. type ApiSourceList struct {
  47. Id int
  48. SourceName string
  49. ApiLists []*ApiList
  50. }
  51. type ApiList struct {
  52. Id int
  53. Method int
  54. ApiName string
  55. }
  56. func init() {
  57. orm.RegisterModel(new(ApiDetail))
  58. }
  59. func ApiTreeData(groupId int) ([]*ApiSourceList, error) {
  60. list := make([]*ApiSourceList, 0)
  61. sql := "SELECT id,source_name FROM pp_api_source WHERE group_id=?"
  62. orm.NewOrm().Raw(sql, groupId).QueryRows(&list)
  63. apiList := make([]*ApiSourceList, 0)
  64. for _, apisource := range list {
  65. detailList := make([]*ApiList, 0)
  66. detail_sql := "SELECT id,method,api_name FROM pp_api_detail WHERE source_id=? AND status=3"
  67. orm.NewOrm().Raw(detail_sql, apisource.Id).QueryRows(&detailList)
  68. apisource.ApiLists = detailList
  69. apiList = append(apiList, apisource)
  70. }
  71. return apiList, nil
  72. }
  73. func (a *ApiDetail) TableName() string {
  74. return models.TableName("api_detail")
  75. }
  76. func ApiDetailAdd(a *ApiDetail) (int64, error) {
  77. return orm.NewOrm().Insert(a)
  78. }
  79. func ApiDetailGetById(id int) (*ApiDetail, error) {
  80. r := new(ApiDetail)
  81. err := orm.NewOrm().QueryTable(models.TableName("api_detail")).Filter("id", id).One(r)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return r, nil
  86. }
  87. func ApiFullDetailById(id int) (detail *ApiDetails, err error) {
  88. sql := "SELECT pp_api_detail.*,a.real_name as create_name,b.real_name as update_name,c.real_name as audit_name FROM pp_api_detail LEFT JOIN pp_uc_admin as a ON pp_api_detail.create_id=a.id LEFT JOIN pp_uc_admin as b ON pp_api_detail.update_id=b.id LEFT JOIN pp_uc_admin as c ON pp_api_detail.audit_id=c.id WHERE pp_api_detail.id=?"
  89. err = orm.NewOrm().Raw(sql, id).QueryRow(&detail)
  90. return
  91. }
  92. func ApiDetailsGetById(id int) ([]*ApiDetails, error) {
  93. list := make([]*ApiDetails, 0)
  94. sql := "SELECT pp_api_detail.*,a.real_name as create_name,b.real_name as update_name,c.real_name as audit_name FROM pp_api_detail LEFT JOIN pp_uc_admin as a ON pp_api_detail.create_id=a.id LEFT JOIN pp_uc_admin as b ON pp_api_detail.update_id=b.id LEFT JOIN pp_uc_admin as c ON pp_api_detail.audit_id=c.id WHERE pp_api_detail.source_id=?"
  95. orm.NewOrm().Raw(sql, id).QueryRows(&list)
  96. return list, nil
  97. }
  98. func ApiDetailGetList(page, pageSize int, filters ...interface{}) ([]*ApiDetail, int64) {
  99. offset := (page - 1) * pageSize
  100. list := make([]*ApiDetail, 0)
  101. query := orm.NewOrm().QueryTable(models.TableName("api_detail"))
  102. if len(filters) > 0 {
  103. l := len(filters)
  104. for k := 0; k < l; k += 2 {
  105. query = query.Filter(filters[k].(string), filters[k+1])
  106. }
  107. }
  108. total, _ := query.Count()
  109. query.OrderBy("-id").Limit(pageSize, offset).All(&list)
  110. return list, total
  111. }
  112. func (a *ApiDetail) Update(fields ...string) error {
  113. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. func ApiChangeStatus(ids string, status int) (num int64, err error) {
  119. sql := "UPDATE pp_api_detail set status=? where id in (" + ids + ")"
  120. res, err := orm.NewOrm().Raw(sql, status).Exec()
  121. num = 0
  122. if err == nil {
  123. num, _ = res.RowsAffected()
  124. }
  125. return num, err
  126. }