<?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.lframework.xingyun.settle.mappers.SettlePreSheetMapper">
    <resultMap id="SettlePreSheet" type="com.lframework.xingyun.settle.entity.SettlePreSheet">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="approve_by" property="approveBy"/>
        <result column="approve_time" property="approveTime"/>
        <result column="status" property="status"/>
        <result column="refuse_reason" property="refuseReason"/>
        <result column="settle_status" property="settleStatus"/>
    </resultMap>

    <resultMap id="SettlePreSheetFullDto" type="com.lframework.xingyun.settle.dto.pre.SettlePreSheetFullDto">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="total_amount" property="totalAmount"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="approve_by" property="approveBy"/>
        <result column="approve_time" property="approveTime"/>
        <result column="status" property="status"/>
        <result column="refuse_reason" property="refuseReason"/>
        <result column="settle_status" property="settleStatus"/>
        <collection property="details" ofType="com.lframework.xingyun.settle.dto.pre.SettlePreSheetFullDto$SheetDetailDto"
                    javaType="java.util.ArrayList">
            <id column="detail_id" property="id"/>
            <result column="detail_item_id" property="itemId"/>
            <result column="detail_amount" property="amount"/>
        </collection>
    </resultMap>

    <sql id="SettlePreSheetDto_sql">
        SELECT
            s.id,
            s.code,
            s.supplier_id,
            s.total_amount,
            s.description,
            s.create_by,
            s.create_time,
            s.update_by,
            s.update_time,
            s.approve_by,
            s.approve_time,
            s.status,
            s.refuse_reason,
            s.settle_status
        FROM settle_pre_sheet AS s
    </sql>

    <sql id="SettlePreSheetFullDto_sql">
        SELECT
            s.id,
            s.code,
            s.supplier_id,
            s.total_amount,
            s.description,
            s.create_by,
            s.create_time,
            s.update_by,
            s.update_time,
            s.approve_by,
            s.approve_time,
            s.status,
            s.refuse_reason,
            s.settle_status,
            d.id AS detail_id,
            d.item_id AS detail_item_id,
            d.amount AS detail_amount
        FROM settle_pre_sheet AS s
        LEFT JOIN settle_pre_sheet_detail AS d ON d.sheet_id = s.id
    </sql>

    <select id="query" resultMap="SettlePreSheet">
        <include refid="SettlePreSheetDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND s.code = #{vo.code}
                </if>
                <if test="vo.supplierId != null and vo.supplierId != ''">
                    AND s.supplier_id = #{vo.supplierId}
                </if>
                <if test="vo.status != null">
                    AND s.status = #{vo.status}
                </if>
                <if test="vo.createBy != null and vo.createBy != ''">
                    AND s.create_by_id = #{vo.createBy}
                </if>
                <if test="vo.approveBy != null and vo.approveBy != ''">
                    AND s.approve_by = #{vo.approveBy}
                </if>
                <if test="vo.createStartTime != null">
                    AND s.create_time >= #{vo.createStartTime}
                </if>
                <if test="vo.createEndTime != null">
                    <![CDATA[
                    AND s.create_time <= #{vo.createEndTime}
                    ]]>
                </if>
                <if test="vo.approveStartTime != null">
                    AND s.approve_time >= #{vo.approveStartTime}
                </if>
                <if test="vo.approveEndTime != null">
                    <![CDATA[
                    AND s.approve_time <= #{vo.approveEndTime}
                    ]]>
                </if>
                <if test="vo.settleStatus != null">
                    AND s.settle_status = #{vo.settleStatus}
                </if>
            </if>
        </where>
        ORDER BY s.create_time DESC
    </select>

    <select id="getDetail" resultMap="SettlePreSheetFullDto">
        <include refid="SettlePreSheetFullDto_sql"/>
        WHERE s.id = #{id}
        ORDER BY d.order_no
    </select>

    <select id="getApprovedList" resultMap="SettlePreSheet">
        <include refid="SettlePreSheetDto_sql"/>
        WHERE s.supplier_id = #{supplierId}
        <if test="startTime != null">
            AND s.approve_time >= #{startTime}
        </if>
        <if test="endTime != null">
            <![CDATA[
            AND s.approve_time <= #{endTime}
            ]]>
        </if>
        AND s.status = 3
        AND s.settle_status = #{settleStatus}
        ORDER BY s.approve_time DESC
    </select>
</mapper>
