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.OrderCreateRecord) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	orderUUID := app.ModuleClients.SfNode.Generate().Base64()
	orderNo := utils.GetOrderNo()
	var addRecords []model.BundleOrderValueAdd
	for _, i := range req.AddRecords {
		addRecords = append(addRecords, model.BundleOrderValueAdd{
			UUID:          app.ModuleClients.SfNode.Generate().Base64(),
			OrderNo:       orderNo,
			OrderUUID:     orderUUID,
			CustomerID:    req.CustomerID,
			CustomerNum:   req.CustomerNum,
			CustomerName:  req.CustomerName,
			ServiceType:   i.ServiceType,
			CurrencyType:  i.CurrencyType,
			Amount:        float64(i.Amount),
			Num:           i.Num,
			Unit:          i.Unit,
			ValueAddUUID:  i.ValueUid,
			Source:        int(i.Source),
			PaymentStatus: int(i.PaymentStatus),
			SignContract:  req.SignContract,
			Signature:     req.Signature,
			SignedTime:    req.SignedTime,
			Snapshot:      req.Snapshot,
		})
	}
	orderRecord := &model.BundleOrderRecords{
		UUID:                  orderUUID,
		OrderNo:               orderNo,
		BundleUUID:            req.BundleUuid,
		BundleName:            req.BundleName,
		CustomerID:            req.CustomerID,
		CustomerNum:           req.CustomerNum,
		CustomerName:          req.CustomerName,
		Amount:                req.Amount,
		AmountType:            req.AmountType,
		TotalAmount:           req.TotalAmount,
		SignContract:          req.SignContract,
		Signature:             req.Signature,
		SignedTime:            req.SignedTime,
		PayType:               int64(req.PayType),
		PayTime:               req.PayTime,
		Status:                req.Status,
		ContractNo:            req.ContractNo,
		BundleCommonUid:       req.BundleCommonUid,
		FinancialConfirmation: model.ConfirmationNotConfirmed,
		ExpirationTime:        req.ExpirationTime,
		Language:              req.Language,
		BundleOrderValueAdd:   addRecords,
	}
	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})
	orderRecord.UUID = req.Uuid
	orderRecord.BundleUUID = req.BundleUuid
	res, err = dao.UpdateOrderRecord(orderRecord)
	return
}

func UpdateOrderRecordByOrderNo(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 = req.Uuid
	orderRecord.BundleUUID = req.BundleUuid
	res, err = dao.UpdateOrderRecordByOrderNO(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
}

func UpdateFinancialConfirmationStatus(req *bundle.FinancialConfirmationRequest) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}).Where("order_no = ?", req.OrderNo).Update("financial_confirmation", model.ConfirmationConfirmed).Error
	if err != nil {
		res.Msg = "更新财务确认状态失败"
		return res, err
	}
	res.Msg = "更新财务确认状态成功"
	return
}
func PackagePriceAndTime(req *bundle.OrderRecord) (res *bundle.PackagePriceAndTimeResponse, err error) {
	res = new(bundle.PackagePriceAndTimeResponse)
	res, err = dao.PackagePriceAndTime(req)
	return

}
func CreateOrderAddRecord(req *bundle.OrderAddRecord) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	res, err = dao.CreateOrderAddRecord(req)
	return
}
func OrderRecordsListV2(req *bundle.OrderRecordsRequestV2) (res *bundle.OrderRecordsResponseV2, err error) {
	res = new(bundle.OrderRecordsResponseV2)
	res, err = dao.OrderRecordsListV2(req)
	return
}
func OrderListByOrderNo(req *bundle.OrderInfoByOrderNoRequest) (res *bundle.OrderInfoByOrderNoResp, err error) {
	res = new(bundle.OrderInfoByOrderNoResp)
	res, err = dao.OrderListByOrderNo(req)
	return
}
func GetReconciliationList(req *bundle.GetReconciliationListReq) (res *bundle.GetReconciliationListResp, err error) {
	res = new(bundle.GetReconciliationListResp)
	res, err = dao.GetReconciliationList(req)
	return
}
func CreateReconciliation(req *bundle.ReconciliationInfo) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	res, err = dao.CreateReconciliation(req)
	return
}
func UpdateReconciliation(req *bundle.ReconciliationInfo) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	res, err = dao.UpdateReconciliation(req)
	return
}

func DeleteValueAddService(req *bundle.DeleteValueAddServiceRequest) (res *bundle.CommonResponse, err error) {
	res = new(bundle.CommonResponse)
	res, err = dao.DeleteValueAddService(req)
	return
}