image_sources.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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,src_url from image_sources where permission_id = ? "
  59. _, err = o.Raw(sql, permissionId).QueryRows(&items)
  60. return
  61. }
  62. func GetImageById(Id int) (item *ImageSource, err error) {
  63. o := orm.NewOrm()
  64. sql := "select id,src_url from image_sources where id = ?"
  65. err = o.Raw(sql, Id).QueryRow(&item)
  66. return
  67. }
  68. func GetImageCountByCondition(condition string, pars []interface{}) (count int, err error) {
  69. o := orm.NewOrm()
  70. sql := `SELECT COUNT(*) AS count FROM image_sources WHERE 1 =1 AND deleted=0 `
  71. if condition != "" {
  72. sql += condition
  73. }
  74. err = o.Raw(sql, pars).QueryRow(&count)
  75. return
  76. }
  77. func GetImageByCondition(condition string, sortCondition string, pars []interface{}, startPage, pageSize int) (items []*ImageSource, err error) {
  78. o := orm.NewOrm()
  79. sql := `SELECT * FROM image_sources WHERE 1 = 1 AND deleted=0`
  80. if condition != "" {
  81. sql += condition
  82. }
  83. if sortCondition != "" {
  84. sql += sortCondition // 排序
  85. }
  86. sql += ` LIMIT ?, ?`
  87. _, err = o.Raw(sql, pars, startPage, pageSize).QueryRows(&items)
  88. return
  89. }