package logic

import (
	"context"

	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"

	"github.com/zeromicro/go-zero/core/logx"
)

type MediaCheckCallbackLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMediaCheckCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MediaCheckCallbackLogic {
	return &MediaCheckCallbackLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}

func (l *MediaCheckCallbackLogic) MediaCheckCallback(in *plant.MediaCheckCallbackReq) (*plant.CommonResp, error) {
	result := plantModel.MediaCheckResult{
		TraceID: in.TraceId,
		PostID:  in.PostId,
		OssID:   in.OssId,
		UserID:  in.UserId,
		Status:  int(in.Status),
		Type:    int(in.Type),
		ErrMsg:  in.ErrMsg,
	}
	if err := l.svcCtx.DB.Where(plantModel.MediaCheckResult{TraceID: in.TraceId}).
		Assign(result).
		FirstOrCreate(&plantModel.MediaCheckResult{}).Error; err != nil {
		return nil, err
	}

	if in.PostId != "" {
		auditStatus := 1
		if in.Status != 0 {
			auditStatus = 2
		}
		_ = l.svcCtx.DB.Model(&plantModel.Post{}).Where("id = ?", in.PostId).Update("has_reviewed", auditStatus).Error
	}

	return &plant.CommonResp{Code: 0, Msg: "ok"}, nil
}
