// Copyright 2019 githup.com. All rights reserved. // Use of this source code is governed by githup.com. package model import ( "fmt" "github.com/jinzhu/gorm" "time" ) type TProviderDevice struct { Id int64 `gorm:"column:id" json:"id" form:"id"` ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"` DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"` Status int32 `gorm:"column:status" json:"status" form:"status"` CreatedAt time.Time UpdatedAt time.Time } func (TProviderDevice) TableName() string { return "t_provider_device" } func (p *TProviderDevice) Insert(db *gorm.DB) error { return db.Create(p).Error } func (p *TProviderDevice) 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 *TProviderDevice) 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()).Table(p.TableName()).Where(cond, val...).Updates(values).Error } func (p *TProviderDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error { cond, val, err := whereBuild(where) if err != nil { return err } ps := []TProviderDevice{} 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 *TProviderDevice) Save(db *gorm.DB) error { return db.Save(p).Error } func (p *TProviderDevice) List(db *gorm.DB, where map[string]interface{}) (list []TProviderDevice, 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...).Find(&list) return list, result.Error } result := db.Table(p.TableName()).Find(&list) return list, result.Error } func (p *TProviderDevice) All(db *gorm.DB) (list []TProviderDevice, err error) { result := db.Table(p.TableName()).Find(&list) return list, result.Error } func (p *TProviderDevice) 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 *TProviderDevice) 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 } type ListWithProviderItem struct { Id int64 ProviderName string SocialCode string TypeCode int32 Status int32 Comment string DevelopApproveTime time.Time ApproveTime time.Time Feedback string CreatedAt time.Time } func listWithProviderSql(page int32, filter string, filter_status []int32)(string, string, []interface{}) { offset := (page - 1) *int32(PageSize) whereArray := []string{} args := []interface{}{} sql := "select t1.id, t1.develop_approve_time, t1.approve_time, t1.feedback, t1.comment, t1.device_code as type_code, t1.status, t1.updated_at, t1.created_at, t2.name as provider_name, t2.social_code from t_provider_device as t1 left join t_provider as t2 on t1.provider_id=t2.id " if filter != "" { whereArray = append(whereArray, fmt.Sprintf("(t2.name like '%%%s%%' or t2.social_code like '%%%s%%')", filter, filter)) } if len(filter_status) > 0 { whereArray = append(whereArray, "t1.status in (?)") args = append(args, filter_status) } where := "" for _, v := range whereArray { if where == "" { where = fmt.Sprintf("where %s", v) continue } where = fmt.Sprintf("%s and %s", where, v) } sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset) countSql := "select count(1) as count from t_provider_device as t1 left join t_provider as t2 on t1.provider_id=t2.id" countSql = fmt.Sprintf("%s %s ", countSql, where) return sql, countSql, args } func (p *TProviderDevice) ListWithProvider(db *gorm.DB, page int32, filter string, filter_status []int32)([]ListWithProviderItem, int64, error) { type ResultCount struct { Count int64 } sql, countSql, args := listWithProviderSql(page, filter, filter_status) array := []ResultCount{} 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 } ret := []ListWithProviderItem{} err = db.Raw(sql, args...).Scan(&ret).Error if err != nil { return nil, array[0].Count, err } return ret, array[0].Count, nil }