<?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.younike.system.mapper.PublishQuoteMapper">

    <resultMap type="PublishQuote" id="PublishQuoteResult">
        <result property="quoteId"    column="quote_id"    />
        <result property="publishId"    column="publish_id"    />
        <result property="serverUserId"    column="server_user_id"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="quoteDesc"    column="quote_desc"    />
        <result property="quoteUnit"    column="quote_unit"    />
        <result property="otherUnit"    column="other_unit"    />
        <result property="singlePrice"    column="single_price"    />
        <result property="singleTime"    column="single_time"    />
        <result property="quoteCounts"    column="quote_counts"    />
        <result property="totalPrice"    column="total_price"    />
        <result property="totalTime"    column="total_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
    </resultMap>

    <resultMap type="com.younike.system.domain.vo.PublishQuoteVo" id="PublishQuoteVoResult">
        <result property="quoteId"    column="quote_id"    />
        <result property="publishId"    column="publish_id"    />
        <result property="serverUserId"    column="server_user_id"    />
        <result property="merchantId"    column="merchant_id"    />
        <result property="quoteDesc"    column="quote_desc"    />
        <result property="quoteUnit"    column="quote_unit"    />
        <result property="otherUnit"    column="other_unit"    />
        <result property="singlePrice"    column="single_price"    />
        <result property="singleTime"    column="single_time"    />
        <result property="quoteCounts"    column="quote_counts"    />
        <result property="totalPrice"    column="total_price"    />
        <result property="totalTime"    column="total_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="column1"    column="column1"    />
        <result property="column2"    column="column2"    />
        <result property="column3"    column="column3"    />
        <result property="monad"    column="monad"    />
        <collection property="quoteReplyVoList" select="quoteReplyData" column="quote_id"
                    ofType="com.younike.system.domain.vo.PublishQuoteReplyVo">
        </collection>
        <collection property="serverUser" select="getServerUser" column="server_user_id"
                    ofType="com.younike.system.domain.vo.ServerUserListVo">
        </collection>
        <collection property="merchant" select="getMerchant" column="merchant_id"
                    ofType="com.younike.system.domain.Merchant">
        </collection>

    </resultMap>
    <sql id="selectPublishQuoteVo">
        select quote_id, publish_id, server_user_id, merchant_id, quote_desc, quote_unit, other_unit, single_price, single_time, quote_counts, total_price, total_time, del_flag, create_by, create_time, update_by, update_time, column1, column2, column3 from publish_quote
    </sql>
    <sql id="selectPublishQuote">
         a.quote_id, a.publish_id, a.server_user_id, a.merchant_id, a.quote_desc, a.quote_unit, a.other_unit, a.single_price, a.single_time, a.quote_counts, a.total_price, a.total_time, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time, a.column1, a.column2, a.column3
    </sql>

    <select id="selectPublishQuoteList" parameterType="PublishQuote" resultMap="PublishQuoteResult">
        <include refid="selectPublishQuoteVo"/>
        <where>
            <if test="serverUserId != null "> and server_user_id = #{serverUserId}</if>
            <if test="quoteDesc != null  and quoteDesc != ''"> and quote_desc = #{quoteDesc}</if>
            <if test="quoteUnit != null  and quoteUnit != ''"> and quote_unit = #{quoteUnit}</if>
            <if test="otherUnit != null  and otherUnit != ''"> and other_unit = #{otherUnit}</if>
            <if test="singlePrice != null "> and single_price = #{singlePrice}</if>
            <if test="singleTime != null "> and single_time = #{singleTime}</if>
            <if test="quoteCounts != null "> and quote_counts = #{quoteCounts}</if>
            <if test="totalPrice != null "> and total_price = #{totalPrice}</if>
            <if test="totalTime != null "> and total_time = #{totalTime}</if>
            <if test="column1 != null  and column1 != ''"> and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''"> and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''"> and column3 = #{column3}</if>
        </where>
    </select>

    <select id="selectPublishQuoteByQuoteId" parameterType="Long" resultMap="PublishQuoteResult">
        <include refid="selectPublishQuoteVo"/>
        where quote_id = #{quoteId}
    </select>

    <insert id="insertPublishQuote" parameterType="PublishQuote" useGeneratedKeys="true" keyProperty="quoteId">
        insert into publish_quote
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="publishId != null">publish_id,</if>
            <if test="serverUserId != null">server_user_id,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="quoteDesc != null">quote_desc,</if>
            <if test="quoteUnit != null">quote_unit,</if>
            <if test="otherUnit != null">other_unit,</if>
            <if test="singlePrice != null">single_price,</if>
            <if test="singleTime != null">single_time,</if>
            <if test="quoteCounts != null">quote_counts,</if>
            <if test="totalPrice != null">total_price,</if>
            <if test="totalTime != null">total_time,</if>
            <if test="delFlag != null and delFlag != ''">del_flag,</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="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="publishId != null">#{publishId},</if>
            <if test="serverUserId != null">#{serverUserId},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="quoteDesc != null">#{quoteDesc},</if>
            <if test="quoteUnit != null">#{quoteUnit},</if>
            <if test="otherUnit != null">#{otherUnit},</if>
            <if test="singlePrice != null">#{singlePrice},</if>
            <if test="singleTime != null">#{singleTime},</if>
            <if test="quoteCounts != null">#{quoteCounts},</if>
            <if test="totalPrice != null">#{totalPrice},</if>
            <if test="totalTime != null">#{totalTime},</if>
            <if test="delFlag != null and delFlag != ''">#{delFlag},</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="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
         </trim>
    </insert>

    <update id="updatePublishQuote" parameterType="PublishQuote">
        update publish_quote
        <trim prefix="SET" suffixOverrides=",">
            <if test="publishId != null">publish_id = #{publishId},</if>
            <if test="serverUserId != null">server_user_id = #{serverUserId},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
            <if test="quoteDesc != null">quote_desc = #{quoteDesc},</if>
            <if test="quoteUnit != null">quote_unit = #{quoteUnit},</if>
            <if test="otherUnit != null">other_unit = #{otherUnit},</if>
            <if test="singlePrice != null">single_price = #{singlePrice},</if>
            <if test="singleTime != null">single_time = #{singleTime},</if>
            <if test="quoteCounts != null">quote_counts = #{quoteCounts},</if>
            <if test="totalPrice != null">total_price = #{totalPrice},</if>
            <if test="totalTime != null">total_time = #{totalTime},</if>
            <if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</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="column1 != null">column1 = #{column1},</if>
            <if test="column2 != null">column2 = #{column2},</if>
            <if test="column3 != null">column3 = #{column3},</if>
        </trim>
        where quote_id = #{quoteId}
    </update>

    <delete id="deletePublishQuoteByQuoteId" parameterType="Long">
        delete from publish_quote where quote_id = #{quoteId}
    </delete>

    <delete id="deletePublishQuoteByQuoteIds" parameterType="String">
        delete from publish_quote where quote_id in
        <foreach item="quoteId" collection="array" open="(" separator="," close=")">
            #{quoteId}
        </foreach>
    </delete>
<!--    获取发布需求详细信息(报价集合（包含留言）)-->
    <select id="getQuoteList" resultMap="PublishQuoteVoResult" parameterType="Long">
        select   <include refid="selectPublishQuote"/>,
        b.dict_label as monad
        FROM publish_quote a
        LEFT JOIN sys_dict_data b
        ON b.dict_value = a.quote_unit
        AND b.dict_type = 'goods_unit'
        WHERE a.del_flag = 0
        AND a.publish_id = #{publishId}
        ORDER BY a.create_time DESC
    </select>

    <select id="quoteReplyVo" resultMap="com.younike.system.mapper.PublishQuoteReplyMapper.PublishQuoteReplyDataResult">
       select <include refid="com.younike.system.mapper.PublishQuoteReplyMapper.selectPublishQuoteReply"/>,
        b.avatar as serverAvatar,
        ifnull(b.nike_name,b.real_name) as serverName,
        c.avatar,
        c.real_name as name,
        pqr.reply_type as replyReplyType,
        su.avatar as replyServerAvatar,
        su.real_name as replyServerName,
        m.avatar as replyAvatar,
        m.real_name as replyName
        from publish_quote_reply a
        left join server_user b on a.server_user_id=b.server_id and b.del_flag=0
        left join  member c on c.member_id=a.member_id and c.del_flag=0
        left join  publish_quote_reply pqr on pqr.re_reply_id=a.reply_id  and pqr.del_flag=0
        left join server_user su on pqr.server_user_id=su.server_id and su.del_flag=0
        left join  member m on m.member_id=pqr.member_id and m.del_flag=0
        where a.quote_id=#{quote_id}
        and a.del_flag=0
        GROUP BY a.reply_id
        ORDER BY a.create_time DESC
    </select>
    <select id="quoteReplyData" resultMap="com.younike.system.mapper.PublishQuoteReplyMapper.PublishQuoteReplyDataResult">
        select <include refid="com.younike.system.mapper.PublishQuoteReplyMapper.selectPublishQuoteReply"/>,
        b.avatar as serverAvatar,
        ifnull(b.real_name, b.nike_name) as serverName,
        c.avatar,
        c.nike_name as name
        from publish_quote_reply a
        left join server_user b on a.server_user_id=b.server_id and b.del_flag=0
        left join  member c on c.member_id=a.member_id and c.del_flag=0
        where a.quote_id=#{quote_id}
        and a.del_flag=0
        GROUP BY a.reply_id
        ORDER BY a.create_time ASC
    </select>
    <select id="getServerUser" resultMap="com.younike.system.mapper.ServerUserMapper.ServerUserListResult">
        select s.*,m.merchant_name, u.phonenumber, c.decode_value as serverLevelName,m.merchant_type
        from server_user s
        left join goods_server g on s.server_id = g.server_id and g.del_flag = 0
        left join merchant m on s.merchant_id = m.merchant_id and m.del_flag = 0
        left join sys_user u on m.admin_id = u.user_id and u.del_flag = 0
        left join sys_code c on s.server_level = c.decode_id and c.code_id = '007'
        where s.del_flag = 0
        and s.server_id=#{server_user_id}
        GROUP BY   s.server_id

    </select>
    <select id="getMerchant" resultMap="com.younike.system.mapper.MerchantMapper.MerchantResult">
        <include refid="com.younike.system.mapper.MerchantMapper.selectMerchantVo"/>
        where
        merchant_id = #{merchant_id}
        and del_flag = 0
    </select>

<!--  根据报价人id以及需求id查询数据  -->
    <select id="selectPublishQuote" resultMap="PublishQuoteResult" parameterType="com.younike.system.param.PublishQuoteParam">
        <include refid="selectPublishQuoteVo"/>
        where publish_id = #{publishId}
        and server_user_id = #{serverUserId}
        and del_flag=0
        limit 1
    </select>
<!--    根据报价id查询数据详情-->
    <select id="selectQuoteByQuoteId" resultMap="PublishQuoteVoResult" parameterType="Long">
        select   <include refid="selectPublishQuote"/>,b.dict_label as monad
        FROM publish_quote a
        LEFT JOIN sys_dict_data b
        ON b.dict_value = a.quote_unit
        AND b.dict_type = 'goods_unit'
        WHERE a.del_flag = 0
        AND a.quote_id = #{quoteId}

    </select>



</mapper>
