package model
|
|
import (
|
"aps_crm/constvar"
|
"aps_crm/pkg/mysqlx"
|
"errors"
|
"fmt"
|
"gorm.io/gorm"
|
)
|
|
type (
|
// ServiceType 服务方式
|
ServiceType struct {
|
Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
|
Name string `json:"name" gorm:"column:name;type:varchar(255);not null;default:''"`
|
}
|
|
// ServiceTypeSearch 服务方式搜索条件
|
ServiceTypeSearch struct {
|
ServiceType
|
Orm *gorm.DB
|
QueryClass constvar.ServiceTypeQueryClass
|
KeywordType constvar.ServiceTypeKeywordType
|
Keyword string
|
PageNum int
|
PageSize int
|
}
|
)
|
|
func (ServiceType) TableName() string {
|
return "service_type"
|
}
|
|
func NewServiceTypeSearch() *ServiceTypeSearch {
|
return &ServiceTypeSearch{
|
Orm: mysqlx.GetDB(),
|
}
|
}
|
|
func (slf *ServiceTypeSearch) build() *gorm.DB {
|
var db = slf.Orm.Model(&ServiceType{})
|
if slf.Id != 0 {
|
db = db.Where("id = ?", slf.Id)
|
}
|
|
return db
|
}
|
|
func (slf *ServiceTypeSearch) Create(record *ServiceType) error {
|
var db = slf.build()
|
return db.Create(record).Error
|
}
|
|
func (slf *ServiceTypeSearch) CreateBatch(records []*ServiceType) error {
|
var db = slf.build()
|
return db.Create(records).Error
|
}
|
|
func (slf *ServiceTypeSearch) Delete() error {
|
var db = slf.build()
|
return db.Delete(&ServiceType{}).Error
|
}
|
|
func (slf *ServiceTypeSearch) Update(record *ServiceType) error {
|
var db = slf.build()
|
return db.Updates(record).Error
|
}
|
|
func (slf *ServiceTypeSearch) FindAll() ([]*ServiceType, error) {
|
var db = slf.build()
|
var record = make([]*ServiceType, 0)
|
err := db.Find(&record).Error
|
return record, err
|
}
|
|
func (slf *ServiceTypeSearch) SetId(id int) *ServiceTypeSearch {
|
slf.Id = id
|
return slf
|
}
|
|
func (slf *ServiceTypeSearch) First() (*ServiceType, error) {
|
var db = slf.build()
|
var record = new(ServiceType)
|
err := db.First(record).Error
|
return record, err
|
}
|
|
func (slf *ServiceTypeSearch) Updates(values interface{}) error {
|
var db = slf.build()
|
return db.Updates(values).Error
|
}
|
|
func (slf *ServiceTypeSearch) Save(record *ServiceType) error {
|
if record.Id == 0 {
|
return errors.New("id为空")
|
}
|
var db = slf.build()
|
|
if err := db.Save(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *ServiceTypeSearch) Find() ([]*ServiceType, int64, error) {
|
var db = slf.build()
|
var records = make([]*ServiceType, 0)
|
var total int64
|
if err := db.Count(&total).Error; err != nil {
|
return records, total, err
|
}
|
if slf.PageNum > 0 && slf.PageSize > 0 {
|
db = db.Limit(slf.PageSize).Offset((slf.PageNum - 1) * slf.PageSize)
|
}
|
|
err := db.Find(&records).Error
|
return records, total, err
|
}
|
|
// InitDefaultData 初始化数据
|
func (slf *ServiceTypeSearch) InitDefaultData() error {
|
var (
|
db = slf.Orm.Table(slf.TableName())
|
total int64 = 0
|
)
|
if err := db.Count(&total).Error; err != nil {
|
return err
|
}
|
if total != 0 {
|
return nil
|
}
|
records := []*ServiceType{
|
{1, "电话"},
|
{2, "远程"},
|
{3, "送修"},
|
{4, "上门"},
|
{5, "其他"},
|
}
|
return slf.CreateBatch(records)
|
}
|