package com.nbport.zgwl.partner.utils;

import com.nbport.zgwl.contractpush.dto.PushPayloadDTO;
import com.nbport.zgwl.partner.dto.PartnerContactDto;
import com.nbport.zgwl.partner.dto.PartnerManageDto;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 相对方同步报文构造工具
 */
public final class PartnerSyncPayloadBuilder {

    private PartnerSyncPayloadBuilder() {
    }

    /** 构建相对方信息采集报文（数据采集接口规范 V3.0 格式） */
    public static PushPayloadDTO buildCollectOppositesPayload(PartnerManageDto partner) {
        Map<String, Object> payload = new LinkedHashMap<>();
        payload.put("o_id", partner.getCustomerSysid() == null ? null : String.valueOf(partner.getCustomerSysid()));
        payload.put("o_name", partner.getCustomerNameC());
        payload.put("o_type", partner.getRelativeType());
        payload.put("o_nature", partner.getOppositeCharacter());
        payload.put("o_orgNature", convertOrgNature(partner.getNatureOfEnterpriseOrg()));
        payload.put("o_orgNature_2", partner.getNatureOfEnterpriseOrgLev());
        payload.put("o_GAT", PartnerManageConstants.GAT_FLAG_YES.equals(partner.getGatFlag())
                ? PartnerManageConstants.FLAG_ONE
                : PartnerManageConstants.FLAG_ZERO);
        payload.put("o_isGroup", PartnerManageConstants.GROUP_FLAG_YES.equals(partner.getGroupFlag())
                ? PartnerManageConstants.FLAG_ONE
                : PartnerManageConstants.FLAG_ZERO);
        payload.put("o_creditCode", partner.getCustomerTaxno());
        payload.put("o_zjType", partner.getCertificateType());
        payload.put("o_zjNo", partner.getCertificateNum());
        payload.put("o_tin", partner.getTinCode());
        payload.put("o_country", partner.getCountry());
        payload.put("o_legalPerson", partner.getLegalPersonName());
        payload.put("o_address", partner.getCustomerRegaddr());
        payload.put("o_capital", partner.getRegCapital());
        payload.put("o_creator", partner.getCreateBy());
        payload.put("o_creationTime", partner.getCreateTime() == null ? null : partner.getCreateTime().toString().replace("T", " "));
        payload.put("o_contacts", buildContactList(partner.getContacts()));

        PushPayloadDTO requestDTO = new PushPayloadDTO();
        requestDTO.setPayload(payload);
        return requestDTO;
    }

    /** 构建联系人列表报文 */
    private static List<Map<String, Object>> buildContactList(List<PartnerContactDto> contacts) {
        if (contacts == null) {
            return List.of();
        }
        return contacts.stream().map(item -> {
            Map<String, Object> contact = new LinkedHashMap<>();
            contact.put("name", item.getContactName());
            contact.put("phone", item.getContactPhone());
            contact.put("postalAddress", item.getPostalAddress());
            contact.put("email", item.getEmail());
            return contact;
        }).collect(Collectors.toList());
    }

    /** 转换组织性质：系统内用 ZY（政府），法务系统用 ZF */
    private static String convertOrgNature(String natureOfEnterpriseOrg) {
        if ("ZY".equals(natureOfEnterpriseOrg)) {
            return "ZF";
        }
        return natureOfEnterpriseOrg;
    }
}
