package fms

import (
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/utils"
	"time"
)

// ContractInvoice 合同开票表
type ContractInvoice struct {
	ContractInvoiceId  int       `gorm:"primaryKey;column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
	ContractRegisterId int       `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
	ContractCode       string    `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
	Amount             float64   `gorm:"column:amount" json:"amount" description:"换算后的金额(人民币)"`
	OriginAmount       float64   `gorm:"column:origin_amount" json:"origin_amount" description:"开票/到款金额"`
	CurrencyUnit       string    `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
	InvoiceType        int       `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记"`
	InvoiceDate        time.Time `gorm:"column:invoice_time" json:"invoice_time" description:"开票日期/到款月"`
	SellerId           int       `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
	SellerName         string    `gorm:"column:seller_name" json:"seller_name" description:"销售名称"`
	SellerGroupId      int       `gorm:"column:seller_group_id" json:"seller_group_id" description:"销售分组ID"`
	SellerGroupName    string    `gorm:"column:seller_group_name" json:"seller_group_name" description:"销售分组名称"`
	SellerTeamId       int       `gorm:"column:seller_team_id" json:"seller_team_id" description:"销售小组ID"`
	SellerTeamName     string    `gorm:"column:seller_team_name" json:"seller_team_name" description:"销售小组名称"`
	PayType            int       `gorm:"column:pay_type" json:"pay_type" description:"付款方式：0-无；1-年付；2-半年付；3-季付；4-次付；5-异常"`
	AdminId            int       `gorm:"column:admin_id" json:"admin_id" description:"操作人ID"`
	AdminName          string    `gorm:"column:admin_name" json:"admin_name" description:"操作人姓名"`
	Remark             string    `gorm:"column:remark" json:"remark" description:"备注信息"`
	IsDeleted          int       `gorm:"column:is_deleted" json:"is_deleted" description:"是否已删除: 0-正常; 1-已删除"`
	ServiceProductId   int       `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型：1ficc套餐，2权益套餐"`
	IsPrePay           int       `gorm:"column:is_pre_pay" json:"is_pre_pay" description:"是否预付款: 0-不是; 1-是"`
	StartDate          time.Time `gorm:"column:start_date" json:"start_date" description:"约定开始时间"`
	EndDate            time.Time `gorm:"column:end_date" json:"end_date" description:"约定结束时间"`
	PreRegisterId      int       `gorm:"column:pre_register_id" json:"pre_register_id" description:"预登记ID"`
	CreateTime         time.Time `gorm:"autoCreateTime;column:create_time" json:"create_time" description:"创建时间"`
	ModifyTime         time.Time `gorm:"autoUpdateTime:milli;column:modify_time" json:"modify_time" description:"最后更新时间"`
}

func (c *ContractInvoice) TableName() string {
	return "contract_invoice"
}

// ContractInvoiceItem 合同开票/到款
type ContractInvoiceItem struct {
	ContractInvoiceId  int     `gorm:"column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
	ContractRegisterId int     `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
	ContractCode       string  `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
	ContractStatus     int     `json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
	Amount             float64 `gorm:"column:amount" json:"amount" description:"换算金额"`
	OriginAmount       float64 `gorm:"column:origin_amount" json:"origin_amount" description:"开票/到款金额"`
	CurrencyUnit       string  `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
	UnitName           string  `json:"unit_name" description:"货币单位名称"`
	InvoiceType        int     `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记;3-开票预登记;4-到款预登记"`
	InvoiceDate        string  `gorm:"column:invoice_time" json:"invoice_time" description:"开票日期/到款月"`
	SellerId           int     `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
	SellerName         string  `gorm:"column:seller_name" json:"seller_name" description:"销售名称"`
	PayType            int     `gorm:"column:pay_type" json:"pay_type" description:"付款方式：0-无；1-年付；2-半年付；3-季付；4-次付；5-异常"`
	Remark             string  `gorm:"column:remark" json:"remark" description:"备注信息"`
	ServiceProductId   int     `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型：1ficc套餐，2权益套餐"`
	IsPrePay           int     `gorm:"column:is_pre_pay" json:"is_pre_pay" description:"是否预付款: 0-不是; 1-是"`
	StartDate          string  `gorm:"column:start_date" json:"start_date" description:"约定开始时间"`
	EndDate            string  `gorm:"column:end_date" json:"end_date" description:"约定结束时间"`
	CreateTime         string  `gorm:"column:create_time" json:"create_time" description:"创建时间"`
	CompanyName        string  `gorm:"column:company_name" json:"company_name" description:"客户名称"`
}

// ContractInvoiceSaveReq 合同开票-请求体
type ContractInvoiceSaveReq struct {
	ContractRegisterId int                        `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
	InvoiceType        int                        `json:"invoice_type" binding:"oneof=1 2 3" description:"类型: 1-开票登记; 2-到款登记; 3-预到款登记"`
	AmountList         []*ContractInvoiceSaveItem `json:"amount_list"`
}

// ContractInvoiceSaveItem 合同开票数据
type ContractInvoiceSaveItem struct {
	ServiceProductId int     `json:"service_product_id" binding:"oneof=1 2" description:"套餐类型：1ficc套餐，2权益套餐"`
	InvoiceId        int     `json:"invoice_id" description:"开票ID"`
	OriginAmount     float64 `json:"origin_amount" description:"开票(到款)金额"`
	Amount           float64 `json:"amount" description:"换算后的金额"`
	InvoiceDate      string  `json:"invoice_date" description:"开票日期/到款月"`
	Remark           string  `json:"remark" description:"备注"`
	SellerId         int     `json:"seller_id" description:"销售ID"`
}

// formatContractInvoice2ItemList 格式化ContractInvoice
func formatContractInvoice2ItemList(list []*ContractInvoice) (itemList []*ContractInvoiceItem) {
	itemList = make([]*ContractInvoiceItem, 0)
	for i := range list {
		itemList = append(itemList, &ContractInvoiceItem{
			ContractInvoiceId:  list[i].ContractInvoiceId,
			ContractRegisterId: list[i].ContractRegisterId,
			ContractCode:       list[i].ContractCode,
			Amount:             list[i].Amount,
			OriginAmount:       list[i].OriginAmount,
			CurrencyUnit:       list[i].CurrencyUnit,
			InvoiceType:        list[i].InvoiceType,
			InvoiceDate:        utils.TimeTransferString(utils.FormatDate, list[i].InvoiceDate),
			SellerId:           list[i].SellerId,
			SellerName:         list[i].SellerName,
			PayType:            list[i].PayType,
			Remark:             list[i].Remark,
			ServiceProductId:   list[i].ServiceProductId,
			IsPrePay:           list[i].IsPrePay,
			StartDate:          list[i].StartDate.Format(utils.FormatDate),
			EndDate:            list[i].StartDate.Format(utils.FormatDate),
			CreateTime:         utils.TimeTransferString(utils.FormatDateTime, list[i].CreateTime),
		})
	}
	return
}

// formatContractInvoice2Item 格式化ContractInvoiceItem
func formatContractInvoice2Item(item *ContractInvoiceRespItem) (formatItem *ContractInvoiceItem) {
	formatItem = new(ContractInvoiceItem)
	formatItem.ContractInvoiceId = item.ContractInvoiceId
	formatItem.ContractRegisterId = item.ContractRegisterId
	formatItem.ContractCode = item.ContractCode
	formatItem.Amount = item.Amount
	formatItem.OriginAmount = item.OriginAmount
	formatItem.CurrencyUnit = item.CurrencyUnit
	formatItem.InvoiceType = item.InvoiceType
	formatItem.InvoiceDate = utils.TimeTransferString(utils.FormatDate, item.InvoiceDate)
	formatItem.SellerId = item.SellerId
	formatItem.SellerName = item.SellerName
	formatItem.PayType = item.PayType
	formatItem.Remark = item.Remark
	formatItem.IsPrePay = item.IsPrePay
	formatItem.ServiceProductId = item.ServiceProductId
	formatItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, item.CreateTime)
	formatItem.CompanyName = item.CompanyName
	return
}

// InvoicePaymentCensusResp 商品到款统计响应体
type InvoicePaymentCensusRespItem struct {
	DataList             []*InvoicePaymentCensusItem `json:"data_list"`
	InvoiceTotal         float64                     `json:"invoice_total" description:"开票总金额(换算后)"`
	PaymentTotal         float64                     `json:"payment_total" description:"到款总金额(换算后)"`
	AmountTotal          float64                     `json:"amount_total" description:"合计总金额(换算后)"`
	InvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"invoice_currency_total" description:"开票-分币种总金额"`
	PaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"payment_currency_total" description:"到款-分币种总金额"`
}
type InvoicePaymentCensusResp struct {
	InvoicePaymentCensusRespItem
	Paging *paging.PagingItem
}

// InvoicePaymentCensusItem 商品到款统计信息
type InvoicePaymentCensusItem struct {
	SummaryId          int                         `json:"summary_id" description:"汇总ID"`
	ContractRegisterId int                         `json:"contract_register_id" description:"登记ID"`
	CompanyName        string                      `json:"company_name" description:"客户名称"`
	NewCompany         int                         `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
	StartDate          string                      `json:"start_date" description:"合同开始日期"`
	EndDate            string                      `json:"end_date" description:"合同结束日期"`
	ServicesName       string                      `json:"services_name" description:"套餐信息字符串拼接"`
	ContractType       int                         `json:"contract_type" description:"合同类型: 0-无 1-新签; 2-续约"`
	ActualPayCompanies string                      `json:"actual_pay_companies" description:"该合同关联的所有代付方, 英文逗号拼接"`
	InvoicePaymentList []*InvoicePaymentCensusInfo `json:"invoice_payment_list" description:"开票到款列表"`
	ContractCode       string                      `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
	ContractAmount     float64                     `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
	UnitName           string                      `gorm:"unit_name" json:"unit_name" description:"单位名称"`
	ProductIds         string                      `gorm:"column:product_ids" json:"product_ids" description:"产品ID：1-FICC; 2-权益, 如果两者都有，则用英文逗号拼接"`
	NotInvoiceTotal    float64                     `json:"not_invoice_total" description:"未开票总金额(换算后)"`
}

// InvoicePaymentCensusInfo 开票到款统计信息
type InvoicePaymentCensusInfo struct {
	InvoiceId         int                                 `json:"invoice_id" description:"开票ID"`
	InvoiceDate       string                              `json:"invoice_time" description:"开票日期"`
	InvoiceAmount     float64                             `json:"invoice_amount" description:"开票金额"`
	SellerId          int                                 `json:"seller_id" description:"销售ID"`
	SellerName        string                              `json:"seller_name" description:"销售名称"`
	SellerGroupId     int                                 `json:"seller_group_id" description:"销售组别ID"`
	SellerGroupName   string                              `json:"seller_group_name" description:"销售组别名称"`
	PaymentId         int                                 `json:"payment_id" description:"到款ID"`
	PaymentDate       string                              `json:"payment_date" description:"到款日期"`
	PaymentAmount     float64                             `json:"payment_amount" description:"到款金额"`
	PayType           int                                 `json:"pay_type" description:"付款方式：0-无；1-年付；2-半年付；3-季付；4-次付；5-异常"`
	ServiceAmountList []*ContractPaymentServiceAmountItem `json:"service_amount_list" description:"到款套餐金额分配信息"`
	SellerType        int                                 `json:"seller_type" description:"销售类型：1ficc销售，2权益销售"`
}

// ContractInvoiceAmountTotal 开票到款金额合计信息
type ContractInvoiceAmountTotal struct {
	InvoiceType int     `json:"invoice_type" description:"类型: 1-开票; 2-到款"`
	TotalAmount float64 `json:"total_amount" description:"金额合计"`
}

// UpdatePaymentPayTypeReq 到款登记-修改付款方式请求体
type UpdatePaymentPayTypeReq struct {
	ContractPaymentId int `json:"contract_payment_id" binding:"required,gte=1" description:"到款登记ID"`
	PayType           int `json:"pay_type" binding:"oneof=1 2 3 4" description:"付款方式: 1-年付; 2-半年付; 3-季付; 4-次付"`
}

// CensusSellerGroupInvoiceItem 销售组开票统计数据
type CensusSellerGroupInvoiceItem struct {
	GroupId       int     `json:"group_id" description:"分组ID"`
	GroupName     string  `json:"group_name" description:"分组名称"`
	InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
	GroupRate     float64 `json:"group_rate" description:"分组占比"`
}

// CensusSellerInvoiceItem 销售组开票统计数据
type CensusSellerInvoiceItem struct {
	SellerId      int     `json:"seller_id" description:"销售ID"`
	SellerName    string  `json:"seller_name" description:"销售名称"`
	GroupId       int     `json:"group_id" description:"分组ID"`
	GroupName     string  `json:"group_name" description:"分组名称"`
	InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
	GroupRate     float64 `json:"group_rate" description:"分组占比"`
	SellerRate    float64 `json:"seller_rate" description:"全员占比"`
}

// InvoiceListCurrencyTotal 开票/到款列表分币种合计信息
type InvoiceListCurrencyTotal struct {
	Name     string  `json:"name" description:"货币名称"`
	UnitName string  `json:"unit_name" description:"单位名称"`
	Code     string  `json:"code" description:"国际代码"`
	Amount   float64 `json:"amount" description:"金额"`
	FlagImg  string  `json:"flag_img" description:"国旗图标"`
}

// InvoiceListCurrencySum 开票/到款列表分币种总和
type InvoiceListCurrencySum struct {
	CurrencyUnit      string  `json:"currency_unit" description:"货币代码"`
	InvoiceType       int     `json:"invoice_type" description:"开票类型：1-开票; 2-到款"`
	AmountTotal       float64 `json:"amount_total" description:"换算后合计金额"`
	OriginAmountTotal float64 `json:"origin_amount_total" description:"原合计金额"`
}

// ContractInvoiceDetailItem 预登记列表详情用-invoice_time换register_time
type ContractInvoiceDetailItem struct {
	ContractInvoiceId  int     `gorm:"column:contract_invoice_id" json:"contract_invoice_id" description:"开票ID"`
	ContractRegisterId int     `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
	ContractCode       string  `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
	ContractStatus     int     `json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
	OriginAmount       float64 `gorm:"column:amount" json:"amount" description:"开票/到款金额"`
	CurrencyUnit       string  `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
	UnitName           string  `json:"unit_name" description:"货币单位名称"`
	InvoiceType        int     `gorm:"column:invoice_type" json:"invoice_type" description:"类型: 1-开票登记; 2-到款登记;3-开票预登记;4-到款预登记"`
	InvoiceDate        string  `gorm:"column:register_date" json:"register_date" description:"开票日期/到款月"`
	SellerId           int     `gorm:"column:seller_id" json:"seller_id" description:"销售ID"`
	SellerName         string  `gorm:"column:seller_name" json:"seller_name" description:"销售名称"`
	PayType            int     `gorm:"column:pay_type" json:"pay_type" description:"付款方式：0-无；1-年付；2-半年付；3-季付；4-次付；5-异常"`
	Remark             string  `gorm:"column:remark" json:"remark" description:"备注信息"`
	ServiceProductId   int     `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型：1ficc套餐，2权益套餐"`
	IsPrePay           int     `gorm:"column:is_pre_pay" json:"is_pre_pay" description:"是否预付款: 0-不是; 1-是"`
	StartDate          string  `gorm:"column:start_date" json:"start_date" description:"约定开始时间"`
	EndDate            string  `gorm:"column:end_date" json:"end_date" description:"约定结束时间"`
	CreateTime         string  `gorm:"column:create_time" json:"create_time" description:"创建时间"`
	PreRegisterId      int     `gorm:"column:pre_register_id" json:"pre_register_id" description:"预登记ID"`
}

type ContractInvoiceRespItem struct {
	ContractInvoice
	CompanyName string `gorm:"column:company_name" json:"company_name" description:"客户名称"`
}

type DupInvoice struct {
	ContractInvoice
	TemplateIds string
}
