package com.ecep.contract.vm;

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

import java.util.Objects;

@Data
@EqualsAndHashCode(callSuper = false)
public class CustomerCatalogViewModel extends IdentityViewModel<CustomerCatalogVo> {
    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty description = new SimpleStringProperty();

    /**
     * 创建CustomerCatalogViewModel实例
     * @param v CustomerCatalogVo对象
     * @return CustomerCatalogViewModel实例
     */
    public static CustomerCatalogViewModel from(CustomerCatalogVo v) {
        CustomerCatalogViewModel vm = new CustomerCatalogViewModel();
        vm.update(v);
        return vm;
    }
    
    @Override
    protected void updateFrom(CustomerCatalogVo v) {
        super.updateFrom(v);
        code.set(v.getCode());
        name.set(v.getName());
        description.set(v.getDescription());
    }

    @Override
    public boolean copyTo(CustomerCatalogVo v) {
        boolean ret = super.copyTo(v);
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            ret = true;
        }
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            ret = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            ret = true;
        }
        return ret;
    }
}
