package logic

import (
	"github.com/fonchain/fonchain-artistinfo/cmd/internal/dao"
	statement "github.com/fonchain/fonchain-artistinfo/pb/artistinfoStatement"
)

type IStatement interface {
	StatementList(req *statement.StatementListRequest) (rep *statement.StatementListRespond, err error)
	GetTxInfoByBatchUid(req *statement.GetTxInfoByBatchUidRequest) (rep *statement.GetTxInfoByBatchUidRespond, err error)
	GetCopyInfoByBatchUid(req *statement.GetCopyInfoByBatchUidRequest) (rep *statement.GetCopyInfoByBatchUidRespond, err error)
}

func NewStatement() IStatement {
	return &Statement{}
}

type Statement struct {
}

func (a *Statement) StatementList(req *statement.StatementListRequest) (rep *statement.StatementListRespond, err error) {
	//查看是否有该画家
	user, err := dao.GetArtistInfoById(req.ArtistUid)
	if err != nil {
		return
	}

	ContractData, err := dao.StatementList(user.MgmtArtistUid, req.State)
	if err != nil {
		return
	}

	rep = &statement.StatementListRespond{
		Data: ContractData,
	}

	return
}

func (a *Statement) GetTxInfoByBatchUid(req *statement.GetTxInfoByBatchUidRequest) (rep *statement.GetTxInfoByBatchUidRespond, err error) {

	rep = &statement.GetTxInfoByBatchUidRespond{}

	//获取画家名
	artworkTx, err := dao.GetTxArtistNameByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取物权画作详细信息
	artworkTxDetails, err := dao.GetArtworkTxDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取物权画作被售卖详细信息
	artworkSoldTxDetails, err := dao.GetArtworkSoldTxDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}

	artworkTxDetail := new(statement.ArtworkTxDetail)
	artworkSoldTxDetail := new(statement.ArtworkSoldTxDetail)

	for _, v := range artworkTxDetails {
		artworkTxDetail.TfNum = v.TfNum
		artworkTxDetail.ArtworkName = v.ArtworkName
		artworkTxDetail.Ruler = v.Ruler
		artworkTxDetail.MinPrice = v.MinPrice
		artworkTxDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkTxDetail = append(rep.ArtworkTxDetail, artworkTxDetail)
	}

	for _, v := range artworkSoldTxDetails {
		artworkSoldTxDetail.TfNum = v.TfNum
		artworkSoldTxDetail.ArtworkName = v.ArtworkName
		artworkSoldTxDetail.Ruler = v.Ruler
		artworkSoldTxDetail.SaleNo = v.SaleNo
		artworkSoldTxDetail.CompleteDate = v.CompleteDate
		artworkSoldTxDetail.MinPrice = v.MinPrice
		artworkSoldTxDetail.SalePrice = v.SalePrice
		artworkSoldTxDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkSoldTxDetail = append(rep.ArtworkSoldTxDetail, artworkSoldTxDetail)
	}

	return
}

func (a *Statement) GetCopyInfoByBatchUid(req *statement.GetCopyInfoByBatchUidRequest) (rep *statement.GetCopyInfoByBatchUidRespond, err error) {

	rep = &statement.GetCopyInfoByBatchUidRespond{}

	//获取画家名
	artworkTx, err := dao.GetCopyArtistNameByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取版权画作详细信息
	artworkTxDetails, err := dao.GetArtworkCopyDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}
	rep.ArtistName = artworkTx.ArtistName

	//获取版权画作被售卖详细信息
	artworkSoldTxDetails, err := dao.GetArtworkSoldCopyDetailByBatchUid(req.BatchUid)
	if err != nil {
		return
	}

	artworkCopyDetail := new(statement.ArtworkCopyDetail)
	artworkSoldCopyDetail := new(statement.ArtworkSoldCopyDetail)

	for _, v := range artworkTxDetails {
		artworkCopyDetail.TfNum = v.TfNum
		artworkCopyDetail.ArtworkName = v.ArtworkName
		artworkCopyDetail.Ruler = v.Ruler
		artworkCopyDetail.MinPrice = v.MinPrice
		artworkCopyDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkCopyDetail = append(rep.ArtworkCopyDetail, artworkCopyDetail)
	}

	for _, v := range artworkSoldTxDetails {
		artworkSoldCopyDetail.TfNum = v.TfNum
		artworkSoldCopyDetail.ArtworkName = v.ArtworkName
		artworkSoldCopyDetail.Ruler = v.Ruler
		artworkSoldCopyDetail.SaleNo = v.SaleNo
		artworkSoldCopyDetail.CompleteDate = v.CompleteDate
		artworkSoldCopyDetail.MinPrice = v.MinPrice
		artworkSoldCopyDetail.SalePrice = v.SalePrice
		artworkSoldCopyDetail.GuaranteePrice = v.GuaranteePrice
		rep.ArtworkSoldCopyDetail = append(rep.ArtworkSoldCopyDetail, artworkSoldCopyDetail)
	}

	return
}