<?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.TqSpecificationMapper">
    
    <resultMap type="TqSpecification" id="TqSpecificationResult">
        <result property="id"    column="id"    />
        <result property="quotationId"    column="quotation_id"    />
        <result property="elevatorModelId"    column="elevator_model_id"    />
        <result property="elevatorNumber"    column="elevator_number"    />
        <result property="elevatorGroupCode"    column="elevator_group_code"    />
        <result property="elevatorCode"    column="elevator_code"    />
        <result property="specification"    column="specification"    />
        <result property="price"    column="price"    />
        <result property="basicPrice"    column="basic_price"    />
        <result property="optionalPrice"    column="optional_price"    />
        <result property="systemCheckResult"    column="system_check_result"    />
        <result property="finalPrice"    column="final_price"    />
        <result property="quotationStatus"    column="quotation_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.quotation.SpecificationVO" id="TqSpecificationVOResult">
        <result property="id"    column="id"    />
        <result property="quotationId"    column="quotation_id"    />
        <result property="elevatorModelId"    column="elevator_model_id"    />
        <result property="elevatorModel"    column="elevator_model"    />
        <result property="elevatorType"    column="elevator_type"    />
        <result property="elevatorNumber"    column="elevator_number"    />
        <result property="elevatorGroupCode"    column="elevator_group_code"    />
        <result property="elevatorCode"    column="elevator_code"    />
        <result property="specification"    column="specification"    />
        <result property="price"    column="price"    />
        <result property="basicPrice"    column="basic_price"    />
        <result property="optionalPrice"    column="optional_price"    />
        <result property="systemCheckResult"    column="system_check_result"    />
        <result property="finalPrice"    column="final_price"    />
        <result property="quotationStatus"    column="quotation_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="discountRate" column="discount_rate"/>
    </resultMap>

    <sql id="selectTqSpecificationVo">
        select id, quotation_id, elevator_model_id, elevator_number, elevator_group_code, elevator_code, specification, price, basic_price, optional_price, system_check_result, final_price, quotation_status, del_flag, create_time, create_by, update_time, update_by, remark from tq_specification
    </sql>

    <select id="selectTqSpecificationList" parameterType="TqSpecification" resultMap="TqSpecificationResult">
        <include refid="selectTqSpecificationVo"/>
        <where>  
            <if test="quotationId != null "> and quotation_id = #{quotationId}</if>
            <if test="elevatorModelId != null "> and elevator_model_id = #{elevatorModelId}</if>
            <if test="elevatorNumber != null "> and elevator_number = #{elevatorNumber}</if>
            <if test="elevatorGroupCode != null  and elevatorGroupCode != ''"> and elevator_group_code = #{elevatorGroupCode}</if>
            <if test="elevatorCode != null  and elevatorCode != ''"> and elevator_code = #{elevatorCode}</if>
            <if test="specification != null  and specification != ''"> and specification = #{specification}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="basicPrice != null "> and basic_price = #{basicPrice}</if>
            <if test="optionalPrice != null "> and optional_price = #{optionalPrice}</if>
            <if test="systemCheckResult != null  and systemCheckResult != ''"> and system_check_result = #{systemCheckResult}</if>
            <if test="finalPrice != null "> and final_price = #{finalPrice}</if>
            <if test="quotationStatus != null  and quotationStatus != ''"> and quotation_status = #{quotationStatus}</if>
        </where>
    </select>

    <select id="selectSpecificationVOList" resultMap="TqSpecificationVOResult">
        select sp.id,
        sp.quotation_id,
        sp.elevator_model_id,
        sp.elevator_number,
        sp.elevator_group_code,
        sp.elevator_code,
        sp.specification,
        sp.price,
        sp.basic_price,
        sp.optional_price,
        sp.system_check_result,
        sp.final_price,
        sp.quotation_status,
        sp.del_flag,
        sp.create_time,
        sp.create_by,
        sp.update_time,
        sp.update_by,
        sp.remark,
        tem.elevator_model,
        tem.elevator_type,
        (select discount_rate from tq_salesperson_set_price tssp where tssp.del_flag = '0'
        and tssp.specification_id = sp.id and tssp.bargain_id = #{bargainId} order by id desc limit 1
        ) as discount_rate
        from tq_specification sp
        left join tp_elevator_model tem on sp.elevator_model_id = tem.id
        <where>
            sp.del_flag = 0
            <if test="quotationId != null ">and sp.quotation_id = #{quotationId}</if>
            <if test="elevatorModelId != null ">and sp.elevator_model_id = #{elevatorModelId}</if>
            <if test="elevatorNumber != null ">and sp.elevator_number = #{elevatorNumber}</if>
            <if test="elevatorGroupCode != null  and elevatorGroupCode != ''">and sp.elevator_group_code =
                #{elevatorGroupCode}
            </if>
            <if test="elevatorCode != null  and elevatorCode != ''">and sp.elevator_code = #{elevatorCode}</if>
            <if test="specification != null  and specification != ''">and sp.specification = #{specification}</if>
            <if test="price != null ">and sp.price = #{price}</if>
            <if test="basicPrice != null ">and sp.basic_price = #{basicPrice}</if>
            <if test="optionalPrice != null ">and sp.optional_price = #{optionalPrice}</if>
            <if test="systemCheckResult != null  and systemCheckResult != ''">and sp.system_check_result =
                #{systemCheckResult}
            </if>
            <if test="finalPrice != null ">and sp.final_price = #{finalPrice}</if>
            <if test="quotationStatus != null  and quotationStatus != ''">and sp.quotation_status = #{quotationStatus}</if>
        </where>
    </select>
    
    <select id="selectTqSpecificationById" parameterType="Long" resultMap="TqSpecificationResult">
        <include refid="selectTqSpecificationVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTqSpecification" parameterType="TqSpecification" useGeneratedKeys="true" keyProperty="id">
        insert into tq_specification
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="quotationId != null">quotation_id,</if>
            <if test="elevatorModelId != null">elevator_model_id,</if>
            <if test="elevatorNumber != null">elevator_number,</if>
            <if test="elevatorGroupCode != null">elevator_group_code,</if>
            <if test="elevatorCode != null">elevator_code,</if>
            <if test="specification != null">specification,</if>
            <if test="price != null">price,</if>
            <if test="basicPrice != null">basic_price,</if>
            <if test="optionalPrice != null">optional_price,</if>
            <if test="systemCheckResult != null">system_check_result,</if>
            <if test="finalPrice != null">final_price,</if>
            <if test="quotationStatus != null">quotation_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="quotationId != null">#{quotationId},</if>
            <if test="elevatorModelId != null">#{elevatorModelId},</if>
            <if test="elevatorNumber != null">#{elevatorNumber},</if>
            <if test="elevatorGroupCode != null">#{elevatorGroupCode},</if>
            <if test="elevatorCode != null">#{elevatorCode},</if>
            <if test="specification != null">#{specification},</if>
            <if test="price != null">#{price},</if>
            <if test="basicPrice != null">#{basicPrice},</if>
            <if test="optionalPrice != null">#{optionalPrice},</if>
            <if test="systemCheckResult != null">#{systemCheckResult},</if>
            <if test="finalPrice != null">#{finalPrice},</if>
            <if test="quotationStatus != null">#{quotationStatus},</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="updateTqSpecification" parameterType="TqSpecification">
        update tq_specification
        <trim prefix="SET" suffixOverrides=",">
            <if test="quotationId != null">quotation_id = #{quotationId},</if>
            <if test="elevatorModelId != null">elevator_model_id = #{elevatorModelId},</if>
            <if test="elevatorNumber != null">elevator_number = #{elevatorNumber},</if>
            <if test="elevatorGroupCode != null">elevator_group_code = #{elevatorGroupCode},</if>
            <if test="elevatorCode != null">elevator_code = #{elevatorCode},</if>
            <if test="specification != null">specification = #{specification},</if>
            <if test="price != null">price = #{price},</if>
            <if test="basicPrice != null">basic_price = #{basicPrice},</if>
            <if test="optionalPrice != null">optional_price = #{optionalPrice},</if>
            <if test="systemCheckResult != null">system_check_result = #{systemCheckResult},</if>
            <if test="finalPrice != null">final_price = #{finalPrice},</if>
            <if test="quotationStatus != null">quotation_status = #{quotationStatus},</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>
    <update id="updateAuditOptionalPrice">
        update tq_specification
        set optional_price   = optional_price + #{price},
            price            = price + #{price},
            quotation_status = 5
        where id = #{id}
    </update>

    <delete id="deleteTqSpecificationById" parameterType="Long">
        delete
        from tq_specification
        where id = #{id}
    </delete>

    <delete id="deleteTqSpecificationByIds" parameterType="String">
        delete from tq_specification where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteByQuotationId">
        update tq_specification set del_flag = 1 where quotation_id = #{quotationId}
    </delete>
</mapper>