enterprise/common/dao/approval_payment.go

168 lines
3.8 KiB
Go
Raw Normal View History

2024-03-09 20:09:51 +08:00
package dao
import (
"enterprise/common/model"
2025-03-14 16:39:33 +08:00
"fmt"
2024-03-09 20:09:51 +08:00
"gorm.io/gorm"
2025-03-14 16:39:33 +08:00
"sort"
2024-03-09 20:09:51 +08:00
"time"
)
type ApprovalPaymentDao struct {
}
func NewApprovalPaymentDao() *ApprovalPaymentDao {
return &ApprovalPaymentDao{}
}
func (d *ApprovalPaymentDao) TableName() string {
return "approval_payment"
}
func (d *ApprovalPaymentDao) Create(o *model.ApprovalPayment) (int64, error) {
o.CreateTime = time.Now().Unix()
res := GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *ApprovalPaymentDao) Update(o *model.ApprovalPayment) error {
o.UpdateTime = time.Now().Unix()
tx := GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *ApprovalPaymentDao) Delete(id int64) error {
res := GetDB().Table(d.TableName()).Delete(&model.ApprovalPayment{}, id)
return res.Error
}
func (d *ApprovalPaymentDao) Get(id int64) (*model.ApprovalPayment, error) {
var u model.ApprovalPayment
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
}
2025-01-08 12:59:56 +08:00
func (d *ApprovalPaymentDao) GetBySpNo(corpId int64, spNo string) (*model.ApprovalPayment, error) {
2024-03-09 20:09:51 +08:00
var u model.ApprovalPayment
tx := GetDB().Table(d.TableName())
tx = tx.Where("sp_no = ?", spNo)
2025-01-08 12:59:56 +08:00
tx = tx.Where("corp_id = ?", corpId)
2024-03-09 20:09:51 +08:00
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 *ApprovalPaymentDao) GetByUsername(username, month string) ([]*model.ApprovalPayment, error) {
var u []*model.ApprovalPayment
tx := GetDB().Table(d.TableName())
tx = tx.Where("month = ?", month)
tx = tx.Where("username = ?", username)
res := tx.Find(&u)
if res.Error != nil {
return nil, res.Error
}
return u, nil
}
2025-03-14 16:39:33 +08:00
func (d *ApprovalPaymentDao) Query(page, size int, corpId int64, username, startDay, endDay, payer, payee string) ([]*model.ApprovalPayment, int64, error) {
var u []*model.ApprovalPayment
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
if username != "" {
tx.Where("username = ?", username)
}
if startDay != "" {
tx.Where("payment_date >= ?", startDay)
}
if endDay != "" {
tx.Where("payment_date <= ?", endDay)
}
if payer != "" {
tx.Where("payment_pyer <= ?", payer)
}
if payee != "" {
tx.Where("payment_pyee <= ?", payee)
}
var count int64
tx.Count(&count)
tx.Offset((page - 1) * size).Limit(size)
res := tx.Find(&u)
if res.Error != nil {
return nil, 0, res.Error
}
return u, count, nil
}
2025-03-14 17:56:55 +08:00
func (d *ApprovalPaymentDao) QueryAmount(corpId int64, username, startDay, endDay, payer, payee string) (int64, error) {
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
if username != "" {
tx.Where("username = ?", username)
}
if startDay != "" {
tx.Where("payment_date >= ?", startDay)
}
if endDay != "" {
tx.Where("payment_date <= ?", endDay)
}
if payer != "" {
tx.Where("payment_pyer <= ?", payer)
}
if payee != "" {
tx.Where("payment_pyee <= ?", payee)
}
var sumAmount float64
var count int64
tx.Count(&count)
if count != 0 {
tx.Select("SUM(payment_amount) AS payment_amount").Pluck("payment_amount", &sumAmount)
if tx.Error != nil {
return 0, tx.Error
}
}
return int64(100 * sumAmount), nil
}
2025-03-14 16:39:33 +08:00
func (d *ApprovalPaymentDao) Count(corpId int64, startDay, endDay string, field string) ([]*CountValue, error) {
var u []*CountValue
tx := GetDB().Table(d.TableName())
tx.Where("corp_id = ?", corpId)
if startDay != "" {
tx.Where("payment_date >= ?", startDay)
}
if endDay != "" {
tx.Where("payment_date <= ?", endDay)
}
tx.Select(fmt.Sprintf("%s AS `key`,COUNT(1)", field))
tx.Group(field)
res := tx.Find(&u)
if res.Error != nil {
return nil, res.Error
}
sort.Sort(SortCountValue(u))
return u, nil
}