package dao

import (
	"gorm.io/gorm"
	"server/global"
)

type DailyExpenses struct {
	global.GVA_MODEL
	Name            string            `json:"name" form:"name" gorm:"comment:名称"`
	Genre           int               `json:"genre" form:"genre" gorm:"comment:费用类型"`
	ExpensesGenre   ExpensesGenre     `json:"expensesGenre" form:"expensesGenre" gorm:"foreignKey:Genre;references:id;"`
	Applicant       string            `json:"applicant" form:"applicant" gorm:"comment:申请人"`
	ApplicationTime string            `json:"applicationTime" form:"applicationTime" gorm:"comment:申请日期"`
	Charge          int               `json:"charge" form:"charge" gorm:"comment:负责人"`
	User            SysUser           `json:"user" form:"user" gorm:"foreignKey:Charge;references:id;"`
	TotalAmount     float64           `json:"totalAmount" form:"totalAmount" gorm:"type:decimal(10,2);comment:总金额"`
	DailyFeeDetails []DailyFeeDetails `json:"dailyFeeDetails" form:"dailyFeeDetails" gorm:"-"`
}

func (DailyExpenses) TableName() string {
	return "daily_expenses"
}

// TODO:日常费用查询

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

// QueryDailyExpenses 查询日常费用详情
func QueryDailyExpenses(id int) (dailyExpenses DailyExpenses, err error) {
	err = global.GVA_DB.Model(&DailyExpenses{}).Where("id = ?", id).Preload("ExpensesGenre").Preload("User").First(&dailyExpenses).Error
	return dailyExpenses, err
}

// TODO:日常费用新增

// CreateDailyExpenses 新增日常费用
func (de DailyExpenses) CreateDailyExpenses() error {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		txErr := tx.Create(&de).Error
		if txErr != nil {
			return txErr
		}
		for _, dailyFeeDetail := range de.DailyFeeDetails {
			dailyFeeDetail.DailyFeeId = de.ID

			txErr := tx.Create(&dailyFeeDetail).Error
			if txErr != nil {
				return txErr
			}
		}
		return txErr
	})
}

// TODO:日常费用修改

// UpdateDailyExpenses 修改日常费用
func (de DailyExpenses) UpdateDailyExpenses() error {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		txErr := tx.Unscoped().Where("daily_fee_id = ?", de.ID).Delete(&DailyFeeDetails{}).Error
		if txErr != nil {
			return txErr
		}
		txErr = tx.Model(&de).Where("id = ?", de.ID).Updates(&de).Error
		if txErr != nil {
			return txErr
		}
		for _, dailyFeeDetail := range de.DailyFeeDetails {
			dailyFeeDetail.DailyFeeId = de.ID
			txErr := tx.Model(&DailyFeeDetails{}).Create(&dailyFeeDetail).Error
			if txErr != nil {
				return txErr
			}
		}
		return txErr
	})
}

// TODO:日常费用删除

func (de DailyExpenses) DeleteDailyExpenses() error {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		txErr := tx.Unscoped().Where("daily_fee_id = ?", de.ID).Delete(&DailyFeeDetails{}).Error
		if txErr != nil {
			return txErr
		}
		txErr = tx.Unscoped().Delete(&de).Error
		if txErr != nil {
			return txErr
		}
		return txErr
	})
}
