package models import ( "fmt" "github.com/shopspring/decimal" "gorm.io/gorm" "silkserver/pkg/mysqlx" ) type ( // PayrollProductionGroup 工资计算-小组每天的产量统计 PayrollProductionGroup struct { BaseModelInt Cycle string `json:"cycle" gorm:"index;size:20;not null;comment:统计周期"` //统计周期(年月日) WorkshopId uint `json:"workshopId" gorm:"type:int(11);comment:车间Id"` WorkshopNumber string `json:"workshopNumber" gorm:"size:50;not null;comment:车间编号"` // 车间编号 WorkshopName string `json:"workshopName" gorm:"size:50;comment:车间名称"` // 车间名称 GroupNumber int `json:"groupNumber" gorm:"size:11;not null;comment:组别"` // 组别 FallingSilkBucket decimal.Decimal `json:"fallingSilkBucket" gorm:"type:decimal(12,2);comment:落丝桶数"` SilkQuantity decimal.Decimal `json:"silkQuantity" gorm:"type:decimal(12,4);comment:丝量"` // 丝量 SilkAvgQuantity decimal.Decimal `json:"silkAvgQuantity" gorm:"type:decimal(12,4);comment:人平均丝量"` // 人平均丝量 SilkTotalAmount decimal.Decimal `json:"silkTotalAmount" gorm:"type:decimal(12,4);comment:丝量总价"` // 丝量总价 SilkTotalAvgAmount decimal.Decimal `json:"silkTotalAvgAmount" gorm:"type:decimal(12,4);comment:丝量人平均总价"` // 丝量人平均总价 BadSilkQuantity decimal.Decimal `json:"badSilkQuantity" gorm:"type:decimal(12,4);comment:野纤数量"` // 野纤数量 BadSilkTotalAmount decimal.Decimal `json:"badSilkTotalAmount" gorm:"type:decimal(12,4);comment:野纤总价"` // 野纤总价 BadSilkTotalAvgAmount decimal.Decimal `json:"badSilkTotalAvgAmount" gorm:"type:decimal(12,4);comment:野纤人平均总价"` // 野纤人平均总价 FinishTotalAmount decimal.Decimal `json:"finishTotalAmount" gorm:"type:decimal(12,4);comment:成品金额"` // 成品金额 FinishTotalAvgAmount decimal.Decimal `json:"finishTotalAvgAmount" gorm:"type:decimal(12,4);comment:成品人平均金额"` // 成品人平均金额 } PayrollProductionGroupSearch struct { PayrollProductionGroup Monthly string Order string PageNum int PageSize int Orm *gorm.DB } ) func (slf PayrollProductionGroup) TableName() string { return "silk_payroll_production_group" } // NewPayrollProductionGroupSearch 小组每天的产量统计 func NewPayrollProductionGroupSearch() *PayrollProductionGroupSearch { return &PayrollProductionGroupSearch{Orm: mysqlx.GetDB()} } func (slf *PayrollProductionGroupSearch) SetOrm(tx *gorm.DB) *PayrollProductionGroupSearch { slf.Orm = tx return slf } func (slf *PayrollProductionGroupSearch) SetPage(page, size int) *PayrollProductionGroupSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *PayrollProductionGroupSearch) SetOrder(order string) *PayrollProductionGroupSearch { slf.Order = order return slf } func (slf *PayrollProductionGroupSearch) SetID(id uint) *PayrollProductionGroupSearch { slf.ID = id return slf } func (slf *PayrollProductionGroupSearch) SetCycle(cycle string) *PayrollProductionGroupSearch { slf.Cycle = cycle return slf } func (slf *PayrollProductionGroupSearch) SetMonthly(monthly string) *PayrollProductionGroupSearch { slf.Monthly = monthly return slf } func (slf *PayrollProductionGroupSearch) SetWorkshopNumber(workshopNumber string) *PayrollProductionGroupSearch { slf.WorkshopNumber = workshopNumber return slf } func (slf *PayrollProductionGroupSearch) SetGroupNumber(groupNumber int) *PayrollProductionGroupSearch { slf.GroupNumber = groupNumber return slf } func (slf *PayrollProductionGroupSearch) build() *gorm.DB { var db = slf.Orm.Table(slf.TableName()) 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.WorkshopNumber != "" { db = db.Where("workshop_number = ?", slf.WorkshopNumber) } if slf.GroupNumber > 0 { db = db.Where("group_number = ?", slf.GroupNumber) } db.Where("1 = 1") if slf.Order != "" { db = db.Order(slf.Order) } return db } // Create 单条插入 func (slf *PayrollProductionGroupSearch) Create(record *PayrollProductionGroup) 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 *PayrollProductionGroupSearch) CreateBatch(records []*PayrollProductionGroup) 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 *PayrollProductionGroupSearch) Save(record *PayrollProductionGroup) 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 *PayrollProductionGroupSearch) SaveBatch(record []*PayrollProductionGroup) 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 *PayrollProductionGroupSearch) 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 *PayrollProductionGroupSearch) 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 *PayrollProductionGroupSearch) Delete() error { var db = slf.build() if err := db.Unscoped().Delete(&PayrollProductionGroup{}).Error; err != nil { return err } return nil } // First 根据条件查询一条记录 func (slf *PayrollProductionGroupSearch) First() (*PayrollProductionGroup, error) { var ( record = new(PayrollProductionGroup) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } // Find 指定条件查询(包含总条数) func (slf *PayrollProductionGroupSearch) Find() ([]*PayrollProductionGroup, int64, error) { var ( records = make([]*PayrollProductionGroup, 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 *PayrollProductionGroupSearch) FindNotTotal() ([]*PayrollProductionGroup, error) { var ( records = make([]*PayrollProductionGroup, 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 *PayrollProductionGroupSearch) FindByQuery(query string, args []interface{}) ([]*PayrollProductionGroup, int64, error) { var ( records = make([]*PayrollProductionGroup, 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 *PayrollProductionGroupSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*PayrollProductionGroup, error) { var ( records = make([]*PayrollProductionGroup, 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 }