// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package model import ( "fmt" "github.com/jinzhu/gorm" "time" ) type DeviceDelJob struct { ID int64 `gorm:"column:ID" json:"id" form:"id"` CreatedAt time.Time `gorm:"column:CreatedAt" json:"created_at" form:"created_at"` Status int64 `gorm:"column:Status" json:"status" form:"status"` UpdatedAt time.Time `gorm:"column:UpdatedAt" json:"updated_at" form:"updated_at"` ProjectId int64 `gorm:"column:ProjectId" json:"project_id" form:"project_id"` Feedback string `gorm:"column:Feedback"` Reason string `gorm:"column:Reason"` ProviderId int64 `gorm:"column:ProviderId"` DeviceId int64 `gorm:"column:DeviceId"` DeviceCode uint32 `gorm:"column:DeviceCode"` Sn string `gorm:"column:SN"` DeviceName string `gorm:"column:DeviceName"` } func (DeviceDelJob) TableName() string { return "DeviceDelJob" } func (p *DeviceDelJob) Insert(db *gorm.DB) error { return db.Table(p.TableName()).Create(p).Error } func (p *DeviceDelJob) 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 *DeviceDelJob) 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 *DeviceDelJob) 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 *DeviceDelJob) Save(db *gorm.DB) error { return db.Table(p.TableName()).Save(p).Error } func (p *DeviceDelJob) 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 *DeviceDelJob) List(db *gorm.DB, where map[string]interface{}, page int) (list []DeviceDelJob, 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 } func (p *DeviceDelJob) All(db *gorm.DB) (list []DeviceDelJob, err error) { result := db.Table(p.TableName()).Find(&list) return list, result.Error } type DeviceDelJobItem struct { Id int64 Sn string TypeCode int32 TypeName string ProjectName string ProviderName string SocialCode string SafetyRecordNo string CreatedTime time.Time Status int32 ApproveTime time.Time Origin string Reason string Feedback string DeviceName string Reviewer string } type DeviceDelJobRequest struct { ProjectId int64 ProviderId int64 Filter string Page int32 PageSize int32 } func DeviceDelJobListSql(req *DeviceDelJobRequest) (string, string) { sql := fmt.Sprintf("select t1.ID as id, t1.Reviewer as reviewer, t1.Reason as reason, t1.Feedback as feedback, t1.Status as status, t1.CreatedAt as created_time, t1.UpdatedAt as approve_time, t1.DeviceName as device_name, t1.SN as sn, t1.DeviceCode as type_code, t2.Name as project_name, t2.SafetyNo as safety_record_no, t3.SocialCode as social_code, t3.Name as provider_name "+ " from DeviceDelJob 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 DeviceDelJob as t1 left join ProjectInfo as t2 on t1.ProjectId= t2.ID left join Provider as t3 on t1.ProviderId=t3.ID") where := "" whereArray := []string{} if req.ProjectId > 0 { whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId=%d", req.ProjectId)) } if req.ProviderId > 0 { whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId)) } if req.Filter != "" { whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t1.DeviceName like '%%%s%%')", req.Filter, req.Filter)) } for _, v := range whereArray { if where == "" { where = fmt.Sprintf("where %s", v) continue } where = fmt.Sprintf("%s %s and ", where, v) } offset := int64(req.Page - 1) *int64(req.PageSize) sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, req.PageSize, offset) countSql = fmt.Sprintf("%s %s", countSql, where) return sql, countSql } type CountItem struct { Count int64 } func DeviceDelJobList(db *gorm.DB, req*DeviceDelJobRequest) ([]DeviceDelJobItem, int64, error) { sql, countSql := DeviceDelJobListSql(req) array := []CountItem{} err := db.Raw(countSql).Scan(&array).Error if err != nil { return nil, 0, err } total := int64(0) if len(array) > 0 { total = array[0].Count } if total == 0 { return nil, 0, nil } list := []DeviceDelJobItem{} err = db.Raw(sql).Scan(&list).Error if err != nil { return nil, 0, err } return list, total, nil }