attendance_device.go 7.4 KB

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