<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="kr.itn.itnhub.org.OrganizationMapper">

  <!--
    담당자 3종은 contact 테이블을 세 번 조인해서 채운다(신청기관/문정원/변호사 배정이 각각
    다른 contact 행을 가리킬 수 있으므로). 각 조인에 별칭 컬럼을 붙여야 resultMap의
    association 세 개가 서로 컬럼을 침범하지 않는다.
  -->
  <resultMap id="organizationResultMap" type="kr.itn.itnhub.org.Organization">
    <id property="id" column="id"/>
    <result property="orgNo" column="org_no"/>
    <result property="orgName" column="org_name"/>
    <result property="channelSlug" column="channel_slug"/>
    <result property="applicantContactId" column="applicant_contact_id"/>
    <result property="mjContactId" column="mj_contact_id"/>
    <result property="lawyerContactId" column="lawyer_contact_id"/>
    <result property="lawyerAssignedDate" column="lawyer_assigned_date"/>
    <result property="channelIdMj" column="channel_id_mj"/>
    <result property="channelIdLaw" column="channel_id_law"/>

    <association property="applicant" javaType="kr.itn.itnhub.contact.Contact">
      <id property="id" column="applicant_id"/>
      <result property="category" column="applicant_category"/>
      <result property="name" column="applicant_name"/>
      <result property="affiliation" column="applicant_affiliation"/>
      <result property="deptName" column="applicant_dept_name"/>
      <result property="title" column="applicant_title"/>
      <result property="phone" column="applicant_phone"/>
      <result property="email" column="applicant_email"/>
    </association>

    <association property="mj" javaType="kr.itn.itnhub.contact.Contact">
      <id property="id" column="mj_c_id"/>
      <result property="category" column="mj_c_category"/>
      <result property="name" column="mj_c_name"/>
      <result property="affiliation" column="mj_c_affiliation"/>
      <result property="deptName" column="mj_c_dept_name"/>
      <result property="title" column="mj_c_title"/>
      <result property="phone" column="mj_c_phone"/>
      <result property="email" column="mj_c_email"/>
    </association>

    <association property="lawyer" javaType="kr.itn.itnhub.contact.Contact">
      <id property="id" column="lawyer_c_id"/>
      <result property="category" column="lawyer_c_category"/>
      <result property="name" column="lawyer_c_name"/>
      <result property="affiliation" column="lawyer_c_affiliation"/>
      <result property="deptName" column="lawyer_c_dept_name"/>
      <result property="title" column="lawyer_c_title"/>
      <result property="phone" column="lawyer_c_phone"/>
      <result property="email" column="lawyer_c_email"/>
    </association>
  </resultMap>

  <sql id="joinedColumns">
    o.id, o.org_no, o.org_name, o.channel_slug,
    o.applicant_contact_id, o.mj_contact_id, o.lawyer_contact_id,
    o.lawyer_assigned_date, o.channel_id_mj, o.channel_id_law,
    ac.id as applicant_id, ac.category as applicant_category, ac.name as applicant_name,
    ac.affiliation as applicant_affiliation, ac.dept_name as applicant_dept_name,
    ac.title as applicant_title, ac.phone as applicant_phone, ac.email as applicant_email,
    mc.id as mj_c_id, mc.category as mj_c_category, mc.name as mj_c_name,
    mc.affiliation as mj_c_affiliation, mc.dept_name as mj_c_dept_name,
    mc.title as mj_c_title, mc.phone as mj_c_phone, mc.email as mj_c_email,
    lc.id as lawyer_c_id, lc.category as lawyer_c_category, lc.name as lawyer_c_name,
    lc.affiliation as lawyer_c_affiliation, lc.dept_name as lawyer_c_dept_name,
    lc.title as lawyer_c_title, lc.phone as lawyer_c_phone, lc.email as lawyer_c_email
  </sql>

  <sql id="joins">
    from organization o
    left join contact ac on ac.id = o.applicant_contact_id
    left join contact mc on mc.id = o.mj_contact_id
    left join contact lc on lc.id = o.lawyer_contact_id
  </sql>

  <select id="findAll" resultMap="organizationResultMap">
    select <include refid="joinedColumns"/>
    <include refid="joins"/>
    order by o.org_no asc, o.org_name asc
  </select>

  <select id="findById" resultMap="organizationResultMap">
    select <include refid="joinedColumns"/>
    <include refid="joins"/>
    where o.id = #{id}
  </select>

  <select id="findByOrgNoAndOrgName" resultMap="organizationResultMap">
    select <include refid="joinedColumns"/>
    <include refid="joins"/>
    where o.org_no = #{orgNo} and o.org_name = #{orgName}
  </select>

  <select id="findByOrgName" resultMap="organizationResultMap">
    select <include refid="joinedColumns"/>
    <include refid="joins"/>
    where o.org_name = #{orgName}
    limit 1
  </select>

  <!--
    시드 upsert. 담당자 정보는 이제 여기서 다루지 않는다(담당자관리에서만 입력) -
    SeedService가 upsert 이후 별도로 신청기관 담당자 연결 여부를 확인해 필요할 때만
    contact를 만들어 연결한다.
  -->
  <insert id="upsertBySeed" parameterType="kr.itn.itnhub.org.Organization">
    insert into organization (org_no, org_name, channel_slug)
    values (#{orgNo}, #{orgName}, #{channelSlug})
    on conflict (org_no, org_name) do update set
      channel_slug = excluded.channel_slug,
      updated_at   = now()
  </insert>

  <update id="updateAssignments">
    update organization set
      applicant_contact_id = #{applicantContactId},
      mj_contact_id         = #{mjContactId},
      lawyer_contact_id     = #{lawyerContactId},
      lawyer_assigned_date  = #{lawyerAssignedDate},
      updated_at            = now()
    where id = #{id}
  </update>

  <update id="updateChannelIdMj">
    update organization set
      channel_id_mj = #{channelId},
      updated_at    = now()
    where id = #{id}
  </update>

  <update id="updateChannelIdLaw">
    update organization set
      channel_id_law = #{channelId},
      updated_at     = now()
    where id = #{id}
  </update>

</mapper>
