package logic

import (
	"github.com/jinzhu/copier"
	"micro-bundle/internal/dao"
	"micro-bundle/internal/model"
	"micro-bundle/pb/bundle"
	"micro-bundle/pkg/app"
	"micro-bundle/pkg/utils"
)

func CreateOrderRecord(req *bundle.OrderRecord) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	orderRecord := new(model.BundleOrderRecords)
	_ = copier.CopyWithOption(&orderRecord, req, copier.Option{DeepCopy: true})
	orderRecord.UUID = app.ModuleClients.SfNode.Generate().Base64()
	orderRecord.OrderNo = utils.GetOrderNo()
	res, err = dao.CreateOrderRecord(orderRecord)
	return
}

func UpdateOrderRecord(req *bundle.OrderRecord) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	orderRecord := new(model.BundleOrderRecords)
	_ = copier.CopyWithOption(&orderRecord, req, copier.Option{DeepCopy: true})
	res, err = dao.UpdateOrderRecord(orderRecord)
	return
}

func OrderRecordsList(req *bundle.OrderRecordsRequest) (res *bundle.OrderRecordsResponse, err error) {
	res = new(bundle.OrderRecordsResponse)
	res, err = dao.OrderRecordsList(req)
	return
}

func OrderRecordsDetail(req *bundle.OrderRecordsDetailRequest) (res *bundle.OrderRecordsDetailResponse, err error) {
	res = new(bundle.OrderRecordsDetailResponse)
	res.OrderRecord = new(bundle.OrderRecord)
	res.OrderRecord, err = dao.OrderRecordDetail(req)
	if err != nil {
		res.Msg = err.Error()
	}
	return
}