<?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.cool.store.mapper.ShopDeliveryPlanMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.plan.ShopDeliveryPlanDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="shop_id" property="shopId" jdbcType="BIGINT"/>
        <result column="expected_delivery_time" property="expectedDeliveryTime" jdbcType="TIMESTAMP"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="create_user" property="createUser" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, shop_id, expected_delivery_time, remark, create_user, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM zxjp_shop_delivery_plan
        WHERE id = #{id}
    </select>

    <select id="getByShopId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM zxjp_shop_delivery_plan
        WHERE shop_id = #{shopId}
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id" parameterType="com.cool.store.entity.plan.ShopDeliveryPlanDO">
        INSERT INTO zxjp_shop_delivery_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="shopId != null">shop_id,</if>
            <if test="expectedDeliveryTime != null">expected_delivery_time,</if>
            <if test="remark != null">remark,</if>
            <if test="createUser != null">create_user,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="shopId != null">#{shopId,jdbcType=BIGINT},</if>
            <if test="expectedDeliveryTime != null">#{expectedDeliveryTime,jdbcType=TIMESTAMP},</if>
            <if test="remark != null">#{remark,jdbcType=VARCHAR},</if>
            <if test="createUser != null">#{createUser,jdbcType=VARCHAR},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP},</if>
        </trim>
    </insert>

    <update id="update" parameterType="com.cool.store.entity.plan.ShopDeliveryPlanDO">
        UPDATE zxjp_shop_delivery_plan
        <set>
            <if test="shopId != null">shop_id = #{shopId,jdbcType=BIGINT},</if>
            <if test="expectedDeliveryTime != null">expected_delivery_time = #{expectedDeliveryTime,jdbcType=TIMESTAMP},</if>
            <if test="remark != null">remark = #{remark,jdbcType=VARCHAR},</if>
            <if test="createUser != null">create_user = #{createUser,jdbcType=VARCHAR},</if>
            <if test="updateTime != null">update_time = #{updateTime,jdbcType=TIMESTAMP},</if>
        </set>
        WHERE id = #{id,jdbcType=BIGINT}
    </update>

    <delete id="deleteById">
        DELETE FROM zxjp_shop_delivery_plan
        WHERE id = #{id}
    </delete>

    <select id="listAll" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM zxjp_shop_delivery_plan
        ORDER BY create_time DESC
    </select>

    <select id="getPendingList" resultType="com.cool.store.vo.desk.DeliveryPendingVO">
        select
            a.shop_id as shopId,
            c.shop_name as shopName,
            c.line_id as lineId,
            c.region_id as regionId,
            c.invest_region_id as investRegionId,
            c.detail_address as address,
            b.item_type as itemType,
            b.delivery_status as deliveryStatus
        from zxjp_shop_delivery_plan a
        left join zxjp_shop_delivery_plan_item b on a.id = b.plan_id
        left join xfsg_shop_info c on a.shop_id = c.id
        <where>
            and b.delivery_status = 0
            <if test="keyword!=null and keyword!=''">
                and (c.shop_name like concat('%', #{keyword}, '%') or c.shop_code like concat('%', #{keyword}, '%'))
            </if>
            <if test="type != null">
                and item_type = #{type}
            </if>
        </where>
    </select>


</mapper>