package cn.estsh.i3plus.pojo.aps.bean;

import cn.estsh.i3plus.pojo.aps.annotation.ExcludeImportExport;
import cn.estsh.i3plus.pojo.aps.annotation.FieldAnnotation;
import cn.estsh.i3plus.pojo.aps.annotation.MainKey;
import cn.estsh.i3plus.pojo.aps.common.BaseAPS;
import cn.estsh.i3plus.pojo.aps.common.BeanRelation;
import cn.estsh.i3plus.pojo.aps.holders.EBaseOrder;
import cn.estsh.i3plus.pojo.aps.validator.InsertGroup;
import cn.estsh.i3plus.pojo.aps.validator.UpdateGroup;
import com.fasterxml.jackson.annotation.JsonBackReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.Min;
import java.util.Date;
import java.util.List;

/**
 * @Description :订单基类
 * @Reference :
 * @Author : jason.niu
 * @CreateDate : 2019-09-17
 * @Modify:
 **/
@Data
@EqualsAndHashCode(callSuper = true)
@MappedSuperclass
@Api("订单基类")
@ExcludeImportExport
@MainKey(groups = {InsertGroup.class, UpdateGroup.class})
public class BaseOrder extends BaseAPS {
    private static final long serialVersionUID = -2571085172798608026L;
    @Column(name="CODE")
    @ApiParam(value ="编码")
    @FieldAnnotation(popSearch = true, mainkey = true)
    private String code;

    @Column(name="NAME")
    @ApiParam(value ="名称")
    @FieldAnnotation(popSearch = true)
    private String name;

    @Column(name="MATERIAL_ID")
    @ApiParam(value ="物料")
    @FieldAnnotation(relation = "Material", notEmpty = true)
    private Long materialId;

    @Column(name="COUNT", columnDefinition = "decimal(18,8)")
    @ApiParam(value ="数量")
    @FieldAnnotation(notEmpty = true)
    @Min(0)
    private Double count;

    @Column(name="PRIORITY")
    @ApiParam(value ="优先级")
    @FieldAnnotation(defaultValue = "10")
    private Integer priority;

    @Column(name="LET")
    @ApiParam(value ="最晚结束时刻")
    private Date let;

    @Column(name="CALC_EST")
    @ApiParam(value ="最早开始时刻计算值")
    @FieldAnnotation(modify = false)
    private Date calcEst;

    @Column(name="CALC_LET")
    @ApiParam(value ="最晚结束时刻计算值")
    @FieldAnnotation(modify = false)
    private Date calcLet;

    @Column(name="PLAN_BEGIN")
    @ApiParam(value ="计划开始")
    @FieldAnnotation(modify = false)
    private Date planBegin;

    @Column(name="PLAN_END")
    @ApiParam(value ="计划结束")
    @FieldAnnotation(modify = false)
    private Date planEnd;

    @Column(name="RECEIVE_DATE")
    @ApiParam(value ="接单日期")
    private Date receiveDate;

    @Column(name="SPECIFY_MATERIAL_NUM")
    @ApiParam(value ="专料号")
    private String specifyMaterialNum;

    @JsonBackReference
    public List<Work> getWorks() {
        return BeanRelation.list(this, EBaseOrder.Works);
    }

    public Material getMaterial() {
        return BeanRelation.get(this, EBaseOrder.Material);
    }

    public void setMaterial(Material material) {
        this.materialId = material != null ? material.getId() : 0l;
        BeanRelation.set(this, EBaseOrder.Material, material);
    }

    @JsonBackReference
    public List<WorkRelation> getPrevRelations() {
        return BeanRelation.list(this, EBaseOrder.PrevRelations);
    }

    @JsonBackReference
    public List<WorkRelation> getPostRelations() {
        return BeanRelation.list(this, EBaseOrder.PostRelations);
    }
}
