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

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.module.qms.core.util.qrcode.QrCodeJsonUtil;
import com.zt.plat.module.qms.resource.material.controller.vo.assist.QrCodeContent;
import com.zt.plat.module.qms.resource.material.enums.MaterialInfomationOneBusinessType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import static com.zt.plat.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;

@Schema(description = "管理后台 - 物料实例 Request VO")
@Data
public class MaterialInfomationQueryVO {


    @Schema(description = "业务场景 outbound-出库，use_record-使用记录，used_mark-用完标记，open_seal-开封")
    private MaterialInfomationOneBusinessType businessType;

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

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

    @Schema(description = "存放位置", example = "13603")
    private String locationId;

    @Schema(description = "编码")
    private String code;

    @Schema(description = "名称")
    private String name;

    @Schema(description = "二维码")
    private String qrCode;

    @Schema(description = "是否危险品,1-是，0-否")
    private Integer hazardous;

    @Schema(description = "是否标准溶液,1-是，0-否")
    private Integer standardSolutionFlag;

    @Schema(description = "是否标准物质,1-是，0-否")
    private Integer standardMaterialFlag;

    @Schema(description = "上架状态,0-未上架，1-已上架", example = "2")
    private Integer publishStatus;

    @Schema(description = "领用状态,0-未领用，1-已领用", example = "1")
    private Integer usageStatus;

    @Schema(description = "所属部门ID", example = "16158")
    private Integer managerDepartmentId;

    @Schema(description = "领用人部门", example = "王五")
    private String managerDepartmentName;

    @Schema(description = "负责人id", example = "7209")
    private Long managerUserId;

    @Schema(description = "负责人", example = "张三")
    private String managerUserName;

    @Schema(description = "开封后保质期是否变化,1-是，0-否")
    private Integer openDueFlag;

    @Schema(description = "开封状态,0-未开封，1-已开封", example = "2")
    private Integer openStatus;

    @Schema(description = "开封人id", example = "8280")
    private Long openUserId;

    @Schema(description = "开封人", example = "王五")
    private String openUserName;

    @Schema(description = "过期状态")
    private String expirationFlag;

    @Schema(description = "剩余量")
    private BigDecimal remainingVolume;

    @Schema(description = "用完标记,0-未标记，1-已用完标记")
    private Integer useEndFlag;

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

    @Schema(description = "创建时间")
    @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
    private LocalDateTime[] createTime;

    public String getCode() {
        // 优先从二维码字符串中解析 code
        if (StrUtil.isNotEmpty(this.qrCode)) {
            QrCodeContent materialQrCode = QrCodeJsonUtil.parseQrCode(this.qrCode);
            if (materialQrCode == null) return this.code;
            return StrUtil.isNotEmpty(materialQrCode.getCode()) ? materialQrCode.getCode() : this.code;
        }
        return this.code;
    }

}