package dao

import (
	"gorm.io/gorm"
	"youngee_b_api/app/entity"
)

type InvoiceInfoDao struct{}

func (d InvoiceInfoDao) Select(invoiceId int64) (*entity.InvoiceInfo, error) {
	var invoiceInfo entity.InvoiceInfo
	err := Db.Debug().Model(&entity.InvoiceInfo{}).Where("invoice_id = ?", invoiceId).Find(&invoiceInfo).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return &invoiceInfo, nil
		}
		return nil, err
	}
	return &invoiceInfo, nil
}

func (d InvoiceInfoDao) Delete(invoiceId int64) error {
	var invoiceInfo entity.InvoiceInfo
	err := Db.Debug().Model(&entity.InvoiceInfo{}).Where("invoice_id = ?", invoiceId).Delete(&invoiceInfo).Error
	if err != nil {
		return err
	}
	return nil
}

func (d InvoiceInfoDao) SelectDefault(enterpriseId string, invoiceType int64, isDefault int64) (*entity.InvoiceInfo, error) {
	var invoiceInfo entity.InvoiceInfo
	err := Db.Debug().Model(&entity.InvoiceInfo{}).Where("enterprise_id = ? AND invoice_type = ? AND is_default = ?", enterpriseId, invoiceType, isDefault).Find(&invoiceInfo).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return &invoiceInfo, nil
		}
		return nil, err
	}
	return &invoiceInfo, nil
}

func (d InvoiceInfoDao) Insert(invoiceInfo *entity.InvoiceInfo) error {
	err := Db.Debug().Model(&entity.InvoiceInfo{}).Create(invoiceInfo).Error
	if err != nil {
		return err
	}
	return nil
}
