<?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.quotation.TqPriceDetailMapper">

    <resultMap type="TqPriceDetail" id="TqPriceDetailResult">
        <result property="id" column="id"/>
        <result property="specificationId" column="specification_id"/>
        <result property="markupKey" column="markup_key"/>
        <result property="markupName" column="markup_name"/>
        <result property="relatedParaInfo" column="related_para_info"/>
        <result property="quantity" column="quantity"/>
        <result property="unit" column="unit"/>
        <result property="buyerPrice" column="buyer_price"/>
        <result property="floatPercent" column="float_percent"/>
        <result property="treasurerPrice" column="treasurer_price"/>
        <result property="price" column="price"/>
        <result property="total" column="total"/>
        <result property="systemCheckResult" column="system_check_result"/>
        <result property="nonStandardReason" column="non_standard_reason"/>
        <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="solution" column="solution"/>

        <result property="buyerPriceMachine" column="buyer_price_machine"/>
        <result property="floatPercentMachine" column="float_percent_machine"/>
        <result property="treasurerPriceMachine" column="treasurer_price_machine"/>
        <result property="quotationFlagMachine" column="quotation_flag_machine"/>
        <result property="quotationFlagElectric" column="quotation_flag_electric"/>
        <result property="solutionMachine" column="solution_machine"/>
        <result property="exportParaCodes" column="export_para_codes"/>
        <result property="defaultRate" column="default_rate"/>
    </resultMap>

    <resultMap type="com.ruoyi.project.business.domain.vo.quotation.PriceDetailVO" id="TqPriceDetailVOResult">
        <result property="id" column="id"/>
        <result property="specificationId" column="specification_id"/>
        <result property="markupKey" column="markup_key"/>
        <result property="markupName" column="markup_name"/>
        <result property="relatedParaInfo" column="related_para_info"/>
        <result property="quantity" column="quantity"/>
        <result property="unit" column="unit"/>
        <result property="buyerPrice" column="buyer_price"/>
        <result property="floatPercent" column="float_percent"/>
        <result property="treasurerPrice" column="treasurer_price"/>
        <result property="price" column="price"/>
        <result property="total" column="total"/>
        <result property="systemCheckResult" column="system_check_result"/>
        <result property="nonStandardReason" column="non_standard_reason"/>
        <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="solution" column="solution"/>

        <result property="buyerPriceMachine" column="buyer_price_machine"/>
        <result property="floatPercentMachine" column="float_percent_machine"/>
        <result property="treasurerPriceMachine" column="treasurer_price_machine"/>
        <result property="quotationFlagMachine" column="quotation_flag_machine"/>
        <result property="quotationFlagElectric" column="quotation_flag_electric"/>
        <result property="solutionMachine" column="solution_machine"/>
    </resultMap>

    <sql id="selectTqPriceDetailVo">
        select tpd.id,
               tpd.specification_id,
               tpd.markup_key,
               tpd.markup_name,
               tpd.related_para_info,
               tpd.quantity,
               tpd.unit,
               tpd.buyer_price,
               tpd.buyer_price_machine,
               tpd.float_percent,
               tpd.float_percent_machine,
               tpd.treasurer_price,
               tpd.treasurer_price_machine,
               tpd.price,
               tpd.total,
               tpd.quotation_flag_machine,
               tpd.quotation_flag_electric,
               tpd.system_check_result,
               tpd.non_standard_reason,
               tpd.del_flag,
               tpd.create_time,
               tpd.create_by,
               tpd.update_time,
               tpd.update_by,
               tpd.remark,
               tpd.solution,
               tpd.solution_machine,
               taop.export_para_codes,
               ts.default_rate
        from tq_price_detail tpd
        left join tq_specification ts on tpd.specification_id = ts.id
        left join tp_area_optional_para taop on ts.elevator_model_id = taop.elevator_model_id and tpd.markup_name = taop.markup_name and taop.del_flag = 0

    </sql>

    <select id="selectTqPriceDetailList" parameterType="TqPriceDetail" resultMap="TqPriceDetailResult">
        <include refid="selectTqPriceDetailVo"/>
        <where>
            tpd.del_flag = 0
            <if test="specificationId != null ">and tpd.specification_id = #{specificationId}</if>
            <if test="specificationIds != null and specificationIds.size() > 0">
                and tpd.specification_id in
                <foreach collection="specificationIds" item="specificationId" open="(" separator="," close=")">
                    #{specificationId}
                </foreach>
            </if>
            <if test="markupKey != null  and markupKey != ''">and tpd.markup_key = #{markupKey}</if>
            <if test="markupName != null  and markupName != ''">and tpd.markup_name like concat('%', #{markupName}, '%')
            </if>
            <if test="relatedParaInfo != null  and relatedParaInfo != ''">and tpd.related_para_info = #{relatedParaInfo}
            </if>
            <if test="quantity != null ">and tpd.quantity = #{quantity}</if>
            <if test="unit != null  and unit != ''">and tpd.unit = #{unit}</if>
            <if test="buyerPrice != null ">and tpd.buyer_price = #{buyerPrice}</if>
            <if test="floatPercent != null ">and tpd.float_percent = #{floatPercent}</if>
            <if test="treasurerPrice != null ">and tpd.treasurer_price = #{treasurerPrice}</if>
            <if test="price != null ">and tpd.price = #{price}</if>
            <if test="total != null ">and tpd.total = #{total}</if>
            <if test="systemCheckResult != null  and systemCheckResult != ''">and tpd.system_check_result =
                #{systemCheckResult}
            </if>
            <if test="nonStandardReason != null  and nonStandardReason != ''">and tpd.non_standard_reason =
                #{nonStandardReason}
            </if>
        </where>
    </select>

    <select id="listAll" parameterType="TqPriceDetail" resultMap="TqPriceDetailVOResult">
        <include refid="selectTqPriceDetailVo"/>
        <where>
            tpd.del_flag = 0
            <if test="specificationId != null ">and tpd.specification_id = #{specificationId}</if>
            <if test="systemCheckResult != null ">and tpd.system_check_result = #{systemCheckResult}</if>
        </where>
    </select>

    <select id="selectTqPriceDetailById" parameterType="Long" resultMap="TqPriceDetailResult">
        <include refid="selectTqPriceDetailVo"/>
        where tpd.id = #{id}
    </select>

    <insert id="insertTqPriceDetail" parameterType="TqPriceDetail" useGeneratedKeys="true" keyProperty="id">
        insert into tq_price_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="specificationId != null">specification_id,</if>
            <if test="markupKey != null">markup_key,</if>
            <if test="markupName != null">markup_name,</if>
            <if test="relatedParaInfo != null">related_para_info,</if>
            <if test="quantity != null">quantity,</if>
            <if test="unit != null">unit,</if>
            <if test="buyerPrice != null">buyer_price,</if>
            <if test="floatPercent != null">float_percent,</if>
            <if test="treasurerPrice != null">treasurer_price,</if>
            <if test="price != null">price,</if>
            <if test="total != null">total,</if>
            <if test="systemCheckResult != null">system_check_result,</if>
            <if test="nonStandardReason != null">non_standard_reason,</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="specificationId != null">#{specificationId},</if>
            <if test="markupKey != null">#{markupKey},</if>
            <if test="markupName != null">#{markupName},</if>
            <if test="relatedParaInfo != null">#{relatedParaInfo},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="unit != null">#{unit},</if>
            <if test="buyerPrice != null">#{buyerPrice},</if>
            <if test="floatPercent != null">#{floatPercent},</if>
            <if test="treasurerPrice != null">#{treasurerPrice},</if>
            <if test="price != null">#{price},</if>
            <if test="total != null">#{total},</if>
            <if test="systemCheckResult != null">#{systemCheckResult},</if>
            <if test="nonStandardReason != null">#{nonStandardReason},</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="updateTqPriceDetail" parameterType="TqPriceDetail">
        update tq_price_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="specificationId != null">specification_id = #{specificationId},</if>
            <if test="markupKey != null">markup_key = #{markupKey},</if>
            <if test="markupName != null">markup_name = #{markupName},</if>
            <if test="relatedParaInfo != null">related_para_info = #{relatedParaInfo},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="buyerPrice != null">buyer_price = #{buyerPrice},</if>
            <if test="floatPercent != null">float_percent = #{floatPercent},</if>
            <if test="treasurerPrice != null">treasurer_price = #{treasurerPrice},</if>
            <if test="price != null">price = #{price},</if>
            <if test="total != null">total = #{total},</if>
            <if test="systemCheckResult != null">system_check_result = #{systemCheckResult},</if>
            <if test="nonStandardReason != null">non_standard_reason = #{nonStandardReason},</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="deleteTqPriceDetailById" parameterType="Long">
        delete
        from tq_price_detail
        where id = #{id}
    </delete>

    <delete id="deleteTqPriceDetailByIds" parameterType="String">
        delete from tq_price_detail where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="listNonstandardStatistics"
        resultType="com.ruoyi.project.business.domain.vo.quotation.NonstandardStatisticsVO">
        select * from (
        select CONCAT('tns', tns.id) as id,
        tns.non_standard_desc as markupName,
        '' as relatedParaInfo,
        '' as nonStandardReason,
        tns.solution as solution,
        tns.solution_machine as solutionMachine,
        ts.create_time as date,
        tem.elevator_type as elevatorTypeId,
        tq.quotation_no as quotationNo,
        tp.project_name as projectName,
        tp.area_id as areaId,
        tp.project_no as projectNo,
        CASE #{language}
        WHEN 'chinese' THEN ta.area_name
        WHEN 'english' THEN ta.area_nameUS
        WHEN 'russia' THEN ta.area_nameRU
        WHEN 'spanish' THEN ta.area_nameES
        END AS areaName,
        CASE #{language}
        WHEN 'chinese' THEN sdd.dict_label
        WHEN 'english' THEN sdd.dict_labelUS
        WHEN 'russia' THEN sdd.dict_labelRU
        WHEN 'spanish' THEN sdd.dict_labelES
        END AS elevatorTypeName
        from tq_specification_non_standard tns
        LEFT JOIN tq_specification ts on ts.id = tns.specification_id and ts.del_flag = '0'
        LEFT JOIN tp_elevator_model tem on tem.id = ts.elevator_model_id and tem.del_flag = '0'
        LEFT JOIN tq_quotation tq on tq.id = ts.quotation_id and tq.del_flag = '0'
        LEFT JOIN tc_project tp on tp.project_id = tq.project_id and tp.del_flag = '0'
        LEFT JOIN sys_dict_data sdd on tem.elevator_type = sdd.dict_value and sdd.dict_type = 'elevator_type'
        LEFT JOIN tb_area ta
        on ta.area_id = tp.area_id and sdd.dict_type = 'elevator_type' and ta.del_flag = '0'
        WHERE tns.del_flag = '0'
        <if test="projectNo != null and projectNo != ''">
            and tp.project_no like concat('%',#{projectNo},'%') escape '/'
        </if>
        <if test="projectName != null and projectName != ''">
            and tp.project_name like concat('%',#{projectName},'%') escape '/'
        </if>
        <if test="quotationNo != null and quotationNo != ''">
            and tq.quotation_no like concat('%',#{quotationNo},'%') escape '/'
        </if>
        <if test="elevatorTypeId != null and elevatorTypeId != ''">
            and tem.elevator_type = #{elevatorTypeId}
        </if>
        <if test="beginTime != null and endTime != null">and ts.create_time between #{beginTime} and #{endTime}</if>
        <if test="markupName != null  and markupName != ''">
            and tns.non_standard_desc like concat('%',#{markupName},'%') escape '/'
        </if>
        <if test="relatedParaInfo != null and relatedParaInfo != ''">
            and 1 != 1
        </if>
        <if test="nonStandardReason != null and nonStandardReason != ''">
            and 1 != 1
        </if>
        <if test="solution != null and solution != ''">
            and tns.solution like concat('%',#{solution},'%') escape '/'
        </if>
        <if test="solutionMachine != null and solutionMachine != ''">
            and tns.solution_machine like concat('%',#{solutionMachine},'%') escape '/'
        </if>
        <if test="areaId != null and areaId.size() > 0">
            and tp.area_id in
            <foreach item="item" collection="areaId" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ids != null and ids.size() > 0">
            and CONCAT('tns',tns.id) in
            <foreach item="item" collection="ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        union all
        SELECT tpd.id,
        tpd.markup_name as markupName,
        tpd.related_para_info as relatedParaInfo,
        tpd.non_standard_reason as nonStandardReason,
        tpd.solution as solution,
        tpd.solution_machine as solutionMachine,
        ts.create_time as date,
        tem.elevator_type as elevatorTypeId,
        tq.quotation_no as quotationNo,
        tp.project_name as projectName,
        tp.area_id as areaId,
        tp.project_no as projectNo,
        CASE #{language}
        WHEN 'chinese' THEN ta.area_name
        WHEN 'english' THEN ta.area_nameUS
        WHEN 'russia' THEN ta.area_nameRU
        WHEN 'spanish' THEN ta.area_nameES
        END AS areaName,
        CASE #{language}
        WHEN 'chinese' THEN sdd.dict_label
        WHEN 'english' THEN sdd.dict_labelUS
        WHEN 'russia' THEN sdd.dict_labelRU
        WHEN 'spanish' THEN sdd.dict_labelES
        END AS elevatorTypeName
        FROM tq_price_detail tpd
        LEFT JOIN tq_specification ts on ts.id = tpd.specification_id and ts.del_flag = '0'
        LEFT JOIN tp_elevator_model tem on tem.id = ts.elevator_model_id and tem.del_flag = '0'
        LEFT JOIN tq_quotation tq on tq.id = ts.quotation_id and tq.del_flag = '0'
        LEFT JOIN tc_project tp on tp.project_id = tq.project_id and tp.del_flag = '0'
        LEFT JOIN sys_dict_data sdd on tem.elevator_type = sdd.dict_value and sdd.dict_type = 'elevator_type'
        LEFT JOIN tb_area ta
        on ta.area_id = tp.area_id and sdd.dict_type = 'elevator_type' and ta.del_flag = '0'
        WHERE tpd.del_flag = '0' and tpd.system_check_result = '01'
        <if test="projectNo != null and projectNo != ''">
            and tp.project_no like concat('%',#{projectNo},'%') escape '/'
        </if>
        <if test="projectName != null and projectName != ''">
            and tp.project_name like concat('%',#{projectName},'%') escape '/'
        </if>
        <if test="quotationNo != null and quotationNo != ''">
            and tq.quotation_no like concat('%',#{quotationNo},'%') escape '/'
        </if>
        <if test="elevatorTypeId != null and elevatorTypeId != ''">
            and tem.elevator_type = #{elevatorTypeId}
        </if>
        <if test="beginTime != null and endTime != null">and ts.create_time between #{beginTime} and #{endTime}</if>
        <if test="markupName != null  and markupName != ''">
            and tpd.markup_name like concat('%',#{markupName},'%') escape '/'
        </if>
        <if test="relatedParaInfo != null and relatedParaInfo != ''">
            and tpd.related_para_info like concat('%',#{relatedParaInfo},'%') escape '/'
        </if>
        <if test="nonStandardReason != null and nonStandardReason != ''">
            and tpd.non_standard_reason like concat('%',#{nonStandardReason},'%') escape '/'
        </if>
        <if test="solution != null and solution != ''">
            and tpd.solution like concat('%',#{solution},'%') escape '/'
        </if>
        <if test="solutionMachine != null and solutionMachine != ''">
            and tpd.solution_machine like concat('%',#{solutionMachine},'%') escape '/'
        </if>
        <if test="areaId != null and areaId.size() > 0">
            and tp.area_id in
            <foreach item="item" collection="areaId" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ids != null and ids.size() > 0">
            and tpd.id in
            <foreach item="item" collection="ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ) merge order by merge.date desc
    </select>
</mapper>