api_public.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "time"
  11. "github.com/astaxie/beego/orm"
  12. "wuyebaoxiuapi/models"
  13. )
  14. type ApiPublic struct {
  15. Id int
  16. ApiPublicName string
  17. Detail string
  18. Sort int
  19. Status int
  20. CreateId int
  21. UpdateId int
  22. CreateTime int64
  23. UpdateTime int64
  24. }
  25. func (a *ApiPublic) TableName() string {
  26. return models.TableName("api_public")
  27. }
  28. func ApiPublicGetList(page, pageSize int, filters ...interface{}) ([]*ApiPublic, int64) {
  29. offset := (page - 1) * pageSize
  30. list := make([]*ApiPublic, 0)
  31. query := orm.NewOrm().QueryTable(models.TableName("api_public"))
  32. if len(filters) > 0 {
  33. l := len(filters)
  34. for k := 0; k < l; k += 2 {
  35. query = query.Filter(filters[k].(string), filters[k+1])
  36. }
  37. }
  38. total, _ := query.Count()
  39. query.OrderBy("sort").Limit(pageSize, offset).All(&list)
  40. return list, total
  41. }
  42. func ApiPublicAdd(a *ApiPublic) (int64, error) {
  43. return orm.NewOrm().Insert(a)
  44. }
  45. func ApiPublicGetById(id int) (ApiPublic, error) {
  46. var list ApiPublic
  47. query := orm.NewOrm().QueryTable(models.TableName("api_public"))
  48. query.Filter("id", id).Filter("status", 1).One(&list)
  49. return list, nil
  50. }
  51. func ApiPublicGetByIds(ids string) ([]*ApiPublic, error) {
  52. list := make([]*ApiPublic, 0)
  53. sql := "SELECT * FROM pp_api_public WHERE id in(" + ids + ")"
  54. orm.NewOrm().Raw(sql).QueryRows(&list)
  55. return list, nil
  56. }
  57. func init() {
  58. orm.RegisterModel(new(ApiPublic))
  59. }
  60. func (a *ApiPublic) Update(fields ...string) error {
  61. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  62. return err
  63. }
  64. return nil
  65. }
  66. func (a *ApiPublic) Delete(id int64, update_id int) (int64, error) {
  67. sql := "UPDATE pp_api_public SET status=0,update_id=?,update_time=? WHERE id=?"
  68. res, err := orm.NewOrm().Raw(sql, update_id, time.Now().Unix(), id).Exec()
  69. if err == nil {
  70. num, _ := res.RowsAffected()
  71. return num, nil
  72. }
  73. return 0, err
  74. }