<?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.zm.order.tool.server.dao.OrderCommentDetailsDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.OrderCommentDetails">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
		<result column="user_id" jdbcType="BIGINT" property="userId"/>
		<result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
		<result column="store_id" jdbcType="BIGINT" property="storeId"/>
		<result column="comment_type" jdbcType="INTEGER" property="commentType"/>
		<result column="comment_score" jdbcType="INTEGER" property="commentScore"/>
		<result column="comment_name" jdbcType="INTEGER" property="commentName"/>
		<result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
		<result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
		<result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
	</resultMap>

	<sql id="tb">
        order_comment_details
    </sql>

    <sql id="cols_all">
        id,
		order_no,
		user_id,
		seller_id,
		store_id,
		comment_type,
		comment_score,
		comment_name,
		created_at,
		updated_at,
		is_deleted
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
		#{orderNo,jdbcType=VARCHAR},
		#{userId,jdbcType=BIGINT},
		#{sellerId},
		#{storeId},
		#{commentType,jdbcType=INTEGER},
		#{commentScore,jdbcType=INTEGER},
		#{commentName},
		now(),
		now(),
		0
    </sql>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.OrderCommentDetails"
                        keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>

    <select id="selectByOrderNo" parameterType = "map" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
            seller_id = #{sellerId} and order_no = #{orderNo} and is_deleted = 0
        </where>
        limit 20
    </select>

    <select id="selectByOrderNoList" parameterType = "map" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
            seller_id in
            <foreach collection="sellerIdList" item="sellerId" index="index" open="(" close=")" separator=",">
                #{sellerId}
            </foreach>
            AND order_no in
            <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                #{orderNo}
            </foreach>
            and is_deleted = 0
        </where>
    </select>

    <select id="selectIdList" parameterType="map" resultMap="ResultMap">
        SELECT id FROM
        <include refid="tb"/>
        <where>
            seller_id = #{sellerId}
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
            <if test="orderNo != null and orderNo != ''">
                and order_no = #{orderNo}
            </if>
            <if test="lastId != null">
                <![CDATA[ AND id < #{lastId} ]]>
            </if>
        </where>
        order by id desc limit ${size}
    </select>

    <update id="update" parameterType="map">
        UPDATE
        <include refid="tb"/>
        <set>
			<if test="commentType != null">
				comment_type = #{commentType,jdbcType=INTEGER},
			</if>
			<if test="commentScore != null">
				comment_score = #{commentScore,jdbcType=INTEGER},
			</if>
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <insert id="insertList" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="record" index="index" separator=",">
            (
            #{record.id}, #{record.orderNo}, #{record.userId}, #{record.sellerId}, #{record.storeId},
            #{record.commentType}, #{record.commentScore}, #{record.commentName}, now(), now(), 0
            )
        </foreach>
    </insert>

    <delete id="delete" parameterType="map">
        delete from
        <include refid="tb"/>
        where id in
        <foreach collection="list" item="record" index="index" open="(" close=")" separator=",">
            #{record}
        </foreach>
        and seller_id = #{sellerId}
    </delete>

    <update id="updateSellerUser" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            seller_id = #{sellerId},
            store_id = #{storeId},
            user_id = #{newUserId},
            updated_at= now()
        </set>
        where
        user_id = #{oldUserId}
        and order_no = #{orderNo}
    </update>

    <update id="deleteByOrderNo" parameterType="map">
        UPDATE
        <include refid="tb"/>
        <set>
            is_deleted = -1,
            updated_at = now()
        </set>
        where order_no = #{orderNo} and seller_id = #{sellerId}
    </update>
</mapper>