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

    <!-- 根据分账单号查询 -->
    <select id="getBySplitNo" resultType="com.cool.store.entity.SplitOrderDO">
        select *
        from zxjp_split_order
        where split_no = #{splitNo}
    </select>

    <!-- 根据状态查询 -->
    <select id="listByStatus" resultType="com.cool.store.entity.SplitOrderDO">
        select *
        from zxjp_split_order
        where status = #{status}
        order by create_time desc
    </select>

    <!-- 根据关联单据号查询 -->
    <select id="listByRelatedDocNo" resultType="com.cool.store.entity.SplitOrderDO">
        select *
        from zxjp_split_order
        where related_doc_no = #{relatedDocNo}
        and status in
        <foreach collection="statusList" item="status" open="(" separator="," close=")">
            #{status}
        </foreach>
        order by create_time desc
    </select>

    <!-- 根据门店ID查询 -->
    <select id="listByStoreId" resultType="com.cool.store.entity.SplitOrderDO">
        select *
        from zxjp_split_order
        where related_store_id = #{storeId}
        order by create_time desc
    </select>

    <!-- 批量更新状态 -->
    <update id="batchUpdateStatus">
        update zxjp_split_order
        set status = #{status}
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <!-- 分页查询 -->
    <select id="pageQuery" resultType="com.cool.store.entity.SplitOrderDO">
        select *
        from zxjp_split_order a
        where deleted = 0
        <if test="request.splitNo != null and request.splitNo != ''">
            and split_no like concat('%', #{request.splitNo}, '%')
        </if>
        <if test="request.splitType != null">
            and split_type = #{request.splitType}
        </if>
        <if test="request.payerAccountName != null and request.payerAccountName != ''">
            and payer_account_name like concat('%', #{request.payerAccountName}, '%')
        </if>
        <if test="request.payeeAccountName != null and request.payeeAccountName != ''">
            and payee_account_name like concat('%', #{request.payeeAccountName}, '%')
        </if>
        <if test="request.expenseTypeCode != null and request.expenseTypeCode != ''">
            and expense_type_code = #{request.expenseTypeCode}
        </if>
        <if test="request.relatedStoreId != null and request.relatedStoreId != ''">
            and related_store_id = #{request.relatedStoreId}
        </if>
        <if test="request.relatedDocNo != null and request.relatedDocNo != ''">
            and related_doc_no like concat('%', #{request.relatedDocNo}, '%')
        </if>
        <if test="request.status != null and request.status != ''">
            and status = #{request.status}
        </if>
        <if test="request.source != null">
            and source = #{request.source}
        </if>
        order by create_time desc
    </select>

    <!-- 分页查询（关联门店信息） -->
    <select id="pageQueryWithStore" resultType="com.cool.store.response.SplitOrderResponse">
        SELECT
            a.id,
            a.split_no AS splitNo,
            a.split_type AS splitType,
            a.related_store_id AS relatedStoreId,
            b.store_name AS storeName,
            b.store_num AS storeNum,
            a.payer_account_name AS payerAccountName,
            a.payee_account_name AS payeeAccountName,
            a.payer_account_no AS payerAccountNo,
            a.payee_account_no AS payeeAccountNo,
            a.expense_type_code AS expenseTypeCode,
            a.split_amount AS splitAmount,
            a.related_doc_no AS relatedDocNo,
            a.remark,
            a.status,
            a.confirmer,
            a.confirm_time AS confirmTime,
            a.create_user_id AS createUserId,
            a.create_time AS createTime,
            a.source
        FROM zxjp_split_order a
        LEFT JOIN store_${enterpriseId} b ON a.related_store_id = b.store_id
        WHERE a.deleted = 0
        <if test="request.splitNo != null and request.splitNo != ''">
            AND a.split_no LIKE CONCAT('%', #{request.splitNo}, '%')
        </if>
        <if test="request.splitType != null">
            AND a.split_type = #{request.splitType}
        </if>
        <if test="request.payerAccountName != null and request.payerAccountName != ''">
            AND a.payer_account_name LIKE CONCAT('%', #{request.payerAccountName}, '%')
        </if>
        <if test="request.payeeAccountName != null and request.payeeAccountName != ''">
            AND a.payee_account_name LIKE CONCAT('%', #{request.payeeAccountName}, '%')
        </if>
        <if test="request.expenseTypeCode != null and request.expenseTypeCode != ''">
            AND a.expense_type_code = #{request.expenseTypeCode}
        </if>
        <if test="request.relatedStoreId != null and request.relatedStoreId != ''">
            AND a.related_store_id = #{request.relatedStoreId}
        </if>
        <if test="request.relatedDocNo != null and request.relatedDocNo != ''">
            AND a.related_doc_no LIKE CONCAT('%', #{request.relatedDocNo}, '%')
        </if>
        <if test="request.status != null and request.status != ''">
            AND a.status = #{request.status}
        </if>
        <if test="request.source != null">
            AND a.source = #{request.source}
        </if>
        <if test="request.keyword != null and request.keyword != ''">
            AND (b.store_name LIKE CONCAT('%', #{request.keyword}, '%') OR b.store_num LIKE CONCAT('%', #{request.keyword}, '%'))
        </if>
        ORDER BY a.create_time DESC
    </select>



</mapper>
