<?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.ruoyi.project.business.mapper.TbAreaMapper">

    <resultMap type="TbArea" id="TbAreaResult">
        <result property="areaId" column="area_id"/>
        <result property="areaName" column="area_name"/>
        <result property="areaNameUS" column="area_nameUS"/>
        <result property="areaNameES" column="area_nameES"/>
        <result property="areaNameRU" column="area_nameRU"/>
        <result property="areaLevel" column="area_level"/>
        <result property="parentArea" column="parent_area"/>
        <result property="permissionChar" column="permission_char"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="countryIds" column="countryIds"/>
        <result property="parentAreaName" column="parentAreaName"/>
        <result property="i18AreaLevel" column="i18AreaLevel"/>
    </resultMap>

    <sql id="selectTbAreaVo">
        select area_id,
               area_name,
               area_nameUS,
               area_nameES,
               area_nameRU,
               area_level,
               parent_area,
               permission_char,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from tb_area
    </sql>

    <sql id="selectTbAreaList">
        select a.area_id,
               a.area_name,
               a.area_nameUS,
               a.area_nameES,
               a.area_nameRU,
               a.area_level,
               a.parent_area,
               a.permission_char,
               a.del_flag,
               a.create_time,
               a.create_by,
               a.update_time,
               a.update_by,
               a.remark,
               b.area_name as parentAreaName
        from tb_area a
                 left join tb_area b on a.parent_area = b.area_id
    </sql>

    <sql id="selectTbAreaListExport">
        select a.area_id,
               a.area_name,
               a.area_nameUS,
               a.area_nameES,
               a.area_nameRU,
               a.area_level,
               a.parent_area,
               a.permission_char,
               a.del_flag,
               a.create_time,
               a.create_by,
               a.update_time,
               a.update_by,
               a.remark,
               b.area_name as parentAreaName,
               CASE #{language}
                   WHEN 'english' THEN d.dict_labelUS
                   WHEN 'russia' THEN d.dict_labelRU
                   WHEN 'spanish' THEN d.dict_labelES
                   ELSE d.dict_label
                   END     AS i18AreaLevel
        from tb_area a
                 left join tb_area b on a.parent_area = b.area_id
                 left join sys_dict_data as d on d.dict_type = 'area_level' and a.area_level = d.dict_value
    </sql>

    <sql id="listWhereSql">
        <where>
            <if test="areaName != null  and areaName != ''">
                and a.area_name like concat('%', #{areaName}, '%') escape '/'
            </if>
            <if test="areaNameUS != null  and areaNameUS != ''">
                and a.area_nameUS like concat('%', #{areaNameUS}, '%') escape '/'
            </if>
            <if test="areaNameES != null  and areaNameES != ''">
                and a.area_nameES like concat('%', #{areaNameES}, '%') escape '/'
            </if>
            <if test="areaNameRU != null  and areaNameRU != ''">
                and a.area_nameRU like concat('%', #{areaNameRU}, '%') escape '/'
            </if>
            <if test="areaLevel != null and areaLevel.size() > 0">
                and a.area_level in
                <foreach collection="areaLevel" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="parentArea != null and parentArea.size() > 0">
                and a.parent_area in
                <foreach collection="parentArea" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
                and date_format(a.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
                and date_format(a.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            <if test="ids != null and ids.size() > 0">
                and a.area_id in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            and a.del_flag = '0'
        </where>
        order by a.area_id desc
    </sql>

    <select id="selectTbAreaList" parameterType="com.ruoyi.project.business.domain.vo.selectParam.AreaParam" resultMap="TbAreaResult">
        <include refid="selectTbAreaList"/>
        <include refid="listWhereSql"/>
    </select>

    <select id="selectTbAreaListExport" parameterType="com.ruoyi.project.business.domain.vo.selectParam.AreaParam" resultMap="TbAreaResult">
        <include refid="selectTbAreaListExport"/>
        <include refid="listWhereSql"/>
    </select>

    <select id="selectTbAreaByAreaId" parameterType="Long" resultMap="TbAreaResult">
        select a.area_id,
               a.area_name,
               a.area_nameUS,
               a.area_nameES,
               a.area_nameRU,
               a.area_level,
               a.parent_area,
               a.permission_char,
               a.del_flag,
               a.create_time,
               a.create_by,
               a.update_time,
               a.update_by,
               a.remark,
               group_concat(c.country_id) as countryIds
        from tb_area a
                 left join tb_country_area c on a.area_id = c.area_id
        where a.area_id = #{areaId}
    </select>

    <select id="getAllAreaList" resultMap="TbAreaResult">
        <include refid="selectTbAreaVo"/>
        where del_flag = '0'
    </select>

    <select id="getAreaListByLevel" resultMap="TbAreaResult">
        <include refid="selectTbAreaVo"/>
        where area_level = #{areaLevel} and del_flag = '0'
    </select>

    <select id="getAreaListByCountryId" resultMap="TbAreaResult">
        select a.area_id,
               a.area_name,
               a.area_nameUS,
               a.area_nameES,
               a.area_nameRU,
               a.area_level,
               a.parent_area,
               a.permission_char,
               a.del_flag,
               a.create_time,
               a.create_by,
               a.update_time,
               a.update_by,
               a.remark
        from tb_area a
                 left join tb_country_area b on a.area_id = b.area_id
        where b.country_id = #{countryId}
    </select>
    <select id="selectAreaListByIds" resultMap="TbAreaResult">
        select * from tb_area where area_id in
        <foreach item="areaId" collection="areaIds" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>

    <select id="selectTbAreaByAreaName" resultType="java.lang.Integer">
        select count(1) from tb_area where area_name = #{areaName} and del_flag = '0'
        <if test="areaId != null">and area_id != #{areaId}</if>
    </select>

    <select id="selectTbAreaByParentAreas" resultType="java.lang.Integer">
        select count(1) from tb_area where del_flag = '0' and parent_area in
        <foreach item="areaId" collection="areaIds" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </select>


    <resultMap id="selectTbAreaWithCountriesListMap" type="com.ruoyi.project.business.domain.vo.country.AreaWithCountriesVo">
        <id column="area_id" property="areaId"/>
        <result column="area_name" property="areaName"/>
        <result column="area_nameUS" property="areaNameUS"/>
        <result column="area_nameRU" property="areaNameRU"/>
        <result column="area_nameES" property="areaNameES"/>
        <result column="area_level" property="areaLevel"/>
        <result column="parent_area" property="parentArea"/>
        <result column="permission_char" property="permissionChar"/>
        <result column="del_flag" property="delFlag"/>
        <result column="create_time" property="createTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="remark" property="remark"/>
        
        <collection property="countryList" ofType="com.ruoyi.project.business.domain.TbCountry" javaType="java.util.ArrayList">
            <id column="country_id" property="countryId"/>
            <result column="country_name" property="countryName"/>
            <result column="country_nameUS" property="countryNameUS"/>
            <result column="country_nameRU" property="countryNameRU"/>
            <result column="country_nameES" property="countryNameES"/>
            <result column="discount" property="discount"/>
            <result column="del_flag" property="delFlag"/>
            <result column="create_time" property="createTime"/>
            <result column="create_by" property="createBy"/>
            <result column="update_time" property="updateTime"/>
            <result column="update_by" property="updateBy"/>
            <result column="remark" property="remark"/>
        </collection>
    </resultMap>
    <select id="selectTbAreaWithCountriesList" resultMap="selectTbAreaWithCountriesListMap">
        select t.*, c.* from (
        <include refid="selectTbAreaVo"/>
        ) t
        left join tb_country_area ca on t.area_id = ca.area_id and ca.del_flag = '0'
        left join tb_country c on ca.country_id = c.country_id and c.del_flag = '0'
        where t.del_flag = '0'
    </select>
    <select id="selectChildAreaList" resultType="com.ruoyi.project.business.domain.TbArea">
        SELECT *
        FROM tb_area
        WHERE del_flag = '0'
          AND REVERSE(permission_char) LIKE CONCAT(#{permissionChar}, '%')
    </select>

    <select id="selectChildAreaByAreaIds" resultType="com.ruoyi.project.business.domain.TbArea">
        select * from tb_area where del_flag = '0'
        <foreach item="areaId" collection="areaIds" separator=" OR " open=" AND (" close=")">
            find_in_set(#{areaId}, permission_char)
        </foreach>
    </select>

    <!--<select id="selectChildAreaIdsByAreaId" resultType="java.lang.Long">-->
    <!--    select area_id from tb_area where find_in_set(#{areaId}, permission_char) and del_flag = '0'-->
    <!--</select>-->
    <select id="selectChildAreaIdsByAreaIds" resultType="java.lang.Long">
        select area_id from tb_area where del_flag = '0'
        <foreach item="areaId" collection="areaIds" separator=" OR " open=" AND (" close=")">
            find_in_set(#{areaId}, permission_char)
        </foreach>
    </select>

    <select id="selectDeepOneAreaList" resultType="java.lang.Long">
        select area_id from tb_area where del_flag = '0' and area_id in
        <foreach item="areaId" collection="areaIds" separator="," open="(" close=")">
            #{areaId}
        </foreach>
        or parent_area in
        <foreach item="areaId" collection="areaIds" separator="," open="(" close=")">
            #{areaId}
        </foreach>
    </select>
    <select id="selectListByIdsAndAncestorId" resultType="com.ruoyi.project.business.domain.TbArea">
        select * from tb_area where del_flag = '0' and area_id in
        <foreach item="areaId" collection="areaIdList" open="(" separator="," close=")">
            #{areaId}
        </foreach>
        and find_in_set(#{ancestorId}, permission_char)
    </select>
    <select id="listByAreaName" resultType="com.ruoyi.project.business.domain.TbArea">
        select * from tb_area where del_flag = '0' and area_name in
        <foreach item="area" collection="areaList" open="(" separator="," close=")">
            #{area}
        </foreach>
    </select>

    <insert id="insertTbArea" parameterType="TbArea" useGeneratedKeys="true" keyProperty="areaId">
        insert into tb_area
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="areaName != null">area_name,</if>
            <if test="areaNameUS != null">area_nameUS,</if>
            <if test="areaNameES != null">area_nameES,</if>
            <if test="areaNameRU != null">area_nameRU,</if>
            <if test="areaLevel != null">area_level,</if>
            <if test="parentArea != null">parent_area,</if>
            <if test="permissionChar != null">permission_char,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="areaNameUS != null">#{areaNameUS},</if>
            <if test="areaNameES != null">#{areaNameES},</if>
            <if test="areaNameRU != null">#{areaNameRU},</if>
            <if test="areaLevel != null">#{areaLevel},</if>
            <if test="parentArea != null">#{parentArea},</if>
            <if test="permissionChar != null">#{permissionChar},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateTbArea" parameterType="TbArea">
        update tb_area
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="areaNameUS != null">area_nameUS = #{areaNameUS},</if>
            <if test="areaNameES != null">area_nameES = #{areaNameES},</if>
            <if test="areaNameRU != null">area_nameRU = #{areaNameRU},</if>
            <if test="areaLevel != null">area_level = #{areaLevel},</if>
            <if test="parentArea != null">parent_area = #{parentArea},</if>
            <if test="permissionChar != null">permission_char = #{permissionChar},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where area_id = #{areaId}
    </update>

    <update id="deleteTbAreaByAreaId" parameterType="Long">
        update tb_area
        set del_flag = '1'
        where area_id = #{areaId}
    </update>

    <update id="deleteTbAreaByAreaIds" parameterType="String">
        update tb_area set del_flag = '1' where area_id in
        <foreach item="areaId" collection="array" open="(" separator="," close=")">
            #{areaId}
        </foreach>
    </update>
</mapper>