device_del_job.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "time"
  8. )
  9. type DeviceDelJob struct {
  10. ID int64 `gorm:"column:ID" json:"id" form:"id"`
  11. CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at" form:"created_at"`
  12. Status int64 `gorm:"column:Status" json:"status" form:"status"`
  13. UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at" form:"updated_at"`
  14. ProjectId int64 `gorm:"column:ProjectId" json:"project_id" form:"project_id"`
  15. Feedback string `gorm:"column:Feedback"`
  16. Reason string `gorm:"column:Reason"`
  17. ProviderId int64 `gorm:"column:ProviderId"`
  18. DeviceId int64 `gorm:"column:DeviceId"`
  19. DeviceCode uint32 `gorm:"column:DeviceCode"`
  20. Sn string `gorm:"column:SN"`
  21. DeviceName string `gorm:"column:DeviceName"`
  22. }
  23. func (DeviceDelJob) TableName() string {
  24. return "DeviceDelJob"
  25. }
  26. func (p *DeviceDelJob) Insert(db *gorm.DB) error {
  27. return db.Table(p.TableName()).Create(p).Error
  28. }
  29. func (p *DeviceDelJob) Del(db *gorm.DB, where map[string]interface{}) error {
  30. cond, val, err := whereBuild(where)
  31. if err != nil {
  32. return err
  33. }
  34. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  35. }
  36. func (p *DeviceDelJob) Find(db *gorm.DB, where map[string]interface{}) error {
  37. cond, val, err := whereBuild(where)
  38. if err != nil {
  39. return err
  40. }
  41. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  42. }
  43. func (p *DeviceDelJob) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  44. cond, val, err := whereBuild(where)
  45. if err != nil {
  46. return err
  47. }
  48. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  49. }
  50. func (p *DeviceDelJob) Save(db *gorm.DB) error {
  51. return db.Table(p.TableName()).Save(p).Error
  52. }
  53. func (p *DeviceDelJob) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  54. if len(where) > 0 {
  55. cond, val, err := whereBuild(where)
  56. if err != nil {
  57. return 0, err
  58. }
  59. ret := int64(0)
  60. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  61. return ret, err
  62. }
  63. ret := int64(0)
  64. err := db.Table(p.TableName()).Count(&ret).Error
  65. return ret, err
  66. }
  67. func (p *DeviceDelJob) List(db *gorm.DB, where map[string]interface{}, page int) (list []DeviceDelJob, err error) {
  68. if len(where) > 0 {
  69. cond, val, err := whereBuild(where)
  70. if err != nil {
  71. return list, err
  72. }
  73. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  74. return list, result.Error
  75. }
  76. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  77. return list, result.Error
  78. }
  79. func (p *DeviceDelJob) All(db *gorm.DB) (list []DeviceDelJob, err error) {
  80. result := db.Table(p.TableName()).Find(&list)
  81. return list, result.Error
  82. }
  83. type DeviceDelJobItem struct {
  84. Id int64
  85. Sn string
  86. TypeCode int32
  87. TypeName string
  88. ProjectName string
  89. ProviderName string
  90. SocialCode string
  91. SafetyRecordNo string
  92. CreatedTime time.Time
  93. Status int32
  94. ApproveTime time.Time
  95. Origin string
  96. Reason string
  97. Feedback string
  98. DeviceName string
  99. Reviewer string
  100. }
  101. type DeviceDelJobRequest struct {
  102. ProjectId int64
  103. ProviderId int64
  104. Filter string
  105. Page int32
  106. PageSize int32
  107. }
  108. func DeviceDelJobListSql(req *DeviceDelJobRequest) (string, string) {
  109. sql := fmt.Sprintf("select t1.ID as id, t1.Reviewer as reviewer, t1.Reason as reason, t1.Feedback as feedback, t1.Status as status, t1.CreatedAt as created_time, t1.UpdatedAt as approve_time, t1.DeviceName as device_name, t1.SN as sn, t1.DeviceCode as type_code, t2.Name as project_name, t2.SafetyNo as safety_record_no, t3.SocialCode as social_code, t3.Name as provider_name "+
  110. " from DeviceDelJob as t1 left join ProjectInfo as t2 on t1.ProjectId= t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  111. countSql := fmt.Sprintf("select count(1) as count "+
  112. " from DeviceDelJob as t1 left join ProjectInfo as t2 on t1.ProjectId= t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  113. where := ""
  114. whereArray := []string{}
  115. if req.ProjectId > 0 {
  116. whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId=%d", req.ProjectId))
  117. }
  118. if req.ProviderId > 0 {
  119. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  120. }
  121. if req.Filter != "" {
  122. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t1.DeviceName like '%%%s%%')", req.Filter, req.Filter))
  123. }
  124. for _, v := range whereArray {
  125. if where == "" {
  126. where = fmt.Sprintf("where %s", v)
  127. continue
  128. }
  129. where = fmt.Sprintf("%s %s and ", where, v)
  130. }
  131. offset := int64(req.Page - 1) *int64(req.PageSize)
  132. sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, req.PageSize, offset)
  133. countSql = fmt.Sprintf("%s %s", countSql, where)
  134. return sql, countSql
  135. }
  136. type CountItem struct {
  137. Count int64
  138. }
  139. func DeviceDelJobList(db *gorm.DB, req*DeviceDelJobRequest) ([]DeviceDelJobItem, int64, error) {
  140. sql, countSql := DeviceDelJobListSql(req)
  141. array := []CountItem{}
  142. err := db.Raw(countSql).Scan(&array).Error
  143. if err != nil {
  144. return nil, 0, err
  145. }
  146. total := int64(0)
  147. if len(array) > 0 {
  148. total = array[0].Count
  149. }
  150. if total == 0 {
  151. return nil, 0, nil
  152. }
  153. list := []DeviceDelJobItem{}
  154. err = db.Raw(sql).Scan(&list).Error
  155. if err != nil {
  156. return nil, 0, err
  157. }
  158. return list, total, nil
  159. }