sy_series.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package model
  2. import (
  3. "git.getensh.com/common/gopkgsv2/database"
  4. "gorm.io/gorm"
  5. )
  6. type SySeriesModel interface {
  7. MakerList(db *gorm.DB) ([]Maker, error)
  8. Update(db *gorm.DB, values interface{}) error
  9. Count(db *gorm.DB, join bool) (int64, error)
  10. List(db *gorm.DB, pagination *Pagination, join bool) ([]SeriesAndBrandName, error)
  11. Get(db *gorm.DB) (*DipSySeries, error)
  12. SeriesList(db *gorm.DB) ([]DipSySeries, error)
  13. }
  14. type DipSySeries struct {
  15. ID int64 `gorm:"column:id" json:"id" form:"id"`
  16. SeriesId string `gorm:"column:series_id" json:"series_id" form:"series_id"`
  17. BrandId string `gorm:"column:brand_id" json:"brand_id" form:"brand_id"`
  18. SeriesName string `gorm:"column:series_name" json:"series_name" form:"series_name"`
  19. Maker string `gorm:"column:maker" json:"maker" form:"maker"`
  20. MakerId string `gorm:"column:maker_id" json:"maker_id" form:"maker_id"`
  21. LowestPrice string `gorm:"column:lowest_price" json:"lowest_price" form:"lowest_price"`
  22. HighestPrice string `gorm:"column:highest_price" json:"highest_price" form:"highest_price"`
  23. HasImg int64 `gorm:"column:has_img" json:"has_img" form:"has_img"`
  24. UpdateTime string `gorm:"column:update_time" json:"update_time" form:"update_time"`
  25. Status int64 `gorm:"column:status" json:"status"`
  26. }
  27. type SeriesAndBrandName struct {
  28. ID int64 `gorm:"column:id" json:"id" form:"id"`
  29. SeriesId string `gorm:"column:series_id" json:"series_id" form:"series_id"`
  30. SeriesName string `gorm:"column:series_name" json:"series_name" form:"series_name"`
  31. BrandId string `gorm:"column:brand_id" json:"brand_id" form:"brand_id"`
  32. BrandName string `gorm:"column:brand_name" json:"brand_name" form:"brand_name"`
  33. Maker string `gorm:"column:maker" json:"maker" form:"maker"`
  34. HasImg int64 `gorm:"column:has_img" json:"has_img" form:"has_img"`
  35. Status int64 `gorm:"column:status" json:"status"`
  36. }
  37. type Maker struct {
  38. BrandId string `gorm:"column:brand_id" json:"brand_id" form:"brand_id"`
  39. Maker string `gorm:"column:maker" json:"maker" form:"maker"`
  40. MakerId string `gorm:"column:maker_id" json:"maker_id" form:"maker_id"`
  41. }
  42. type defalutSySerieModel struct {
  43. tableName string
  44. fields string
  45. }
  46. func NewSySerieModel() SySeriesModel {
  47. return &defalutSySerieModel{
  48. tableName: "db_adm_ads.t_adm_ads4",
  49. fields: "id, series_id,series_name, brand_id, maker, maker_id, lowest_price, highest_price, has_img, update_time",
  50. }
  51. }
  52. func (d *defalutSySerieModel) MakerList(db *gorm.DB) ([]Maker, error) {
  53. var res []Maker
  54. err := database.List(db, &res, database.Option{
  55. TableName: d.tableName,
  56. Fields: "brand_id, maker, maker_id",
  57. Group: "maker",
  58. })
  59. return res, err
  60. }
  61. func (d *defalutSySerieModel) Update(db *gorm.DB, values interface{}) error {
  62. return database.Update(db, values, database.Option{
  63. TableName: d.tableName,
  64. })
  65. }
  66. func (d *defalutSySerieModel) List(db *gorm.DB, pagination *Pagination, join bool) ([]SeriesAndBrandName, error) {
  67. var res []SeriesAndBrandName
  68. option := database.Option{
  69. TableName: d.tableName + " AS t1",
  70. Joins: []string{"LEFT JOIN db_adm_ads.t_adm_ads2 AS t2 ON t1.brand_id = t2.brand_id"},
  71. Fields: "t1.id, t1.series_id, t1.series_name, t1.maker, t1.status, t1.has_img, t2.brand_name, t1.brand_id",
  72. Limit: pagination.Limit,
  73. OffSet: pagination.Offset,
  74. }
  75. if join {
  76. option.Joins = []string{}
  77. }
  78. err := database.List(db, &res, option)
  79. return res, err
  80. }
  81. func (d *defalutSySerieModel) Count(db *gorm.DB, join bool) (int64, error) {
  82. option := database.Option{
  83. TableName: d.tableName + " AS t1",
  84. }
  85. if join {
  86. option.Joins = []string{"LEFT JOIN db_adm_ads.t_adm_ads2 AS t2 ON t1.brand_id = t2.brand_id"}
  87. }
  88. return database.Count(db, option)
  89. }
  90. func (d *defalutSySerieModel) Get(db *gorm.DB) (*DipSySeries, error) {
  91. var res DipSySeries
  92. err := database.Get(db, &res, database.Option{
  93. TableName: d.tableName,
  94. Fields: d.fields,
  95. })
  96. return &res, err
  97. }
  98. func (d *defalutSySerieModel) SeriesList(db *gorm.DB) ([]DipSySeries, error) {
  99. var res []DipSySeries
  100. err := database.List(db, &res, database.Option{
  101. TableName: d.tableName,
  102. Fields: d.fields,
  103. })
  104. return res, err
  105. }