package agency

import (
	"context"
	"log"

	"bsm/full/module/ec/market/internal/impl"
	"bsm/full/module/ec/market/internal/models"
	pb "bsm/full/module/ec/market/pb"

	"git.apinb.com/bsm-sdk/core/errcode"
)

// 审核
func Approve(ctx context.Context, in *pb.ApproveRequest) (reply *pb.IdentityStatusReply, err error) {
	var data *models.MarketAgency
	if in.GetIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}

	if err := impl.DBService.Where("identity = ?", in.GetIdentity()).First(&data).Error; err != nil {
		return nil, err
	}
	if data.Status != 1 {
		return nil, errcode.ErrInvalidArgument
	}
	switch in.GetApprove() {
	case 1:
		if err := impl.DBService.Model(&data).Update("approve", 2).Error; err != nil {
			log.Println("更新代理商数据失败：", err)
			return nil, errcode.ErrDB
		}
	case 2:
		if err := impl.DBService.Model(&data).Update("approve", -2).Error; err != nil {
			log.Println("更新代理商数据失败：", err)
			return nil, errcode.ErrDB
		}
	}

	return &pb.IdentityStatusReply{
		Code:     0,
		Message:  "OK",
		Identity: data.Identity,
	}, nil

}
