<?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.quot.mapper.QuotMapper">

    <resultMap type="Quot" id="QuotResult">
        <result property="quotId"    column="quot_id"    />
        <result property="quotCode"    column="quot_code"    />
        <result property="quotSalesmanBm"    column="quot_salesman_bm"    />
        <result property="quotSalesmanName"    column="quot_salesman_name"    />
        <result property="quotCustomerBm"    column="quot_customer_bm"    />
        <result property="quotCustomerName"    column="quot_customer_name"    />
        <result property="quotSalesmanDeptId"    column="quot_salesman_dept_id"    />
        <result property="quotSalesmanDeptName"    column="quot_salesman_dept_name"    />
        <result property="quotAddress"    column="quot_address"    />
        <result property="quotPhone"    column="quot_phone"    />
        <result property="quotInquiryDate"    column="quot_inquiry_date"    />
        <result property="quotProject"    column="quot_project"    />
        <result property="quotQuotationDate"    column="quot_quotation_date"    />
        <result property="quotQuotationFrom"    column="quot_quotation_from"    />
        <result property="quotQuotationRequire"    column="quot_quotation_require"    />
        <result property="quotFeedbackExplanation"    column="quot_feedback_explanation"    />
        <result property="quotQuantity"    column="quot_quantity"    />
        <result property="quotTotalPrice"    column="quot_total_price"    />
        <result property="quotCheckUserName"    column="quot_check_user_name"    />
        <result property="quotCheckUserNickname"    column="quot_check_user_nickname"    />
        <result property="quotApprovalStatus"    column="quot_approval_status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createName"     column="create_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />

        <result property="quotJsxzStandard"    column="quot_jsxz_standard"    />
        <result property="quotJsxzApprovalStatus"    column="quot_jsxz_approval_status"    />
        <result property="quotJsxzChapter"    column="quot_jsxz_chapter"    />
        <result property="quotJsxzTechnicalRequirement"    column="quot_jsxz_technical_requirement"    />
        <result property="quotJsxzGroup"    column="quot_jsxz_group"    />
        <result property="quotJsxzConfirmId"    column="quot_jsxz_confirm_id"    />
        <result property="quotJsxzConfirmCode"    column="quot_jsxz_confirm_code"    />

    </resultMap>

    <resultMap id="QuotQuotMaterialResult" type="Quot" extends="QuotResult">
        <collection property="quotMaterialList" notNullColumn="sub_mat_id" javaType="java.util.List" resultMap="QuotMaterialResult" />
    </resultMap>

    <resultMap type="QuotMaterial" id="QuotMaterialResult">
        <result property="matId"    column="sub_mat_id"    />
        <result property="matXingh"    column="sub_mat_xingh"    />
        <result property="matGuig"    column="sub_mat_guig"    />
        <result property="matDiany"    column="sub_mat_diany"    />
        <result property="matDanw"    column="sub_mat_danw"    />
        <result property="matSl"    column="sub_mat_sl"    />
        <result property="quotId"    column="sub_quot_id"    />
    </resultMap>

    <sql id="quotJoins">
        left join sys_user u on u.user_name=a.create_by
        left join sys_dept d on u.dept_id = d.dept_id
        left join quot_jsqr q on q.quot_jsqr_id = a.quot_jsxz_confirm_id
    </sql>

    <sql id="selectQuotVo">
        select a.quot_id, a.quot_code, a.quot_salesman_bm, a.quot_salesman_name, a.quot_customer_bm,
               a.quot_customer_name,a.quot_salesman_dept_id, a.quot_salesman_dept_name, a.quot_address,
               a.quot_phone, a.quot_inquiry_date, a.quot_project, a.quot_quotation_date, a.quot_quotation_from,
               a.quot_quotation_require, a.quot_feedback_explanation, a.quot_quantity, a.quot_total_price,
               a.quot_check_user_name, a.quot_check_user_nickname, a.quot_approval_status,
               a.create_by, a.create_time, a.update_by, a.update_time, u.nick_name create_name,

                a.quot_jsxz_standard,a.quot_jsxz_approval_status,a.quot_jsxz_chapter,
                a.quot_jsxz_technical_requirement,a.quot_jsxz_group,a.quot_jsxz_confirm_id,q.quot_jsqr_code quot_jsxz_confirm_code
        from quot a
        <include refid="quotJoins"/>
    </sql>

    <select id="selectQuotList" parameterType="Quot" resultMap="QuotResult">
        <include refid="selectQuotVo"/>
        <where>
            <if test="quotCode != null  and quotCode != ''"> and quot_code like concat('%', #{quotCode}, '%')</if>
            <if test="quotCustomerName != null  and quotCustomerName != ''"> and quot_customer_name like concat('%', #{quotCustomerName}, '%')</if>
            <if test="quotProject != null  and quotProject != ''"> and quot_project like concat('%', #{quotProject}, '%')</if>
            <if test="quotApprovalStatus != null  and quotApprovalStatus != ''"> and quot_approval_status = #{quotApprovalStatus}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>

    <select id="selectQuotByQuotId" parameterType="String" resultMap="QuotQuotMaterialResult">
        select a.quot_id, a.quot_code, a.quot_salesman_bm, a.quot_salesman_name, a.quot_customer_name,
               a.quot_salesman_dept_id, a.quot_salesman_dept_name, a.quot_address, a.quot_phone, a.quot_inquiry_date,
               a.quot_project, a.quot_quotation_date, a.quot_quotation_from, a.quot_quotation_require, a.quot_feedback_explanation,
               a.quot_quantity, a.quot_total_price, a.quot_check_user_name, a.quot_check_user_nickname, a.quot_approval_status,
               a.create_by, a.create_time, a.update_by, a.update_time,
               a.quot_jsxz_standard,a.quot_jsxz_approval_status,a.quot_jsxz_chapter,
               a.quot_jsxz_technical_requirement,a.quot_jsxz_group,a.quot_jsxz_confirm_id,q.quot_jsqr_code quot_jsxz_confirm_code,

                b.mat_id as sub_mat_id, b.mat_xingh as sub_mat_xingh, b.mat_guig as sub_mat_guig,
               b.mat_diany as sub_mat_diany, b.mat_danw as sub_mat_danw, b.mat_sl as sub_mat_sl,
               b.quot_id as sub_quot_id
        from quot a
        left join quot_material b on b.quot_id = a.quot_id
        <include refid="quotJoins"/>
        where a.quot_id = #{quotId}
    </select>

    <insert id="insertQuot" parameterType="Quot">
        insert into quot
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="quotId != null and quotId != ''">quot_id,</if>
            <if test="quotCode != null and quotCode != ''">quot_code,</if>
            <if test="quotSalesmanBm != null and quotSalesmanBm != ''">quot_salesman_bm,</if>
            <if test="quotSalesmanName != null and quotSalesmanName != ''">quot_salesman_name,</if>
            <if test="quotCustomerBm != null and quotCustomerBm != ''">quot_customer_bm,</if>
            <if test="quotCustomerName != null and quotCustomerName != ''">quot_customer_name,</if>
            <if test="quotSalesmanDeptId != null">quot_salesman_dept_id,</if>
            <if test="quotSalesmanDeptName != null">quot_salesman_dept_name,</if>
            <if test="quotAddress != null">quot_address,</if>
            <if test="quotPhone != null">quot_phone,</if>
            <if test="quotInquiryDate != null">quot_inquiry_date,</if>
            <if test="quotProject != null and quotProject != ''">quot_project,</if>
            <if test="quotQuotationDate != null">quot_quotation_date,</if>
            <if test="quotQuotationFrom != null">quot_quotation_from,</if>
            <if test="quotQuotationRequire != null and quotQuotationRequire != ''">quot_quotation_require,</if>
            <if test="quotFeedbackExplanation != null">quot_feedback_explanation,</if>
            <if test="quotQuantity != null">quot_quantity,</if>
            <if test="quotTotalPrice != null">quot_total_price,</if>
            <if test="quotCheckUserName != null">quot_check_user_name,</if>
            <if test="quotCheckUserNickname != null">quot_check_user_nickname,</if>
            <if test="quotApprovalStatus != null">quot_approval_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>

            <if test="quotJsxzStandard != null">quot_jsxz_standard,</if>
            <if test="quotJsxzApprovalStatus != null">quot_jsxz_approval_status,</if>
            <if test="quotJsxzChapter != null">quot_jsxz_chapter,</if>
            <if test="quotJsxzTechnicalRequirement != null">quot_jsxz_technical_requirement,</if>
            <if test="quotJsxzGroup != null">quot_jsxz_group,</if>
            <if test="quotJsxzConfirmId != null">quot_jsxz_confirm_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="quotId != null and quotId != ''">#{quotId},</if>
            <if test="quotCode != null and quotCode != ''">#{quotCode},</if>
            <if test="quotSalesmanBm != null and quotSalesmanBm != ''">#{quotSalesmanBm},</if>
            <if test="quotSalesmanName != null and quotSalesmanName != ''">#{quotSalesmanName},</if>
            <if test="quotCustomerBm != null and quotCustomerBm != ''">#{quotCustomerBm},</if>
            <if test="quotCustomerName != null and quotCustomerName != ''">#{quotCustomerName},</if>
            <if test="quotSalesmanDeptId != null">#{quotSalesmanDeptId},</if>
            <if test="quotSalesmanDeptName != null">#{quotSalesmanDeptName},</if>
            <if test="quotAddress != null">#{quotAddress},</if>
            <if test="quotPhone != null">#{quotPhone},</if>
            <if test="quotInquiryDate != null">#{quotInquiryDate},</if>
            <if test="quotProject != null and quotProject != ''">#{quotProject},</if>
            <if test="quotQuotationDate != null">#{quotQuotationDate},</if>
            <if test="quotQuotationFrom != null">#{quotQuotationFrom},</if>
            <if test="quotQuotationRequire != null and quotQuotationRequire != ''">#{quotQuotationRequire},</if>
            <if test="quotFeedbackExplanation != null">#{quotFeedbackExplanation},</if>
            <if test="quotQuantity != null">#{quotQuantity},</if>
            <if test="quotTotalPrice != null">#{quotTotalPrice},</if>
            <if test="quotCheckUserName != null">#{quotCheckUserName},</if>
            <if test="quotCheckUserNickname != null">#{quotCheckUserNickname},</if>
            <if test="quotApprovalStatus != null">#{quotApprovalStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>

            <if test="quotJsxzStandard != null">#{quotJsxzStandard},</if>
            <if test="quotJsxzApprovalStatus != null">#{quotJsxzApprovalStatus},</if>
            <if test="quotJsxzChapter != null">#{quotJsxzChapter},</if>
            <if test="quotJsxzTechnicalRequirement != null">#{quotJsxzTechnicalRequirement},</if>
            <if test="quotJsxzGroup != null">#{quotJsxzGroup},</if>
            <if test="quotJsxzConfirmId != null">#{quotJsxzConfirmId},</if>
         </trim>
    </insert>

    <update id="updateQuot" parameterType="Quot">
        update quot
        <trim prefix="SET" suffixOverrides=",">
            <if test="quotCode != null and quotCode != ''">quot_code = #{quotCode},</if>
            <if test="quotSalesmanBm != null and quotSalesmanBm != ''">quot_salesman_bm = #{quotSalesmanBm},</if>
            <if test="quotSalesmanName != null and quotSalesmanName != ''">quot_salesman_name = #{quotSalesmanName},</if>
            <if test="quotCustomerBm != null and quotCustomerBm != ''">quot_customer_bm = #{quotCustomerBm},</if>
            <if test="quotCustomerName != null and quotCustomerName != ''">quot_customer_name = #{quotCustomerName},</if>
            <if test="quotSalesmanDeptId != null">quot_salesman_dept_id = #{quotSalesmanDeptId},</if>
            <if test="quotSalesmanDeptName != null">quot_salesman_dept_name = #{quotSalesmanDeptName},</if>
            <if test="quotAddress != null">quot_address = #{quotAddress},</if>
            <if test="quotPhone != null">quot_phone = #{quotPhone},</if>
            <if test="quotInquiryDate != null">quot_inquiry_date = #{quotInquiryDate},</if>
            <if test="quotProject != null and quotProject != ''">quot_project = #{quotProject},</if>
            <if test="quotQuotationDate != null">quot_quotation_date = #{quotQuotationDate},</if>
            <if test="quotQuotationFrom != null">quot_quotation_from = #{quotQuotationFrom},</if>
            <if test="quotQuotationRequire != null and quotQuotationRequire != ''">quot_quotation_require = #{quotQuotationRequire},</if>
            <if test="quotFeedbackExplanation != null">quot_feedback_explanation = #{quotFeedbackExplanation},</if>
            <if test="quotQuantity != null">quot_quantity = #{quotQuantity},</if>
            <if test="quotTotalPrice != null">quot_total_price = #{quotTotalPrice},</if>
            <if test="quotCheckUserName != null">quot_check_user_name = #{quotCheckUserName},</if>
            <if test="quotCheckUserNickname != null">quot_check_user_nickname = #{quotCheckUserNickname},</if>
            <if test="quotApprovalStatus != null">quot_approval_status = #{quotApprovalStatus},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>

            <if test="quotJsxzStandard != null">quot_jsxz_standard = #{quotJsxzStandard},</if>
            <if test="quotJsxzApprovalStatus != null">quot_jsxz_approval_status = #{quotJsxzApprovalStatus},</if>
            <if test="quotJsxzChapter != null">quot_jsxz_chapter = #{quotJsxzChapter},</if>
            <if test="quotJsxzTechnicalRequirement != null">quot_jsxz_technical_requirement = #{quotJsxzTechnicalRequirement},</if>
            <if test="quotJsxzGroup != null">quot_jsxz_group = #{quotJsxzGroup},</if>
            <if test="quotJsxzConfirmId != null">quot_jsxz_confirm_id = #{quotJsxzConfirmId},</if>
        </trim>
        where quot_id = #{quotId}
    </update>

    <delete id="deleteQuotByQuotId" parameterType="String">
        delete from quot where quot_id = #{quotId}
    </delete>

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

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

    <delete id="deleteQuotMaterialByQuotId" parameterType="String">
        delete from quot_material where quot_id = #{quotId}
    </delete>

    <insert id="batchQuotMaterial">
        insert into quot_material( mat_id, mat_xingh, mat_guig, mat_diany, mat_danw, mat_sl, quot_id) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.matId}, #{item.matXingh}, #{item.matGuig}, #{item.matDiany}, #{item.matDanw}, #{item.matSl}, #{item.quotId})
        </foreach>
    </insert>
</mapper>