code.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-09-25 11:48:17
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "github.com/astaxie/beego/orm"
  11. "wuyebaoxiuapi/models"
  12. )
  13. type Code struct {
  14. Id int
  15. Code string
  16. Desc string
  17. Detail string
  18. Status int
  19. CreateId int
  20. UpdateId int
  21. CreateTime int64
  22. UpdateTime int64
  23. }
  24. func (a *Code) TableName() string {
  25. return models.TableName("set_code")
  26. }
  27. func CodeAdd(a *Code) (int64, error) {
  28. return orm.NewOrm().Insert(a)
  29. }
  30. func CodeGetByName(CodeName string) (*Code, error) {
  31. a := new(Code)
  32. err := orm.NewOrm().QueryTable(models.TableName("set_code")).Filter("code", CodeName).One(a)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return a, nil
  37. }
  38. func CodeGetList(page, pageSize int, filters ...interface{}) ([]*Code, int64) {
  39. offset := (page - 1) * pageSize
  40. list := make([]*Code, 0)
  41. query := orm.NewOrm().QueryTable(models.TableName("set_code"))
  42. if len(filters) > 0 {
  43. l := len(filters)
  44. for k := 0; k < l; k += 2 {
  45. query = query.Filter(filters[k].(string), filters[k+1])
  46. }
  47. }
  48. total, _ := query.Count()
  49. query.OrderBy("-id").Limit(pageSize, offset).All(&list)
  50. return list, total
  51. }
  52. func CodeGetByIds(ids string) ([]*Code, error) {
  53. list := make([]*Code, 0)
  54. sql := "SELECT * FROM pp_set_code WHERE id in(" + ids + ")"
  55. orm.NewOrm().Raw(sql).QueryRows(&list)
  56. return list, nil
  57. }
  58. func init() {
  59. orm.RegisterModel(new(Code))
  60. }
  61. func CodeGetById(id int) (*Code, error) {
  62. r := new(Code)
  63. err := orm.NewOrm().QueryTable(models.TableName("set_code")).Filter("id", id).One(r)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return r, nil
  68. }
  69. func (a *Code) Update(fields ...string) error {
  70. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  71. return err
  72. }
  73. return nil
  74. }