package com.ycwl.basic.model.pc.faceDetectLog.entity;

import com.ycwl.basic.utils.JacksonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.aliyuncs.facebody.model.v20191230.SearchFaceRequest;
import com.aliyuncs.facebody.model.v20191230.SearchFaceResponse;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.ycwl.basic.model.pc.faceDetectLog.resp.MatchLocalRecord;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
@TableName("face_detect_log")
public class FaceDetectLog {
    @TableId(type = IdType.AUTO)
    private Integer id;

    private Date createTime;

    private String reason;

    private String faceUrl;

    private String dbName;

    private String matchRawResult;

    private String matchLocalRecord;

    @TableField(exist = false)
    private List matchRawRecord;

    public static FaceDetectLog quickCreate(String reason) {
        FaceDetectLog log = new FaceDetectLog();
        log.reason = reason;
        return log;
    }
    public static FaceDetectLog quickCreate(String reason, SearchFaceRequest request) {
        FaceDetectLog log = new FaceDetectLog();
        log.reason = reason;
        return log.fillRequest(request);
    }

    public FaceDetectLog fillRequest(SearchFaceRequest request) {
        this.dbName = request.getDbName();
        this.faceUrl = request.getImageUrl();
        this.createTime = new Date();
        return this;
    }

    public FaceDetectLog fillResponse(Object response) {
        this.matchRawResult = JacksonUtil.toJSONString(response);
        return this;
    }

    public List matchLocalRecord() {
        if (matchLocalRecord == null) {
            return null;
        }
        return JacksonUtil.parseObject(matchLocalRecord, new TypeReference<List<MatchLocalRecord>>() {});
    }

    public void matchLocalRecord(List matchLocalRecord) {
        if (matchLocalRecord == null) {
            this.matchLocalRecord = null;
        } else {
            this.matchLocalRecord = JacksonUtil.toJSONString(matchLocalRecord);
        }
    }

}
