project_job.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. "github.com/jinzhu/gorm"
  6. "time"
  7. "fmt"
  8. )
  9. type TProjectJob struct {
  10. Id int64 `gorm:"column:id" json:"id" form:"id"`
  11. Type int64 `gorm:"column:type" json:"type" form:"type"`
  12. Origin string `gorm:"column:origin" json:"origin" form:"origin"`
  13. Content string `gorm:"column:content" json:"content" form:"content"`
  14. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  15. Status int64 `gorm:"column:status" json:"status" form:"status"`
  16. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  17. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  18. Feedback string `json:"feedback"`
  19. Reason string `json:"reason"`
  20. ProviderId int64 `json:"provider_id"`
  21. DeviceId int64 `json:"device_id"`
  22. }
  23. func (TProjectJob) TableName() string {
  24. return "t_project_job"
  25. }
  26. func (p *TProjectJob) Insert(db *gorm.DB) error {
  27. return db.Create(p).Error
  28. }
  29. func (p *TProjectJob) 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 *TProjectJob) 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 *TProjectJob) 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 *TProjectJob) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  51. cond, val, err := whereBuild(where)
  52. if err != nil {
  53. return err
  54. }
  55. ps := []TProjectJob{}
  56. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  57. if err != nil {
  58. return err
  59. }
  60. if len(ps) > 0 {
  61. *p = ps[0]
  62. }
  63. return nil
  64. }
  65. func (p *TProjectJob) Save(db *gorm.DB) error {
  66. return db.Save(p).Error
  67. }
  68. func (p *TProjectJob) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  69. if len(where) > 0 {
  70. cond, val, err := whereBuild(where)
  71. if err != nil {
  72. return 0, err
  73. }
  74. ret := int64(0)
  75. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  76. return ret, err
  77. }
  78. ret := int64(0)
  79. err := db.Table(p.TableName()).Count(&ret).Error
  80. return ret, err
  81. }
  82. func (p *TProjectJob) List(db *gorm.DB, where map[string]interface{}, page int32) (list []TProjectJob, err error) {
  83. offset := (page - 1)*PageSize
  84. if len(where) > 0 {
  85. cond, val, err := whereBuild(where)
  86. if err != nil {
  87. return list, err
  88. }
  89. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(offset).Find(&list)
  90. return list, result.Error
  91. }
  92. result := db.Table(p.TableName()).Limit(10).Offset(offset).Find(&list)
  93. return list, result.Error
  94. }
  95. func (p *TProjectJob) All(db *gorm.DB) (list []TProjectJob, err error) {
  96. result := db.Table(p.TableName()).Find(&list)
  97. return list, result.Error
  98. }
  99. type ProjectChangeItem struct {
  100. Origin string
  101. Content string
  102. Status int32
  103. ApplyTime time.Time
  104. ApproveTime time.Time
  105. ProjectName string
  106. SafetyRecordNo string
  107. Reason string
  108. Feedback string
  109. SocialCode string
  110. Addr string
  111. Id int64
  112. CompanyName string
  113. }
  114. func projectChangeListSql(page int32, filter string, filterStatus []int32)(string, string, []interface{}) {
  115. offset := (page - 1) *PageSize
  116. args := []interface{}{}
  117. sql := "select t1.id, t1.origin, t1.content, t1.status, t1.created_at as apply_time, t1.updated_at as approve_time, t1.reason, t1.feedback, t2.name as project_name, t3.social_code, t3.name as company_name, t2.safety_record_no, t2.location as addr "+
  118. " from t_project_job as t1 left join t_project as t2 on t1.project_id=t2.id and t1.type = 2 left join t_company as t3 on t2.cid = t3.id "
  119. countSql := "select count(1) as count "+
  120. " from t_project_job as t1 left join t_project as t2 on t1.project_id=t2.id and t1.type = 2 left join t_company as t3 on t2.cid = t3.id "
  121. whereArray := []string{}
  122. whereArray = append(whereArray, fmt.Sprintf("t1.type=2"))
  123. if filter != "" {
  124. whereArray = append(whereArray, fmt.Sprintf("(t2.name like '%%%s%%' or t3.social_code like '%%%s%%' or t2.safety_record_no like '%%%s%%')", filter, filter,filter))
  125. }
  126. if len(filterStatus) > 0 {
  127. args = append(args, filterStatus)
  128. whereArray = append(whereArray, fmt.Sprintf("t1.status in(?)"))
  129. }
  130. where := ""
  131. for _, v := range whereArray {
  132. if where == "" {
  133. where = fmt.Sprintf("where %s", v)
  134. continue
  135. }
  136. where = fmt.Sprintf("%s and %s", where, v)
  137. }
  138. sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, PageSize, offset)
  139. countSql = fmt.Sprintf("%s %s", countSql, where)
  140. return sql, countSql, args
  141. }
  142. func (p *TProjectJob)ProjectChangeList(db *gorm.DB, page int32, filter string, filterStatus []int32)([]ProjectChangeItem, int64, error) {
  143. type ResultCount struct {
  144. Count int64
  145. }
  146. sql, countSql, args := projectChangeListSql(page, filter, filterStatus)
  147. array := []ResultCount{}
  148. err := db.Raw(countSql, args...).Scan(&array).Error
  149. if err != nil {
  150. return nil, 0, err
  151. }
  152. if len(array) == 0 {
  153. return nil, 0, nil
  154. }
  155. if array[0].Count == 0 {
  156. return nil, 0, nil
  157. }
  158. ret := []ProjectChangeItem{}
  159. err = db.Raw(sql, args...).Scan(&ret).Error
  160. if err != nil {
  161. return nil, array[0].Count, err
  162. }
  163. return ret, array[0].Count, nil
  164. }