package agency

import (
	"context"
	"log"

	"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.StatusReply, err error) {
	var data *models.MarketAgency
	if in.GetIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}

	if err := models.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 := models.DBService.Model(&data).Update("approve", 2).Error; err != nil {
			log.Println("更新代理商数据失败：", err)
			return nil, errcode.ErrDB
		}
	case 2:
		if err := models.DBService.Model(&data).Update("approve", -2).Error; err != nil {
			log.Println("更新代理商数据失败：", err)
			return nil, errcode.ErrDB
		}
	}

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

}
