package model

import (
	"errors"
	"github.com/fonchain_enterprise/fonchain-approval/api/approval"
	m2 "github.com/fonchain_enterprise/fonchain-approval/pkg/m"
	"gorm.io/plugin/soft_delete"
	"time"
)

// BundlePayPrice 画展包付款单审批
type BundlePayPrice struct {
	ID           uint64                `gorm:"column:id" json:"id"`
	DeletedAt    soft_delete.DeletedAt `gorm:"column:deleted_at" json:"deletedAt"` // 删除时间
	CreatedAt    time.Time             `gorm:"column:created_at" json:"createdAt"` // 创建时间
	UpdatedAt    time.Time             `gorm:"column:updated_at" json:"updatedAt"` // 更新时间
	ApprovalID   uint64                `gorm:"type:int(11);column:approval_id;default:0;comment:画展的uid" json:"approvalId"`
	ShowUid      string                `gorm:"type:varchar(32);column:show_uid;default:'';comment:画展的uid" json:"showUid"`
	ShowName     string                `gorm:"type:varchar(32);column:show_name;default:'';comment:画展的名称" json:"showName"`
	ReceivedAt   string                `gorm:"type:varchar(32);column:received_at;default:'';comment:领取时间" json:"receivedAt"` // 领取时间
	ShowDate     string                `gorm:"type:varchar(32);column:show_date;default:'';comment:画展时间" json:"showDate"`
	ShowLowPrice string                `gorm:"type:varchar(32);column:show_low_price;default:'';comment:画展宝低价" json:"showLowPrice"`
	ArtworkNum   uint64                `gorm:"type:int(11);column:artworkNum;default:0;comment:作品数量" json:"artworkNum"`
	ArtworkSize  string                `gorm:"type:varchar(32);column:artwork_size;default:'';comment:作品总平尺" json:"artworkSize"`
	ShowAddress  string                `gorm:"type:varchar(50);column:show_address;default:'';comment:画展地址" json:"showAddress"`
	ArtistName   string                `gorm:"type:varchar(50);column:artist_name;default:'';comment:画家名称" json:"artistName"`
	Status       uint64                `gorm:"type:int(11);column:status;default:1;comment:状态" json:"status"`
}

// TableName get sql table name.获取数据库表名
func (m *BundlePayPrice) TableName() string {
	return "bundle_pay_price"
}


func (m *BundlePayPrice) GetApproval(id uint64) (*Approval,error) {

	var entity *Approval
	if err := DB.
		Preload("ApprovalWorkFlows").
		Preload("ApprovalType").
		Preload("BundlePayPrice").
		First(&entity, id).Error; err != nil {
		return entity,err
	}

	return entity,nil
}

func (m *BundlePayPrice) SaveApprovalContent(in *approval.CreateRequest, a *Approval) error {

	m.ApprovalID = a.ID
	m.ShowUid = in.BundlePayPrice.ShowUid
	m.ShowName = in.BundlePayPrice.ShowName
	m.ReceivedAt = in.BundlePayPrice.ReceivedAt
	m.ShowDate = in.BundlePayPrice.ShowDate
	m.ShowLowPrice = in.BundlePayPrice.ShowLowPrice
	m.ArtworkNum = in.BundlePayPrice.ArtworkNum
	m.ArtworkSize = in.BundlePayPrice.ArtworkSize
	m.ShowAddress = in.BundlePayPrice.ShowAddress
	m.ArtistName = in.BundlePayPrice.ArtistName

	return DB.Create(&m).Error
}

func (m *BundlePayPrice) UpdateApprovalContent(in *approval.CreateRequest, a *Approval) error {

	var entity *BundlePayPrice

	if err := DB.Where(&BundlePayPrice{ApprovalID: a.ID}).First(&entity).Error; err != nil {
		return errors.New(m2.ErrorNotFound)
	}

	m.ID = entity.ID
	m.ShowUid = in.BundlePayPrice.ShowUid
	m.ShowName = in.BundlePayPrice.ShowName
	m.ReceivedAt = in.BundlePayPrice.ReceivedAt
	m.ShowDate = in.BundlePayPrice.ShowDate
	m.ShowLowPrice = in.BundlePayPrice.ShowLowPrice
	m.ArtworkNum = in.BundlePayPrice.ArtworkNum
	m.ArtworkSize = in.BundlePayPrice.ArtworkSize
	m.ShowAddress = in.BundlePayPrice.ShowAddress
	m.ArtistName = in.BundlePayPrice.ArtistName

	return DB.Model(&m).Updates(m).Error
}

func (m *BundlePayPrice) BuildResContent(a *Approval, request *approval.CreateRequest) {

	if a.BundlePayPrice != nil {
		request.BundlePayPrice = &approval.BundlePayPrice{
			ShowUid:      a.BundlePayPrice.ShowUid,
			ApprovalID:   a.BundlePayPrice.ApprovalID,
			ShowName:     a.BundlePayPrice.ShowName,
			ReceivedAt:   a.BundlePayPrice.ReceivedAt,
			ShowDate:     a.BundlePayPrice.ShowDate,
			ShowLowPrice: a.BundlePayPrice.ShowLowPrice,
			ArtworkNum:   a.BundlePayPrice.ArtworkNum,
			ArtworkSize:  a.BundlePayPrice.ArtworkSize,
			ShowAddress:  a.BundlePayPrice.ShowAddress,
			Status:       a.BundlePayPrice.Status,
			ArtistName:   a.BundlePayPrice.ArtistName,
			ID:           a.BundlePayPrice.ID,
		}
	}
}

func (m *BundlePayPrice) DeleteApproval(p *Approval) error {
	return DB.Where(&BundlePayPrice{ApprovalID: p.ID}).Delete(&BundlePayPrice{}).Error
}