package dao

import (
	"errors"
	"youngee_b_api/app/entity"
)

type InvoiceRecordDao struct{}

func (d InvoiceRecordDao) Insert(invoiceRecord *entity.InvoiceRecord) error {
	err := Db.Debug().Model(&entity.InvoiceRecord{}).Omit("billing_at").Create(invoiceRecord).Error
	if err != nil {
		return err
	}
	return nil
}

// 开票记录
func (d InvoiceRecordDao) GetBillList(enterpriseId string, subAccountId int64, status int64, page int, pageSize int) ([]*entity.InvoiceRecord, int64, error) {
	invoiceRecords := []*entity.InvoiceRecord{}
	var total int64
	query := Db.Debug().Model(&entity.InvoiceRecord{}).Where("status = ?", status)
	if subAccountId == 0 {
		if enterpriseId == "" {
			return invoiceRecords, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", enterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", subAccountId)
	}
	query.Count(&total)
	query = query.Select("billing_id, enterprise_id, sub_account_id, invoice_amount, invoice_body, invoice_type, task_ids, status, submit_at, billing_at, invoice_url")
	offset := (page - 1) * pageSize
	var err error
	if status == 1 {
		err = query.Order("submit_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
	} else if status == 2 {
		err = query.Order("billing_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
	}
	if err != nil {
		return nil, 0, err
	}

	return invoiceRecords, total, nil
}

// 可开票账单——电商带货
func (d InvoiceRecordDao) GetBillableSelectionList(enterpriseId string, subAccountId int64, page int, pageSize int) ([]*entity.SelectionInfo, int64, error) {
	billableSelections := []*entity.SelectionInfo{}
	var total int64
	query := Db.Debug().Model(&entity.SelectionInfo{}).Where("selection_status = ? AND invoice_status = ?", 8, 0)
	if subAccountId == 0 {
		if enterpriseId == "" {
			return billableSelections, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", enterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", subAccountId)
	}
	query.Count(&total)
	query = query.Select("selection_id, enterprise_id, sub_account_id, product_id, platform, settlement_amount")
	//offset := (page - 1) * pageSize
	//err := query.Order("finish_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
	err := query.Order("finish_at desc").Find(&billableSelections).Error
	if err != nil {
		return nil, 0, err
	}

	return billableSelections, total, nil
}

// 可开票账单——品牌种草
func (d InvoiceRecordDao) GetBillableProjectList(enterpriseId string, subAccountId int64, page int, pageSize int) ([]*entity.Project, int64, error) {
	billableProjects := []*entity.Project{}
	var total int64
	query := Db.Debug().Model(&entity.Project{}).Where("project_status = ? AND invoice_status = ?", 10, 0)
	if subAccountId == 0 {
		if enterpriseId == "" {
			return billableProjects, 0, errors.New("enterpriseId is empty")
		}
		query = query.Where("enterprise_id = ?", enterpriseId)
	} else {
		query = query.Where("sub_account_id = ?", subAccountId)
	}
	query.Count(&total)
	query = query.Select("project_id, enterprise_id, sub_account_id, product_id, project_platform, settlement_amount")
	//offset := (page - 1) * pageSize
	//err := query.Order("finish_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
	err := query.Order("finish_at desc").Find(&billableProjects).Error
	if err != nil {
		return nil, 0, err
	}

	return billableProjects, total, nil
}

// 可开票账单——本地生活
//func (d InvoiceRecordDao) GetBillableProjectList(enterpriseId string, subAccountId int64, page int, pageSize int) ([]*entity.Project, int64, error) {
//	billableProjects := []*entity.Project{}
//	var total int64
//	query := Db.Debug().Model(&entity.Project{}).Where("project_status = ? AND invoice_status = ?", 10, 0)
//	if subAccountId == 0 {
//		if enterpriseId == "" {
//			return billableProjects, 0, errors.New("enterpriseId is empty")
//		}
//		query = query.Where("enterprise_id = ?", enterpriseId)
//	} else {
//		query = query.Where("sub_account_id = ?", subAccountId)
//	}
//	query.Count(&total)
//	query = query.Select("project_id, enterprise_id, sub_account_id, product_id, project_platform, settlement_amount")
//	//offset := (page - 1) * pageSize
//	//err := query.Order("finish_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
//	err := query.Order("finish_at desc").Find(&billableProjects).Error
//	if err != nil {
//		return nil, 0, err
//	}
//
//	return billableProjects, total, nil
//}

// 获取指定企业id的开票中/已开票金额
func (d InvoiceRecordDao) GetInvoiceAmount(enterpriseId string, status int64) (float64, error) {
	var totalAmount float64
	query := Db.Debug().Model(&entity.InvoiceRecord{})
	err := query.Where("enterprise_id = ? AND status = ?", enterpriseId, status).Select("COALESCE(SUM(invoice_amount), 0)").Scan(&totalAmount).Error
	if err != nil {
		return 0, err
	}
	return totalAmount, nil
}
