package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"silkserver/pkg/mysqlx"
|
)
|
|
type (
|
// PayrollProductionWeavers 工资计算-挡车工每天的产量、金额统计
|
PayrollProductionWeavers struct {
|
BaseModelInt
|
Cycle string `json:"index;cycle" gorm:"size:20;not null;comment:统计周期(年月日)"` //统计周期
|
WorkTypeID uint `json:"workTypeID" gorm:"type:bigint(20);not null;comment:工种ID"` //工种ID
|
WorkType WorkTypeManage `json:"workType" gorm:"foreignKey:WorkTypeID;references:ID"`
|
WorkerID string `json:"workerId" gorm:"size:200;not null;comment:员工ID"` //员工ID
|
Worker Worker `json:"worker" gorm:"foreignKey:WorkerID;references:ID"`
|
WorkshopId uint `json:"workshopId" gorm:"type:int(11);comment:车间Id"`
|
WorkshopNumber string `json:"workshopNumber" gorm:"size:255;not null;comment:车间编号"` // 车间编号
|
GroupNumber int `json:"groupNumber" gorm:"size:11;not null;comment:组别"` // 组别
|
CarNumbers string `json:"carNumbers" gorm:"size:255;not null;comment:车台号"` // 车台号
|
|
SilkQuantity decimal.Decimal `json:"silkQuantity" gorm:"type:decimal(12,4);comment:丝量"` // 丝量
|
SilkTotalAmount decimal.Decimal `json:"silkTotalAmount" 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:野纤总价"` // 野纤总价
|
FinishTotalAmount decimal.Decimal `json:"finishTotalAmount" gorm:"type:decimal(12,4);comment:成品金额"` // 成品金额
|
}
|
|
PayrollProductionWeaversSearch struct {
|
PayrollProductionWeavers
|
Monthly string
|
|
Order string
|
PageNum int
|
PageSize int
|
Preload bool
|
Orm *gorm.DB
|
}
|
)
|
|
func (slf PayrollProductionWeavers) TableName() string {
|
return "silk_payroll_production_weavers"
|
}
|
|
// NewPayrollProductionWeaversSearch 员工每天的产量统计
|
func NewPayrollProductionWeaversSearch() *PayrollProductionWeaversSearch {
|
return &PayrollProductionWeaversSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetOrm(tx *gorm.DB) *PayrollProductionWeaversSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetPage(page, size int) *PayrollProductionWeaversSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetOrder(order string) *PayrollProductionWeaversSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetID(id uint) *PayrollProductionWeaversSearch {
|
slf.ID = id
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetCycle(cycle string) *PayrollProductionWeaversSearch {
|
slf.Cycle = cycle
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetMonthly(monthly string) *PayrollProductionWeaversSearch {
|
slf.Monthly = monthly
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetWorkTypeID(workTypeID uint) *PayrollProductionWeaversSearch {
|
slf.WorkTypeID = workTypeID
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetWorkerID(workerID string) *PayrollProductionWeaversSearch {
|
slf.WorkerID = workerID
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetWorkshopNumber(workshopNumber string) *PayrollProductionWeaversSearch {
|
slf.WorkshopNumber = workshopNumber
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetGroupNumber(groupNumber int) *PayrollProductionWeaversSearch {
|
slf.GroupNumber = groupNumber
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) SetCarNumbers(carNumbers string) *PayrollProductionWeaversSearch {
|
slf.CarNumbers = carNumbers
|
return slf
|
}
|
|
func (slf *PayrollProductionWeaversSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
if slf.Preload {
|
db = db.Model(&PayrollProductionWeavers{}).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)
|
}
|
|
if slf.WorkshopNumber != "" {
|
db = db.Where("workshop_number = ?", slf.WorkshopNumber)
|
}
|
|
if slf.GroupNumber > 0 {
|
db = db.Where("group_number = ?", slf.GroupNumber)
|
}
|
|
if slf.CarNumbers != "" {
|
db = db.Where("car_numbers like ?", "%"+slf.CarNumbers+"%")
|
}
|
|
db.Where("1 = 1")
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *PayrollProductionWeaversSearch) Create(record *PayrollProductionWeavers) 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 *PayrollProductionWeaversSearch) CreateBatch(records []*PayrollProductionWeavers) 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 *PayrollProductionWeaversSearch) Save(record *PayrollProductionWeavers) 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 *PayrollProductionWeaversSearch) SaveBatch(record []*PayrollProductionWeavers) 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 *PayrollProductionWeaversSearch) 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 *PayrollProductionWeaversSearch) 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 *PayrollProductionWeaversSearch) Delete() error {
|
var db = slf.build()
|
|
if err := db.Unscoped().Delete(&PayrollProductionWeavers{}).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
// First 根据条件查询一条记录
|
func (slf *PayrollProductionWeaversSearch) First() (*PayrollProductionWeavers, error) {
|
var (
|
record = new(PayrollProductionWeavers)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
// Find 指定条件查询(包含总条数)
|
func (slf *PayrollProductionWeaversSearch) Find() ([]*PayrollProductionWeavers, int64, error) {
|
var (
|
records = make([]*PayrollProductionWeavers, 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 *PayrollProductionWeaversSearch) FindNotTotal() ([]*PayrollProductionWeavers, error) {
|
var (
|
records = make([]*PayrollProductionWeavers, 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 *PayrollProductionWeaversSearch) FindByQuery(query string, args []interface{}) ([]*PayrollProductionWeavers, int64, error) {
|
var (
|
records = make([]*PayrollProductionWeavers, 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 *PayrollProductionWeaversSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*PayrollProductionWeavers, error) {
|
var (
|
records = make([]*PayrollProductionWeavers, 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
|
}
|
|
type WeaversAmount struct {
|
WorkerID string `json:"workerID"`
|
SilkQuantity decimal.Decimal `json:"silkQuantity"` // 丝量
|
SilkTotalAmount decimal.Decimal `json:"silkTotalAmount"` // 丝量总价
|
BadSilkQuantity decimal.Decimal `json:"badSilkQuantity"` // 野纤数量
|
BadSilkTotalAmount decimal.Decimal `json:"badSilkTotalAmount"` // 野纤总价
|
FinishTotalAmount decimal.Decimal `json:"finishTotalAmount"` // 成品金额
|
}
|
|
// FindWeaversAmount 挡车工月工资
|
func (slf *PayrollProductionWeaversSearch) FindWeaversAmount(monthly string) ([]*WeaversAmount, error) {
|
var (
|
records = make([]*WeaversAmount, 0)
|
db = slf.Orm.Table(slf.TableName())
|
)
|
db.Select("worker_id, sum(silk_quantity) as silk_quantity, sum(silk_total_amount) as silk_total_amount, sum(bad_silk_quantity) as bad_silk_quantity, sum(bad_silk_total_amount) as bad_silk_total_amount, sum(finish_total_amount) as finish_total_amount").
|
Where("cycle like ?", monthly+"%").
|
Group("worker_id")
|
|
return records, db.Find(&records).Error
|
}
|