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