package com.emoon.chat.domain.dto;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Data
public class RecordDetailItemDTO {

    private Long type;
    private String typeName;
    private String content;

    private Integer qcResult;
    private String qcResultText; // 动态
    private String qcScore;
    private String qcComments; // 原始字符串存储
    private List<Map<String, Object>> qcCommentsList; // 解析后的qcComments
    private String qcReviewComments; // 新增字段
    private String qcName;
    private LocalDateTime qcStartTime;
    private LocalDateTime qcEndTime;

    // --- Getters and Setters ---

    public Long getType() { return type; }
    public void setType(Long type) { this.type = type; }

    public String getTypeName() { return typeName; }
    public void setTypeName(String typeName) { this.typeName = typeName; }

    public String getContent() { return content; }
    public void setContent(String content) { this.content = content; }

    public String getQcReviewComments() { return qcReviewComments != null ? qcReviewComments : ""; }
    public void setQcReviewComments(String qcReviewComments) { this.qcReviewComments = qcReviewComments; }

    public Integer getQcResult() { return qcResult; }
    public void setQcResult(Integer qcResult) { this.qcResult = qcResult; }

    public String getQcResultText() {
        if (qcResult == null) return "未质控";
        return qcResult == 1 ? "通过" : "不通过";
    }

    public String getQcScore() { return qcScore != null ? qcScore : "—"; }
    public void setQcScore(String qcScore) { this.qcScore = qcScore; }

    // 修改：确保返回格式化后的 qcComments
    public String getQcComments() {
        return qcComments != null ? qcComments : "—";
    }
    public void setQcComments(String qcComments) {
        this.qcComments = qcComments;
    }

    // 新增：解析 qcComments 字符串为 List<Map<String, Object>> 格式
    public List<Map<String, Object>> getQcCommentsList() {
        if (qcComments != null && !qcComments.isEmpty()) {
            try {
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(qcComments, List.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace(); // 处理异常
            }
        }
        return null; // 如果为空，返回 null
    }

    public void setQcCommentsList(List<Map<String, Object>> qcCommentsList) {
        // 如果需要将解析后的 List 重新转为 JSON 字符串存储
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            this.qcComments = objectMapper.writeValueAsString(qcCommentsList);
        } catch (JsonProcessingException e) {
            e.printStackTrace(); // 处理异常
        }
    }

    public String getQcName() { return qcName != null ? qcName : ""; }
    public void setQcName(String qcName) { this.qcName = qcName; }

    public LocalDateTime getQcStartTime() { return qcStartTime; }
    public void setQcStartTime(LocalDateTime qcStartTime) { this.qcStartTime = qcStartTime; }

    public LocalDateTime getQcEndTime() { return qcEndTime; }
    public void setQcEndTime(LocalDateTime qcEndTime) { this.qcEndTime = qcEndTime; }
}
