attendance_device.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 AttendanceDeviceInfo 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. Kind int `gorm:"column:Kind"`
  34. SubKind int `gorm:"column:SubKind"`
  35. }
  36. func (AttendanceDeviceInfo) TableName() string {
  37. return "db_smart_v2.AttendanceDeviceInfo"
  38. }
  39. func (p *AttendanceDeviceInfo) Insert(db *gorm.DB) error {
  40. return db.Table(p.TableName()).Create(p).Error
  41. }
  42. func (p *AttendanceDeviceInfo) Del(db *gorm.DB, where map[string]interface{}) error {
  43. cond, val, err := whereBuild(where)
  44. if err != nil {
  45. return err
  46. }
  47. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  48. }
  49. func (p *AttendanceDeviceInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  50. cond, val, err := whereBuild(where)
  51. if err != nil {
  52. return err
  53. }
  54. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  55. }
  56. func (p *AttendanceDeviceInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  57. cond, val, err := whereBuild(where)
  58. if err != nil {
  59. return err
  60. }
  61. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  62. }
  63. func (p *AttendanceDeviceInfo) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  64. cond, val, err := whereBuild(where)
  65. if err != nil {
  66. return err
  67. }
  68. ps := []AttendanceDeviceInfo{}
  69. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  70. if err != nil {
  71. return err
  72. }
  73. if len(ps) > 0 {
  74. *p = ps[0]
  75. }
  76. return nil
  77. }
  78. func (p *AttendanceDeviceInfo) Save(db *gorm.DB) error {
  79. return db.Save(p).Error
  80. }
  81. func (p *AttendanceDeviceInfo) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  82. if len(where) > 0 {
  83. cond, val, err := whereBuild(where)
  84. if err != nil {
  85. return 0, err
  86. }
  87. ret := int64(0)
  88. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  89. return ret, err
  90. }
  91. ret := int64(0)
  92. err := db.Table(p.TableName()).Count(&ret).Error
  93. return ret, err
  94. }
  95. func (p *AttendanceDeviceInfo) List(db *gorm.DB, where map[string]interface{}, page int) (list []AttendanceDeviceInfo, err error) {
  96. if len(where) > 0 {
  97. cond, val, err := whereBuild(where)
  98. if err != nil {
  99. return list, err
  100. }
  101. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  102. return list, result.Error
  103. }
  104. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  105. return list, result.Error
  106. }
  107. func (p *AttendanceDeviceInfo) All(db *gorm.DB) (list []AttendanceDeviceInfo, err error) {
  108. result := db.Table(p.TableName()).Find(&list)
  109. return list, result.Error
  110. }
  111. type AttendanceDeviceItem struct {
  112. Id int64
  113. Sn string
  114. TypeCode int32
  115. TypeName string
  116. ProjectName string
  117. ProjectId int64
  118. SafetyRecordNo string
  119. CreatedTime time.Time
  120. Status int32
  121. State int32
  122. ProjectApproveTime time.Time
  123. Key string
  124. ProviderName string
  125. SocialCode string
  126. Name string
  127. Manufacturer string
  128. Batch string
  129. Person string
  130. Phone string
  131. Unit string
  132. Lon string
  133. Lat string
  134. Model string
  135. Kind int
  136. SubKind int
  137. }
  138. func attendanceDeviceListSql(req AttendanceDeviceListRequest)(string, string, []interface{}) {
  139. sql := fmt.Sprintf("select t1.ID as id, t1.DeviceModel as model, t1.Batch as batch, t1.Person as person, t1.Kind as kind, t1.SubKind as sub_kind, 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, "+
  140. "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 AttendanceDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  141. countSql := fmt.Sprintf("select count(1) as count from AttendanceDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID ")
  142. args := []interface{}{}
  143. whereArray := []string{}
  144. if req.ProviderId > 0 {
  145. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  146. }
  147. if req.Filter != "" {
  148. 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))
  149. }
  150. if req.CanDel {
  151. req.IsAll = true
  152. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  153. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  154. }
  155. if len(req.StatusFilter) > 0 {
  156. args = append(args, req.StatusFilter)
  157. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  158. }
  159. where := ""
  160. for _, v := range whereArray {
  161. if where == "" {
  162. where = fmt.Sprintf(" where %s", v)
  163. continue
  164. }
  165. where = fmt.Sprintf("%s and %s", where, v)
  166. }
  167. offset := (req.Page - 1) *int32(PageSize)
  168. if req.IsAll {
  169. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  170. countSql = fmt.Sprintf("%s %s", countSql, where)
  171. } else {
  172. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  173. countSql = fmt.Sprintf("%s %s", countSql, where)
  174. }
  175. return sql, countSql, args
  176. }
  177. type AttendanceDeviceListRequest struct {
  178. ProviderId int64
  179. Filter string
  180. StatusFilter []int32
  181. IsAll bool
  182. CanDel bool
  183. Page int32
  184. ProjectId int64
  185. }
  186. func (p *AttendanceDeviceInfo) AttendanceDeviceList(db *gorm.DB, req AttendanceDeviceListRequest) ([]AttendanceDeviceItem, int64, error) {
  187. type ResultCount struct {
  188. Count int64
  189. }
  190. array := []ResultCount{}
  191. ret := []AttendanceDeviceItem{}
  192. var err error
  193. sql, countSql, args := attendanceDeviceListSql(req)
  194. err = db.Raw(countSql, args...).Scan(&array).Error
  195. if err != nil {
  196. return nil, 0, err
  197. }
  198. if len(array) == 0 {
  199. return nil, 0, nil
  200. }
  201. if array[0].Count == 0 {
  202. return nil, 0, nil
  203. }
  204. err = db.Raw(sql, args...).Scan(&ret).Error
  205. if err != nil {
  206. return nil, array[0].Count, err
  207. }
  208. return ret, array[0].Count, nil
  209. }