package dto

import (
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

type CreditAccountItem struct {
	CreditAccountId           int     `json:"creditAccountId,string"`    // 账期结算单ID
	ActuallyPaidAmount        float64 `json:"actuallyPaidAmount"`        // 账期结算实付金额
	CreditAccountOrderNum     string  `json:"creditAccountOrderNum"`     // 账期结算单号
	PaymentStatus             int     `json:"paymentStatus"`             // 账期结算支付状态,1待支付,2已支付
	PaymentTime               int64   `json:"paymentTime"`               // 共创账期结算支付时间
	SettlementAmount          float64 `json:"settlementAmount"`          // 账期结算金额
	SettlementTime            int64   `json:"settlementTime"`            // 共创账期结算时间
	CooperationContractNumber string  `json:"cooperationContractNumber"` // 关联共创合约编号
	Participator              struct {
		UserName  string `json:"userName"`  // 用户姓名
		UserPhone string `json:"userPhone"` // 用户手机号
		UserType  int    `json:"userType"`  // 用户类型,1员工,2共创用户,3公开
	} `json:"participator"` // 参与人
	ParticipateType           string             `json:"participateType"`           // 参与类型
	PaymentDocumentAttachment *domain.Attachment `json:"paymentDocumentAttachment"` // 支付凭证附件
	Org                       struct {
		OrgID   int    `json:"orgId,string"`
		OrgName string `json:"orgName"`
	} `json:"org"` // 数据所属组织机构
	Company       domain.CompanyData `json:"-"`         // TODO:暂时不需要公司
	CreatedAt     int64              `json:"createdAt"` // 创建时间
	UpdatedAt     int64              `json:"updatedAt"` // 更新时间
	AccountDetail []struct {
		DividendsEstimateOrderNumber string  `json:"dividendsEstimateOrderNumber"`
		DividendsType                int     `json:"dividendsType"`
		DividendsAmount              float64 `json:"dividendsAmount"`
	} `json:"accountDetail"` //结算明细
	Operator struct {
		UserId     int    `json:"userId,string,"`     // 用户ID,
		UserBaseId int    `json:"userBaseId,string,"` // 用户基本id
		UserName   string `json:"userName"`           // 用户姓名
		UserPhone  string `json:"userPhone"`          // 用户手机号
		UserInfo   struct {
			UserName    string `json:"userName"`    // 共创人员姓名
			UserPhone   string `json:"userPhone"`   // 用户手机号
			UserAccount string `json:"userAccount"` // 用户账号,
		} `json:"userInfo"`
	} `json:"operator"`
	AuthFlag bool `json:"authFlag"`
}

func ToCreditAccountItem(param *allied_creation_cooperation.CreditAccount, operatorOrgId int64) *CreditAccountItem {
	data := CreditAccountItem{
		CreditAccountId:           param.CreditAccountId,
		ActuallyPaidAmount:        param.ActuallyPaidAmount,
		CreditAccountOrderNum:     param.CreditAccountOrderNum,
		PaymentStatus:             param.PaymentStatus,
		PaymentTime:               0,
		SettlementAmount:          param.SettlementAmount,
		SettlementTime:            0,
		CooperationContractNumber: param.CooperationContractNumber,
		ParticipateType:           param.ParticipateType,            // 参与类型
		PaymentDocumentAttachment: nil,                              // 支付凭证附件
		Company:                   param.Company,                    // 公司
		CreatedAt:                 param.CreatedAt.UnixNano() / 1e6, // 创建时间
		UpdatedAt:                 param.UpdatedAt.UnixNano() / 1e6, // 更新时间
		AccountDetail:             param.AccountDetail,
		AuthFlag:                  param.Org.OrgID == int(operatorOrgId),
	}
	if !param.PaymentTime.IsZero() {
		data.PaymentTime = param.PaymentTime.UnixNano() / 1e6
	}
	if !param.SettlementTime.IsZero() {
		data.SettlementTime = param.SettlementTime.Unix() * 1e3
	}
	if len(param.PaymentDocumentAttachment.Url) > 0 {
		data.PaymentDocumentAttachment = &param.PaymentDocumentAttachment
	}
	data.Org.OrgID = param.Org.OrgID
	data.Org.OrgName = param.Org.OrgName
	data.Participator.UserName = param.Participator.UserName
	data.Participator.UserPhone = param.Participator.UserPhone
	data.Participator.UserType = param.Participator.UserType
	data.Operator.UserId = param.Operator.UserId
	data.Operator.UserBaseId = param.Operator.UserBaseId
	data.Operator.UserName = param.Operator.UserName
	data.Operator.UserPhone = param.Operator.UserPhone
	data.Operator.UserInfo.UserName = param.Operator.UserName
	data.Operator.UserInfo.UserPhone = param.Operator.UserPhone
	return &data
}