package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"silkserver/constvar"
|
"silkserver/pkg/mysqlx"
|
)
|
|
type (
|
//WorkTypeManage 工种管理
|
WorkTypeManage struct {
|
gorm.Model
|
Code constvar.JobType `json:"code" gorm:"type:varchar(255);comment:工种编码"`
|
WorkName string `json:"workName" gorm:"type:varchar(255);comment:工种名称"`
|
IsGuaranteed bool `json:"isGuaranteed" gorm:"type:int(1);comment:不达标保底"`
|
GuaranteedWages decimal.Decimal `json:"guaranteedWages" gorm:"type:decimal(20,3);comment:保底工资"`
|
CreateTime string `json:"createTime" gorm:"type:varchar(255);comment:添加时间"`
|
AddPeople string `json:"addPeople" gorm:"type:varchar(255);comment:添加人"`
|
SalaryPlans []*SalaryPlan `json:"salaryPlans" gorm:"many2many:silk_salaryPlan_workType"`
|
}
|
WorkTypeManageSearch struct {
|
WorkTypeManage
|
PageNum int
|
PageSize int
|
Preload bool
|
Ids []uint
|
Orm *gorm.DB
|
}
|
)
|
|
func (slf WorkTypeManage) TableName() string {
|
return "silk_work_type_manage"
|
}
|
|
func NewWorkTypeManageSearch() *WorkTypeManageSearch {
|
return &WorkTypeManageSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *WorkTypeManageSearch) SetOrm(tx *gorm.DB) *WorkTypeManageSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *WorkTypeManageSearch) SetPage(page, size int) *WorkTypeManageSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *WorkTypeManageSearch) SetId(id int) *WorkTypeManageSearch {
|
slf.ID = uint(id)
|
return slf
|
}
|
|
func (slf *WorkTypeManageSearch) SetPreload(preload bool) *WorkTypeManageSearch {
|
slf.Preload = preload
|
return slf
|
}
|
|
func (slf *WorkTypeManageSearch) SetIds(ids []uint) *WorkTypeManageSearch {
|
slf.Ids = ids
|
return slf
|
}
|
|
func (slf *WorkTypeManageSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
if slf.ID > 0 {
|
db = db.Where("id = ?", slf.ID)
|
}
|
|
if len(slf.Ids) > 0 {
|
db = db.Where("id in (?)", slf.Ids)
|
}
|
|
if slf.Preload {
|
db = db.Model(WorkTypeManage{}).Preload("SalaryPlans")
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *WorkTypeManageSearch) Create(record *WorkTypeManage) error {
|
var db = slf.build()
|
|
if err := db.Create(record).Error; err != nil {
|
return fmt.Errorf("create err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *WorkTypeManageSearch) Save(record *WorkTypeManage) error {
|
var db = slf.build()
|
|
if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *WorkTypeManageSearch) UpdateByMap(upMap map[string]interface{}) error {
|
var (
|
db = slf.build()
|
)
|
|
if err := db.Updates(upMap).Error; err != nil {
|
return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
|
}
|
|
return nil
|
}
|
|
func (slf *WorkTypeManageSearch) Delete() error {
|
var db = slf.build()
|
|
if err := db.Delete(&WorkTypeManage{}).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
func (slf *WorkTypeManageSearch) Find() ([]*WorkTypeManage, int64, error) {
|
var (
|
records = make([]*WorkTypeManage, 0)
|
total int64
|
db = slf.build()
|
)
|
|
if err := db.Count(&total).Error; err != nil {
|
return records, total, fmt.Errorf("find count err: %v", err)
|
}
|
if slf.PageNum*slf.PageSize > 0 {
|
db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
|
}
|
if err := db.Find(&records).Error; err != nil {
|
return records, total, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, total, nil
|
}
|
|
func (slf *WorkTypeManageSearch) FindNotTotal() ([]*WorkTypeManage, error) {
|
var (
|
records = make([]*WorkTypeManage, 0)
|
db = slf.build()
|
)
|
|
if err := db.Find(&records).Error; err != nil {
|
return records, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, nil
|
}
|
|
func (slf *WorkTypeManageSearch) Count() (int64, error) {
|
var (
|
total int64
|
db = slf.build()
|
)
|
|
if err := db.Count(&total).Error; err != nil {
|
return total, fmt.Errorf("find count err: %v", err)
|
}
|
return total, nil
|
}
|