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

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.EWork;
import cn.estsh.i3plus.pojo.aps.validator.InsertGroup;
import cn.estsh.i3plus.pojo.aps.validator.UpdateGroup;
import cn.estsh.i3plus.pojo.base.enumutil.ApsEnumUtil;
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.Entity;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;

/**
 * @Description :工作
 * @Reference :
 * @Author : jason.niu
 * @CreateDate : 2019-09-17
 * @Modify:
 **/
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "APS_WORK")
@Api("工作")
@MainKey(groups = {InsertGroup.class, UpdateGroup.class})
public class Work extends BaseAPS {
    private static final long serialVersionUID = 6471740978550523187L;
    @Column(name="CODE")
    @ApiParam(value ="编码")
    @FieldAnnotation(popSearch = true, mainkey = true, modify = false)
    private String code;

    @Column(name="ORDER_ID")
    @ApiParam(value ="订单")
    @FieldAnnotation(relation = "Order", modify = false)
    private Long orderId;

    @Column(name="WORK_TYPE")
    @ApiParam(value ="工作类型")
    @FieldAnnotation(property = false)
    private ApsEnumUtil.WORK_TYPE workType;

    @Column(name="count", columnDefinition = "decimal(18,8)")
    @ApiParam(value ="数量")
    @FieldAnnotation(modify = false)
    private Double count;

    @Column(name="WORK_STATUS")
    @ApiParam(value ="工作状态")
    private ApsEnumUtil.WORK_STATUS workStatus;

    @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="PLAN_RESOURCE")
    @ApiParam(value ="计划主资源")
    @FieldAnnotation(modify = false)
    private String planResource;

    @Column(name="SPECIFY_COUNT", columnDefinition = "decimal(18,8)")
    @ApiParam(value ="指定数量")
    private Double specifyCount;

    @Column(name="SPECIFY_RESOURCE_ID")
    @ApiParam(value ="指定主资源")
    @FieldAnnotation(relation = "SpecifyResource")
    private Long specifyResourceId;

    @Column(name="SPECIFY_PRODUCE_TIME")
    @ApiParam(value ="指定生产时间")
    private String specifyProduceTime;

    @Column(name="SPECIFY_PREV_TIME")
    @ApiParam(value ="指定前设置时间")
    private String specifyPrevTime;

    @Column(name="SPECIFY_POST_TIME")
    @ApiParam(value ="指定后设置时间")
    private String specifyPostTime;

    @Column(name="SPECIFY_EST")
    @ApiParam(value ="指定最早开始时间")
    private Date specifyEst;

    @Column(name="SPECIFY_LET")
    @ApiParam(value ="指定最晚结束时间")
    private Date specifyLet;

    @Column(name="SPECIFY_BEGIN")
    @ApiParam(value ="指定开始时间")
    private Date specifyBegin;

    @Column(name="SPECIFY_END")
    @ApiParam(value ="指定结束时间")
    private Date specifyEnd;

    @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="FIX_SPLIT")
    @ApiParam(value ="已分割的工作是否固定")
    private Boolean fixSplit;

    @Column(name="NEED_PREV_COUNT", columnDefinition = "decimal(18,8)")
    @ApiParam(value ="需要的前工作数量")
    @FieldAnnotation(display = false, modify = false)
    private Double needPrevCount;

    @Column(name="SCH_FAILED")
    @ApiParam(value ="是否排程失败")
    @FieldAnnotation(modify = false)
    private Boolean schFailed;

    @Column(name="FAILED_REASON")
    @ApiParam(value ="排程失败原因")
    @FieldAnnotation(modify = false)
    private String failedReason;

    @Column(name="REMARK")
    @ApiParam(value ="备注")
    private String remark;

    @Column(name="OPERATION_ID")
    @ApiParam(value ="工序")
    @FieldAnnotation(property = false)
    private Long operationId;

    @Column(name="PARENT_WORK_ID")
    @ApiParam(value ="父工作")
    @FieldAnnotation(property = false)
    private Long parentWorkId;

    @Column(name="MAIN_PLAN_ID")
    @ApiParam(value ="主资源的工作计划")
    @FieldAnnotation(property = false)
    private Long mainPlanId;

    public BaseOrder getOrder() {
        return BeanRelation.get(this, EWork.Order);
    }

    public void setOrder(BaseOrder order) {
        this.orderId = order != null ? order.getId() : 0l;
        BeanRelation.set(this, EWork.Order, order);
    }

    public Operation getOperation() {
        return BeanRelation.get(this, EWork.Operation);
    }

    public void setOperation(Operation oper) {
        this.operationId = oper != null ? oper.getId() : 0l;
        BeanRelation.set(this, EWork.Operation, oper);
    }

    public ParentWork getParentWork() {
        return BeanRelation.get(this, EWork.ParentWork);
    }

    public void setParentWork(ParentWork parentWork) {
        this.parentWorkId = parentWork != null ? parentWork.getId() : 0l;
        BeanRelation.set(this, EWork.ParentWork, parentWork);
    }

    @JsonBackReference
    public List<WorkResource> getWorkResources() {
        return BeanRelation.list(this, EWork.WorkResources);
    }

    @JsonBackReference
    public List<WorkInput> getWorkInputs() {
        return BeanRelation.list(this, EWork.WorkInputs);
    }

    @JsonBackReference
    public List<WorkOutput> getWorkOutputs() {
        return BeanRelation.list(this, EWork.WorkOutputs);
    }

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

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

    @JsonBackReference
    public List<PlanFeedback> getPlanFeedbacks() {
        return BeanRelation.list(this, EWork.PlanFeedbacks);
    }

    public WorkPlan getMainPlan() { return BeanRelation.get(this, EWork.MainPlan); }

    public void setMainPlan(WorkPlan plan) {
        this.mainPlanId = plan != null ? plan.getId() : 0;
        BeanRelation.set(this, EWork.MainPlan, plan);
    }

    @JsonBackReference
    public List<WorkPlan> getWorkPlans() { return BeanRelation.list(this, EWork.WorkPlans); }

    public Resource getSpecifyResource() {
        return BeanRelation.get(this, EWork.SpecifyResource);
    }

    public void setSpecifyResource(Resource resource) {
        this.specifyResourceId = resource != null ? resource.getId() : 0;
        BeanRelation.set(this, EWork.SpecifyResource, resource);
    }
}
