provider_device.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package model
  4. import (
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "time"
  8. )
  9. type TProviderDevice struct {
  10. Id int64 `gorm:"column:id" json:"id" form:"id"`
  11. ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"`
  12. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  13. Status int32 `gorm:"column:status" json:"status" form:"status"`
  14. CreatedAt time.Time
  15. UpdatedAt time.Time
  16. }
  17. func (TProviderDevice) TableName() string {
  18. return "t_provider_device"
  19. }
  20. func (p *TProviderDevice) Insert(db *gorm.DB) error {
  21. return db.Create(p).Error
  22. }
  23. func (p *TProviderDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  24. cond, val, err := whereBuild(where)
  25. if err != nil {
  26. return err
  27. }
  28. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  29. }
  30. func (p *TProviderDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  31. cond, val, err := whereBuild(where)
  32. if err != nil {
  33. return err
  34. }
  35. return db.Table(p.TableName()).Table(p.TableName()).Where(cond, val...).Updates(values).Error
  36. }
  37. func (p *TProviderDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  38. cond, val, err := whereBuild(where)
  39. if err != nil {
  40. return err
  41. }
  42. ps := []TProviderDevice{}
  43. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  44. if err != nil {
  45. return err
  46. }
  47. if len(ps) > 0 {
  48. *p = ps[0]
  49. }
  50. return nil
  51. }
  52. func (p *TProviderDevice) Save(db *gorm.DB) error {
  53. return db.Save(p).Error
  54. }
  55. func (p *TProviderDevice) List(db *gorm.DB, where map[string]interface{}) (list []TProviderDevice, err error) {
  56. if len(where) > 0 {
  57. cond, val, err := whereBuild(where)
  58. if err != nil {
  59. return list, err
  60. }
  61. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  62. return list, result.Error
  63. }
  64. result := db.Table(p.TableName()).Find(&list)
  65. return list, result.Error
  66. }
  67. func (p *TProviderDevice) All(db *gorm.DB) (list []TProviderDevice, err error) {
  68. result := db.Table(p.TableName()).Find(&list)
  69. return list, result.Error
  70. }
  71. func (p *TProviderDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  72. if len(where) > 0 {
  73. cond, val, err := whereBuild(where)
  74. if err != nil {
  75. return 0, err
  76. }
  77. ret := int64(0)
  78. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  79. return ret, err
  80. }
  81. ret := int64(0)
  82. err := db.Table(p.TableName()).Count(&ret).Error
  83. return ret, err
  84. }
  85. func (p *TProviderDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  86. cond, val, err := whereBuild(where)
  87. if err != nil {
  88. return err
  89. }
  90. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  91. }
  92. type ListWithProviderItem struct {
  93. Id int64
  94. ProviderName string
  95. SocialCode string
  96. TypeCode int32
  97. Status int32
  98. Comment string
  99. DevelopApproveTime time.Time
  100. ApproveTime time.Time
  101. Feedback string
  102. CreatedAt time.Time
  103. }
  104. func listWithProviderSql(page int32, filter string, filter_status []int32)(string, string, []interface{}) {
  105. offset := (page - 1) *int32(PageSize)
  106. whereArray := []string{}
  107. args := []interface{}{}
  108. sql := "select t1.id, t1.develop_approve_time, t1.approve_time, t1.feedback, t1.comment, t1.device_code as type_code, t1.status, t1.updated_at, t1.created_at, t2.name as provider_name, t2.social_code from t_provider_device as t1 left join t_provider as t2 on t1.provider_id=t2.id "
  109. if filter != "" {
  110. whereArray = append(whereArray, fmt.Sprintf("(t2.name like '%%%s%%' or t2.social_code like '%%%s%%')", filter, filter))
  111. }
  112. if len(filter_status) > 0 {
  113. whereArray = append(whereArray, "t1.status in (?)")
  114. args = append(args, filter_status)
  115. }
  116. where := ""
  117. for _, v := range whereArray {
  118. if where == "" {
  119. where = fmt.Sprintf("where %s", v)
  120. continue
  121. }
  122. where = fmt.Sprintf("%s and %s", where, v)
  123. }
  124. sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset)
  125. countSql := "select count(1) as count from t_provider_device as t1 left join t_provider as t2 on t1.provider_id=t2.id"
  126. countSql = fmt.Sprintf("%s %s ", countSql, where)
  127. return sql, countSql, args
  128. }
  129. func (p *TProviderDevice) ListWithProvider(db *gorm.DB, page int32, filter string, filter_status []int32)([]ListWithProviderItem, int64, error) {
  130. type ResultCount struct {
  131. Count int64
  132. }
  133. sql, countSql, args := listWithProviderSql(page, filter, filter_status)
  134. array := []ResultCount{}
  135. err := db.Raw(countSql, args...).Scan(&array).Error
  136. if err != nil {
  137. return nil, 0, err
  138. }
  139. if len(array) == 0 {
  140. return nil, 0, nil
  141. }
  142. if array[0].Count == 0 {
  143. return nil, 0, nil
  144. }
  145. ret := []ListWithProviderItem{}
  146. err = db.Raw(sql, args...).Scan(&ret).Error
  147. if err != nil {
  148. return nil, array[0].Count, err
  149. }
  150. return ret, array[0].Count, nil
  151. }