<?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.order.PreAllocationRecordMapper">

    <resultMap id="BaseResultMap" type="com.cool.store.entity.order.PreAllocationRecordDO">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="order_id" property="orderId" jdbcType="BIGINT"/>
        <result column="shop_id" property="shopId" jdbcType="BIGINT"/>
        <result column="pay_no" property="payNo" jdbcType="VARCHAR"/>
        <result column="expense_type" property="expenseType" jdbcType="VARCHAR"/>
        <result column="payee_name" property="payeeName" jdbcType="VARCHAR"/>
        <result column="payee_code" property="payeeCode" jdbcType="VARCHAR"/>
        <result column="pay_amount" property="payAmount" jdbcType="DECIMAL"/>
        <result column="allocation_status" property="allocationStatus" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="deleted" property="deleted" jdbcType="TINYINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, order_id, shop_id, pay_no, expense_type, payee_name, payee_code,
        pay_amount, allocation_status, create_time, update_time, remark, deleted
    </sql>

    <sql id="Not_Deleted_Condition">
        deleted = 0
    </sql>

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

    <select id="queryPageByOrderId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_pre_allocation_record
        WHERE order_id = #{orderId}
          AND <include refid="Not_Deleted_Condition"/>
        ORDER BY create_time DESC
    </select>

    <select id="queryPageByShopId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_pre_allocation_record
        WHERE shop_id = #{shopId}
          AND <include refid="Not_Deleted_Condition"/>
        ORDER BY create_time DESC
    </select>

    <select id="queryPageByShopIdAndExpenseType" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_pre_allocation_record
        WHERE shop_id = #{shopId}
          AND expense_type = #{expenseType}
        ORDER BY create_time DESC
        limit 1
    </select>

    <select id="queryPageByPayNo" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"/>
        FROM zxjp_pre_allocation_record
        WHERE pay_no = #{payNo}
          AND <include refid="Not_Deleted_Condition"/>
        ORDER BY create_time DESC
    </select>

    <update id="updateStatus">
        UPDATE zxjp_pre_allocation_record
        SET allocation_status = #{status},
            update_time = NOW()
        WHERE id = #{id}
    </update>


    <delete id="deleteByShopId">
        DELETE FROM zxjp_pre_allocation_record WHERE shop_id = #{shopId}
    </delete>

    <insert id="insertBatch">
        INSERT INTO zxjp_pre_allocation_record
        (order_id,shop_id,pay_no,expense_type,payee_name,payee_code,pay_amount,allocation_status,create_time)
        VALUES
        <foreach collection="list" item="i" separator=",">
            (#{i.orderId},#{i.shopId},#{i.payNo},#{i.expenseType},#{i.payeeName},#{i.payeeCode},
            #{i.payAmount},#{i.allocationStatus},NOW())
        </foreach>
    </insert>

    <update id="updateBatch">
        <foreach collection="list" item="i" separator=";">
            UPDATE zxjp_pre_allocation_record
            SET pay_no = #{i.payNo},
                allocation_status = #{i.allocationStatus},
                update_time = NOW()
            WHERE id = #{i.id}
        </foreach>
    </update>

    <update id="batchUpdateEditableFields">
        <foreach collection="list" item="i" separator=";">
            UPDATE zxjp_pre_allocation_record
            SET expense_type = #{i.expenseType},
                payee_name = #{i.payeeName},
                payee_code = #{i.payeeCode},
                pay_amount = #{i.payAmount},
                update_time = NOW()
            WHERE id = #{i.id}
              AND allocation_status != 1
        </foreach>
    </update>

    <update id="updateByOrderId">
        <foreach collection="list" item="i" separator=";">
            UPDATE zxjp_pre_allocation_record
            SET pay_no = #{i.payNo},
            allocation_status = #{i.allocationStatus},
            update_time = NOW()
            where order_id = #{orderId}
        </foreach>
    </update>

    <update id="updateByPayNoList">
            UPDATE zxjp_pre_allocation_record
            SET allocation_status = #{status},
            update_time = NOW()
            where pay_no in
            <foreach collection="payNoList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
    </update>

    <delete id="deleteByIdsNotPaid">
        DELETE FROM zxjp_pre_allocation_record
        WHERE allocation_status != 1
          AND id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="softDeleteById">
        UPDATE zxjp_pre_allocation_record
        SET deleted = 1,
            update_time = NOW()
        WHERE id = #{id}
          AND <include refid="Not_Deleted_Condition"/>
    </update>

    <update id="softDeleteByIds">
        UPDATE zxjp_pre_allocation_record
        SET deleted = 1,
            update_time = NOW()
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
          AND <include refid="Not_Deleted_Condition"/>
    </update>

</mapper>
