package com.jwipc.order.dto;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jwipc.util.DateUtils;
import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 采购订单明细
 * </p>
 *
 * @author zhangjie
 * @since 2024-03-05
 */
@Getter
@Setter
public class PurchaseOrderDetailListDTO extends PurchaseOrderDetailBaseDTO {

    @Serial
    private static final long serialVersionUID = -8995270081378830318L;
    private String orderTypeCode;
    private String orderTypeName;
    private String orderHeadRemark;
    private String supplierName;
    private String companyCode;
    private String companyName;
    private String orderDate;
    private BigDecimal onlineNum = BigDecimal.ZERO;
    private BigDecimal unclearNum = BigDecimal.ZERO;
    private BigDecimal receiveNum = BigDecimal.ZERO;
    private BigDecimal deliveryNum = BigDecimal.ZERO;
    private BigDecimal orderTaxAmount = BigDecimal.ZERO;
    private String purchaseAppCode;
    private String purchaseAppRemark;
    private String manufacturer;
    private String storageConditions;
    private String quantityRange;
    private String materialSpec;
    private String approvalNumber;
    private String transferOrderNum;
    private String requireCode;
    private String applyType;
    private BigDecimal markCount;
    private String markDate;
    private String markFlag;
    private Integer markType;
    private String currencyCode;
    private String purchaseGroupCode;
    private String purchaseGroupName;
    private String supplierCode1;
    private String supplierName1;
    private String supplierCode2;
    private String supplierName2;
    private String remark;
    /**
     * 创建时间
     */
    private LocalDateTime createTime;
    /**
     * 付款条款
     */
    private String paymentMethod;

    /**
     * 付款条款名称
     */
    private String paymentMethodName;
    private String purchaseOrgName;
    private String purchaseOrgCode;

    private String differenceDays;

    public String getDifferenceDays() {
        if (StrUtil.isNotEmpty(this.getRequireDate()) && Objects.nonNull(this.getReplayRequireDate())) {
            final DateTime lastRequireDate = DateUtil.parse(this.getRequireDate(), DateUtils.yyyyMMddDash);
            final DateTime scheduleDeliveryDate = DateUtil.parse(
                    DateUtils.formatYYYYMMDDDash(this.getReplayRequireDate()), DateUtils.yyyyMMddDash);
            final long between = DateUtil.between(lastRequireDate,scheduleDeliveryDate , DateUnit.DAY);
            // 手动计算方向差值
            final long difference = lastRequireDate.isBefore(scheduleDeliveryDate) ? -between : between;
            return difference + "天";
        }
        return StrUtil.EMPTY;
    }

    private String requirementStatus;

    public String getRequirementStatus() {
        if (StrUtil.isNotEmpty(this.getDifferenceDays())) {
            if (Long.valueOf(this.getDifferenceDays().replace("天", "")) >= 0) {
                return "OK";
            } else {
                return "NG";

            }
        }
        return "";
    }
}
