2023-09-01 20:01:30 +08:00
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
|
|
|
"enterprise/common/model"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StaffSalaryDao struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStaffSalaryDao() *StaffSalaryDao {
|
|
|
|
return &StaffSalaryDao{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *StaffSalaryDao) TableName() string {
|
|
|
|
return "staff_salary"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *StaffSalaryDao) Create(o *model.StaffSalary) (int64, error) {
|
|
|
|
o.CreateTime = time.Now().Unix()
|
|
|
|
res := GetDB().Table(d.TableName()).Create(o)
|
|
|
|
return o.Id, res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *StaffSalaryDao) Update(o *model.StaffSalary) error {
|
|
|
|
o.UpdateTime = time.Now().Unix()
|
|
|
|
tx := GetDB().Table(d.TableName())
|
|
|
|
res := tx.Save(o)
|
|
|
|
return res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *StaffSalaryDao) Delete(id int64) error {
|
|
|
|
res := GetDB().Table(d.TableName()).Delete(&model.StaffSalary{}, id)
|
|
|
|
return res.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *StaffSalaryDao) Get(id int64) (*model.StaffSalary, error) {
|
|
|
|
var u model.StaffSalary
|
|
|
|
tx := GetDB().Table(d.TableName())
|
|
|
|
tx = tx.Where("id = ?", id)
|
|
|
|
res := tx.First(&u)
|
|
|
|
if res.Error == gorm.ErrRecordNotFound {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
return nil, res.Error
|
|
|
|
}
|
|
|
|
return &u, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *StaffSalaryDao) GetBy(username, month string) (*model.StaffSalary, error) {
|
|
|
|
var u model.StaffSalary
|
|
|
|
tx := GetDB().Table(d.TableName())
|
|
|
|
tx = tx.Where("username = ?", username)
|
|
|
|
tx = tx.Where("month = ?", month)
|
|
|
|
res := tx.First(&u)
|
|
|
|
if res.Error == gorm.ErrRecordNotFound {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
return nil, res.Error
|
|
|
|
}
|
|
|
|
return &u, nil
|
|
|
|
}
|
|
|
|
|
2024-11-04 23:30:31 +08:00
|
|
|
func (d *StaffSalaryDao) Query(month, username string) ([]*model.StaffSalary, error) {
|
2023-09-01 20:01:30 +08:00
|
|
|
var u []*model.StaffSalary
|
|
|
|
tx := GetDB().Table(d.TableName())
|
2023-09-04 21:58:26 +08:00
|
|
|
if month != "" {
|
|
|
|
tx = tx.Where("month = ?", month)
|
2023-09-01 20:01:30 +08:00
|
|
|
}
|
2024-11-04 23:30:31 +08:00
|
|
|
if username != "" {
|
|
|
|
tx.Where("username = ?", username)
|
|
|
|
}
|
|
|
|
tx.Order("month DESC")
|
2023-09-01 20:01:30 +08:00
|
|
|
|
|
|
|
res := tx.Find(&u)
|
|
|
|
if res.Error == gorm.ErrRecordNotFound {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
return nil, res.Error
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|