package com.zt.plat.module.qms.resource.material.controller.vo;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.zt.plat.module.qms.core.aspect.annotation.Dict;
import com.zt.plat.module.qms.resource.material.controller.vo.assist.MaterialAssayResult;
import com.zt.plat.module.qms.resource.material.enums.MaterialAssayStatus;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
@Schema(description = "管理后台 - 物料检化验关联信息 Response VO")
@Data
@ExcelIgnoreUnannotated
public class MaterialCorrelationAssayRespVO {

    @Schema(description = "主键id", requiredMode = Schema.RequiredMode.REQUIRED, example = "6949")
    @ExcelProperty("主键id")
    private Long id;

    @Schema(description = "检化验委托id,用于关联到检化验模块", example = "8699")
    @ExcelProperty("检化验委托id,用于关联到检化验模块")
    private Long entrustId;

    @Schema(description = "业务类型,验收检化验等", example = "2")
    @ExcelProperty("业务类型,验收检化验等")
    private String businessType;

    @Schema(description = "检验来源,如验收明细id")
    @ExcelProperty("检验来源,如验收明细id")
    private String source;

    @Schema(description = "物料大类id", example = "31683")
    @ExcelProperty("物料大类id")
    private Long productId;

    @Schema(description = "样品大类id,检化验业务样品大类", example = "1909")
    @ExcelProperty("样品大类id,检化验业务样品大类")
    private Long sampleId;

    @Schema(description = "样品编号")
    private String sampleCode;

    @Schema(description = "样品标签模板")
    private String sampleLabelTemplate;

    @Schema(description = "批次id", example = "3150")
    @ExcelProperty("批次id")
    private Long batchId;

    @Schema(description = "批次拆分/工段id", example = "2292")
    @ExcelProperty("批次拆分/工段id")
    private Long gongduanId;

    @Schema(description = "检测项")
    @ExcelProperty("检测项")
    private String item;

    @Schema(description = "检测项列表")
    @ExcelProperty("检测项列表")
    private List<Long> items;

    @Schema(description = "检测项显示名称")
    @ExcelProperty("检测项显示名称")
    private String itemsShowName;

    @Schema(description = "状态,未开始、进行中、已完成", example = "2")
    @ExcelProperty("状态,未开始、进行中、已完成")
    @Dict(dicCode = "material_assay_status")
    private String assayStatus;

    @Schema(description = "结果,检化验结果")
    @ExcelProperty("结果,检化验结果")
    private String result;

    @Schema(description = "是否合格,字典")
    @ExcelProperty("是否合格,字典")
    private String qualified;

    @Schema(description = "说明")
    @ExcelProperty("说明")
    private String reason;

    @Schema(description = "所属部门")
    @ExcelProperty("所属部门")
    private String systemDepartmentCode;

    @Schema(description = "备注")
    @ExcelProperty("备注")
    private String remark;

    @Schema(description = "创建时间")
    @ExcelProperty("创建时间")
    private LocalDateTime createTime;

    // 设置检测项列表
    public List<Long> getItems() {
        if (StrUtil.isBlank(this.item)) {
            return List.of();
        }
        JSONArray array = JSONUtil.parseArray(this.item);
        return array.toList(Long.class);
    }

    public String getAssayStatus_dictText() {
        if (StrUtil.isBlank(this.assayStatus)) {
            return MaterialAssayStatus.not_start.getName();
        }
        try {
            return MaterialAssayStatus.valueOf(this.assayStatus).getName();
        } catch (IllegalArgumentException e) {
            return "未知状态";
        }
    }

    public MaterialAssayResult getResultObj() {
        try {
            return JSONUtil.toBean(this.result, MaterialAssayResult.class);
        } catch (Exception e) {
            log.error("result解析失败:{}", e.getMessage());
            return null;
        }
    }

}