package com.ecep.contract.cloud.old;

import java.util.Map;
import java.util.Objects;

import com.ecep.contract.model.CompanyContact;
import com.ecep.contract.model.CompanyContract;

/**
 *
 */
public class CompanyContactUtils {
    /**
     * 使用 map 应用到 contact, 如有更新, 则返回 true
     */
    public static boolean applyContactByMap(CompanyContact contact, Map<String, Object> map) {
        boolean modified = false;
        String name = (String) map.get("NAME");
        String phone = (String) map.get("PHONE");
        String email = (String) map.get("EMAIL");
        String address = (String) map.get("ADDRESS");
        if (name != null) {
            // 更新备注
            if (!Objects.equals(contact.getName(), name)) {
                contact.setName(name);
                modified = true;
            }
        }
        if (phone != null) {
            // 更新备注
            if (!Objects.equals(contact.getPhone(), phone)) {
                contact.setPhone(phone);
                modified = true;
            }
        }
        if (email != null) {
            // 更新备注
            if (!Objects.equals(contact.getEmail(), email)) {
                contact.setEmail(email);
                modified = true;
            }
        }
        if (address != null) {
            // 更新备注
            if (!Objects.equals(contact.getAddress(), address)) {
                contact.setAddress(address);
                modified = true;
            }
        }
        return modified;
    }

    /**
     * 要和{@link CompanyContract#getContactKey(CompanyContact)}一致
     */
    public static String getContactKey(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        String name = (String) map.get("NAME");
        String phone = (String) map.get("PHONE");
        String email = (String) map.get("EMAIL");
        return name + phone + email;
    }

}
