image_sources.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. func (i *ImageSource) TableName() string {
  7. return "image_sources"
  8. }
  9. type ImageSource struct {
  10. Id int ` description:"Id"`
  11. ImgName string `description:"图片名称"`
  12. SrcUrl string `description:"图片资源"`
  13. PermissionId int `description:"品种"`
  14. Deleted int `description:"是否删除"`
  15. CreatedTime time.Time `description:"创建时间"`
  16. UpdatedTime time.Time `description:"更新时间"`
  17. }
  18. type ImageSourceView struct {
  19. Id int ` description:"Id"`
  20. ImgName string `description:"图片名称"`
  21. SrcUrl string `description:"图片资源"`
  22. PermissionId int `description:"品种名称"`
  23. PermissionName string `description:"品种名称"`
  24. CreatedTime string `description:"创建时间"`
  25. }
  26. func (i *ImageSource) ToView() (item *ImageSourceView, err error) {
  27. item = &ImageSourceView{
  28. Id: i.Id,
  29. ImgName: i.ImgName,
  30. SrcUrl: i.SrcUrl,
  31. PermissionId: i.PermissionId,
  32. CreatedTime: i.CreatedTime.Format(time.DateTime),
  33. }
  34. name, err := GetPermissionNameById(i.PermissionId)
  35. if err != nil {
  36. return
  37. }
  38. item.PermissionName = name
  39. return
  40. }
  41. func (i *ImageSource) Insert() (insertId int64, err error) {
  42. o := orm.NewOrm()
  43. insertId, err = o.Insert(i)
  44. return
  45. }
  46. func (i *ImageSource) Update() (err error) {
  47. o := orm.NewOrm()
  48. _, err = o.Update(i, "img_name", "src_url", "permission_id")
  49. return
  50. }
  51. func (i *ImageSource) Delete() (err error) {
  52. o := orm.NewOrm()
  53. _, err = o.Update(i, "deleted")
  54. return
  55. }
  56. func GetImageByPermissionId(permissionId int) (items []ImageSource, err error) {
  57. o := orm.NewOrm()
  58. sql := "select id from image_sources where permission_id = ?"
  59. _, err = o.Raw(sql, permissionId).QueryRows(&items)
  60. return
  61. }
  62. func GetImageIdByPermissionId(permissionId int) (items []int, err error) {
  63. o := orm.NewOrm()
  64. sql := "select id from image_sources where permission_id = ?"
  65. _, err = o.Raw(sql, permissionId).QueryRows(&items)
  66. return
  67. }
  68. func GetImageById(Id int) (item *ImageSource, err error) {
  69. o := orm.NewOrm()
  70. sql := "select id from image_sources where id = ?"
  71. err = o.Raw(sql, Id).QueryRow(&item)
  72. return
  73. }
  74. func GetImageCountByCondition(condition string, pars []interface{}) (count int, err error) {
  75. o := orm.NewOrm()
  76. sql := `SELECT COUNT(*) AS count FROM image_sources WHERE 1 =1 AND deleted=0 `
  77. if condition != "" {
  78. sql += condition
  79. }
  80. err = o.Raw(sql, pars).QueryRow(&count)
  81. return
  82. }
  83. func GetImageByCondition(condition string, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*ImageSource, err error) {
  84. o := orm.NewOrm()
  85. sql := `SELECT * FROM image_sources WHERE 1 = 1 AND deleted=0`
  86. if condition != "" {
  87. sql += condition
  88. }
  89. if sortCondition != "" {
  90. sql += sortCondition // 排序
  91. }
  92. sql += ` LIMIT ?, ?`
  93. _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
  94. return
  95. }