package com.zioinfo.kintex.system.company;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/** 회사(거래처) 매퍼. 테넌트 스코프(V31 표준). */
@Mapper
public interface CompanyMapper {

    @Select("""
            <script>
            SELECT id, company_name AS "companyName", biz_no AS "bizNo", ceo_name AS "ceoName",
                   biz_type AS "bizType", contact_name AS "contactName", contact_tel AS "contactTel",
                   address, partner_type AS "partnerType", use_yn AS "useYn"
            FROM company
            <where>
              AND tenant_id = #{tenantId}
              <if test="keyword != null and keyword != ''">
                AND (company_name ILIKE '%'||#{keyword}||'%' OR biz_no ILIKE '%'||#{keyword}||'%'
                     OR contact_name ILIKE '%'||#{keyword}||'%')
              </if>
              <if test="partnerType != null and partnerType != ''">AND partner_type = #{partnerType}</if>
            </where>
            ORDER BY company_name
            LIMIT #{size} OFFSET #{offset}
            </script>
            """)
    List<Map<String, Object>> search(@Param("tenantId") String tenantId, @Param("keyword") String keyword,
                                     @Param("partnerType") String partnerType,
                                     @Param("size") int size, @Param("offset") int offset);

    @Select("""
            <script>
            SELECT count(*) FROM company
            <where>
              AND tenant_id = #{tenantId}
              <if test="keyword != null and keyword != ''">
                AND (company_name ILIKE '%'||#{keyword}||'%' OR biz_no ILIKE '%'||#{keyword}||'%'
                     OR contact_name ILIKE '%'||#{keyword}||'%')
              </if>
              <if test="partnerType != null and partnerType != ''">AND partner_type = #{partnerType}</if>
            </where>
            </script>
            """)
    long count(@Param("tenantId") String tenantId, @Param("keyword") String keyword,
               @Param("partnerType") String partnerType);

    @Insert("INSERT INTO company (tenant_id, id, company_name, biz_no, ceo_name, biz_type, contact_name, "
            + "contact_tel, address, partner_type, use_yn) VALUES (#{tenantId}, #{id}, #{companyName}, #{bizNo}, "
            + "#{ceoName}, #{bizType}, #{contactName}, #{contactTel}, #{address}, #{partnerType}, COALESCE(#{useYn},'Y')) "
            + "ON CONFLICT (tenant_id, id) DO UPDATE SET company_name=EXCLUDED.company_name, biz_no=EXCLUDED.biz_no, "
            + "ceo_name=EXCLUDED.ceo_name, biz_type=EXCLUDED.biz_type, contact_name=EXCLUDED.contact_name, "
            + "contact_tel=EXCLUDED.contact_tel, address=EXCLUDED.address, partner_type=EXCLUDED.partner_type, "
            + "use_yn=EXCLUDED.use_yn, updated_at=now()")
    int upsert(Map<String, Object> p);

    @Delete("DELETE FROM company WHERE tenant_id = #{tenantId} AND id = #{id}")
    int delete(@Param("tenantId") String tenantId, @Param("id") String id);
}
