<?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.TpElevatorModelMapper">

    <resultMap type="com.ruoyi.project.business.domain.TpElevatorModel" id="TpElevatorModelResult">
        <result property="id"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="elevatorType"    column="elevator_type"    />
        <result property="elevatorModel"    column="elevator_model"    />
        <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"    />
        <result property="targetCountryIds"    column="target_country_ids"    />
        <result property="targetUserIds"    column="target_user_ids"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.paramSelectItem.ElevatorModeParamlVo" id="TpElevatorModelParamResult">
        <result property="id"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="elevatorType"    column="elevator_type"    />
        <result property="elevatorModel"    column="elevator_model"    />
        <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"    />
        <collection property="paraSelect" ofType="com.ruoyi.project.business.domain.vo.paramSelectItem.AreaElevatorModelParaVO">
            <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="inputType"    column="input_type"    />
            <result property="paramId"    column="paramId"    />
            <result property="paraId"    column="paraId"    />
        </collection>
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.paramSelectItem.AreaElevatorModelParaVO" id="TpElevatorModelParamSelectResult">
        <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="inputType"    column="input_type"    />
        <result property="paramId"    column="paramId"    />
        <result property="paraId"    column="paraId"    />
        <result property="standardId"    column="standardId"    />
        <result property="paraDefaultValue"    column="para_default_value"    />
        <result property="paraDefaultId"    column="para_default_id"    />
        <result property="elevatorModelId"    column="elevator_model_id"    />
        <result property="sort"    column="sort_number"    />

        <collection property="selectItemList" ofType="com.ruoyi.project.business.domain.vo.paramSelectItem.AreaParaSelectItemVo">
            <result property="paraSelectItemId"    column="para_select_item_id"    />
            <result property="paraSelectItemValue"    column="para_select_item_value"    />
            <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"    />
        </collection>
    </resultMap>

    <sql id="selectTpElevatorModelVo">
        select id,
               area_id,
               elevator_type,
               elevator_model,
               status,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark,
               target_country_ids,
               target_user_ids
        from tp_elevator_model
    </sql>

    <select id="selectTpElevatorModelList" parameterType="com.ruoyi.project.business.domain.TpElevatorModel"
            resultMap="TpElevatorModelResult">
        <include refid="selectTpElevatorModelVo"/>
        <where>
            del_flag = '0'
            <if test="areaId != null ">and area_id = #{areaId}</if>
            <if test="elevatorType != null  and elevatorType != ''">and elevator_type = #{elevatorType}</if>
            <if test="elevatorModel != null  and elevatorModel != ''">and elevator_model like CONCAT('%', #{elevatorModel},'%')</if>
            <if test="elevatorModelSame != null  and elevatorModelSame != ''">and elevator_model = #{elevatorModelSame}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
    </select>

    <select id="selectTpElevatorModelById" parameterType="Long" resultMap="TpElevatorModelResult">
        <include refid="selectTpElevatorModelVo"/>
        where id = #{id}
    </select>

    <insert id="insertTpElevatorModel" parameterType="com.ruoyi.project.business.domain.TpElevatorModel"
            useGeneratedKeys="true" keyProperty="id">
        insert into tp_elevator_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="elevatorType != null">elevator_type,</if>
            <if test="elevatorModel != null">elevator_model,</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>
            <if test="targetCountryIds != null">target_country_ids,</if>
            <if test="targetUserIds != null">target_user_ids,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaId != null">#{areaId},</if>
            <if test="elevatorType != null">#{elevatorType},</if>
            <if test="elevatorModel != null">#{elevatorModel},</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>
            <if test="targetCountryIds != null">#{targetCountryIds},</if>
            <if test="targetUserIds != null">#{targetUserIds},</if>
        </trim>
    </insert>

    <update id="updateTpElevatorModel" parameterType="com.ruoyi.project.business.domain.TpElevatorModel">
        update tp_elevator_model
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="elevatorType != null">elevator_type = #{elevatorType},</if>
            <if test="elevatorModel != null">elevator_model = #{elevatorModel},</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>
            <if test="targetCountryIds != null">target_country_ids = #{targetCountryIds},</if>
            <if test="targetUserIds != null">target_user_ids = #{targetUserIds},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTpElevatorModelById" parameterType="Long">
        delete
        from tp_elevator_model
        where id = #{id}
    </delete>

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


    <select id="selectTpElevatorModelListNew"
            parameterType="com.ruoyi.project.business.domain.vo.paramSelectItem.TpElevatorModelVo"
            resultMap="TpElevatorModelResult">
        <include refid="selectTpElevatorModelVo"/>
        <where>
            <if test="areaId != null and areaId.size() > 0">
                and area_id in
                <foreach collection="areaId" item="aId" open="(" separator="," close=")">
                    #{aId}
                </foreach>
            </if>
            <if test="elevatorType != null  and elevatorType.size() > 0">
                and elevator_type in
                <foreach collection="elevatorType" item="etype" open="(" separator="," close=")">
                    #{etype}
                </foreach>
            </if>
            <if test="elevatorModel != null  and elevatorModel != ''">and elevator_model like CONCAT('%',
                #{elevatorModel},'%')
            </if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="delFlag != null  and delFlag != ''">and del_flag = #{delFlag}</if>
        </where>
    </select>

    <select id="selectTpElevatorModelListNewByCountryUser"
        parameterType="com.ruoyi.project.business.domain.vo.paramSelectItem.TpElevatorModelVo"
        resultMap="TpElevatorModelResult">
        <include refid="selectTpElevatorModelVo"/>
        <where>
            <if test="countryId != null and countryId != ''">
                and FIND_IN_SET(#{countryId},target_country_ids)
            </if>
            <if test="userId != null and userId != ''">
                and FIND_IN_SET(#{userId},target_user_ids)
            </if>
            <if test="elevatorModel != null  and elevatorModel != ''">and elevator_model like CONCAT('%',
                #{elevatorModel},'%')
            </if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="delFlag != null  and delFlag != ''">and del_flag = #{delFlag}</if>
        </where>
    </select>

    <sql id="selectTpElevatorModelParamVo">
        select elev.id,
               elev.area_id,
               elev.elevator_type,
        elev.elevator_model,
        elev.update_time,
        library.id as paramId,
        library.id as paraId,
        library.para_code,
        library.para_name,
        library.para_nameUS,
        library.para_nameRU,
        library.para_nameES,
        library.para_category,
        library.input_type
        from tp_elevator_model elev
        left join tp_area_elevator_model_para param on elev.id = param.elevator_model_id  and param.del_flag = 0
        left join tp_para_library library on param.para_code = library.para_code and library.del_flag = 0
    </sql>

    <select id="selectTpElevatorModelParamById" parameterType="Long" resultMap="TpElevatorModelParamResult">
        <include refid="selectTpElevatorModelParamVo"/>
        where elev.del_flag = 0

        and elev.id = #{id}
    </select>



    <sql id="selectTpElevatorModelParamSelectItemVo">
        select
        library.id as paramId,
        library.id as paraId,
        model.para_code,
		model.elevator_model_id,
        library.para_name,
        library.para_nameUS,
        library.para_nameRU,
        library.para_nameES,
        library.para_category,
        library.input_type,
        selectItem.para_select_item_id,
        selectItem.para_select_item_value,
        selectlibrary.value,
        selectlibrary.label,
        selectlibrary.labelUS,
        selectlibrary.labelRU,
        selectlibrary.labelES
        from tp_area_elevator_model_para model
        left join tp_para_library library on model.para_id = library.id
        left join  tp_area_para_select_item selectItem on selectItem.elevator_model_id = model.elevator_model_id and selectItem.para_id = model.para_id and selectItem.del_flag = 0
        left join tp_para_select_item selectlibrary on selectlibrary.id = selectItem.para_select_item_id and selectlibrary.del_flag = 0
        left join tp_area_para_standard stand on stand.para_id = library.id and stand.elevator_model_id =model.elevator_model_id and stand.del_flag = 0
    </sql>

    <select id="selectTpElevatorModelParamSelectItemById" parameterType="Long" resultMap="TpElevatorModelParamSelectResult">
        <include refid="selectTpElevatorModelParamSelectItemVo"/>
        where model.del_flag = 0
        and model.elevator_model_id = #{id}
        and (library.input_type = '02' or library.input_type = '04')
        order by library.sort_number asc
    </select>


    <sql id="selectTpElevatorModelParamStandVo">
      select
		library.id as paramId,
        library.id as paraId,
        param.para_code,
		param.elevator_model_id,
        library.para_name,
        library.para_nameUS,
        library.para_nameRU,
        library.para_nameES,
        library.para_category,
        library.input_type,
        library.sort_number,

        stand.para_default_value,
        stand.para_default_id,
        stand.id as standardId,

        selectItem.para_select_item_id,
        selectItem.para_select_item_value,
        selectlibrary.value,
        selectlibrary.label,
        selectlibrary.labelUS,
        selectlibrary.labelRU,
        selectlibrary.labelES
        from  tp_area_elevator_model_para param
        left join tp_para_library library on param.para_id = library.id
        left join tp_area_para_select_item selectItem on selectItem.elevator_model_id = param.elevator_model_id and  param.para_id = selectItem.para_id  and selectItem.del_flag = 0
        left join tp_para_select_item selectlibrary on selectlibrary.id = selectItem.para_select_item_id and selectlibrary.del_flag = 0
        left join tp_area_para_standard stand on stand.para_id = param.para_id  and stand.elevator_model_id = param.elevator_model_id and stand.del_flag = 0
    </sql>

    <select id="selectTpElevatorModelParamSelectItemAllById" parameterType="Long" resultMap="TpElevatorModelParamSelectResult">
        <include refid="selectTpElevatorModelParamStandVo"/>
        where param.del_flag = 0 and library.del_flag = 0
        and param.elevator_model_id = #{id}
        ORDER BY library.sort_number,selectlibrary.sort_number
    </select>

    <resultMap type="com.ruoyi.project.business.dto.TpElevatorModelDto" id="TpElevatorModelDtoResult"
               extends="TpElevatorModelResult">
        <result property="elevatorModelId" column="elevator_model_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"/>
    </resultMap>

    <select id="listForComponents" parameterType="com.ruoyi.project.business.dto.TpElevatorModelDto"
            resultMap="TpElevatorModelDtoResult">
        SELECT tem.id as elevator_model_id,
        tem.area_id,
        tem.elevator_type,
        tem.elevator_model,
        ta.area_name,
        ta.area_nameES,
        ta.area_nameRU,
        ta.area_nameUS
        FROM tp_elevator_model tem
        INNER JOIN tb_area ta on ta.area_id = tem.area_id
        WHERE tem.del_flag = '0'
        and tem.`status` = '0'
        <if test="areaName!=null and areaName !=''">
            and ta.area_name like concat('%',#{areaName},'%')
        </if>
        <if test="elevatorModel!=null and elevatorModel !=''">
            and  tem.elevator_model like concat('%',#{elevatorModel},'%')
        </if>
        <if test="areaNameUS!=null and areaNameUS !=''">
            and ta.area_nameUS like concat('%',#{areaNameUS},'%')
        </if>
        <if test="areaNameES!=null and areaNameES !=''">
            and ta.area_nameES like concat('%',#{areaNameES},'%')
        </if>
        <if test="areaNameRU!=null and areaNameRU !=''">
            and ta.area_nameRU like concat('%',#{areaNameRU},'%')
        </if>
        <if test="selectedModelIds!=null and selectedModelIds.length > 0">
            and  tem.id in
            <foreach item="id" collection="selectedModelIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>

    <resultMap type="com.ruoyi.project.business.domain.vo.paramSelectItem.ElevatorModeParamlVo" id="TpElevatorModelParamOptionResult">
        <result property="id"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="elevatorType"    column="elevator_type"    />
        <result property="elevatorModel"    column="elevator_model"    />
        <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"    />
        <collection property="paraSelect" ofType="com.ruoyi.project.business.domain.vo.paramSelectItem.AreaElevatorModelParaVO">
            <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="dictSort" column="dict_sort"/>
            <result property="paraCategoryLabel" column="dict_label"/>
            <result property="paraCategoryLabelUS" column="dict_labelUS"/>
            <result property="paraCategoryLabelES" column="dict_labelES"/>
            <result property="paraCategoryLabelRU" column="dict_labelRU"/>
            <result property="inputType" column="input_type"/>
            <result property="paramId" column="paramId"/>
            <result property="paraId" column="paraId"/>
            <result property="sort" column="sort"/>
            <result property="paraDefaultValue" column="para_default_value"/>
            <result property="paraDefaultId" column="para_default_id"/>
            <result property="isQuotation" column="is_quotation"/>
            <result property="isCalc" column="is_calc"/>
            <collection property="selectItemList"
                ofType="com.ruoyi.project.business.domain.vo.paramSelectItem.AreaParaSelectItemVo">
                <result property="paraSelectItemId" column="para_select_item_id"/>
                <result property="paraSelectItemValue" column="para_select_item_value"/>
                <result property="label" column="label"/>
                <result property="labelUS" column="labelUS"/>
                <result property="labelRU" column="labelRU"/>
                <result property="labelES" column="labelES"/>
                <result property="paraSource" column="prar_source"/>
            </collection>
        </collection>
    </resultMap>

    <select id="selectTpElevatorModelParamOptionById" parameterType="Long" resultMap="TpElevatorModelParamOptionResult">
        select
        elev.id,
        elev.area_id,
        elev.elevator_type,
        elev.elevator_model,
        elev.update_time,
        library.id as paramId,
        library.id as paraId,
        library.para_code,
        library.para_name,
        library.para_nameUS,
        library.para_nameRU,
        library.para_nameES,
        library.para_category,
        library.input_type,
        library.is_quotation,
        library.is_calc,
        stand.para_default_value,
        stand.para_default_id,
        item.para_select_item_id,
        item.para_select_item_value,
        iteml.label,
        iteml.labelUS,
        iteml.labelRU,
        iteml.labelES,
        iteml.prar_source,
        dict.dict_label,
        dict.dict_labelUS,
        dict.dict_labelRU,
        dict.dict_labelES,
        dict.dict_sort,
        library.sort_number as sort
        -- 梯形
        from tp_elevator_model elev
        -- 梯形参数
        left join tp_area_elevator_model_para param on elev.id = param.elevator_model_id
        -- 梯形参数库
        left join tp_para_library library on param.para_code = library.para_code
        -- 梯形参数默认值
        LEFT JOIN tp_area_para_standard stand
        on stand.para_id = param.para_id and elev.id = stand.elevator_model_id and
        stand.del_flag != 1
        -- 梯形参数选项
        LEFT JOIN tp_area_para_select_item item
        on item.para_id = param.para_id and elev.id = item.elevator_model_id
        -- 梯形参数选项库
        LEFT JOIN tp_para_select_item iteml on iteml.para_id = item.para_id
        and item.para_select_item_value = iteml.value
        -- 字典（参数类别）
        LEFT JOIN sys_dict_data dict
        ON dict.dict_type = 'para_category' and dict.dict_value = library.para_category
        where elev.del_flag != 1
        and param.del_flag != 1
        and library.del_flag != 1
        <if test="quotationFlag">
            -- 过滤报价参数
            and library.is_quotation = 0
        </if>
        <if test="!quotationFlag">
            -- 土建参数
            and library.is_civil = 0
        </if>
        and elev.id = #{id}
        order by library.sort_number asc,iteml.sort_number asc
    </select>



</mapper>