template.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**********************************************
  2. ** @Des: markdown模板
  3. ** @Author: haodaquan
  4. ** @Date: 2018-01-16 15:42:43
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2018-01-16 11:48:17
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "time"
  11. "github.com/astaxie/beego/orm"
  12. "wuyebaoxiuapi/models"
  13. )
  14. type Template struct {
  15. Id int
  16. TemplateName string
  17. Detail string
  18. Status int
  19. CreateId int
  20. UpdateId int
  21. CreateTime int64
  22. UpdateTime int64
  23. }
  24. func (a *Template) TableName() string {
  25. return models.TableName("set_template")
  26. }
  27. func TemplateGetList(page, pageSize int, filters ...interface{}) ([]*Template, int64) {
  28. offset := (page - 1) * pageSize
  29. list := make([]*Template, 0)
  30. query := orm.NewOrm().QueryTable(models.TableName("set_template"))
  31. if len(filters) > 0 {
  32. l := len(filters)
  33. for k := 0; k < l; k += 2 {
  34. query = query.Filter(filters[k].(string), filters[k+1])
  35. }
  36. }
  37. total, _ := query.Count()
  38. query.OrderBy("-id").Limit(pageSize, offset).All(&list)
  39. return list, total
  40. }
  41. func TemplateAdd(a *Template) (int64, error) {
  42. return orm.NewOrm().Insert(a)
  43. }
  44. func TemplateGetById(id int) (Template, error) {
  45. var list Template
  46. query := orm.NewOrm().QueryTable(models.TableName("set_template"))
  47. query.Filter("id", id).Filter("status", 1).One(&list)
  48. return list, nil
  49. }
  50. func init() {
  51. orm.RegisterModel(new(Template))
  52. }
  53. func (a *Template) Update(fields ...string) error {
  54. if _, err := orm.NewOrm().Update(a, fields...); err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. func (a *Template) Delete(id int64, update_id int) (int64, error) {
  60. sql := "UPDATE pp_set_template SET status=0,update_id=?,update_time=? WHERE id=?"
  61. res, err := orm.NewOrm().Raw(sql, update_id, time.Now().Unix(), id).Exec()
  62. if err == nil {
  63. num, _ := res.RowsAffected()
  64. return num, nil
  65. }
  66. return 0, err
  67. }