PREFIX gesli: <https://gesli.example/ontology#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?partnerName ?partnerRole ?partnerType ?contactName ?contactRole
       ?phone ?socialAccountType ?socialAccountNo
WHERE {
  ?partner a/rdfs:subClassOf* gesli:BusinessPartner ;
           rdfs:label ?partnerName ;
           gesli:hasBusinessPartnerContact ?contact .

  OPTIONAL { ?partner gesli:platformSupplierType ?partnerType . }
  OPTIONAL {
    ?partner gesli:hasBusinessPartnerBasicInfo ?basic .
    OPTIONAL { ?basic gesli:businessPartnerRole ?basicPartnerRole . }
  }
  OPTIONAL {
    ?partner a/rdfs:subClassOf* gesli:Customer .
    BIND("客户" AS ?customerRole)
  }
  OPTIONAL {
    ?partner a/rdfs:subClassOf* gesli:Supplier .
    BIND("平台供应商" AS ?supplierRole)
  }

  OPTIONAL { ?contact gesli:contactName ?contactName . }
  OPTIONAL { ?contact gesli:contactRole ?contactRole . }
  OPTIONAL { ?contact gesli:phone ?phone . }
  OPTIONAL { ?contact gesli:socialAccountType ?socialAccountType . }
  OPTIONAL { ?contact gesli:socialAccountNo ?socialAccountNo . }

  BIND(COALESCE(?basicPartnerRole, ?customerRole, ?supplierRole) AS ?partnerRole)
}
ORDER BY ?partnerRole ?partnerType ?partnerName ?contactRole
