package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"silkserver/constvar"
|
"silkserver/pkg/mysqlx"
|
)
|
|
type (
|
// PayrollOtherSubsidies 其它补贴、奖惩
|
PayrollOtherSubsidies struct {
|
BaseModelInt
|
Cycle string `json:"cycle" gorm:"index;size:20;not null;comment:统计周期"` //月份
|
WorkerID string `json:"workerId" gorm:"size:200;not null;comment:员工ID"` //员工ID
|
Worker Worker `json:"worker" gorm:"foreignKey:WorkerID;references:ID"`
|
WorkTypeID uint `json:"workTypeID" gorm:"type:bigint(20);not null;comment:工种ID"` //工种ID
|
WorkTypeCode constvar.JobType `json:"workTypeCode" gorm:"size:255;not null;comment:工种代码"` //工种代码
|
WorkTypeName string `json:"workTypeName" gorm:"size:255;not null;comment:工种名称"` //工种名称
|
SalaryPlanId uint `json:"salaryPlanId" gorm:"type:bigint(20);not null;comment:薪资方案ID"` //薪资方案ID
|
SalaryPlan SalaryPlan `json:"subsidyTypeName" gorm:"foreignKey:SalaryPlanId;references:ID"` //薪资方案
|
SalaryFormula string `json:"salaryFormula" gorm:"size:255;not null;comment:薪资方案(翻译后)"` //薪资方案
|
Amount decimal.Decimal `json:"amount" gorm:"type:decimal(12,4);comment:金额"` // 金额
|
}
|
|
PayrollOtherSubsidiesSearch struct {
|
PayrollOtherSubsidies
|
Monthly string
|
Order string
|
PageNum int
|
PageSize int
|
Preload bool
|
Orm *gorm.DB
|
}
|
)
|
|
func (slf PayrollOtherSubsidies) TableName() string {
|
return "silk_payroll_other_subsidies"
|
}
|
|
// NewPayrollOtherSubsidiesSearch 其它补贴
|
func NewPayrollOtherSubsidiesSearch() *PayrollOtherSubsidiesSearch {
|
return &PayrollOtherSubsidiesSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetOrm(tx *gorm.DB) *PayrollOtherSubsidiesSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetPage(page, size int) *PayrollOtherSubsidiesSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetOrder(order string) *PayrollOtherSubsidiesSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetID(id uint) *PayrollOtherSubsidiesSearch {
|
slf.ID = id
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetCycle(cycle string) *PayrollOtherSubsidiesSearch {
|
slf.Cycle = cycle
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetMonthly(monthly string) *PayrollOtherSubsidiesSearch {
|
slf.Monthly = monthly
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetWorkTypeID(workTypeID uint) *PayrollOtherSubsidiesSearch {
|
slf.WorkTypeID = workTypeID
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) SetWorkerID(workerID string) *PayrollOtherSubsidiesSearch {
|
slf.WorkerID = workerID
|
return slf
|
}
|
|
func (slf *PayrollOtherSubsidiesSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
if slf.Preload {
|
db = db.Model(&PayrollOtherSubsidies{}).Preload("Worker")
|
}
|
|
if slf.ID > 0 {
|
db = db.Where("id = ?", slf.ID)
|
}
|
|
if slf.Cycle != "" {
|
db = db.Where("cycle = ?", slf.Cycle)
|
}
|
|
if slf.Monthly != "" {
|
db = db.Where("cycle like ?", slf.Monthly+"%")
|
}
|
|
if slf.WorkTypeID > 0 {
|
db = db.Where("work_type_id = ?", slf.WorkTypeID)
|
}
|
|
if slf.WorkerID != "" {
|
db = db.Where("worker_id = ?", slf.WorkerID)
|
}
|
|
db.Where("1 = 1")
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *PayrollOtherSubsidiesSearch) Create(record *PayrollOtherSubsidies) 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
|
}
|
|
// CreateBatch 批量插入
|
func (slf *PayrollOtherSubsidiesSearch) CreateBatch(records []*PayrollOtherSubsidies) error {
|
var db = slf.build()
|
|
if err := db.Create(&records).Error; err != nil {
|
return fmt.Errorf("create batch err: %v, records: %+v", err, records)
|
}
|
|
return nil
|
}
|
|
// Save 单条更新
|
func (slf *PayrollOtherSubsidiesSearch) Save(record *PayrollOtherSubsidies) 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
|
}
|
|
// SaveBatch 批量更新
|
func (slf *PayrollOtherSubsidiesSearch) SaveBatch(record []*PayrollOtherSubsidies) 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
|
}
|
|
// UpdateByMap 单条更新
|
func (slf *PayrollOtherSubsidiesSearch) 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
|
}
|
|
// UpdateByQuery 批量更新
|
func (slf *PayrollOtherSubsidiesSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
|
var (
|
db = slf.Orm.Table(slf.TableName()).Where(query, args...)
|
)
|
|
if err := db.Updates(upMap).Error; err != nil {
|
return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
|
}
|
|
return nil
|
}
|
|
// Delete 删除
|
func (slf *PayrollOtherSubsidiesSearch) Delete() error {
|
var db = slf.build()
|
|
if err := db.Unscoped().Delete(&PayrollOtherSubsidies{}).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
// First 根据条件查询一条记录
|
func (slf *PayrollOtherSubsidiesSearch) First() (*PayrollOtherSubsidies, error) {
|
var (
|
record = new(PayrollOtherSubsidies)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
// Find 指定条件查询(包含总条数)
|
func (slf *PayrollOtherSubsidiesSearch) Find() ([]*PayrollOtherSubsidies, int64, error) {
|
var (
|
records = make([]*PayrollOtherSubsidies, 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
|
}
|
|
// FindNotTotal 指定条件查询
|
func (slf *PayrollOtherSubsidiesSearch) FindNotTotal() ([]*PayrollOtherSubsidies, error) {
|
var (
|
records = make([]*PayrollOtherSubsidies, 0)
|
db = slf.build()
|
)
|
|
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, fmt.Errorf("find records err: %v", err)
|
}
|
|
return records, nil
|
}
|
|
// FindByQuery 指定条件查询(包含总条数)
|
func (slf *PayrollOtherSubsidiesSearch) FindByQuery(query string, args []interface{}) ([]*PayrollOtherSubsidies, int64, error) {
|
var (
|
records = make([]*PayrollOtherSubsidies, 0)
|
total int64
|
db = slf.Orm.Table(slf.TableName()).Where(query, args...)
|
)
|
|
if err := db.Count(&total).Error; err != nil {
|
return records, total, fmt.Errorf("find by query 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 by query records err: %v, query: %s, args: %+v", err, query, args)
|
}
|
|
return records, total, nil
|
}
|
|
// FindByQueryNotTotal 指定条件查询&不查询总条数.
|
func (slf *PayrollOtherSubsidiesSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*PayrollOtherSubsidies, error) {
|
var (
|
records = make([]*PayrollOtherSubsidies, 0)
|
db = slf.Orm.Table(slf.TableName()).Where(query, args...)
|
)
|
|
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, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
|
}
|
|
return records, nil
|
}
|