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

    <!-- 根据调整单号查询 -->
    <select id="getByAdjustmentNo" resultType="com.cool.store.entity.AdjustmentOrderDO">
        select *
        from zxjp_adjustment_order
        where adjustment_no = #{adjustmentNo}
    </select>

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

    <!-- 根据关联分账单号查询 -->
    <select id="listByRelatedSplitNo" resultType="com.cool.store.entity.AdjustmentOrderDO">
        select *
        from zxjp_adjustment_order
        where related_split_no = #{relatedSplitNo}
    </select>

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

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

    <!-- 根据ID更新关联分账单号 -->
    <update id="updateRelatedSplitNo">
        update zxjp_adjustment_order
        set related_split_no = #{relatedSplitNo}
        where id = #{id}
    </update>

    <!-- 分页查询 -->
    <select id="pageQuery" resultType="com.cool.store.response.AdjustmentOrderResponse">
        select
            a.*,
            b.store_name as storeName,
            b.store_num as storeNum,
            a.pay_time as payTime
         from zxjp_adjustment_order a
          left join store_${enterpriseId} b
        on a.store_id = b.store_id
        where a.deleted = 0
        <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>
        <if test="request.adjustmentNo != null and request.adjustmentNo != ''">
            and a.adjustment_no like concat('%', #{request.adjustmentNo}, '%')
        </if>
        <if test="request.storeId != null and request.storeId != ''">
            and a.store_id = #{request.storeId}
        </if>
        <if test="request.expenseTypeCode != null and request.expenseTypeCode != ''">
            and a.expense_type_code = #{request.expenseTypeCode}
        </if>
        <if test="request.adjustType != null">
            and a.adjust_type = #{request.adjustType}
        </if>
        <if test="request.statusList != null and request.statusList.size() > 0">
            and a.status in
            <foreach collection="request.statusList" item="status" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
        <if test="request.adjustReason != null and request.adjustReason != ''">
            and a.adjust_reason like concat('%', #{request.adjustReason}, '%')
        </if>
        <if test="request.businessTypeCode != null and request.businessTypeCode != ''">
            and a.business_type_code = #{request.businessTypeCode}
        </if>
        <if test="request.claimStartTime != null">
            and a.claim_time &gt;= #{request.claimStartTime}
        </if>
        <if test="request.claimEndTime != null">
            and a.claim_time &lt;= #{request.claimEndTime}
        </if>
        <if test="request.expenseSheetType != null and request.expenseSheetType != ''">
            and a.expense_sheet_type = #{request.expenseSheetType}
        </if>

        order by create_time desc
    </select>

</mapper>
