package com.ecep.contract.vm;

import com.ecep.contract.vo.VendorEntityVo;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDate;
import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = false)
public class VendorEntityViewModel extends IdentityViewModel<VendorEntityVo> {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty abbName = new SimpleStringProperty();
    private SimpleStringProperty code = new SimpleStringProperty();
    /**
     * 分组
     */
    private SimpleObjectProperty<Integer> catalog = new SimpleObjectProperty<>();
    /**
     * 关联的企业
     */
    private SimpleObjectProperty<Integer> vendor = new SimpleObjectProperty<>();
    /**
     * 发展日期
     */
    private SimpleObjectProperty<LocalDate> developDate = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> creator = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> modifier = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> modifyDate = new SimpleObjectProperty<>();
    /**
     * 从U8系统同步时间
     */
    private SimpleObjectProperty<LocalDate> updatedDate = new SimpleObjectProperty<>();


    public static VendorEntityViewModel from(VendorEntityVo cc) {
        VendorEntityViewModel model = new VendorEntityViewModel();
        model.update(cc);
        return model;
    }

    @Override
    protected void updateFrom(VendorEntityVo v) {
        super.updateFrom(v);
        getName().set(v.getName());
        getAbbName().set(v.getAbbName());
        getCode().set(v.getCode());
        getCatalog().set(v.getCatalogId());
        getVendor().set(v.getVendorId());
        getDevelopDate().set(v.getDevelopDate());
        getCreator().set(v.getCreatorId());
        getModifier().set(v.getModifierId());
        getModifyDate().set(v.getModifyDate());
        getUpdatedDate().set(v.getUpdatedDate());
    }

    @Override
    public boolean copyTo(VendorEntityVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(abbName.get(), v.getAbbName())) {
            v.setAbbName(abbName.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(catalog.get(), v.getCatalogId())) {
            v.setCatalogId(catalog.get());
            modified = true;
        }
        if (!Objects.equals(vendor.get(), v.getVendorId())) {
            v.setVendorId(vendor.get());
            modified = true;
        }
        if (!Objects.equals(developDate.get(), v.getDevelopDate())) {
            v.setDevelopDate(developDate.get());
            modified = true;
        }
        if (!Objects.equals(creator.get(), v.getCreatorId())) {
            v.setCreatorId(creator.get());
            modified = true;
        }
        if (!Objects.equals(modifier.get(), v.getModifierId())) {
            v.setModifierId(modifier.get());
            modified = true;
        }
        if (!Objects.equals(modifyDate.get(), v.getModifyDate())) {
            v.setModifyDate(modifyDate.get());
            modified = true;
        }
        if (!Objects.equals(updatedDate.get(), v.getUpdatedDate())) {
            v.setUpdatedDate(updatedDate.get());
            modified = true;
        }
        return modified;
    }
}
