package models
|
|
import (
|
"fmt"
|
"github.com/shopspring/decimal"
|
"gorm.io/gorm"
|
"silkserver/constvar"
|
"silkserver/pkg/mysqlx"
|
)
|
|
type (
|
//AttendanceManage 考勤管理表
|
AttendanceManage struct {
|
gorm.Model
|
Date string `json:"date" gorm:"type:varchar(255);comment:考勤时间"`
|
WorkerId string `json:"workerId" gorm:"type:varchar(255);comment:人员id"`
|
WorkerName string `json:"workerName" gorm:"type:varchar(255);comment:人员姓名"`
|
StartWorkTime string `json:"startWorkTime" gorm:"type:varchar(255);comment:上班打卡时间"`
|
EndWorkTime string `json:"endWorkTime" gorm:"type:varchar(255);comment:下班打卡时间"`
|
Classes string `json:"classes" gorm:"type:varchar(255);comment:班次"`
|
ClassesStartTime string `json:"classesStartTime" gorm:"type:varchar(255);comment:班次开始时间"`
|
ClassesEndTime string `json:"classesEndTime" gorm:"type:varchar(255);comment:班次下班时间"`
|
CreateTime string `json:"createTime" gorm:"type:varchar(255);comment:添加时间"`
|
AddPeople string `json:"addPeople" gorm:"type:varchar(255);comment:添加人"`
|
WorkTypeId uint `json:"workTypeId" gorm:"type:int(11);comment:工种id"`
|
WorkType WorkTypeManage `json:"workType" gorm:"foreignKey:WorkTypeId"`
|
Status constvar.AttendanceStatus `json:"status" gorm:"type:int(11);comment:状态"`
|
OverTimeDuration decimal.Decimal `json:"overTimeDuration" gorm:"type:decimal(20,2);comment:加班时长"`
|
PhoneNum string `gorm:"type:varchar(191);comment:手机号" json:"phoneNum"`
|
}
|
AttendanceManageSearch struct {
|
AttendanceManage
|
PageNum int
|
PageSize int
|
Preload bool
|
Ids []uint
|
Month string
|
Keyword string
|
Order string
|
Orm *gorm.DB
|
}
|
)
|
|
func (slf AttendanceManage) TableName() string {
|
return "silk_attendance_manage"
|
}
|
|
func NewAttendanceManageSearch() *AttendanceManageSearch {
|
return &AttendanceManageSearch{Orm: mysqlx.GetDB()}
|
}
|
|
func (slf *AttendanceManageSearch) SetOrm(tx *gorm.DB) *AttendanceManageSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetPage(page, size int) *AttendanceManageSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetIds(ids []uint) *AttendanceManageSearch {
|
slf.Ids = ids
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetPreload(preload bool) *AttendanceManageSearch {
|
slf.Preload = preload
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetMonth(month string) *AttendanceManageSearch {
|
slf.Month = month
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetDate(date string) *AttendanceManageSearch {
|
slf.Date = date
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetWorkerId(workerId string) *AttendanceManageSearch {
|
slf.WorkerId = workerId
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetKeyword(keyword string) *AttendanceManageSearch {
|
slf.Keyword = keyword
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) SetOrder(order string) *AttendanceManageSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *AttendanceManageSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
if len(slf.Ids) > 0 {
|
db = db.Where("id in (?)", slf.Ids)
|
}
|
|
if slf.Preload {
|
db = db.Model(&AttendanceManage{}).Preload("WorkType")
|
}
|
|
if slf.Month != "" {
|
db = db.Where("date like ?", slf.Month+"%")
|
}
|
|
if slf.Date != "" {
|
db = db.Where("date = ?", slf.Date)
|
}
|
|
if slf.WorkerId != "" {
|
db = db.Where("worker_id = ?", slf.WorkerId)
|
}
|
|
if slf.Keyword != "" {
|
db = db.Where("worker_name like ? or worker_id like ?", "%"+slf.Keyword+"%", "%"+slf.Keyword+"%")
|
}
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *AttendanceManageSearch) Create(record *AttendanceManage) 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 *AttendanceManageSearch) CreateBatch(record []*AttendanceManage) 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 *AttendanceManageSearch) Save(record *AttendanceManage) 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 *AttendanceManageSearch) 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 *AttendanceManageSearch) Delete() error {
|
var db = slf.build()
|
|
if err := db.Unscoped().Delete(&AttendanceManage{}).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
func (slf *AttendanceManageSearch) Find() ([]*AttendanceManage, int64, error) {
|
var (
|
records = make([]*AttendanceManage, 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 *AttendanceManageSearch) FindNotTotal() ([]*AttendanceManage, error) {
|
var (
|
records = make([]*AttendanceManage, 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 *AttendanceManageSearch) 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
|
}
|