package dao

import (
	"github.com/shopspring/decimal"
	"server/global"
)

type Collection struct {
	global.GVA_MODEL
	ProjectCode     string          `json:"projectCode" from:"projectCode" gorm:"comment:项目编码"`
	CollectionPrice decimal.Decimal `json:"collectionPrice" from:"collectionPrice" gorm:"type:decimal(10,2);not null;comment:收款金额"`
	Remarks         string          `json:"remarks" from:"remarks" gorm:"comment:备注"`
	CollectionTime  string          `json:"collectionTime" from:"collectionTime" gorm:"comment:收款日期;type:datetime"`
	SerialNumber    string          `json:"serialNumber" from:"serialNumber" gorm:"comment:银行流水单号"`
}

func (Collection) TableName() string {
	return "collection"
}

// TODO:收款查询

// QueryCollection 查询收款
func QueryCollection(code string) (collection []Collection, err error) {
	err = global.GVA_DB.Model(&Collection{}).Where("project_code = ?", code).Find(&collection).Error
	return collection, err
}

// QueryCollectionList 查询报销列表
func QueryCollectionList(limit, offset int, code, time string) (collection []Collection, total int64, err error) {
	// 创建db
	db := global.GVA_DB.Model(&Collection{}).Where("project_code = ?", code)
	// 如果有条件搜索 下方会自动创建搜索语句
	if time != "" {
		db = db.Where("DATE_FORMAT(collection_time, '%Y-%m') = ?", time)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("id desc").Limit(limit).Offset(offset).Find(&collection).Error
	return collection, total, err
}

// TODO:收款新增

// CreateCollection 新增收款
func (c Collection) CreateCollection() error {
	return global.GVA_DB.Create(&c).Error
}

// TODO:收款修改

// UpdateCollection 修改收款
func (c Collection) UpdateCollection() error {
	return global.GVA_DB.Model(&c).Where("id = ?", c.ID).Updates(&c).Error
}

// TODO:收款删除

// DeleteCollection 删除收款
func (c Collection) DeleteCollection() error {
	return global.GVA_DB.Unscoped().Delete(&c).Error
}
