package dao

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

type Reimbursement struct {
	global.GVA_MODEL
	ProjectCode        string          `json:"projectCode" form:"projectCode" gorm:"comment:项目编号"`
	Name               string          `json:"name" form:"name" gorm:"comment:项目名称"`
	Applicant          string          `json:"applicant" form:"applicant" gorm:"comment:报销申请人"`
	ApplicationTime    string          `json:"applicationTime" form:"applicationTime" gorm:"comment:申请日期"`
	Approval           int             `json:"approval" form:"approval" gorm:"comment:报销审批人"`
	User               SysUser         `json:"user" form:"user" gorm:"foreignKey:Approval;references:id;"`
	ReimbursementPrice decimal.Decimal `json:"reimbursementPrice" form:"reimbursementPrice" gorm:"comment:项目金额;type:decimal(10,2)"`
	Genre              int             `json:"genre" form:"genre" gorm:"comment:费用类型"`
	ExpensesGenre      ExpensesGenre   `json:"expensesGenre" form:"expensesGenre" gorm:"foreignKey:Genre;references:id;"`
	FeeDetails         []FeeDetails    `json:"feeDetails" form:"feeDetails" gorm:"-"`
}

func (Reimbursement) TableName() string {
	return "reimbursement"
}

// TODO:报销查询

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

func QueryReimbursement(code string) (reimbursement []Reimbursement, err error) {
	err = global.GVA_DB.Model(&Reimbursement{}).Where("project_code = ?", code).Preload("ExpensesGenre").Preload("User").Find(&reimbursement).Error
	return reimbursement, err
}

// TODO:报销新增

// CreateReimbursement 新增报销单
func (r Reimbursement) CreateReimbursement() error {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		txErr := tx.Create(&r).Error
		if txErr != nil {
			return txErr
		}
		for _, feeDetail := range r.FeeDetails {
			feeDetail.ReimbursementId = r.ID
			txErr := tx.Create(&feeDetail).Error
			if txErr != nil {
				return txErr
			}
		}
		return txErr
	})
}

// TODO:报销修改

// UpdateReimbursement 修改报销单
func (r Reimbursement) UpdateReimbursement() error {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		txErr := tx.Unscoped().Where("reimbursement_id = ?", r.ID).Delete(&FeeDetails{}).Error
		if txErr != nil {
			return txErr
		}
		txErr = tx.Model(&r).Where("id = ?", r.ID).Updates(&r).Error
		if txErr != nil {
			return txErr
		}
		for _, feeDetail := range r.FeeDetails {
			feeDetail.ReimbursementId = r.ID
			txErr := tx.Create(&feeDetail).Error
			if txErr != nil {
				return txErr
			}
		}
		return txErr
	})
}

// TODO:报销删除

// DeleteReimbursement 删除报销单
func (r Reimbursement) DeleteReimbursement() error {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		txErr := tx.Unscoped().Where("reimbursement_id = ?", r.ID).Delete(&FeeDetails{}).Error
		if txErr != nil {
			return txErr
		}
		txErr = tx.Unscoped().Delete(&r).Error
		if txErr != nil {
			return txErr
		}
		return txErr
	})
}
