package model import ( "fmt" "github.com/jinzhu/gorm" "time" "smart-enterprise-management/consts" ) type DeviceAll struct { ID int64 `gorm:"column:ID" json:"id"` SN string `gorm:"column:SN" json:"sn"` DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"` Longitude string `gorm:"column:Longitude" json:"longitude"` Latitude string `gorm:"column:Latitude" json:"latitude"` DeviceCode uint32 `gorm:"column:DeviceCode"` DeviceName string `gorm:"column:DeviceName"` SubId int64 `gorm:"column:SubId"` ProjectId int64 `gorm:"column:ProjectId"` ProviderId int64 `gorm:"column:ProviderId"` CreatedAt time.Time `gorm:"column:CreatedAt"` VerifyTime time.Time `gorm:"column:VerifyTime"` VerifyStatus int `gorm:"column:VerifyStatus"` } func (DeviceAll) TableName() string { return "db_smart_v2.DeviceAll" } func (p *DeviceAll) Insert(db *gorm.DB) error { return db.Table(p.TableName()).Create(p).Error } /* func (p *Dustdeviceinfo) Statistic(db *gorm.DB, projectId int64, deviceType int32, result interface{}) error { sql := fmt.Sprintf("select device_code as device_type, sum(status=0) as offline, sum(status=1) as online from t_device where project_id=%d group by type", projectId) if deviceType > 0 { sql = fmt.Sprintf("select device_code as device_type, sum(status=0) as offline, sum(status=1) as online from t_device where project_id=%d and type = %d group by type", projectId, deviceType) } err := db.Raw(sql).Scan(result).Error return err } */ func (p *DeviceAll) Del(db *gorm.DB, where map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error } func (p *DeviceAll) Find(db *gorm.DB, where map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).First(p).Error } func (p *DeviceAll) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error { cond, val, err := whereBuild(where) if err != nil { return err } return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error } func (p *DeviceAll) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error { cond, val, err := whereBuild(where) if err != nil { return err } ps := []DeviceAll{} err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error if err != nil { return err } if len(ps) > 0 { *p = ps[0] } return nil } func (p *DeviceAll) Save(db *gorm.DB) error { return db.Save(p).Error } func (p *DeviceAll) Count(db *gorm.DB, where map[string]interface{}) (int64, error) { if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return 0, err } ret := int64(0) err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error return ret, err } ret := int64(0) err := db.Table(p.TableName()).Count(&ret).Error return ret, err } func (p *DeviceAll) List(db *gorm.DB, where map[string]interface{}, page int) (list []DeviceAll, err error) { if len(where) > 0 { cond, val, err := whereBuild(where) if err != nil { return list, err } result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list) return list, result.Error } result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list) return list, result.Error } type DeviceAllItem struct { Id int64 Sn string TypeCode int32 TypeName string ProjectName string ProjectId int64 SafetyRecordNo string CreatedTime time.Time Status int32 State int32 ProjectApproveTime time.Time Key string ProviderName string SocialCode string Name string Ip string Port int MediaTransport string ChannelCount int } func deviceAllListSql(req DeviceAllListRequest)(string, string, []interface{}) { sql := fmt.Sprintf("select t1.DeviceCode as type_code, t1.ID as id, t1.DeviceName as name, t1.SN as sn, t1.VerifyTime, "+ "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 DeviceAll as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID") countSql := fmt.Sprintf("select count(1) as count from DeviceAll as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID ") args := []interface{}{} whereArray := []string{} if req.ProviderId > 0 { whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId)) } if req.Cid > 0 { whereArray = append(whereArray, fmt.Sprintf("t2.Cid=%d", req.Cid)) } if req.Filter != "" { whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t1.DeviceName like '%%%s%%')", req.Filter, req.Filter)) } if req.ProjectId > 0 { whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId=%d", req.ProjectId)) } if req.State >= 0 { whereArray = append(whereArray, fmt.Sprintf("t1.DeviceState=%d", req.State)) } if req.CanDel { req.IsAll = true req.StatusFilter = []int32{consts.DeviceStatusAddAuditted} whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0")) } if len(req.StatusFilter) > 0 { args = append(args, req.StatusFilter) whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)")) } where := "" for _, v := range whereArray { if where == "" { where = fmt.Sprintf(" where %s", v) continue } where = fmt.Sprintf("%s and %s", where, v) } offset := (req.Page - 1) *int32(PageSize) if req.IsAll { sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where) countSql = fmt.Sprintf("%s %s", countSql, where) } else { sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset) countSql = fmt.Sprintf("%s %s", countSql, where) } return sql, countSql, args } type DeviceAllListRequest struct { ProviderId int64 Filter string StatusFilter []int32 IsAll bool CanDel bool Page int32 ProjectId int64 Cid int64 State int32 } func (p *DeviceAll) DeviceAllList(db *gorm.DB, req DeviceAllListRequest) ([]DeviceAllItem, int64, error) { type ResultCount struct { Count int64 } array := []ResultCount{} ret := []DeviceAllItem{} var err error sql, countSql, args := deviceAllListSql(req) err = db.Raw(countSql, args...).Scan(&array).Error if err != nil { return nil, 0, err } if len(array) == 0 { return nil, 0, nil } if array[0].Count == 0 { return nil, 0, nil } err = db.Raw(sql, args...).Scan(&ret).Error if err != nil { return nil, array[0].Count, err } return ret, array[0].Count, nil }