package merchant

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

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gtime"

	v1 "kami/api/merchant/v1"
)

func (c *ControllerV1) MerchantDeployUpdate(ctx context.Context, req *v1.MerchantDeployUpdateReq) (res *v1.MerchantDeployUpdateRes, 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().Update(ctx, &do.V1MerchantDeployInfo{
		Id:                     req.ID,
		Status:                 consts.StatusActive,
		MerchantUid:            req.MerchantUid,
		PayType:                road.PayType,
		SingleRoadUid:          req.SingleRoadUid,
		SingleRoadName:         road.RoadName,
		SingleRoadPlatformRate: req.SingleRoadPlatformRate,
		SingleRoadAgentRate:    req.SingleRoadAgentRate,
		RollRoadCode:           req.RollRoadCode,
		RollRoadName:           roadPool.RoadPoolName,
		RollRoadPlatformRate:   req.RollRoadPlatformRate,
		RestrictArea:           req.RestrictArea,
		RollRoadAgentRate:      req.RollRoadAgentRate,
		AutoSettle:             req.AutoSettle,
		AutoPayfor:             req.AutoPayfor,
		IsLoan:                 consts.StatusNo,
		WaitUnfreezeAmount:     0,
		LoanAmount:             0,
		IsRestrictDevice:       req.IsRestrictDevice,
		IsRestrictIp:           req.IsRestrictIp,
		IsRestrictAgent:        req.IsRestrictAgent,
		IsRestrictCardPass:     req.IsRestrictCardPass,
		IsRestrictInternalIp:   req.IsRestrictInternalIp,
		SubmitStrategy:         req.SubmitStrategy,
		UpdateTime:             gtime.Now(),
	})
	if err != nil {
		err = gerror.New("更新失败")
		return
	}
	return
}
