<?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="com.hotwj.platform.common.mapper.PublicMapper">

    <select id="selectList" resultType="com.hotwj.platform.common.vo.AddressVo">
        select
            a.id as id,
            a.code as value,
            a.name as label
        from address_components a
        where a.province = '51' and a.town = '0'
    </select>
	
    <!-- 单SQL透视：将6位区划代码转换为 省 市 区 名称（以空格拼接） -->
    <select id="selectRegionFullNameByCode" resultType="java.lang.String">
        SELECT CONCAT_WS('',
               MAX(CASE WHEN city = '0' AND area = '0' AND town = '0' THEN name END),
               MAX(CASE WHEN city = SUBSTRING(#{code}, 3, 2) AND area = '0' AND town = '0' THEN name END),
               MAX(CASE WHEN city = SUBSTRING(#{code}, 3, 2) AND area = SUBSTRING(#{code}, 5, 2) AND town = '0' THEN name END)
        ) AS full_name
        FROM address_components
        WHERE province = SUBSTRING(#{code}, 1, 2)
          AND (
                (city = '0' AND area = '0' AND town = '0')
             OR (city = SUBSTRING(#{code}, 3, 2) AND area = '0' AND town = '0')
             OR (city = SUBSTRING(#{code}, 3, 2) AND area = SUBSTRING(#{code}, 5, 2) AND town = '0')
          )
    </select>

    <select id="selectAddressByCode" resultType="com.hotwj.platform.common.vo.AddressVo">
        select
            a.id as id,
            a.code as value,
            a.name as label
        from address_components a
        where a.code = #{code} and a.town = '0'
    </select>

    <select id="selectUserNamesByIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT(nick_name SEPARATOR '、')
        FROM sys_user
        WHERE user_id IN
        <foreach collection="userIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND del_flag = '0'
    </select>

    <select id="selectCourseNamesByIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT(course_name SEPARATOR '、')
        FROM hot_course
        WHERE id IN
        <foreach collection="courseIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND is_deleted = 0
    </select>

    <select id="selectAttendeeNamesByIds" resultType="java.lang.String">
        SELECT GROUP_CONCAT(u.name ORDER BY u.create_time DESC, u.id_str DESC SEPARATOR '、') AS names
        FROM (
        SELECT CAST(d.id AS CHAR) AS id_str, d.name AS name, d.create_time AS create_time
        FROM hot_driver d
        WHERE d.is_deleted = 0
        UNION ALL
        SELECT CAST(m.id AS CHAR) AS id_str, m.name AS name, m.create_time AS create_time
        FROM hot_company_safety_manager m
        WHERE m.is_deleted = 0
        ) u
        WHERE u.id_str IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>
