79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package dao
|
|
|
|
import (
|
|
"enterprise/common/model"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"time"
|
|
)
|
|
|
|
type SalaryCalculatorDao struct {
|
|
}
|
|
|
|
func NewSalaryCalculatorDao() *SalaryCalculatorDao {
|
|
return &SalaryCalculatorDao{}
|
|
}
|
|
|
|
func (d *SalaryCalculatorDao) TableName() string {
|
|
return "salary_calculator"
|
|
}
|
|
|
|
func (d *SalaryCalculatorDao) Create(o *model.SalaryCalculator) (int64, error) {
|
|
o.CreateTime = time.Now().Unix()
|
|
res := GetDB().Table(d.TableName()).Create(o)
|
|
return o.Id, res.Error
|
|
}
|
|
|
|
func (d *SalaryCalculatorDao) Update(o *model.SalaryCalculator) error {
|
|
o.UpdateTime = time.Now().Unix()
|
|
tx := GetDB().Table(d.TableName())
|
|
res := tx.Save(o)
|
|
return res.Error
|
|
}
|
|
|
|
func (d *SalaryCalculatorDao) Delete(id int64) error {
|
|
res := GetDB().Table(d.TableName()).Delete(&model.SalaryCalculator{}, id)
|
|
return res.Error
|
|
}
|
|
|
|
func (d *SalaryCalculatorDao) Get(id int64) (*model.SalaryCalculator, error) {
|
|
var u model.SalaryCalculator
|
|
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 *SalaryCalculatorDao) Query(page, size int, corpId int64, name string) ([]*model.SalaryCalculator, int64, error) {
|
|
var u []*model.SalaryCalculator
|
|
tx := GetDB().Table(d.TableName())
|
|
|
|
tx.Where("corp_id = ?", corpId)
|
|
|
|
if name != "" {
|
|
tx = tx.Where("name LIKE ?", fmt.Sprintf("%%%s%%", name))
|
|
}
|
|
|
|
var count int64
|
|
tx.Count(&count)
|
|
tx.Offset((page - 1) * size).Limit(size)
|
|
tx.Order("create_time DESC")
|
|
|
|
res := tx.Find(&u)
|
|
if res.Error == gorm.ErrRecordNotFound {
|
|
return nil, 0, nil
|
|
}
|
|
|
|
if res.Error != nil {
|
|
return nil, 0, res.Error
|
|
}
|
|
return u, count, nil
|
|
}
|