<?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.TpParabarMapper">
    
    <resultMap type="TpParabar" id="TpParabarResult">
        <result property="id"    column="id"    />
        <result property="parabarName"    column="parabar_name"    />
        <result property="parabarNameUS"    column="parabar_nameUS"    />
        <result property="parabarNameRU"    column="parabar_nameRU"    />
        <result property="parabarNameES"    column="parabar_nameES"    />
        <result property="decorateFlag"    column="decorate_flag"    />
        <result property="status"    column="status"    />
        <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"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.parabar.RelatedModelVO" id="RelatedModelResult" >
        <result property="parabarId"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="elevatorType"    column="elevator_type"    />
        <result property="elevatorModel"    column="elevator_model"    />
        <result property="relatedModel"    column="related_model"    />
    </resultMap>

    <resultMap type="TpParabar" id="TpParabarResultForList"  extends="TpParabarResult">
        <result property="relatedModel"    column="related_model"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.parabar.ParaModelRelatedVO" id="ParaModelRelatedResult">
        <result property="elevatorModelId"    column="elevator_model_id"    />
        <result property="paraCode"    column="para_code"    />
        <result property="paraName"    column="para_name"    />
        <result property="paraNameUS"    column="para_nameUS"    />
        <result property="paraNameRU"    column="para_nameRU"    />
        <result property="paraNameES"    column="para_nameES"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.parabar.CommonPara" id="CommonParaResult" >
        <result property="paraId"    column="id"    />
        <result property="paraCode"    column="para_code"    />
        <result property="paraName"    column="para_name"    />
        <result property="paraNameUS"    column="para_nameUS"    />
        <result property="paraNameRU"    column="para_nameRU"    />
        <result property="paraNameES"    column="para_nameES"    />
        <result property="paraCategory"    column="para_category"    />
        <result property="paraCategoryName"    column="para_category_name"    />
        <result property="inputType"    column="input_type"    />
        <result property="sortNumber"    column="sort_number"    />
    </resultMap>


    <resultMap type="com.ruoyi.project.business.domain.vo.parabar.CommonSelectItem" id="CommonSelectItemResult" >
        <result property="paraId"    column="para_id"    />
        <result property="paraSelectItemId"    column="para_select_item_id"    />
        <result property="value"    column="value"    />
        <result property="label"    column="label"    />
        <result property="labelUS"    column="labelUS"    />
        <result property="labelRU"    column="labelRU"    />
        <result property="labelES"    column="labelES"    />
    </resultMap>

    <sql id="selectTpParabarVo">
        select id, parabar_name, parabar_nameUS, parabar_nameRU, parabar_nameES, decorate_flag, status, del_flag, create_time, create_by, update_time, update_by, remark from tp_parabar
    </sql>

    <select id="selectTpParabarList" parameterType="TpParabar" resultMap="TpParabarResultForList">
        SELECT distinct  tab1.*,  sub1.related_model
        FROM tp_parabar tab1
            INNER JOIN (
                SELECT
                    T1.id,
                    GROUP_CONCAT(
                        CASE #{language}
                        WHEN 'english' THEN
                        CONCAT_WS( '-', T5.area_nameUS, T4.dict_labelUS, T3.elevator_model )
                        WHEN 'russia' THEN
                        CONCAT_WS( '-', T5.area_nameRU, T4.dict_labelRU, T3.elevator_model )
                        WHEN 'spanish' THEN
                        CONCAT_WS( '-', T5.area_nameES, T4.dict_labelES, T3.elevator_model )
                        ELSE CONCAT_WS( '-', T5.area_name, T4.dict_label, T3.elevator_model )
                        END SEPARATOR ';'
                    ) AS related_model
                FROM tp_parabar T1
                    INNER JOIN tp_parabar_related_model T2 ON T2.parabar_id = T1.id
                    INNER JOIN tp_elevator_model T3 ON T3.id = T2.elevator_model_id
                    LEFT JOIN sys_dict_data AS T4 ON T4.dict_type = 'elevator_type'
                    AND T4.dict_value = T3.elevator_type
                    LEFT JOIN tb_area T5 ON T5.area_id = T3.area_id
                GROUP BY	T1.id
                ) sub1 ON tab1.id = sub1.id
        INNER JOIN tp_parabar_related_model tab2 ON tab2.parabar_id = tab1.id
        <where>
            tab1.del_flag = 0
            <if test="parabarName != null  and parabarName != ''"> and tab1.parabar_name like concat('%', #{parabarName}, '%')</if>
            <if test="parabarNameUS != null  and parabarNameUS != ''"> and tab1.parabar_nameUS like concat('%', #{parabarNameUS}, '%')</if>
            <if test="parabarNameRU != null  and parabarNameRU != ''"> and tab1.parabar_nameRU = #{parabarNameRU}</if>
            <if test="parabarNameES != null  and parabarNameES != ''"> and tab1.parabar_nameES = #{parabarNameES}</if>
            <if test="decorateFlag != null  and decorateFlag != ''"> and tab1.decorate_flag = #{decorateFlag}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="relatedModelIdArrary != null  and relatedModelIdArrary.length != 0">
                and tab2.elevator_model_id in
                <foreach item="modelId" collection="relatedModelIdArrary" open="(" separator="," close=")">
                    #{modelId}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectTpParabarById" parameterType="Long" resultMap="TpParabarResult">
        <include refid="selectTpParabarVo"/>
        where id = #{id}
    </select>

    <select id="getRelatedModelList"  parameterType="TpParabar" resultMap="RelatedModelResult">
        SELECT
            T3.id,
            T3.area_id,
            T3.elevator_type,
            T3.elevator_model,
            <if test="showType">
                CASE #{language}
                WHEN 'english' THEN
                CONCAT_WS( '-', T5.area_nameUS, T4.dict_labelUS, T3.elevator_model )
                WHEN 'russia' THEN
                CONCAT_WS( '-', T5.area_nameRU, T4.dict_labelRU, T3.elevator_model )
                WHEN 'spanish' THEN
                CONCAT_WS( '-', T5.area_nameES, T4.dict_labelES, T3.elevator_model )
                ELSE CONCAT_WS( '-', T5.area_name, T4.dict_label, T3.elevator_model )
                END  AS related_model
            </if>
            <if test="!showType">
                CASE #{language}
                WHEN 'english' THEN
                CONCAT_WS( '-', T5.area_nameUS, T3.elevator_model )
                WHEN 'russia' THEN
                CONCAT_WS( '-', T5.area_nameRU, T3.elevator_model )
                WHEN 'spanish' THEN
                CONCAT_WS( '-', T5.area_nameES, T3.elevator_model )
                ELSE CONCAT_WS( '-', T5.area_name, T3.elevator_model )
                END  AS related_model
            </if>
        FROM
            tp_elevator_model T3
                    <if test="relatedType == '1'.toString()">
                        INNER JOIN (SELECT DISTINCT T1.elevator_model_id FROM	tp_parabar_related_model T1  WHERE T1.del_flag = 0 	) SUB1 ON T3.id = SUB1.elevator_model_id
                    </if>
                    <if test="relatedType == '2'.toString()">
                        INNER JOIN (SELECT DISTINCT T1.elevator_model_id FROM	tp_case_elevator T1  WHERE T1.del_flag = 0 	) SUB1 ON T3.id = SUB1.elevator_model_id
                    </if>
                    <if test="relatedType == '3'.toString()">
                        INNER JOIN (SELECT DISTINCT T1.elevator_model_id FROM	tp_para_validation_elevator T1  WHERE T1.del_flag = 0 	) SUB1 ON T3.id = SUB1.elevator_model_id
                    </if>

                LEFT JOIN sys_dict_data AS T4 ON T4.dict_type = 'elevator_type'
                AND T4.dict_value = T3.elevator_type
                LEFT JOIN tb_area T5 ON T5.area_id = T3.area_id
        WHERE T3.del_flag = 0
                    <if test="ids != null and ids.size() > 0">
                    and id in
                        <foreach collection="ids" item="id" open="(" separator="," close=")">
                            #{id}
                        </foreach>
                    </if>
    </select>

    <select id="getCommonParaList"  parameterType="com.ruoyi.project.business.domain.vo.parabar.ParaForGetCommonPara" resultMap="CommonParaResult">
        SELECT
            tab1.*, t3.dict_label AS para_category_name
        FROM
            tp_para_library tab1
                INNER JOIN (
                SELECT
                    para_code,
                    Count( para_code ) AS groupCount
                FROM
                    tp_area_elevator_model_para
                WHERE
                    del_flag = 0
        <if test="relatedModel != null  and relatedModel.length != 0">
                    and elevator_model_id in
            <foreach item="modelId" collection="relatedModel" open="(" separator="," close=")">
                #{modelId}
            </foreach>
        </if>
                GROUP BY para_code) sub1 ON tab1.para_code = sub1.para_code
                LEFT JOIN sys_dict_data AS t3 ON t3.dict_type = 'para_category' AND tab1.para_category = t3.dict_value
        <where>
        <if test="modelCount > 0">
            and groupCount = #{modelCount}
        </if>
        <if test="selectedPara != null  and selectedPara.length != 0">
            and tab1.id in
            <foreach item="para" collection="selectedPara" open="(" separator="," close=")">
                #{para}
            </foreach>
        </if>
        <if test="query != null  and query != ''">
            and  (
            tab1.para_code like concat('%',#{query},'%') or
            tab1.para_name like concat('%',#{query},'%')
            )
        </if>
        </where>
    </select>

    <select id="getCommonParaAndItemList"  parameterType="com.ruoyi.project.business.domain.vo.parabar.ParaForGetCommonPara" resultMap="CommonSelectItemResult">
        SELECT
            sub1.para_id ,
            sub1.para_select_item_id,
            tab1.value,
            tab1.label,
            tab1.labelUS,
            tab1.labelRU,
            tab1.labelES,
            sub1.item_count
        FROM tp_para_select_item tab1
                 INNER JOIN
             (
                 SELECT
                     para_id AS para_id,
                     para_select_item_id AS para_select_item_id,
                     COUNT(*) AS item_count
                 FROM
                     tp_area_para_select_item
                 WHERE
                     del_flag = 0
        <if test="relatedModel != null  and relatedModel.length != 0">
                    AND elevator_model_id in
            <foreach item="modelId" collection="relatedModel" open="(" separator="," close=")">
                #{modelId}
            </foreach>
        </if>
        GROUP BY para_id, para_select_item_id
        ) sub1
        ON sub1.para_id = tab1.para_id and sub1.para_select_item_id = tab1.id
        WHERE
        sub1.item_count = #{modelCount}
        <if test="selectedPara != null  and selectedPara.length != 0">
            AND sub1.para_id in
            <foreach item="para" collection="selectedPara" open="(" separator="," close=")">
                #{para}
            </foreach>
        </if>
        ORDER BY tab1.sort_number ASC
    </select>

    <select id="selectParaModelRelatedList"  parameterType="com.ruoyi.project.business.domain.vo.parabar.ParaModelRelatedVO" resultMap="ParaModelRelatedResult">
        SELECT
            T1.elevator_model_id
            ,tab1.para_code
            ,tab2.para_name
            ,tab2.para_nameUS
            ,tab2.para_nameRU
            ,tab2.para_nameES
        FROM
            tp_parabar_related_model T1
                INNER JOIN tp_parabar_detail_value tab1 on tab1.parabar_id = T1.parabar_id
                LEFT JOIN tp_para_library tab2 on tab2.para_code = tab1.para_code and tab2.del_flag = 0 and tab2.status = 0
        WHERE T1.del_flag = 0 and
              T1.elevator_model_id in
        <foreach item="modelId" collection="modelIds" open="(" separator="," close=")">
            #{modelId}
        </foreach>
    </select>

    <resultMap type="com.ruoyi.project.business.domain.vo.parabar.ElParabarVO" id="ElParabarVOResult">
        <result property="id" column="id"/>
        <result property="parabarName" column="parabar_name"/>
        <result property="parabarNameUS" column="parabar_nameUS"/>
        <result property="parabarNameRU" column="parabar_nameRU"/>
        <result property="parabarNameES" column="parabar_nameES"/>
        <result property="decorateFlag" column="decorate_flag"/>
        <collection property="paramList" ofType="com.ruoyi.project.business.domain.vo.parabar.ElParabarDetailVO">
            <result property="id" column="detailId"/>
            <result property="url" column="file_path"/>
            <result property="parabarDetailName" column="parabar_detail_name"/>
            <result property="parabarDetailNameUS" column="parabar_detail_nameUS"/>
            <result property="parabarDetailNameRU" column="parabar_detail_nameRU"/>
            <result property="parabarDetailNameES" column="parabar_detail_nameES"/>
            <collection property="paramOptionList"
                ofType="com.ruoyi.project.business.domain.vo.parabar.ElParabarDetailValueVO">
                <result property="paraCategory" column="para_category"/>
                <result property="paraCode" column="para_code"/>
                <result property="inputType" column="input_type"/>
                <result property="paraValue" column="para_value"/>
                <result property="sortNumber" column="param_sort"/>
                <result property="paraName" column="para_name"/>
                <result property="paraNameUS" column="para_nameUS"/>
                <result property="paraNameES" column="para_nameES"/>
                <result property="paraNameRU" column="para_nameRU"/>
                <result property="paramOptionLabel" column="label"/>
                <result property="paramOptionLabelUS" column="labelUS"/>
                <result property="paramOptionLabelES" column="labelES"/>
                <result property="paramOptionLabelRU" column="labelRU"/>
            </collection>
        </collection>
    </resultMap>

    <select id="getParabarConfigByElevatorModelId" resultMap="ElParabarVOResult">
        SELECT tpl.sort_number as param_sort,
               tp.*,
               tpd.*,
               tpdv.*,
               library.para_category,
               tf.file_path,
               tpd.id          as detailId,
               tpl.*,
               tpsi.*
        FROM tp_parabar tp
                 LEFT JOIN tp_parabar_detail tpd on tp.id = tpd.parabar_id
                 LEFT JOIN tp_parabar_detail_value tpdv on tp.id = tpdv.parabar_id and tpd.id = tpdv.parabar_detail_id
                 LEFT JOIN tp_para_library library on tpdv.para_code = library.para_code
                 LEFT JOIN tb_file tf on tpd.id = tf.business_id and tf.business_type = '6' and tf.del_flag != 1
                 LEFT JOIN tp_para_library tpl on tpl.para_code = tpdv.para_code
                 LEFT JOIN tp_para_select_item tpsi on tpsi.para_id = tpl.id and tpdv.para_value = tpsi.value
        WHERE tp.del_flag != 1
          and tp.status != 1
          and tpd.del_flag != 1
          and tpd.status != 1
          and tpdv.del_flag != 1
          and tp.id in (SELECT parabar_id FROM tp_parabar_related_model tprm WHERE tprm.elevator_model_id = #{id})
        order by tpd.sort_number asc
    </select>

    <insert id="insertTpParabar" parameterType="TpParabar" useGeneratedKeys="true" keyProperty="id">
        insert into tp_parabar
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parabarName != null">parabar_name,</if>
            <if test="parabarNameUS != null">parabar_nameUS,</if>
            <if test="parabarNameRU != null">parabar_nameRU,</if>
            <if test="parabarNameES != null">parabar_nameES,</if>
            <if test="decorateFlag != null">decorate_flag,</if>
            <if test="status != null">status,</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="parabarName != null">#{parabarName},</if>
            <if test="parabarNameUS != null">#{parabarNameUS},</if>
            <if test="parabarNameRU != null">#{parabarNameRU},</if>
            <if test="parabarNameES != null">#{parabarNameES},</if>
            <if test="decorateFlag != null">#{decorateFlag},</if>
            <if test="status != null">#{status},</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="updateTpParabar" parameterType="TpParabar">
        update tp_parabar
        <trim prefix="SET" suffixOverrides=",">
            <if test="parabarName != null">parabar_name = #{parabarName},</if>
            <if test="parabarNameUS != null">parabar_nameUS = #{parabarNameUS},</if>
            <if test="parabarNameRU != null">parabar_nameRU = #{parabarNameRU},</if>
            <if test="parabarNameES != null">parabar_nameES = #{parabarNameES},</if>
            <if test="decorateFlag != null">decorate_flag = #{decorateFlag},</if>
            <if test="status != null">status = #{status},</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 id = #{id}
    </update>

    <delete id="deleteTpParabarById" parameterType="Long">
        update tp_parabar set  del_flag = 1  where id = #{id}
    </delete>

    <delete id="deleteTpParabarByIds" parameterType="String">
        update tp_parabar set  del_flag = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>