<?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.gic.haoban.manage.service.dao.mapper.contact.TabContactOrderMapper">

    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.contact.TabContactOrder">
        <id column="id" property="id" />
        <result column="order_id" property="orderId" />
        <result column="member_id" property="memberId" />
        <result column="order_number" property="orderNumber" />
        <result column="paid_amount" property="paidAmount" />
        <result column="product_count" property="productCount" />
        <result column="goods_info" property="goodsInfo" />
        <result column="pay_time" property="payTime" />
        <result column="receipts_date" property="receiptsDate" />
        <result column="channel_code" property="channelCode" />
        <result column="channel_type" property="channelType" />
        <result column="clerk_id" property="clerkId" />
        <result column="clerk_code" property="clerkCode" />
        <result column="clerk_name" property="clerkName" />
        <result column="store_id" property="storeId" />
        <result column="contact_log_id" property="contactLogId" />
        <result column="contact_time" property="contactTime" />
        <result column="potential_time" property="potentialTime" />
        <result column="delete_flag" property="deleteFlag" />
        <result column="enterprise_id" property="enterpriseId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <sql id="Base_Column_List">
        id, order_id, member_id, order_number, paid_amount, product_count, goods_info, pay_time, receipts_date, channel_code, channel_type, clerk_id, clerk_code,clerk_name, store_id, contact_log_id, contact_time, potential_time, delete_flag, enterprise_id, create_time, update_time
    </sql>

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

    <select id="queryByIds" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tab_contact_order
        WHERE id IN
        <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="queryContactOrder" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM
        tab_contact_order
        WHERE
        enterprise_id = #{enterpriseId}
        <if test="search.memberId != null and search.memberId != ''">
            AND member_id = #{search.memberId}
        </if>
        <if test="search.clerkId != null and search.clerkId != ''">
            AND clerk_id = #{search.clerkId}
        </if>
        <if test="search.storeIdList != null and search.storeIdList.size() > 0">
            AND store_id IN
            <foreach item="storeId" index="index" collection="search.storeIdList" open="(" separator="," close=")">
                #{storeId}
            </foreach>
        </if>
        <if test="search.clerkSearch != null and search.clerkSearch != ''">
            AND (clerk_code LIKE CONCAT('%', #{search.clerkSearch}, '%') OR clerk_name LIKE CONCAT('%', #{search.clerkSearch}, '%'))
        </if>
        <if test="search.beginTime != null">
            AND receipts_date >= #{search.beginTime}
        </if>
        <if test="search.endTime != null">
            AND receipts_date <![CDATA[<=]]> #{search.endTime}
        </if>
        ORDER BY receipts_date DESC,id desc
    </select>

    <select id="count" resultType="long">
        SELECT COUNT(*)
        FROM tab_contact_order
        <where>
            <if test="orderId != null">
                AND order_id = #{orderId}
            </if>
            <if test="memberId != null">
                AND member_id = #{memberId}
            </if>
            <if test="enterpriseId != null">
                AND enterprise_id = #{enterpriseId}
            </if>
        </where>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO tab_contact_order (order_id, member_id, order_number, paid_amount, product_count, goods_info, pay_time, receipts_date, channel_code, channel_type, clerk_id, clerk_code,clerk_name, store_id, contact_log_id, contact_time, potential_time, enterprise_id)
        VALUES (#{orderId}, #{memberId}, #{orderNumber}, #{paidAmount}, #{productCount}, #{goodsInfo}, #{payTime}, #{receiptsDate}, #{channelCode}, #{channelType}, #{clerkId}, #{clerkCode},#{clerkName}, #{storeId}, #{contactLogId}, #{contactTime}, #{potentialTime}, #{enterpriseId})
    </insert>

    <update id="update">
        UPDATE tab_contact_order
        <set>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="memberId != null">member_id = #{memberId},</if>
            <if test="orderNumber != null">order_number = #{orderNumber},</if>
            <if test="paidAmount != null">paid_amount = #{paidAmount},</if>
            <if test="productCount != null">product_count = #{productCount},</if>
            <if test="goodsInfo != null">goods_info = #{goodsInfo},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="receiptsDate != null">receipts_date = #{receiptsDate},</if>
            <if test="channelCode != null">channel_code = #{channelCode},</if>
            <if test="channelType != null">channel_type = #{channelType},</if>
            <if test="clerkId != null">clerk_id = #{clerkId},</if>
            <if test="clerkCode != null">clerk_code = #{clerkCode},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="contactLogId != null">contact_log_id = #{contactLogId},</if>
            <if test="contactTime != null">contact_time = #{contactTime},</if>
            <if test="potentialTime != null">potential_time = #{potentialTime},</if>
            <if test="deleteFlag != null">delete_flag = #{deleteFlag},</if>
            <if test="enterpriseId != null">enterprise_id = #{enterpriseId},</if>
            update_time = #{updateTime}
        </set>
        WHERE id = #{id}
    </update>

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