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

    <resultMap type="TqQuotation" id="TqQuotationResult">
        <result property="id" column="id"/>
        <result property="quotationNo" column="quotation_no"/>
        <result property="version" column="version"/>
        <result property="agentId" column="agent_id"/>
        <result property="projectId" column="project_id"/>
        <result property="currencyId" column="currency_id"/>
        <result property="exchangeRate" column="exchange_rate"/>
        <result property="price" column="price"/>
        <result property="finalPrice" column="final_price"/>
        <result property="freightPrice" column="freight_price"/>
        <result property="basicPrice" column="basic_price"/>
        <result property="optionalPrice" column="optional_price"/>
        <result property="quotationStatus" column="quotation_status"/>
        <result property="submitTime" column="submit_time"/>
        <result property="orderTime" column="order_time"/>
        <result property="systemCheckResult" column="system_check_result"/>
        <result property="permissionChar" column="permission_char"/>
        <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.QuotationVO" id="TqQuotationVOResult">
        <result property="id" column="id"/>
        <result property="quotationNo" column="quotation_no"/>
        <result property="version" column="version"/>
        <result property="currencyId" column="currency_id"/>
        <result property="finalPrice" column="final_price"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="submitTime" column="submit_time"/>
        <result property="quotationStatus" column="quotation_status"/>
        <result property="projectNo" column="project_no"/>
        <result property="projectName" column="project_name"/>
        <result property="type" column="type"/>
        <result property="segmentedMarket" column="segmented_market"/>
        <result property="countryId" column="country_id"/>
        <result property="city" column="city"/>
        <result property="nickName" column="nick_name"/>
        <result property="additionalFees" column="additional_fees"/>
        <result property="elevatorModel" column="elevator_model"/>
    </resultMap>

    <sql id="selectTqQuotationVo">
        select id,
               quotation_no,
               version,
               agent_id,
               project_id,
               currency_id,
               exchange_rate,
               price,
               final_price,
               freight_price,
               basic_price,
               optional_price,
               quotation_status,
               submit_time,
               order_time,
               system_check_result,
               permission_char,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from tq_quotation
    </sql>

    <select id="selectTqQuotationList" resultMap="TqQuotationVOResult">
        SELECT
        quo.id,
        quo.quotation_no,
        quo.version,
        quo.currency_id,
        quo.create_by,
        quo.freight_price as "additional_cost",
        quo.final_price,
        quo.submit_time,
        quo.quotation_status,
        project.project_no,
        project.project_name,
        project.type,
        project.segmented_market,
        project.country_id,
        project.city,
        u.nick_name,
        (SELECT sum(additional_fees) FROM tq_additional_fees taf WHERE taf.quotation_id = quo.id and taf.del_flag = '0')
        as additional_fees,
        (SELECT GROUP_CONCAT(DISTINCT tem.elevator_model) FROM tq_specification ts
        LEFT JOIN tp_elevator_model tem on tem.id = ts.elevator_model_id
        WHERE ts.quotation_id = quo.id and ts.del_flag = '0') as elevator_model
        FROM tq_quotation quo
        LEFT JOIN tc_project project on quo.project_id = project.project_id
        LEFT JOIN sys_user u on quo.create_by = u.user_id
        <where>
            <if test="quotationNo != null  and quotationNo != ''">
                and quo.quotation_no like concat('%',#{quotationNo},'%')
            </if>
            <if test="excludeQuotationIdList != null and excludeQuotationIdList.size() > 0">
                and quo.id not in
                <foreach item="item" collection="excludeQuotationIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="quotationNoList != null and quotationNoList.size() > 0">
                and quo.quotation_no in
                <foreach item="item" collection="quotationNoList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="areaIdList != null and areaIdList.size() > 0">
                and quo.area_id in
                <foreach item="item" collection="areaIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="projectName != null  and projectName != ''">and project.project_name like
                concat('%',#{projectName},'%')
            </if>
            <if test="type != null and type.size() > 0">
                and project.type in
                <foreach item="item" collection="type" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="segmentedMarket != null and segmentedMarket.size() > 0">
                and project.segmented_market in
                <foreach item="item" collection="segmentedMarket" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="countryId != null and countryId.size() > 0">
                and project.country_id in
                <foreach item="item" collection="countryId" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="city != null  and city != ''">and project.city like concat('%',#{city},'%')</if>
            <if test="currencyId != null and currencyId.size() > 0">
                and quo.currency_id in
                <foreach item="item" collection="currencyId" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="nickName != null  and nickName != ''">and u.nick_name like concat('%',#{nickName},'%')</if>
            <if test="createTime != null and createTime.size() == 2">
                AND quo.create_time BETWEEN #{createTime[0]} AND #{createTime[1]}
            </if>
            <if test="quotationStatus != null and quotationStatus.size() > 0">
                and quo.quotation_status in
                <foreach item="item" collection="quotationStatus" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        <if test="isGroupBy">
            having quo.id in (SELECT MAX(id) as id FROM tq_quotation GROUP BY quotation_no)
        </if>
        order by id desc
    </select>

    <select id="selectTqQuotationById" parameterType="Long" resultMap="TqQuotationResult">
        <include refid="selectTqQuotationVo"/>
        where id = #{id}
    </select>

    <select id="selectDetailById" parameterType="Long"
        resultType="com.ruoyi.project.business.domain.vo.quotation.QuotationDetailVO">
        select quo.quotation_no as quotationNo,
               quo.version,
               quo.project_id   as projectId,
               quo.create_by    as createBy,
               quo.agent_id     as agentId,
               quo.currency_id  as currencyId,
               su.nick_name     as quoter
        FROM tq_quotation quo
                 left join sys_user su on quo.create_by = su.user_id
        where id = #{id}
    </select>

    <insert id="insertTqQuotation" parameterType="TqQuotation" useGeneratedKeys="true" keyProperty="id">
        insert into tq_quotation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="quotationNo != null">quotation_no,</if>
            <if test="version != null">version,</if>
            <if test="agentId != null">agent_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="currencyId != null">currency_id,</if>
            <if test="exchangeRate != null">exchange_rate,</if>
            <if test="price != null">price,</if>
            <if test="finalPrice != null">final_price,</if>
            <if test="freightPrice != null">freight_price,</if>
            <if test="basicPrice != null">basic_price,</if>
            <if test="optionalPrice != null">optional_price,</if>
            <if test="quotationStatus != null">quotation_status,</if>
            <if test="submitTime != null">submit_time,</if>
            <if test="orderTime != null">order_time,</if>
            <if test="systemCheckResult != null">system_check_result,</if>
            <if test="permissionChar != null and permissionChar != ''">permission_char,</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="areaId != null">area_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="quotationNo != null">#{quotationNo},</if>
            <if test="version != null">#{version},</if>
            <if test="agentId != null">#{agentId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="currencyId != null">#{currencyId},</if>
            <if test="exchangeRate != null">#{exchangeRate},</if>
            <if test="price != null">#{price},</if>
            <if test="finalPrice != null">#{finalPrice},</if>
            <if test="freightPrice != null">#{freightPrice},</if>
            <if test="basicPrice != null">#{basicPrice},</if>
            <if test="optionalPrice != null">#{optionalPrice},</if>
            <if test="quotationStatus != null">#{quotationStatus},</if>
            <if test="submitTime != null">#{submitTime},</if>
            <if test="orderTime != null">#{orderTime},</if>
            <if test="systemCheckResult != null">#{systemCheckResult},</if>
            <if test="permissionChar != null and permissionChar != ''">#{permissionChar},</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="areaId != null">#{areaId},</if>
        </trim>
    </insert>

    <update id="updateTqQuotation" parameterType="TqQuotation">
        update tq_quotation
        <trim prefix="SET" suffixOverrides=",">
            <if test="quotationNo != null">quotation_no = #{quotationNo},</if>
            <if test="version != null">version = #{version},</if>
            <if test="agentId != null">agent_id = #{agentId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="currencyId != null">currency_id = #{currencyId},</if>
            <if test="exchangeRate != null">exchange_rate = #{exchangeRate},</if>
            <if test="price != null">price = #{price},</if>
            <if test="finalPrice != null">final_price = #{finalPrice},</if>
            <if test="freightPrice != null">freight_price = #{freightPrice},</if>
            <if test="basicPrice != null">basic_price = #{basicPrice},</if>
            <if test="optionalPrice != null">optional_price = #{optionalPrice},</if>
            <if test="quotationStatus != null">quotation_status = #{quotationStatus},</if>
            <if test="submitTime != null">submit_time = #{submitTime},</if>
            <if test="orderTime != null">order_time = #{orderTime},</if>
            <if test="systemCheckResult != null">system_check_result = #{systemCheckResult},</if>
            <if test="permissionChar != null and permissionChar != ''">permission_char = #{permissionChar},</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_quotation
        set optional_price   = optional_price + #{price},
            price            = price + #{price},
            quotation_status = 5
        where id = #{id}
    </update>

    <delete id="deleteTqQuotationById" parameterType="Long">
        delete
        from tq_quotation
        where id = #{id}
    </delete>

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

    <select id="getMaxVersionById" resultType="com.ruoyi.project.business.domain.vo.quotation.QuotationVersionVO">
        SELECT max(version) as version,quotation_no FROM tq_quotation WHERE id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY quotation_no
    </select>

    <select id="listByProjectId" resultMap="TqQuotationVOResult">
        select q.id, q.quotation_no, q.version, p.project_name
        from tq_quotation q
                 left join tc_project p on q.project_id = p.project_id
        where q.project_id = #{projectId}
          and q.del_flag = '0'
    </select>

    <select id="getAreaIdByProjectId" resultType="java.lang.Long">
        SELECT tca.area_id
        FROM tb_country_area tca
                 LEFT JOIN tc_project tp on tca.country_id = tp.country_id and tp.del_flag = '0'
        WHERE tca.del_flag = '0'
          and tp.project_id = #{projectId}
    </select>
</mapper>