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

  <sql id="driverSearchCondition">
    <if test="keyword != null and keyword != ''">
      AND (USER_NAME ILIKE '%' || #{keyword} || '%'
           OR CELL_PHONE ILIKE '%' || #{keyword} || '%'
           OR VEHICLE_NUMBER ILIKE '%' || #{keyword} || '%')
    </if>
  </sql>

  <!-- 운전자 목록 (signup_type = 'DRIVER' 인 user_info 기반) -->
  <select id="getDriverList" parameterType="map" resultType="map">
    SELECT
        USER_ID
        , USER_NAME
        , CELL_PHONE
        , LICENSE_NUMBER
        , VEHICLE_NUMBER
        , SIGNUP_TYPE
        , BRANCH_NAME
        , COMPANY_CODE

    FROM USER_INFO

    WHERE SIGNUP_TYPE = 'DRIVER'
    <include refid="common.companyCodeFilter"/>
    <include refid="driverSearchCondition"/>

    <choose>
      <when test="sort_column != null and sort_column != ''">
        ORDER BY ${sortColumn}
        <if test="sort_direction != null and sort_direction != ''">
          ${sortDirection}
        </if>
      </when>
      <otherwise>
        ORDER BY CREATED_DATE DESC
      </otherwise>
    </choose>
    <include refid="common.pagination"/>
  </select>

  <select id="getDriverListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)

    FROM USER_INFO

    WHERE SIGNUP_TYPE = 'DRIVER'
    <include refid="common.companyCodeFilter"/>
    <include refid="driverSearchCondition"/>
  </select>

  <!-- 사용자 정보 조회 (user_id 기준) -->
  <select id="getDriverUserInfo" parameterType="map" resultType="map">
    SELECT
        USER_ID
        , USER_NAME
        , CELL_PHONE
        , LICENSE_NUMBER
        , VEHICLE_NUMBER
        , SIGNUP_TYPE
        , BRANCH_NAME
        , COMPANY_CODE

    FROM USER_INFO

    WHERE USER_ID = #{user_id}
  </select>

  <!-- 차량 정보 조회 (user_id 기준) -->
  <select id="getDriverVehicle" parameterType="map" resultType="map">
    SELECT
        VEHICLE_NUMBER
        , VEHICLE_TYPE
        , DRIVER_NAME
        , DRIVER_PHONE
        , STATUS
        , BRANCH_NAME
        , COMPANY_CODE

    FROM VEHICLES

    WHERE USER_ID = #{user_id}
  </select>

  <!-- 사용자 정보 수정 -->
  <update id="updateDriverUserInfo" parameterType="map">
    UPDATE USER_INFO

    SET
        USER_NAME      = COALESCE(#{user_name},      USER_NAME)
        , CELL_PHONE     = COALESCE(#{phone_number},   CELL_PHONE)
        , LICENSE_NUMBER = COALESCE(#{license_number}, LICENSE_NUMBER)
        , VEHICLE_NUMBER = COALESCE(#{vehicle_number}, VEHICLE_NUMBER)
        , BRANCH_NAME    = COALESCE(#{branch_name},    BRANCH_NAME)

    WHERE USER_ID = #{user_id}
  </update>

  <!-- 차량 정보 수정 -->
  <update id="updateDriverVehicle" parameterType="map">
    UPDATE VEHICLES

    SET
        VEHICLE_NUMBER = COALESCE(#{vehicle_number}, VEHICLE_NUMBER)
        , VEHICLE_TYPE   = COALESCE(#{vehicle_type},   VEHICLE_TYPE)
        , DRIVER_NAME    = COALESCE(#{user_name},       DRIVER_NAME)
        , DRIVER_PHONE   = COALESCE(#{phone_number},    DRIVER_PHONE)
        , BRANCH_NAME    = COALESCE(#{branch_name},     BRANCH_NAME)
        , UPDATED_DATE     = NOW()

    WHERE USER_ID = #{user_id}
  </update>

  <!-- 차량 상태 변경 (대기: off, 정비: maintenance) -->
  <update id="updateDriverVehicleStatus" parameterType="map">
    UPDATE VEHICLES

    SET
        STATUS     = #{status}
        , UPDATED_DATE = NOW()

    WHERE USER_ID = #{user_id}
  </update>

  <!-- 차량 소프트 삭제 (user_id = NULL, status = 'disabled') -->
  <update id="softDeleteDriverVehicle" parameterType="map">
    UPDATE VEHICLES

    SET
        USER_ID    = NULL
        , STATUS     = 'disabled'
        , UPDATED_DATE = NOW()

    WHERE USER_ID = #{user_id}
  </update>

  <!-- user_info.vehicle_number 초기화 -->
  <update id="clearDriverVehicleNumber" parameterType="map">
    UPDATE USER_INFO

    SET VEHICLE_NUMBER = NULL

    WHERE USER_ID = #{user_id}
  </update>

  <!-- 새 차량 등록 -->
  <insert id="insertDriverVehicle" parameterType="map">
    INSERT INTO VEHICLES (
        VEHICLE_NUMBER
        , VEHICLE_TYPE
        , USER_ID
        , DRIVER_NAME
        , DRIVER_PHONE
        , BRANCH_NAME
        , STATUS
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    ) VALUES (
        #{vehicle_number}
        , #{vehicle_type}
        , #{user_id}
        , #{user_name}
        , #{phone_number}
        , #{branch_name}
        , 'off'
        , #{company_code}
        , NOW()
        , NOW()
    )
  </insert>

  <!-- 차량 물리 삭제 (회원 탈퇴 시) -->
  <delete id="deleteDriverVehicles" parameterType="map">
    DELETE FROM VEHICLES

    WHERE USER_ID = #{user_id}
  </delete>

  <!-- 사용자 계정 삭제 -->
  <delete id="deleteDriverUserInfo" parameterType="map">
    DELETE FROM USER_INFO

    WHERE USER_ID = #{user_id}
  </delete>

</mapper>
