<?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="originalBasicPrice" column="original_basic_price"/>
        <result property="originalOptionalPrice" column="original_optional_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="defaultRate" column="default_rate"/>
        <result property="discountRate" column="discount_rate"/>
        <result property="discountPrice" column="discount_price"/>
        <result property="areaId" column="area_id"/>
        <result property="landShipFees" column="land_ship_fees"/>
        <result property="seaShipFees" column="sea_ship_fees"/>
        <result property="freightRemark" column="freight_remark"/>
        <result property="floorDiffPrice" column="floor_diff_price"/>
    </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.original_basic_price,
        sp.original_optional_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,
        sp.default_rate,
        tem.elevator_model,
        tem.elevator_type,
        tem.area_id,
        ifnull(tssp.discount_rate, sp.discount_rate) as discount_rate,
        tssp.discount_price,
        ifnull(tss.land_ship_fees, 0) as land_ship_fees,
        ifnull(tss.sea_ship_fees, 0) as sea_ship_fees,
        tss.remark as freight_remark,
        ifnull(tpd.total, 0) as floor_diff_price
        from tq_specification sp
        left join tq_specification_surcharge tss on tss.specification_id = sp.id and tss.del_flag = 0
        left join tp_elevator_model tem on sp.elevator_model_id = tem.id
        left join tq_salesperson_set_price tssp on tssp.specification_id = sp.id and tssp.bargain_id = #{bargainId}
        and tssp.id = (select max(id) from tq_salesperson_set_price where specification_id = sp.id and bargain_id = #{bargainId} and del_flag = 0)
        left join tq_price_detail tpd on tpd.specification_id = sp.id and tpd.del_flag = 0 and tpd.markup_name = '层数差价'
        <where>
            sp.del_flag = 0
            <if test="quotationId != null ">and sp.quotation_id = #{quotationId}</if>
            <if test="id != null ">and sp.id = #{id}</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>
            <if test="originalBasicPrice != null">original_basic_price,</if>
            <if test="originalOptionalPrice != null">original_optional_price,</if>
            <if test="defaultRate != null">default_rate,</if>
            <if test="discountRate != null">discount_rate,</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>
            <if test="originalBasicPrice != null">#{originalBasicPrice},</if>
            <if test="originalOptionalPrice != null">#{originalOptionalPrice},</if>
            <if test="defaultRate != null">#{defaultRate},</if>
            <if test="discountRate != null">#{discountRate},</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>
            <if test="defaultRate != null">default_rate = #{defaultRate},</if>
            <if test="discountRate != null">discount_rate = #{discountRate},</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>

    <select id="getCount" parameterType="long" resultType="int">
        SELECT count(*)
        FROM tq_specification
        WHERE elevator_model_id = #{id}
          and del_flag = '0'
    </select>
    <select id="listSpecification" resultType="com.ruoyi.project.business.domain.vo.quotation.H5ElevatorVO">
        SELECT ts.id,
               ts.basic_price     as basicPrice,
               ts.optional_price  as optionalPrice,
               tem.elevator_model as elevatorModel,
               ts.elevator_code   as elevatorCode,
               ts.specification,
               ts.elevator_number as elevatorNumber,
               ts.original_basic_price as originalBasicPrice,
               ts.original_optional_price as originalOptionalPrice,
               ts.price,
               (select discount_rate
                from tq_salesperson_set_price tssp
                where tssp.del_flag = '0'
                  and tssp.specification_id = ts.id
                  and tssp.bargain_id = #{bargainId}
                order by id desc
                limit 1)          as discountRate
        FROM tq_specification ts
                 LEFT JOIN tp_elevator_model tem ON tem.id = ts.elevator_model_id
        WHERE ts.quotation_id = #{id}
          and ts.del_flag = 0
    </select>
    <select id="listByQuotationNoAndVersion" 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,
        tem.area_id
        from tq_specification sp
        left join tp_elevator_model tem on sp.elevator_model_id = tem.id
        left join tq_quotation tq on tq.id = sp.quotation_id
        <where>
            sp.del_flag = 0
            and tq.quotation_no = #{quotationNo}
            and tq.version = #{version}
        </where>
    </select>

    <resultMap type="com.ruoyi.project.business.domain.vo.quotation.ElevatorExportVO" id="listElevatorExportResult">
        <result property="quotationId" column="quotation_id"/>
        <result property="elevatorType" column="elevator_type"/>
        <result property="elevatorTypeName" column="elevator_type_name"/>
        <result property="elevatorCode" column="elevator_code"/>
        <result property="specification" column="specification"/>
        <result property="elevatorNumber" column="elevator_number"/>
        <result property="price" column="price"/>
        <collection property="paramList" ofType="com.ruoyi.project.business.domain.vo.quotation.ElevatorParamExportVO">
            <result property="paraName" column="para_name"/>
            <result property="paraCode" column="para_code"/>
            <result property="paraValue" column="para_value"/>
            <result property="itemCode" column="item_code"/>
            <result property="paraSource" column="prar_source"/>
            <result property="label" column="label"/>
        </collection>
    </resultMap>

    <select id="listElevatorExport" resultMap="listElevatorExportResult">
        SELECT
        spe.quotation_id,
        tem.elevator_type,
        spe.elevator_code,
        dict.dict_label as elevator_type_name,
        spe.specification,
        spe.elevator_number,
        spe.price,
        lib.para_name,
        tsd.para_code,
        tsd.para_value,
        item.para_code as item_code,
        item.prar_source,
        item.label
        FROM
        tq_specification spe
        LEFT JOIN tq_specification_detail tsd ON tsd.specification_id = spe.id
        LEFT JOIN tp_elevator_model tem ON spe.elevator_model_id = tem.id
        LEFT JOIN tp_para_library lib ON tsd.para_code = lib.para_code
        LEFT JOIN tp_para_select_item item ON item.`value` = tsd.para_value and item.para_id = lib.id
        LEFT JOIN sys_dict_data dict ON dict.dict_value = tem.elevator_type and dict.dict_type = 'elevator_type'
        WHERE
        spe.quotation_id in
        <foreach item="id" collection="quotationIdList" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND spe.del_flag = 0
    </select>

    <select id="getQuotation4Contract" resultType="com.ruoyi.project.business.domain.vo.quotation.QuotationContractVo">
        SELECT
            tq.id,
            SUBSTRING_INDEX(SUBSTRING_INDEX(ts.elevator_code, ',', numbers.n), ',', -1) AS elevatorCode,
            te.elevator_type AS elevatorType,
            te.elevator_model AS elevatorModel,
            ts.specification AS specification,
            CONCAT(tq.quotation_no, '-', tq.version) AS quotationNoVersion,
            tp.project_name AS projectName,
            tq.create_by AS quoter,
            COALESCE(ts.final_price, ts.price) AS unitPrice,
            ts.discount_rate AS quotationDiscount,
            1 AS count,
            '0' AS origin,
            '0' as  productCategory,
            tq.quotation_status as quotationStatus,
            tq.currency_id as currencyId,
            COALESCE(tss.land_ship_fees, 0.00) as planInlandFreightCost,
            COALESCE(tss.sea_ship_fees, 0.00) as planOceanFreight ,
            COALESCE(tss.commission_fees, 0.00) as  reviewCommission,
            COALESCE(tss.other_fees, 0.00) as  otherFees,
            COALESCE((tss.land_ship_fees +tss.sea_ship_fees+tss.commission_fees+tss.other_fees), 0.00) as additionalCost,
            (COALESCE (
            tq.final_price,
            ROUND( tq.price * tq.exchange_rate, 2 ))+COALESCE (( tss.land_ship_fees + tss.sea_ship_fees + tss.commission_fees + tss.other_fees ), 0.00 )) as liftAmount
        FROM
            tq_quotation AS tq
        LEFT JOIN sys_user AS su ON su.user_id = tq.create_by AND su.del_flag = '0'
        LEFT JOIN tq_specification AS ts ON ts.quotation_id = tq.id AND ts.del_flag = '0'
        LEFT JOIN tp_elevator_model AS te ON ts.elevator_model_id = te.id  AND te.del_flag = '0'
        LEFT JOIN tq_specification_surcharge tss ON ts.id = tss.specification_id
        LEFT JOIN tc_project AS tp ON tp.project_id = tq.project_id
        JOIN (
            SELECT 1 n UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
            SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL
            SELECT 9 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
            ) AS numbers ON CHAR_LENGTH(ts.elevator_code) - CHAR_LENGTH(REPLACE(ts.elevator_code, ',', '')) >= numbers.n - 1
        WHERE   tq.del_flag = '0' and ts.del_flag = '0' and te.del_flag = '0' and tp.del_flag = '0' and tq.agent_id = #{agentId}  AND (tss.del_flag = '0' or tss.del_flag is null)

        <choose>
            <when test="quotationStatus != null">
                    and tq.quotation_status in
                    <foreach collection="quotationStatus" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
            </when>
            <otherwise>
                and  tq.quotation_status in ('6','7','8','9')
            </otherwise>
        </choose>
        <if test="elevatorCode != null">and SUBSTRING_INDEX(SUBSTRING_INDEX(ts.elevator_code, ',', numbers.n), ',', -1) like concat('%',#{elevatorCode},'%')</if>
        <if test="elevatorType != null">and te.elevator_type =  #{elevatorType}</if>
        <if test="elevatorModel != null">and te.elevator_model like concat('%',#{elevatorModel},'%') </if>
        <if test="specification != null">and ts.specification like concat('%',#{specification},'%') </if>
        <if test="quotationNoVersion != null">and CONCAT(tq.quotation_no, '-', tq.version) like concat('%',#{quotationNoVersion},'%') </if>
        <if test="projectName != null">and tp.project_name like concat('%',#{projectName},'%') </if>

        <if test="quoter != null">and su.nick_name like concat('%',#{quoter},'%') </if>
        <if test="quotationDiscount != null">and ts.discount_rate like concat('%',#{quotationDiscount},'%') </if>
        order by tq.id desc
    </select>
</mapper>