lift_device.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. "smart-supplier-management/consts"
  8. "time"
  9. )
  10. type LiftDeviceInfo struct {
  11. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"ID"`
  12. Code string `gorm:"column:Code" json:"code"`
  13. Name string `gorm:"column:Name" json:"name"`
  14. DeviceId string `gorm:"column:DeviceId" json:"deviceid"`
  15. DeviceModel string `gorm:"column:DeviceModel" json:"devicemodel"`
  16. DeviceName string `gorm:"column:DeviceName" json:"devicename"`
  17. Manufacturer string `gorm:"column:Manufacturer" json:"manufacturer"`
  18. Batch string `gorm:"column:Batch" json:"batch"`
  19. SN string `gorm:"column:SN" json:"sn"`
  20. DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"`
  21. Unit string `gorm:"column:Unit" json:"unit"`
  22. Person string `gorm:"column:Person" json:"person"`
  23. Phone string `gorm:"column:phone" json:"phone"`
  24. Remark string `gorm:"column:remark" json:"remark"`
  25. Longitude string `gorm:"column:Longitude" json:"longitude"`
  26. Latitude string `gorm:"column:Latitude" json:"latitude"`
  27. VerifyTime time.Time `gorm:"column:VerifyTime"`
  28. VerifyStatus int `gorm:"column:VerifyStatus"`
  29. Key string `gorm:"column:Key"`
  30. ProjectId int64 `gorm:"column:ProjectId"`
  31. ProviderId int64 `gorm:"column:ProviderId"`
  32. CreatedAt time.Time `gorm:"column:CreatedAt"`
  33. ChannelId int64 `gorm:"column:ChannelId"`
  34. }
  35. func (LiftDeviceInfo) TableName() string {
  36. return "db_smart_v2.LiftDeviceInfo"
  37. }
  38. func (p *LiftDeviceInfo) Insert(db *gorm.DB) error {
  39. return db.Table(p.TableName()).Create(p).Error
  40. }
  41. func (p *LiftDeviceInfo) Del(db *gorm.DB, where map[string]interface{}) error {
  42. cond, val, err := whereBuild(where)
  43. if err != nil {
  44. return err
  45. }
  46. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  47. }
  48. func (p *LiftDeviceInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  49. cond, val, err := whereBuild(where)
  50. if err != nil {
  51. return err
  52. }
  53. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  54. }
  55. func (p *LiftDeviceInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  56. cond, val, err := whereBuild(where)
  57. if err != nil {
  58. return err
  59. }
  60. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  61. }
  62. func (p *LiftDeviceInfo) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  63. cond, val, err := whereBuild(where)
  64. if err != nil {
  65. return err
  66. }
  67. ps := []LiftDeviceInfo{}
  68. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  69. if err != nil {
  70. return err
  71. }
  72. if len(ps) > 0 {
  73. *p = ps[0]
  74. }
  75. return nil
  76. }
  77. func (p *LiftDeviceInfo) Save(db *gorm.DB) error {
  78. return db.Save(p).Error
  79. }
  80. func (p *LiftDeviceInfo) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  81. if len(where) > 0 {
  82. cond, val, err := whereBuild(where)
  83. if err != nil {
  84. return 0, err
  85. }
  86. ret := int64(0)
  87. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  88. return ret, err
  89. }
  90. ret := int64(0)
  91. err := db.Table(p.TableName()).Count(&ret).Error
  92. return ret, err
  93. }
  94. func (p *LiftDeviceInfo) List(db *gorm.DB, where map[string]interface{}, page int) (list []LiftDeviceInfo, err error) {
  95. if len(where) > 0 {
  96. cond, val, err := whereBuild(where)
  97. if err != nil {
  98. return list, err
  99. }
  100. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  101. return list, result.Error
  102. }
  103. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  104. return list, result.Error
  105. }
  106. func (p *LiftDeviceInfo) All(db *gorm.DB) (list []LiftDeviceInfo, err error) {
  107. result := db.Table(p.TableName()).Find(&list)
  108. return list, result.Error
  109. }
  110. type LiftDeviceItem struct {
  111. Id int64
  112. Sn string
  113. TypeCode int32
  114. TypeName string
  115. ProjectName string
  116. ProjectId int64
  117. SafetyRecordNo string
  118. CreatedTime time.Time
  119. Status int32
  120. State int32
  121. ProjectApproveTime time.Time
  122. Key string
  123. ProviderName string
  124. SocialCode string
  125. Name string
  126. Manufacturer string
  127. Batch string
  128. Person string
  129. Phone string
  130. Unit string
  131. Lon string
  132. Lat string
  133. Model string
  134. }
  135. func LiftDeviceListSql(req LiftDeviceListRequest)(string, string, []interface{}) {
  136. sql := fmt.Sprintf("select t1.ID as id, t1.DeviceModel as model, t1.Batch as batch, t1.Person as person, t1.Phone as phone, t1.Manufacturer as manufacturer, t1.Unit as unit, t1.Longitude as lon, t1.Latitude as lat, t1.Key as 'key', t1.DeviceName as name, t1.SN as sn, t1.VerifyTime as project_approve_time, "+
  137. "t1.CreatedAt as created_time, t1.DeviceState as state, t1.VerifyStatus as status, t2.SafetyNo as safety_record_no, t2.Name as project_name, t3.Name as provider_name, t3.SocialCode as social_code from LiftDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  138. countSql := fmt.Sprintf("select count(1) as count from LiftDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID ")
  139. args := []interface{}{}
  140. whereArray := []string{}
  141. if req.ProviderId > 0 {
  142. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  143. }
  144. if req.Filter != "" {
  145. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t2.SafetyNo like '%%%s%%' or t2.Name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  146. }
  147. if req.CanDel {
  148. req.IsAll = true
  149. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  150. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  151. }
  152. if len(req.StatusFilter) > 0 {
  153. args = append(args, req.StatusFilter)
  154. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  155. }
  156. where := ""
  157. for _, v := range whereArray {
  158. if where == "" {
  159. where = fmt.Sprintf(" where %s", v)
  160. continue
  161. }
  162. where = fmt.Sprintf("%s and %s", where, v)
  163. }
  164. offset := (req.Page - 1) *int32(PageSize)
  165. if req.IsAll {
  166. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  167. countSql = fmt.Sprintf("%s %s", countSql, where)
  168. } else {
  169. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  170. countSql = fmt.Sprintf("%s %s", countSql, where)
  171. }
  172. return sql, countSql, args
  173. }
  174. type LiftDeviceListRequest struct {
  175. ProviderId int64
  176. Filter string
  177. StatusFilter []int32
  178. IsAll bool
  179. CanDel bool
  180. Page int32
  181. ProjectId int64
  182. }
  183. func (p *LiftDeviceInfo) LiftDeviceList(db *gorm.DB, req LiftDeviceListRequest) ([]LiftDeviceItem, int64, error) {
  184. type ResultCount struct {
  185. Count int64
  186. }
  187. array := []ResultCount{}
  188. ret := []LiftDeviceItem{}
  189. var err error
  190. sql, countSql, args := LiftDeviceListSql(req)
  191. err = db.Raw(countSql, args...).Scan(&array).Error
  192. if err != nil {
  193. return nil, 0, err
  194. }
  195. if len(array) == 0 {
  196. return nil, 0, nil
  197. }
  198. if array[0].Count == 0 {
  199. return nil, 0, nil
  200. }
  201. err = db.Raw(sql, args...).Scan(&ret).Error
  202. if err != nil {
  203. return nil, array[0].Count, err
  204. }
  205. return ret, array[0].Count, nil
  206. }