<?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.PerformanceSelfTakeDao">

    <resultMap id="PerformanceSelfTakeMap" type="com.zm.order.tool.server.model.PerformanceSelfTake">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
        <result column="store_id" jdbcType="BIGINT" property="storeId"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="self_take_name" jdbcType="VARCHAR" property="selfTakeName"/>
        <result column="self_take_mobile" jdbcType="VARCHAR" property="selfTakeMobile"/>
        <result column="self_take_code" jdbcType="BIGINT" property="selfTakeCode"/>
        <result column="code_url" jdbcType="VARCHAR" property="codeUrl"/>
        <result column="bar_code_url" jdbcType="VARCHAR" property="barCodeUrl"/>
        <result column="verify_type" jdbcType="INTEGER" property="verifyType"/>
        <result column="appointed_date_type" jdbcType="INTEGER" property="appointedDateType"/>
        <result column="self_take_start_at" jdbcType="TIMESTAMP" property="selfTakeStartAt"/>
        <result column="self_take_end_at" jdbcType="TIMESTAMP" property="selfTakeEndAt"/>
        <result column="pick_at" jdbcType="TIMESTAMP" property="pickAt"/>
        <result column="auto_verify_at" jdbcType="TIMESTAMP" property="autoVerifyAt"/>
        <result column="self_take_status" jdbcType="INTEGER" property="selfTakeStatus"/>
        <result column="unusual_reason" jdbcType="VARCHAR" property="unusualReason"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <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="biz_type" jdbcType="INTEGER" property="bizType"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        order_no, seller_id, store_id, user_id, self_take_name, self_take_mobile, self_take_code, code_url, bar_code_url,
        verify_type, appointed_date_type, self_take_start_at, self_take_end_at,
        pick_at, auto_verify_at, self_take_status, unusual_reason, remark, features, created_at, updated_at, is_deleted, biz_type
    </sql>

    <sql id="values">
        #{id}, #{orderNo}, #{sellerId}, #{storeId},
        #{userId}, #{selfTakeName}, #{selfTakeMobile}, #{selfTakeCode}, #{codeUrl}, #{barCodeUrl},
        #{verifyType}, #{appointedDateType}, #{selfTakeStartAt}, #{selfTakeEndAt}, #{pickAt}, #{autoVerifyAt},
        #{selfTakeStatus}, #{unusualReason}, #{remark}, #{features},
        now(), now(), 0, #{bizType}
    </sql>

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

    <insert id="insertList" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="order" index="index" separator=",">
            (
            #{order.id}, #{order.orderNo}, #{order.sellerId}, #{order.storeId},
            #{order.userId}, #{order.selfTakeName}, #{order.selfTakeMobile}, #{order.selfTakeCode},
            #{order.codeUrl}, #{order.barCodeUrl},
            #{order.verifyType}, #{order.appointedDateType}, #{order.selfTakeStartAt},
            #{order.selfTakeEndAt}, #{order.pickAt}, #{order.autoVerifyAt},
            #{order.selfTakeStatus}, #{order.unusualReason}, #{order.remark}, #{order.features},
            #{order.createdAt}, #{order.updatedAt}, #{order.isDeleted}, #{order.bizType}
            )
        </foreach>
    </insert>

    <update id="update" parameterType="com.zm.order.tool.server.model.PerformanceSelfTake">
        update
        <include refid="tb"/>
        <set>
            <if test="selfTakeName != null">
                self_take_name = #{selfTakeName},
            </if>
            <if test="selfTakeMobile != null">
                self_take_mobile = #{selfTakeMobile},
            </if>
            <if test="selfTakeCode != null">
                self_take_code = #{selfTakeCode},
            </if>
            <if test="codeUrl != null">
                code_url = #{codeUrl},
            </if>
            <if test="barCodeUrl != null">
                bar_code_url = #{barCodeUrl},
            </if>
            <if test="verifyType != null">
                verify_type = #{verifyType},
            </if>
            <if test="appointedDateType != null">
                appointed_date_type = #{appointedDateType},
            </if>
            <if test="selfTakeStartAt != null">
                self_take_start_at = #{selfTakeStartAt},
            </if>
            <if test="selfTakeEndAt != null">
                self_take_end_at = #{selfTakeEndAt},
            </if>
            <if test="pickAt != null">
                pick_at = #{pickAt},
            </if>
            <if test="autoVerifyAt != null">
                auto_verify_at = #{autoVerifyAt},
            </if>
            <if test="selfTakeStatus != null">
                self_take_status = #{selfTakeStatus},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateBatch" parameterType="com.zm.order.tool.server.model.PerformanceSelfTake">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = #{bizType},
            seller_id = #{sellerId},
            updated_at = now(),
            <trim prefix="store_id = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.storeId}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.id}
        </foreach>
        and user_id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.userId}
        </foreach>
    </update>

    <update id="deleteTemp" parameterType="map">
        delete from
        <include refid="tb"/>
        where user_id = #{userId}
        and order_no = #{orderNo}
    </update>

    <update id="delete" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = -1,
        updated_at = now()
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="realDeleteByOrderNo" parameterType="map">
        delete from
        <include refid="tb"/>
        where order_no = #{orderNo} and user_id = #{userId} limit 1
    </update>

    <select id="selectOne" parameterType="map" resultMap="PerformanceSelfTakeMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo}
        and user_id = #{userId}
        limit 1
    </select>

    <select id="selectByOne" parameterType="com.zm.order.tool.server.model.PerformanceSelfTake" resultMap="PerformanceSelfTakeMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            user_id = #{userId}
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="orderNo != null">
                AND order_no = #{orderNo}
            </if>
            <if test="sellerId != null">
                AND seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                AND store_id = #{storeId}
            </if>
        </where>
        limit 1
    </select>

    <update id="updateUpdatedAt" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <select id="selectList" parameterType="com.zm.order.tool.server.dto.QueryPerformanceDTO" resultMap="PerformanceSelfTakeMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            user_id = #{userId}
            <choose>
                <when test="orderNoList != null and orderNoList.size > 0">
                    AND order_no in
                    <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                        #{orderNo}
                    </foreach>
                </when>
                <otherwise>
                    <if test="orderNo != null">
                        AND order_no = #{orderNo}
                    </if>
                </otherwise>
            </choose>
            <if test="selfTakeCode != null">
                AND self_take_code = #{selfTakeCode}
            </if>
        </where>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
    </select>


    <select id="selectListByUserIdOrderNo" parameterType="com.zm.order.tool.server.dto.QueryPerformanceDTO" resultMap="PerformanceSelfTakeMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <choose>
                <when test="orderNoList != null and orderNoList.size > 0">
                    AND order_no in
                    <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                        #{orderNo}
                    </foreach>
                </when>
                <otherwise>
                    <if test="orderNo != null">
                        AND order_no = #{orderNo}
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="userIdList != null and userIdList.size > 0">
                    AND user_id in
                    <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
                        #{userId}
                    </foreach>
                </when>
                <otherwise>
                    <if test="userId != null">
                        AND  user_id = #{userId}
                    </if>
                </otherwise>
            </choose>
        </where>
    </select>

    <update id="updateByOrderNo" parameterType="map">
        update
        <include refid="tb"/>
        set
        <if test="selfTakeCode != null">
            self_take_code = #{selfTakeCode},
        </if>
        updated_at = now()
        where order_no = #{orderNo}
        and user_id = #{userId}
    </update>
</mapper>