package com.ycwl.basic.puzzle.fill.datasource;

import com.fasterxml.jackson.databind.JsonNode;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
import com.ycwl.basic.puzzle.fill.enums.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 人脸URL数据源策略
 */
@Slf4j
@Component
public class FaceUrlDataSourceStrategy implements DataSourceStrategy {

    @Autowired
    private FaceMapper faceMapper;

    @Override
    public String resolve(JsonNode sourceFilter, String sortStrategy, DataSourceContext context) {
        try {
            FaceEntity face = faceMapper.get(context.getFaceId());
            if (face != null && face.getFaceUrl() != null) {
                log.debug("解析FACE_URL成功, faceId={}, url={}", context.getFaceId(), face.getFaceUrl());
                return face.getFaceUrl();
            }
        } catch (Exception e) {
            log.error("解析FACE_URL异常, faceId={}", context.getFaceId(), e);
        }
        return null;
    }

    @Override
    public String getSupportedType() {
        return DataSourceType.FACE_URL.getCode();
    }
}
