<?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.jeeplus.test.cw.invoice.mapper.CwFinanceInvoiceMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
            <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
            <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
            <result property="delFlag" column="del_flag" jdbcType="INTEGER"/>
            <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="no" column="no" jdbcType="VARCHAR"/>
            <result property="billingType" column="billing_type" jdbcType="VARCHAR"/>
            <result property="billingWorkplaceReal" column="billing_workplace_real" jdbcType="VARCHAR"/>
            <result property="taxpayerIdentificationNo" column="taxpayer_identification_no" jdbcType="VARCHAR"/>
            <result property="address" column="address" jdbcType="VARCHAR"/>
            <result property="telPhone" column="tel_phone" jdbcType="VARCHAR"/>
            <result property="openBank" column="open_bank" jdbcType="VARCHAR"/>
            <result property="bankAccount" column="bank_account" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="receivablesType" column="receivables_type" jdbcType="VARCHAR"/>
            <result property="billingContent" column="billing_content" jdbcType="VARCHAR"/>
            <result property="account" column="account" jdbcType="VARCHAR"/>
            <result property="billingContentTerms" column="billing_content_terms" jdbcType="VARCHAR"/>
            <result property="billingPeople" column="billing_people" jdbcType="VARCHAR"/>
            <result property="billingDate" column="billing_date" jdbcType="TIMESTAMP"/>
            <result property="collectDate" column="collect_date" jdbcType="TIMESTAMP"/>
            <result property="billingPeopleReal" column="billing_people_real" jdbcType="VARCHAR"/>
            <result property="reconciliationPeople" column="reconciliation_people" jdbcType="VARCHAR"/>
            <result property="reconciliationArea" column="reconciliation_area" jdbcType="VARCHAR"/>
            <result property="billingWorkplaceRealId" column="billing_workplace_real_id" jdbcType="VARCHAR"/>
            <result property="billingPeopleId" column="billing_people_id" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="procInsId" column="proc_ins_id" jdbcType="VARCHAR"/>
            <result property="processDefinitionId" column="process_definition_id" jdbcType="VARCHAR"/>
            <result property="receivablesStatus" column="receivables_status" jdbcType="VARCHAR"/>
            <result property="invalidStatus" column="invalid_status" jdbcType="VARCHAR"/>
            <result property="receivablesDate" column="receivables_date" jdbcType="TIMESTAMP"/>
            <result property="operator" column="operator" jdbcType="VARCHAR"/>
            <result property="operatorOffice" column="operator_office" jdbcType="VARCHAR"/>
            <association property="financeInvoiceInvalidDTO" column="id" select="getFii" javaType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceInvalidDTO"></association>
            <collection property="financeInvoiceReceivablesDTOList" column="id" select="getFirList" ofType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceReceivablesDTO"></collection>
            <collection property="financeInvoiceBaseDTOList" column="id" select="getBaseList" ofType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceBaseDTO"></collection>
            <collection property="financeInvoiceDetailDTOList" column="id" select="getFidList" ofType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceDetailDTO"></collection>
            <collection property="workAttachmentDtoList" ofType="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto" column="id" select="getFileList"></collection>
    </resultMap>

    <resultMap id="AttachmentMap" type="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="VARCHAR"/>
        <result property="url" column="url" jdbcType="VARCHAR"/>
        <result property="name" column="attachment_name" jdbcType="VARCHAR"/>
        <result property="size" column="file_size" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="FIR_Column_List">
        fir.id,
        fir.create_by,
        fir.create_date,
        fir.update_by,
        fir.update_date,
        fir.del_flag,
        fir.remarks,
        fir.invoice_id,
        fir.remittance_unit,
        fir.remittance_amount,
        fir.remittance_date,
        fir.status
    </sql>

    <sql id="FII_Column_List">
        fii.id,
        fii.create_by,
        fii.create_date,
        fii.update_by,
        fii.update_date,
        fii.del_flag,
        fii.remarks,
        fii.invoice_id,
        fii.invalid_status,
        fii.proc_ins_id,
        fii.process_definition_id
    </sql>

    <sql id="FID_Column_List">
        fid.id,
        fid.create_by,
        fid.create_date,
        fid.update_by,
        fid.update_date,
        fid.del_flag,
        fid.remarks,
        fid.invoice_id,
        fid.code,
        fid.number,
        fid.account,
        fid.rate,
        fid.amount,
        fid.tax,
        fid.all_amount
    </sql>

    <sql id="Base_Column_List">
        fi.id,
        fi.create_by,
        fi.create_date,
        fi.update_by,
        fi.update_date,
        fi.del_flag,
        fi.remarks,
        fi.type,
        fi.no,
        fi.billing_type,
        fi.billing_workplace_real,
        fi.taxpayer_identification_no,
        fi.address,
        fi.tel_phone,
        fi.open_bank,
        fi.bank_account,
        fi.name,
        fi.receivables_type,
        fi.billing_content,
        fi.account,
        fi.billing_content_terms,
        fi.billing_people,
        fi.billing_date,
        fi.collect_date,
        fi.billing_people_real,
        fi.reconciliation_people,
        fi.reconciliation_area,
        fi.billing_workplace_real_id,
        fi.billing_people_id,
        fi.status,
        fi.proc_ins_id,
        fi.process_definition_id,
        fi.receivables_status,
        fi.invalid_status,
        fi.receivables_date
    </sql>

    <sql id="File_Column_List">
        wa.id,
        wa.create_by,
        wa.create_date,
        wa.update_by,
        wa.update_date,
        wa.remarks,
        wa.del_flag,
        wa.url,
        wa.type,
        wa.attachment_id,
        wa.attachment_name,
        wa.attachment_flag,
        wa.module_type,
        wa.attachment_type,
        wa.file_size,
        wa.sort,
        wa.description
    </sql>

    <sql id="FIB_Column_List">
        fib.id,
        fib.create_by,
        fib.create_date,
        fib.update_by,
        fib.update_date,
        fib.del_flag,
        fib.invoice_id,
        fib.program_id,
        fib.program_name,
        fib.contract_name,
        fib.program_no,
        fib.contract_id
    </sql>

    <select id="getFileList" resultMap="AttachmentMap">
        select
        <include refid="File_Column_List"></include>,
        su.name as create_name
        from work_attachment wa
        left join sys_user su on su.id = wa.create_by and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id}
    </select>

    <select id="getFii" resultType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceInvalidDTO">
        select
        <include refid="FII_Column_List"></include>
        from cw_finance_invoice_invalid fii
        where fii.del_flag = '0' and fii.invoice_id = ${id}
    </select>

    <select id="getFirList" resultType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceReceivablesDTO">
        select
        <include refid="FIR_Column_List"></include>
        from cw_finance_invoice_receivables fir
        where fir.del_flag = '0' and fir.invoice_id = ${id}
    </select>

    <select id="getFidList" resultType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceDetailDTO">
        select
        <include refid="FID_Column_List"></include>
        from cw_finance_invoice_detail fid
        where fid.del_flag = '0' and fid.invoice_id = ${id}
    </select>

    <select id="getBaseList" resultType="com.jeeplus.test.cw.invoice.service.dto.CwFinanceInvoiceBaseDTO">
        select
        <include refid="FIB_Column_List"></include>
        from cw_finance_invoice_base fib

        where fib.del_flag = '0' and fib.invoice_id = ${id}
    </select>
    <select id="findList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        su.name as operator,
        so.name as operator_office
        from cw_finance_invoice fi
        left join sys_user su on fi.create_by = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>
        from cw_finance_invoice fi
        where fi.del_flag = '0' and fi.id = ${id}
    </select>
</mapper>
