package merchant

import (
	"context"
	"errors"
	v1 "kami/api/merchant/v1"
	"kami/internal/consts"
	"kami/internal/model/do"
	"kami/internal/service"

	"github.com/gogf/gf/v2/os/gtime"
)

func (c *ControllerV1) MerchantDeployAdd(ctx context.Context, req *v1.MerchantDeployAddReq) (res *v1.MerchantDeployAddRes, err error) {
	roadPool, err := service.RoadPool().GetDetailByCode(ctx, req.RollRoadCode)
	if err != nil || roadPool.RoadPoolName == "" {
		err = errors.New("选取轮询通道错误")
		return
	}
	road, err := service.Road().GetDetailByUid(ctx, req.SingleRoadUid)
	if err != nil || road.RoadName == "" {
		err = errors.New("选取通道错误")
		return
	}
	err = service.MerchantDeployInfo().AddOrUpdate(ctx, &do.V1MerchantDeployInfo{
		Status:                 consts.StatusActive,
		MerchantUid:            req.MerchantUid,
		SubmitStrategy:         req.SubmitStrategy,
		PayType:                road.PayType,
		SingleRoadUid:          req.SingleRoadUid,
		SingleRoadName:         road.RoadName,
		SingleRoadPlatformRate: req.SingleRoadPlatformRate,
		SingleRoadAgentRate:    req.SingleRoadAgentRate,
		RollRoadCode:           req.RollRoadCode,
		RollRoadName:           roadPool.RoadPoolName,
		RollRoadPlatformRate:   req.RollRoadPlatformRate,
		RollRoadAgentRate:      req.RollRoadAgentRate,
		IsLoan:                 consts.StatusNo,
		RestrictArea:           req.RestrictArea,
		AutoPayfor:             req.AutoPayfor,
		AutoSettle:             req.AutoSettle,
		WaitUnfreezeAmount:     0,
		LoanAmount:             0,
		IsRestrictDevice:       req.IsRestrictDevice,
		IsRestrictIp:           req.IsRestrictIp,
		IsRestrictAgent:        req.IsRestrictAgent,
		IsRestrictCardPass:     req.IsRestrictCardPass,
		IsRestrictInternalIp:   req.IsRestrictInternalIp,
		CreateTime:             gtime.Now(),
		UpdateTime:             gtime.Now(),
	})
	return
}
