<?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.SourceNoIndexDao">
    <resultMap id="SourceNoIndexMap" type="com.zm.order.tool.server.model.SourceNoIndex">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
        <result column="source_no" jdbcType="VARCHAR" property="sourceNo"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="order_id" jdbcType="BIGINT" property="orderId"/>
        <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">
        source_index
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
      source_no, seller_id, biz_type, user_id, order_id, created_at, updated_at, is_deleted
    </sql>

    <sql id="values">
    #{id}, #{sourceNo}, #{sellerId}, #{bizType},
      #{userId}, #{orderId}, now(), now(), 0
  </sql>

    <select id="selectBySourceNo" parameterType="map" resultMap="SourceNoIndexMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where
        source_no= #{sourceNo}
        and seller_id = #{sellerId}
    </select>


    <insert id="insert" parameterType="com.zm.order.tool.server.model.SourceNoIndex">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
  </insert>



    <update id="update" parameterType="com.zm.order.tool.server.model.SourceNoIndex">
        update
        <include refid="tb"/>
        <set>
            <if test="userId != null">
                user_id = #{userId},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and source_no = #{sourceNo}
    </update>

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