<?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.OrderItemCommentCountDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.OrderItemCommentCount">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
		<result column="store_id" jdbcType="BIGINT" property="storeId"/>
		<result column="goods_id" jdbcType="BIGINT" property="goodsId"/>
		<result column="item_id" jdbcType="VARCHAR" property="itemId"/>
		<result column="comment_count" jdbcType="INTEGER" property="commentCount"/>
		<result column="score1_count" jdbcType="INTEGER" property="score1Count"/>
		<result column="score2_count" jdbcType="INTEGER" property="score2Count"/>
		<result column="score3_count" jdbcType="INTEGER" property="score3Count"/>
		<result column="score4_count" jdbcType="INTEGER" property="score4Count"/>
		<result column="score5_count" jdbcType="INTEGER" property="score5Count"/>
		<result column="reply_count" jdbcType="INTEGER" property="replyCount"/>
		<result column="quality_count" jdbcType="INTEGER" property="qualityCount"/>
		<result column="image_count" jdbcType="INTEGER" property="imageCount"/>
		<result column="anonymous_count" jdbcType="INTEGER" property="anonymousCount"/>
		<result column="version" jdbcType="INTEGER" property="version"/>
		<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_item_comment_count
    </sql>

    <sql id="cols_all">
        id,
		seller_id,
		store_id,
		goods_id,
		item_id,
		comment_count,
		score1_count,
		score2_count,
		score3_count,
		score4_count,
		score5_count,
		reply_count,
		quality_count,
		image_count,
		anonymous_count,
		version,
		created_at,
		updated_at,
		is_deleted
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
		#{sellerId,jdbcType=BIGINT},
		#{storeId,jdbcType=BIGINT},
		#{goodsId,jdbcType=BIGINT},
		#{itemId,jdbcType=VARCHAR},
		#{commentCount,jdbcType=INTEGER},
		#{score1Count,jdbcType=INTEGER},
		#{score2Count,jdbcType=INTEGER},
		#{score3Count,jdbcType=INTEGER},
		#{score4Count,jdbcType=INTEGER},
		#{score5Count,jdbcType=INTEGER},
		#{replyCount,jdbcType=INTEGER},
		#{qualityCount,jdbcType=INTEGER},
		#{imageCount,jdbcType=INTEGER},
		#{anonymousCount,jdbcType=INTEGER},
		0,
		now(),
		now(),
		0
    </sql>

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

    <select id="selectList" parameterType="map" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
			seller_id = #{sellerId}
			and goods_id = #{goodsId}
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="itemId != null">
				and item_id = #{itemId}
			</if>
            and is_deleted = 0
        </where>
        order by id desc
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
    </select>

    <select id="selectOne" parameterType="map" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
			seller_id = #{sellerId}
			and goods_id = #{goodsId}
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="itemId != null">
				and item_id = #{itemId}
			</if>
        </where>
        limit 1
    </select>

    <update id="update" parameterType="com.zm.order.tool.server.model.OrderItemCommentCount">
        UPDATE
        <include refid="tb"/>
        <set>
			<if test="commentCount != null">
				comment_count = #{commentCount,jdbcType=INTEGER},
			</if>
			<if test="score1Count != null">
				score1_count = #{score1Count,jdbcType=INTEGER},
			</if>
			<if test="score2Count != null">
				score2_count = #{score2Count,jdbcType=INTEGER},
			</if>
			<if test="score3Count != null">
				score3_count = #{score3Count,jdbcType=INTEGER},
			</if>
			<if test="score4Count != null">
				score4_count = #{score4Count,jdbcType=INTEGER},
			</if>
			<if test="score5Count != null">
				score5_count = #{score5Count,jdbcType=INTEGER},
			</if>
			<if test="replyCount != null">
				reply_count = #{replyCount,jdbcType=INTEGER},
			</if>
			<if test="qualityCount != null">
				quality_count = #{qualityCount,jdbcType=INTEGER},
			</if>
			<if test="imageCount != null">
				image_count = #{imageCount,jdbcType=INTEGER},
			</if>
			<if test="anonymousCount != null">
				anonymous_count = #{anonymousCount,jdbcType=INTEGER},
			</if>
			updated_at = now(),
			version = version + 1
        </set>
        where id = #{id}
		<if test="version != null">
			AND version = #{version}
		</if>
    </update>

</mapper>