package com.jwipc.order.model.po;

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.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Author: swq
 * @Date: 2024/4/1 11:28
 */
@Getter
@Setter
public class PurchaseOrderPlanPO implements Serializable {
    private Long id;

    private Long orderId;
    /**
     * 订单编号
     */
    private String orderCode;
    private String subjectTypeCode;
    private String rowTypeCode;

    /**
     * 订单行号
     */
    private String orderRow;
    /**
     * 物料编号
     */
    private String materialCode;

    /**
     * 物料名称
     */
    private String materialName;


    /**
     * 订单单位名称
     */
    private String unitName;

    /**
     * 下单数量
     */
    private BigDecimal num;
    /**
     * 订单状态
     */
    private Integer state;
    private Integer componentCount;

    /**
     * 1-删除
     */
    private String delFlag;
    /**
     * 单头备注
     */
    private String orderRemark;
    /**
     * 备注
     */
    private String remark;
    /**
     * 已收货数量
     */
    private BigDecimal receivedQuantity;
    /**
     * 计划行号
     */
    private String scheduleLine;

    /**
     * 计划交货日期
     */
    private String scheduleDeliveryDate;

    /**
     * 计划交货数量
     */
    private BigDecimal scheduleDeliveryQuantity;
    /**
     * 已交数量
     */
    private BigDecimal receivedNum;
    /**
     * 未交数量
     */
    private BigDecimal noClearNum;
    /**
     * 供应商编码
     */
    private String supplierErpCode;
    /**
     * 供应商名称
     */
    private String supplierName;
    /**
     * 需求日期 yyyy-MM-dd
     */
    private String lastRequireDate;

    /**
     * 采购组名称
     */
    private String purchaseGroupName;
    private String purchaseGroupCode;
    /**
     * 是否更新
     */
    private String isUpdate;

    private String day1;
    private String day2;
    private String day3;
    private String day4;
    private String day5;
    private String day6;
    private String day7;
    private String week1;
    private String week2;
    private String week3;
    private String week4;
    private String week5;
    private String week6;
    private String week7;
    private String week8;
    private String month1;
    private String month2;
    private String month3;
    private String month4;
    private String companyCode;
    private String orderTypeCode;
    private String factoryCode;
    private String factoryName;
    private LocalDateTime createTime;
    /**
     * 物料组编码
     */
    private String materialGroupCode;

    /**
     * 物料组名称
     */
    private String materialGroupName;
    /**
     * 采购申请号
     */
    private String purchApplyNo;

    /**
     * 采购申请行
     */
    private String purchApplyRow;
    private String originalRequireDate;
    private String purchaseOrgName;
    private String purchaseOrgCode;
    private String tariff;
    private String webre;
    private BigDecimal onlineNum;
    private String diffDays;

    public BigDecimal getNoClearNum() {
        if (Objects.nonNull(this.scheduleDeliveryQuantity) && Objects.nonNull(this.receivedQuantity)) {
            return this.scheduleDeliveryQuantity.subtract(this.receivedQuantity);
        }
        return noClearNum;
    }

    public String getDiffDays() {
        if (StrUtil.isNotEmpty(this.lastRequireDate) && StrUtil.isNotEmpty(this.scheduleDeliveryDate)) {
            final DateTime lastRequireDate = DateUtil.parse(this.lastRequireDate, DateUtils.yyyyMMddDash);
            final DateTime scheduleDeliveryDate = DateUtil.parse(this.scheduleDeliveryDate, 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 okStatus;

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

            }
        }
        return "";
    }
}
