<?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.newdatasource.NewDataSourceOrderItemCommentDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.OrderItemComment">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="biz_type" jdbcType="INTEGER" property="bizType"/>
		<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="goods_id" jdbcType="BIGINT" property="goodsId"/>
		<result column="item_id" jdbcType="VARCHAR" property="itemId"/>
		<result column="comment_score" jdbcType="INTEGER" property="commentScore"/>
		<result column="comment_info" jdbcType="VARCHAR" property="commentInfo"/>
		<result column="anonymous" jdbcType="INTEGER" property="anonymous"/>
		<result column="seller_reply" jdbcType="INTEGER" property="sellerReply"/>
		<result column="reply_at" jdbcType="TIMESTAMP" property="replyAt"/>
		<result column="seller_reply_info" jdbcType="VARCHAR" property="sellerReplyInfo"/>
		<result column="exist_image" jdbcType="INTEGER" property="existImage"/>
		<result column="comment_image" jdbcType="VARCHAR" property="commentImage"/>
		<result column="hide" jdbcType="INTEGER" property="hide"/>
		<result column="comment_at" jdbcType="TIMESTAMP" property="commentAt"/>
		<result column="features" jdbcType="VARCHAR" property="features"/>
		<result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
		<result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
		<result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
		<result column="version" jdbcType="INTEGER" property="version"/>
		<result column="sync_flag" jdbcType="INTEGER" property="syncFlag"/>
		<result column="shares" jdbcType="INTEGER" property="shares"/>
		<result column="order_type" jdbcType="INTEGER" property="orderType"/>
		<result column="item_info" jdbcType="VARCHAR" property="itemInfo"/>
		<result column="performance_type" jdbcType="INTEGER" property="performanceType"/>
		<result column="master_data_source" jdbcType="INTEGER" property="masterDataSource" />
		<result column="order_sub_type" jdbcType="INTEGER" property="orderSubType" />
	</resultMap>

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

    <sql id="cols_all">
        id,
		biz_type,
		order_no,
		user_id,
		seller_id,
		store_id,
		goods_id,
		item_id,
		comment_score,
		comment_info,
		anonymous,
		seller_reply,
		reply_at,
		seller_reply_info,
		exist_image,
		comment_image,
		hide,
		comment_at,
		features,
		created_at,
		updated_at,
		is_deleted,
		version,
		sync_flag,
		shares,
		order_type,
		item_info,
		performance_type,master_data_source,
		order_sub_type
    </sql>

    <sql id="values">
    	#{id,jdbcType=BIGINT},
		#{bizType,jdbcType=INTEGER},
		#{orderNo,jdbcType=VARCHAR},
		#{userId,jdbcType=BIGINT},
		#{mobile,jdbcType=VARCHAR},
		#{orderType,jdbcType=INTEGER},
		#{commentType,jdbcType=INTEGER},
		#{sellerId,jdbcType=BIGINT},
		#{storeId,jdbcType=BIGINT},
		#{anonymous,jdbcType=INTEGER},
		#{commentInfo,jdbcType=VARCHAR},
		#{existRecommend,jdbcType=INTEGER},
		#{recommendItemInfo,jdbcType=VARCHAR},
		#{buyerRemarks,jdbcType=VARCHAR},
		#{sellerReply,jdbcType=INTEGER},
		#{sellerReplyInfo,jdbcType=VARCHAR},
		#{existImage,jdbcType=INTEGER},
		#{commentImage,jdbcType=VARCHAR},
		#{hide},
		#{quality},
		#{deliveryComment},
		#{commentAt},
		#{replyAt},
		#{features,jdbcType=VARCHAR},
		now(),
		now(),
		0,
		0,
		0,
		#{shares,jdbcType=INTEGER},
		#{orderType,jdbcType=INTEGER},
		#{itemInfo,jdbcType=VARCHAR},
		#{performanceType,jdbcType=INTEGER},#{masterDataSource},#{orderSubType}
    </sql>

    <select id="selectByUserId" parameterType="map" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
			seller_id = #{sellerId}
			<if test="userId != null">
				and user_id = #{userId}
			</if>
			<if test="hide != null">
				and hide = #{hide}
			</if>
			<if test="existRecommend != null">
				and exist_recommend = #{existRecommend}
			</if>
			<if test="commentType != null">
				and comment_type = #{commentType}
			</if>
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="mobile != null and mobile != ''">
				and mobile = #{mobile}
			</if>
			<if test="orderNo != null and orderNo != ''">
				and order_no = #{orderNo}
			</if>
			<if test="sellerReply != null">
				and seller_reply = #{sellerReply}
			</if>
			<if test="existImage != null">
				and exist_image = #{existImage}
			</if>
			<if test="commentAtStart != null">
				<![CDATA[ AND comment_at >= #{commentAtStart} ]]>
			</if>
			<if test="commentAtEnd != null">
				<![CDATA[ AND comment_at <= #{commentAtEnd} ]]>
			</if>
			<if test="replyAtStart != null">
				<![CDATA[ AND reply_at >= #{replyAtStart} ]]>
			</if>
			<if test="replyAtEnd != null">
				<![CDATA[ AND reply_at <= #{replyAtEnd} ]]>
			</if>
			<if test="orderTypeList != null">
				AND order_type in
				<foreach collection="orderTypeList" item="orderType" index="index" open="(" close=")" separator=",">
					#{orderType}
				</foreach>
			</if>
			<if test="storeIdList != null">
				AND store_id in
				<foreach collection="storeIdList" item="storeId" index="index" open="(" close=")" separator=",">
					#{storeId}
				</foreach>
			</if>
			AND is_deleted = 0
        </where>
        order by comment_at desc
		<if test="start != null">
			limit ${start}, ${pageSize}
		</if>
		<if test="size != null">
			limit ${size}
		</if>
    </select>

	<select id="selectOne" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			seller_id = #{sellerId}
			<if test="itemCommentId != null">
				and id = #{itemCommentId}
			</if>
			<if test="userId != null">
				and user_id = #{userId}
			</if>
			<if test="goodsId != null">
				and goods_id = #{goodsId}
			</if>
			<if test="itemId != null and itemId != ''">
				and item_id = #{itemId}
			</if>
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="orderNo != null and orderNo != ''">
				and order_no = #{orderNo}
			</if>
			<if test="existImage != null">
				and exist_image = #{existImage}
			</if>
			<if test="commentAtStart != null">
				<![CDATA[ AND comment_at >= #{commentAtStart} ]]>
			</if>
			<if test="commentAtEnd != null">
				<![CDATA[ AND comment_at <= #{commentAtEnd} ]]>
			</if>
			AND is_deleted = 0
		</where>
		limit 1
	</select>


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

	<select id="selectById" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			seller_id = #{sellerId}
			and id = #{id}
		</where>
	</select>

	<update id="resetFlag" parameterType="map">
		update
		<include refid="tb"/>
		set sync_flag = 0, updated_at = now()
		where id = #{id}
		and seller_id = #{sellerId}
		and version = #{version}
	</update>

	<select id="selectList" parameterType="map" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			seller_id = #{sellerId}
			<if test="userId != null">
				and user_id = #{userId}
			</if>
			<if test="goodsId != null">
				and goods_id = #{goodsId}
			</if>
			<if test="itemId != null and itemId != ''">
				and item_id = #{itemId}
			</if>
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="orderNo != null and orderNo != ''">
				and order_no = #{orderNo}
			</if>
			<if test="existImage != null">
				and exist_image = #{existImage}
			</if>
			<if test="hide != null">
				and hide = #{hide}
			</if>
			<if test="commentAtStart != null">
				<![CDATA[ AND comment_at >= #{commentAtStart} ]]>
			</if>
			<if test="commentAtEnd != null">
				<![CDATA[ AND comment_at <= #{commentAtEnd} ]]>
			</if>
			<if test="commentScoreStart != null">
				<![CDATA[ AND comment_score >= #{commentScoreStart} ]]>
			</if>
			<if test="commentScoreEnd != null">
				<![CDATA[ AND comment_score <= #{commentScoreEnd} ]]>
			</if>
			<if test="orderNoList != null and orderNoList.size > 0">
				AND order_no in
				<foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
					#{orderNo}
				</foreach>
			</if>
			<if test="commentScoreList != null and commentScoreList.size > 0">
				AND comment_score in
				<foreach collection="commentScoreList" item="commentScore" index="index" open="(" close=")" separator=",">
					#{commentScore}
				</foreach>
			</if>
			<if test="goodsIdList != null and goodsIdList.size > 0">
				AND goods_id in
				<foreach collection="goodsIdList" item="goodsId" index="index" open="(" close=")" separator=",">
					#{goodsId}
				</foreach>
			</if>
			<if test="itemIdList != null and itemIdList.size > 0">
				AND item_id in
				<foreach collection="itemIdList" item="itemId" index="index" open="(" close=")" separator=",">
					#{itemId}
				</foreach>
			</if>
			<if test="allIsDeletedFlag == null">
				and is_deleted = 0
			</if>
		</where>
		order by comment_at desc
		<if test="start != null">
			limit ${start}, ${pageSize}
		</if>
		<if test="size != null">
			limit ${size}
		</if>
	</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="goodsId != null">
				and goods_id = #{goodsId}
			</if>
			<if test="itemId != null and itemId != ''">
				and item_id = #{itemId}
			</if>
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="orderNo != null and orderNo != ''">
				and order_no = #{orderNo}
			</if>
			<if test="existImage != null">
				and exist_image = #{existImage}
			</if>
			<if test="hide != null">
				and hide = #{hide}
			</if>
			<if test="commentAtStart != null">
				<![CDATA[ AND comment_at >= #{commentAtStart} ]]>
			</if>
			<if test="commentAtEnd != null">
				<![CDATA[ AND comment_at <= #{commentAtEnd} ]]>
			</if>
			<if test="commentScoreStart != null">
				<![CDATA[ AND comment_score >= #{commentScoreStart} ]]>
			</if>
			<if test="commentScoreEnd != null">
				<![CDATA[ AND comment_score <= #{commentScoreEnd} ]]>
			</if>
			<if test="goodsIdList != null and goodsIdList.size > 0">
				AND goods_id in
				<foreach collection="goodsIdList" item="goodsId" index="index" open="(" close=")" separator=",">
					#{goodsId}
				</foreach>
			</if>
			<if test="commentScoreList != null and commentScoreList.size > 0">
				AND comment_score in
				<foreach collection="commentScoreList" item="commentScore" index="index" open="(" close=")" separator=",">
					#{commentScore}
				</foreach>
			</if>
			<if test="itemIdList != null and itemIdList.size > 0">
				AND item_id in
				<foreach collection="itemIdList" item="itemId" index="index" open="(" close=")" separator=",">
					#{itemId}
				</foreach>
			</if>
			AND is_deleted = 0
		</where>
		<if test="start != null">
			order by comment_at desc
			<choose>
				<when test="fakePageSize != null">
					limit ${start}, ${fakePageSize}
				</when>
				<otherwise>
					limit ${start}, ${pageSize}
				</otherwise>
			</choose>
		</if>
		<if test="size != null">
			order by id
			limit ${size}
		</if>
	</select>

	<update id="update" parameterType="com.zm.order.tool.server.model.OrderItemComment">
        UPDATE
        <include refid="tb"/>
        <set>
			<if test="anonymous != null">
				anonymous = #{anonymous,jdbcType=INTEGER},
			</if>
			<if test="commentInfo != null">
				comment_info = #{commentInfo,jdbcType=VARCHAR},
			</if>
			<if test="hide != null">
				hide = #{hide,jdbcType=INTEGER},
			</if>
			<if test="sellerReply != null">
				seller_reply = #{sellerReply,jdbcType=INTEGER},
			</if>
			<if test="sellerReplyInfo != null">
				seller_reply_info = #{sellerReplyInfo,jdbcType=VARCHAR},
			</if>
			<if test="existImage != null">
				exist_image = #{existImage,jdbcType=INTEGER},
			</if>
			<if test="commentImage != null">
				comment_image = #{commentImage,jdbcType=VARCHAR},
			</if>
			<if test="commentAt != null">
				comment_at = #{commentAt,jdbcType=TIMESTAMP},
			</if>
			<if test="replyAt != null">
				reply_at = #{replyAt},
			</if>
			<if test="features != null">
				features = #{features,jdbcType=VARCHAR},
			</if>
			<if test="shares != null">
				shares = #{shares,jdbcType=INTEGER},
			</if>
			updated_at = now(),
			version = version + 1
        </set>
        where id = #{id} and seller_id = #{sellerId}
		<if test="version != null">
			AND version = #{version}
		</if>
    </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,jdbcType=BIGINT},
			#{record.bizType,jdbcType=INTEGER},
			#{record.orderNo,jdbcType=VARCHAR},
			#{record.userId,jdbcType=BIGINT},
			#{record.sellerId,jdbcType=BIGINT},
			#{record.storeId,jdbcType=BIGINT},
			#{record.goodsId,jdbcType=BIGINT},
			#{record.itemId,jdbcType=VARCHAR},
			#{record.commentScore,jdbcType=INTEGER},
			#{record.commentInfo,jdbcType=VARCHAR},
			#{record.anonymous,jdbcType=INTEGER},
			#{record.sellerReply,jdbcType=INTEGER},
			#{record.replyAt,jdbcType=VARCHAR},
			#{record.sellerReplyInfo,jdbcType=VARCHAR},
			#{record.existImage,jdbcType=INTEGER},
			#{record.commentImage,jdbcType=VARCHAR},
			#{record.hide,jdbcType=INTEGER},
			#{record.commentAt,jdbcType=VARCHAR},
			#{record.features,jdbcType=VARCHAR},
			now(),
			now(),
			0,
		    0,
		    0,
			#{record.shares,jdbcType=INTEGER},
			#{record.orderType,jdbcType=INTEGER},
			#{record.itemInfo,jdbcType=VARCHAR},
			#{record.performanceType},#{record.masterDataSource},#{record.orderSubType}
			)
		</foreach>
	</insert>

	<insert id="replaceList" parameterType="list">
		replace into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		values
		<foreach collection="list" item="record" index="index" separator=",">
			(
			#{record.id,jdbcType=BIGINT},
			#{record.bizType,jdbcType=INTEGER},
			#{record.orderNo,jdbcType=VARCHAR},
			#{record.userId,jdbcType=BIGINT},
			#{record.sellerId,jdbcType=BIGINT},
			#{record.storeId,jdbcType=BIGINT},
			#{record.goodsId,jdbcType=BIGINT},
			#{record.itemId,jdbcType=VARCHAR},
			#{record.commentScore,jdbcType=INTEGER},
			#{record.commentInfo,jdbcType=VARCHAR},
			#{record.anonymous,jdbcType=INTEGER},
			#{record.sellerReply,jdbcType=INTEGER},
			#{record.replyAt,jdbcType=VARCHAR},
			#{record.sellerReplyInfo,jdbcType=VARCHAR},
			#{record.existImage,jdbcType=INTEGER},
			#{record.commentImage,jdbcType=VARCHAR},
			#{record.hide,jdbcType=INTEGER},
			#{record.commentAt,jdbcType=VARCHAR},
			#{record.features,jdbcType=VARCHAR},
			#{record.createdAt},
			#{record.updatedAt},
			#{record.isDeleted},
			#{record.version},
			#{record.syncFlag},
			#{record.shares,jdbcType=INTEGER},
			#{record.orderType,jdbcType=INTEGER},
			#{record.itemInfo,jdbcType=VARCHAR},
			#{record.performanceType},#{record.masterDataSource},#{record.orderSubType}
			)
		</foreach>
	</insert>

	<insert id="replace" parameterType="com.zm.order.tool.server.model.OrderItemComment">
		replace into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		value
			(
			#{id,jdbcType=BIGINT},
			#{bizType,jdbcType=INTEGER},
			#{orderNo,jdbcType=VARCHAR},
			#{userId,jdbcType=BIGINT},
			#{sellerId,jdbcType=BIGINT},
			#{storeId,jdbcType=BIGINT},
			#{goodsId,jdbcType=BIGINT},
			#{itemId,jdbcType=VARCHAR},
			#{commentScore,jdbcType=INTEGER},
			#{commentInfo,jdbcType=VARCHAR},
			#{anonymous,jdbcType=INTEGER},
			#{sellerReply,jdbcType=INTEGER},
			#{replyAt,jdbcType=VARCHAR},
			#{sellerReplyInfo,jdbcType=VARCHAR},
			#{existImage,jdbcType=INTEGER},
			#{commentImage,jdbcType=VARCHAR},
			#{hide,jdbcType=INTEGER},
			#{commentAt,jdbcType=VARCHAR},
			#{features,jdbcType=VARCHAR},
			#{createdAt},
			#{updatedAt},
			#{isDeleted},
			#{version},
			#{syncFlag},
			#{shares,jdbcType=INTEGER},
			#{orderType,jdbcType=INTEGER},
			#{itemInfo,jdbcType=VARCHAR},
			#{performanceType},#{masterDataSource}

			)
	</insert>

	<update id="delete" parameterType="map">
		UPDATE
		<include refid="tb"/>
		<set>
			is_deleted = #{deleteType},
			updated_at = now(),
			version = version + 1
		</set>
		where id = #{id} and seller_id = #{sellerId}
	</update>


</mapper>