<?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.finance.invoice.mapper.CwFinanceInvoiceMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
            <result property="updateTime" column="update_time" 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"/>
            <result property="invalidTaskId" column="invalid_task_id" jdbcType="VARCHAR"/>
            <result property="taskId" column="task_id" jdbcType="VARCHAR"/>
            <result property="isMultiple" column="is_multiple" jdbcType="VARCHAR"/>
            <result property="billingId" column="billing_id" jdbcType="VARCHAR"/>
            <result property="redInvoiceFlag" column="red_invoice_flag" jdbcType="VARCHAR"/>
            <result property="redInvoiceRelevancyId" column="red_invoice_relevancy_id" jdbcType="VARCHAR"/>
            <result property="redInvoiceRelevancyNumber" column="red_invoice_relevancy_number" jdbcType="VARCHAR"/>

            <association property="financeInvoiceInvalidDTO" column="id" select="getFii" javaType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceInvalidDTO"></association>
            <collection property="financeInvoiceReceivablesDTOList" column="id" select="getFirList" ofType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceReceivablesDTO"></collection>
            <collection property="financeInvoiceBaseDTOList" column="id" select="getBaseList" ofType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceBaseDTO"></collection>
            <collection property="financeInvoiceDetailDTOList" column="id" select="getFidList" ofType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDetailDTO"></collection>
        <collection property="workAttachmentDtoList" ofType="com.jeeplus.sys.domain.WorkAttachmentInfo" column="id" select="getFileList"></collection>
        <collection property="workAttachmenSignContract" ofType="com.jeeplus.sys.domain.WorkAttachmentInfo" column="id" select="getFileListSignContarct"></collection>
    </resultMap>
    <resultMap id="BaseExportResultMap" type="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDTO">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
            <result property="updateTime" column="update_time" 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"/>
            <result property="invalidTaskId" column="invalid_task_id" jdbcType="VARCHAR"/>
            <result property="taskId" column="task_id" jdbcType="VARCHAR"/>
            <result property="isMultiple" column="is_multiple" jdbcType="VARCHAR"/>
            <result property="billingId" column="billing_id" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="AttachmentMap" type="com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy.id" column="create_by_id" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy.id" column="update_by_id" jdbcType="VARCHAR"/>
        <result property="createBy.name" column="create_name" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" 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_id,
        fir.create_time,
        fir.update_by_id,
        fir.update_time,
        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_id,
        fii.create_time,
        fii.update_by_id,
        fii.update_time,
        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_id,
        fid.create_time,
        fid.update_by_id,
        fid.update_time,
        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_id,
        fi.create_time,
        fi.update_by_id,
        fi.update_time,
        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,
        ifnull(fi.account,"0") as "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,
        fi.is_multiple,
        fi.billing_id,
        fi.report_type,
        fi.actual_drawer_email_address,
        fi.red_invoice_flag,
        fi.red_invoice_relevancy_id,
        fi.red_invoice_relevancy_number
    </sql>

    <sql id="File_Column_List">
        wa.id,
        wa.create_by_id,
        wa.create_time,
        wa.update_by_id,
        wa.update_time,
        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_id,
        fib.create_time,
        fib.update_by_id,
        fib.update_time,
        fib.del_flag,
        fib.invoice_id,
        fib.program_id,
        fib.program_name,
        fib.contract_name,
        fib.program_no,
        fib.contract_id,
        fib.account
    </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_id and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id} and wa.attachment_flag = 'cw_invoice'
    </select>

    <select id="getFileListSignContarct" 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_id and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id} and wa.attachment_flag = 'cw_invoice_sign_contract'
    </select>

    <select id="getFii" resultType="com.jeeplus.finance.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.finance.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.finance.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.finance.invoice.service.dto.CwFinanceInvoiceBaseDTO">
        select
        <include refid="FIB_Column_List"></include>
        ,rpl.report_no
        from cw_finance_invoice_base fib
        left join cw_project_report_new_line rpl on fib.program_id=rpl.report_id
        where fib.del_flag = '0' and fib.invoice_id = #{id}
    </select>
    <select id="findList" resultMap="BaseResultMap">
        select DISTINCT a.id as 'baseId',
        <include refid="Base_Column_List"></include>,
        su.name as operator,
        so.name as operator_office,
        d.ID_ AS task_id,
        (
        case
                 when a.type = '0' then (concat('其他：',a.program_name))
                 when a.type = '1' then (select rpr.project_name from cw_project_records rpr where id = a.program_id)
                 when a.type = '2' then (select rpr.project_name from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
                 when a.type = '4' then (select rpr.project_name from cw_project_records rpr where id = a.program_id)
            else '' end
        ) as programName,

        COALESCE(NULLIF(TRIM(fi.report_type), ''),
        case
                 when a.type = '0' then (fi.report_type)
                 when a.type = '1' then (select rpr.report_type from cw_project_records rpr where id = a.program_id)
                 when a.type = '4' then (select rpr.report_type from cw_project_records rpr where id = a.program_id)
                 when a.type = '2' then (select rpr.report_type from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
            else '' end
        ) as reportType,
        (
        case
            when a.type = '0' then ("")
            when a.type = '1' then (select rpr.project_classification from cw_project_records rpr where id = a.program_id)
            when a.type = '4' then (select rpr.project_classification from cw_project_records rpr where id = a.program_id)
            when a.type = '2' then (select rpr.project_classification from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
            else '' end
        ) as projectClassification,
        (
        CASE WHEN a.type = '0' THEN ( "")
        WHEN a.type = '1' THEN ( select GROUP_CONCAT(cprnlz.report_no) from cw_project_report cprz
        left join cw_project_report_new_line cprnlz on cprz.id = cprnlz.report_id
        left join cw_project_records cprsz on cprz.project_id= cprsz.id
        where cprsz.id = a.program_id )
        WHEN a.type = '2' THEN (
        select cprnlz.report_no from cw_project_report cprz
        left join cw_project_report_new_line cprnlz on cprz.id = cprnlz.report_id
        where cprz.id = a.program_id)
        WHEN a.type = '4' THEN ( select GROUP_CONCAT(cprnlz.report_no) from cw_project_report cprz
        left join cw_project_report_new_line cprnlz on cprz.id = cprnlz.report_id
        left join cw_project_records cprsz on cprz.project_id= cprsz.id
        where cprsz.id = a.program_id )ELSE ''
        END
        ) AS reportNos,
        cfid.number as "number",
        ifnull(cfid.account,"0") as "accountDetail",
        a.program_id,
        pr.is_pre_invoice as isPreInvoice,
        a.type as 'recordType'
        from cw_finance_invoice_base a
        left join cw_finance_invoice fi on a.invoice_id = fi.id
        left join cw_finance_invoice_detail cfid on cfid.invoice_id = fi.id and cfid.del_flag = '0'
        left join sys_user su on fi.create_by_id = su.id and su.del_flag = '0'
        left join sys_user su1 on fi.reconciliation_people = su1.id and su1.del_flag = '0'
        left join sys_user_manage_office sumo on sumo.office_id = su.office_id
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        LEFT JOIN act_ru_task d ON fi.proc_ins_id = d.PROC_INST_ID_
        LEFT JOIN cw_project_records pr on a.program_id=pr.id
        ${ew.customSqlSegment}
--         ORDER BY
--         fi.create_time DESC, cfid.number asc
    </select>

<select id="findExportList" resultMap="BaseExportResultMap">
        select DISTINCT a.id as 'baseId',
    ifnull(a.account,"0") as "baseAccount",
        <include refid="Base_Column_List"></include>,
        su.name as operator,
        so.name as operator_office,
        d.ID_ AS task_id,
        (
        case
                 when a.type = '0' then (concat('其他：',a.program_name))
                 when a.type = '1' then (select rpr.project_name from cw_project_records rpr where id = a.program_id)
                 when a.type = '2' then (select rpr.project_name from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
                 when a.type = '4' then (select rpr.project_name from cw_project_records rpr where id = a.program_id)
            else '' end
        ) as programName,

        (
        case
                 when a.type = '0' then (fi.report_type)
                 when a.type = '1' then (select rpr.report_type from cw_project_records rpr where id = a.program_id)
                 when a.type = '2' then (select rpr.report_type from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
                 when a.type = '4' then (select rpr.report_type from cw_project_records rpr where id = a.program_id)
    else '' end
        ) as reportType,
    (
    case
    when a.type = '0' then ("")
    when a.type = '1' then (select rpr.project_classification from cw_project_records rpr where id = a.program_id)
    when a.type = '2' then (select rpr.project_classification from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
    when a.type = '4' then (select rpr.project_classification from cw_project_records rpr where id = a.program_id)
    else '' end
    ) as projectClassification,
    (
    CASE WHEN a.type = '0' THEN ( "")
    WHEN a.type = '1' THEN ( select GROUP_CONCAT(cprnlz.report_no) from cw_project_report cprz
    left join cw_project_report_new_line cprnlz on cprz.id = cprnlz.report_id
    left join cw_project_records cprsz on cprz.project_id= cprsz.id
    where cprsz.id = a.program_id )
    WHEN a.type = '2' THEN (
    select cprnlz.report_no from cw_project_report cprz
    left join cw_project_report_new_line cprnlz on cprz.id = cprnlz.report_id
    where cprz.id = a.program_id)
    WHEN a.type = '4' THEN ("")
     ELSE ''
    END
    ) AS reportNos,
        cfid.number as "number",
        ifnull(cfid.account,"0") as "accountDetail",
        a.program_id,
        a.type as 'recordType',
        (case when a.type = '1' then (select pr.is_pre_invoice from cw_project_records pr where pr.id = a.program_id)
        when a.type = '2' then (select rpr.is_pre_invoice from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
        when a.type = '4' then (select pr.is_pre_invoice from cw_project_records pr where pr.id = a.program_id)
        else '' end ) as isPreInvoice
        from cw_finance_invoice_base a
        left join cw_finance_invoice fi on a.invoice_id = fi.id
        left join cw_finance_invoice_detail cfid on cfid.invoice_id = fi.id and cfid.del_flag = '0'
        left join sys_user su on fi.create_by_id = su.id and su.del_flag = '0'
        left join sys_user_manage_office sumo on sumo.office_id = su.office_id
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        LEFT JOIN act_ru_task d ON fi.proc_ins_id = d.PROC_INST_ID_
        ${ew.customSqlSegment}
        /*ORDER BY
        fi.create_time DESC, cfid.number asc*/
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        su.name as reconciliationPeopleName,
        su1.name as billingPeopleRealName
        from cw_finance_invoice fi
        left join cw_finance_invoice_base fib on fib.invoice_id = fi.id
        left join cw_project_report_new_line rpl on fib.program_id=rpl.report_id
        left join sys_user su on fi.reconciliation_people = su.id
        left join sys_user su1 on fi.billing_people_real = su1.id
        left join cw_project_records pr on fib.program_id=pr.id
        where fi.del_flag = '0' and fi.id = #{id}
    </select>

    <select id="getIdByProjectId" resultType="string">
        select
            invoice_id
        from cw_finance_invoice_base cw_fib
        where cw_fib.del_flag = '0' and cw_fib.program_id = #{id}
    </select>

    <select id="getByIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        su.name as operator
        from cw_finance_invoice fi
        left join sys_user su on fi.create_by_id = su.id and su.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="getIdByContractId" resultType="string">
        select
        a.invoice_id
        from
        (select distinct
            cw_fib.invoice_id
        from cw_finance_invoice_base cw_fib
        where cw_fib.del_flag = '0'
        and cw_fib.program_id in (select cw_pr.id from cw_project_records cw_pr where cw_pr.contract_id = #{id} and cw_pr.del_flag = '0')
        union
        select distinct
            fi.invoice_id
        from cw_finance_invoice_base fi
        where (fi.program_id is null or fi.program_id = '')
        and fi.contract_id = #{id}
        AND fi.del_flag = '0') a
    </select>

    <select id="getIdByClientId" resultType="string">
        select
            cw_fi.id
        from cw_finance_invoice cw_fi
        where cw_fi.del_flag = '0' and cw_fi.billing_workplace_real_id = #{id}
    </select>

    <update id="setReceivablesDateNull" parameterType="string">
        update cw_finance_invoice set receivables_date = null where id = #{id}
    </update>

    <update id="updateInfoById">
        update cw_finance_invoice set
        <if test="null != delFlag and delFlag != ''">
            del_flag = #{delFlag},
        </if>
        <if test="null != type and type != ''">
            type = #{type},
        </if>
        <if test="null != no and no != ''">
            no = #{no},
        </if>
        <if test="null != billingType and billingType != ''">
            billing_type = #{billingType},
        </if>
        <if test="null != billingWorkplaceReal and billingWorkplaceReal != ''">
            billing_workplace_real = #{billingWorkplaceReal},
        </if>
        <if test="null != taxpayerIdentificationNo and taxpayerIdentificationNo != ''">
            taxpayer_identification_no = #{taxpayerIdentificationNo},
        </if>
        <if test="null != address and address != ''">
            address = #{address},
        </if>
        <if test="null != telPhone and telPhone != ''">
            tel_phone = #{telPhone},
        </if>
        <if test="null != openBank and openBank != ''">
            open_bank = #{openBank},
        </if>
        <if test="null != bankAccount and bankAccount != ''">
            bank_account = #{bankAccount},
        </if>
        <if test="null != name and name != ''">
            name = #{name},
        </if>
        <if test="null != receivablesType and receivablesType != ''">
            receivables_type = #{receivablesType},
        </if>
        <if test="null != billingContent and billingContent != ''">
            billing_content = #{billingContent},
        </if>
        <if test="null != account and account != ''">
            account = #{account},
        </if>
        <if test="null != billingContentTerms and billingContentTerms != ''">
            billing_content_terms = #{billingContentTerms},
        </if>
        <if test="null != billingPeople and billingPeople != ''">
            billing_people = #{billingPeople},
        </if>
        <if test="null != billingDate">
            billing_date = #{billingDate},
        </if>
        <if test="null != collectDate">
            collect_date = #{collectDate},
        </if>
        <if test="null != billingPeopleReal and billingPeopleReal != ''">
            billing_people_real = #{billingPeopleReal},
        </if>
        <if test="null != reconciliationPeople and reconciliationPeople != ''">
            reconciliation_people = #{reconciliationPeople},
        </if>
        <if test="null != reconciliationArea and reconciliationArea != ''">
            reconciliation_area = #{reconciliationArea},
        </if>
        <if test="null != billingWorkplaceRealId and billingWorkplaceRealId != ''">
            billing_workplace_real_id = #{billingWorkplaceRealId},
        </if>
        <if test="null != billingPeopleId and billingPeopleId != ''">
            billing_people_id = #{billingPeopleId},
        </if>
        <if test="null != status and status != ''">
            status = #{status},
        </if>
        <if test="null != procInsId and procInsId != ''">
            proc_ins_id = #{procInsId},
        </if>
        <if test="null != processDefinitionId and processDefinitionId != ''">
            process_definition_id = #{processDefinitionId},
        </if>
        <if test="null != receivablesStatus and receivablesStatus != ''">
            receivables_status = #{receivablesStatus},
        </if>
        <if test="null != invalidStatus and invalidStatus != ''">
            invalid_status = #{invalidStatus},
        </if>
        <if test="null != receivablesDate">
            receivables_date = #{receivablesDate},
        </if>
        <if test="null != isMultiple and isMultiple != ''">
            is_multiple = #{isMultiple},
        </if>
        <if test="null != billingId and billingId != ''">
            billing_id = #{billingId},
        </if>
        <if test="null != reportType and reportType != ''">
            report_type = #{reportType},
        </if>
        <if test="null != actualDrawerEmailAddress and actualDrawerEmailAddress != ''">
            actual_drawer_email_address = #{actualDrawerEmailAddress},
        </if>
        red_invoice_flag = #{redInvoiceFlag},
        red_invoice_relevancy_id = #{redInvoiceRelevancyId},
        red_invoice_relevancy_number = #{redInvoiceRelevancyNumber},

        remarks = #{remarks}
        where id = #{id}
    </update>

    <select id="listByProgramId" resultType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDTO">
        select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        su.name as createName
        from cw_finance_invoice fi
        left join cw_finance_invoice_base fib on fib.invoice_id = fi.id
        left join sys_user su on fi.create_by_id = su.id
        ${ew.customSqlSegment}
    </select>

    <select id="getByNo" resultType="com.jeeplus.finance.invoice.domain.CwFinanceInvoiceBase">
        select
        DISTINCT
        <include refid="FIB_Column_List"></include>
        from cw_finance_invoice fi
        left join cw_finance_invoice_base fib on fib.invoice_id = fi.id
        where fi.no = #{invoiceNumber}
    </select>

    <select id="getCountByNo" resultType="integer">
        SELECT COUNT(*)
        FROM cw_finance_invoice fi
        LEFT JOIN cw_project_report pr ON pr.invoice_number = fi.no AND pr.del_flag = '0'
        LEFT JOIN cw_project_records cpr ON cpr.id = pr.project_id AND cpr.del_flag = '0'
        WHERE fi.no = #{no} AND cpr.is_pre_invoice = '1';
    </select>

    <select id="getExceedCwFinance" resultType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDTO">
        SELECT
        DISTINCT
        <include refid="Base_Column_List"></include>,
        (
        case
        when a.type = '0' then (concat('其他：',a.program_name))
        when a.type = '1' then (select rpr.project_name from cw_project_records rpr where id = a.program_id)
        when a.type = '2' then (select rpr.project_name from cw_project_records rpr where id = (select project_id from cw_project_report where id = a.program_id))
        when a.type = '4' then (select rpr.project_name from cw_project_records rpr where id = a.program_id)
        else '' end
        ) as programName,
        fi.create_by_id as 'createBy.id'
        FROM
        cw_finance_invoice_base a
        left join cw_finance_invoice fi on a.invoice_id = fi.id
        LEFT JOIN cw_project_records pr on a.program_id=pr.id
        WHERE
        ( fi.receivables_date IS NULL and fi.receivables_status = '0' )
        and fi.status != 1 and fi.status != 2 and fi.status != 3 and fi.status != 4 and fi.status != 7
        AND DATE( fi.billing_date ) &lt;= DATE(
        DATE_SUB( CURDATE(), INTERVAL 1 MONTH ))
        ORDER BY
        fi.billing_date DESC
    </select>



    <select id="getInvoiceNumberStr" resultType="java.lang.String">
		select group_concat(number) from cw_finance_invoice_detail where invoice_id = #{invoiceId}
	</select>


    <select id="getList" resultType="com.jeeplus.finance.invoice.service.dto.CwFinanceInvoiceDTO">
        SELECT
        DISTINCT
        <include refid="Base_Column_List"></include>
        FROM
        cw_finance_invoice_base a
        left join cw_finance_invoice fi on a.invoice_id = fi.id
        where a.del_flag = '0' and fi.account >= '0' AND (fi.create_by_id = #{userId})
        order by fi.create_time desc
    </select>

    <select id="getAllArea" resultType="com.jeeplus.sys.service.dto.AreaDTO">
        select id,parent_id as `parent.id`,parent_ids,name,sort,code,type,create_by_id,create_time,update_by_id,update_time,remarks
        from sys_area where del_flag = '0'
        order by sort asc,parent_id asc
    </select>

    <insert id="insertBatch">
        insert into cw_finance_invoice(
        id,
        create_by_id,
        create_time,
        update_by_id,
        update_time,
        del_flag,
        `type`,
        billing_type,
        billing_workplace_real,
        billing_workplace_real_id,
        taxpayer_identification_no,
        address,
        tel_phone,
        open_bank,
        bank_account,
        `name`,
        receivables_type,
        billing_content,
        account,
        billing_content_terms,
        billing_people_real,
        reconciliation_people,
        reconciliation_area,
        status,
        is_multiple,
        actual_drawer_email_address,
        red_invoice_flag,
        red_invoice_relevancy_id,
        red_invoice_relevancy_number,
        receivables_status,
        invalid_status,
        tenant_id
        )values
        <foreach collection="invoiceArrayList" item="item" separator=",">
            (#{item.id},
            #{item.createById},
            #{item.createTime},
            #{item.updateById},
            #{item.updateTime},
            #{item.delFlag},
            #{item.type},
            #{item.billingType},
            #{item.billingWorkplaceReal},
            #{item.billingWorkplaceRealId},
            #{item.taxpayerIdentificationNo},
            #{item.address},
            #{item.telPhone},
            #{item.openBank},
            #{item.bankAccount},
            #{item.name},
            #{item.receivablesType},
            #{item.billingContent},
            #{item.account},
            #{item.billingContentTerms},
            #{item.billingPeopleReal},
            #{item.reconciliationPeople},
            #{item.reconciliationArea},
            #{item.status},
            #{item.isMultiple},
            #{item.actualDrawerEmailAddress},
            #{item.redInvoiceFlag},
            #{item.redInvoiceRelevancyId},
            #{item.redInvoiceRelevancyNumber},
            #{item.receivablesStatus},
            #{item.invalidStatus},
            #{item.tenantId})
        </foreach>
    </insert>

    <select id="findZhglgsUserList" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.`name`,
        a.office_id AS parent_id,
        b.`name` AS office_name,
        true AS is_user
        FROM
        sys_user a
        LEFT JOIN sys_office b ON a.office_id = b.id
        WHERE
        a.del_flag = 0 and a.is_admin is null and a.tenant_id = '10002'
    </select>

    <select id="findZsUserList" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.`name`,
        a.office_id AS parent_id,
        b.`name` AS office_name,
        true AS is_user
        FROM
        sys_user a
        LEFT JOIN sys_office b ON a.office_id = b.id
        WHERE
        a.del_flag = 0 and a.is_admin is null and a.tenant_id = '10004' and b.name = #{zsOfficeName}

    </select>

    <select id="findCwglgsUserList" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.`name`,
        a.office_id AS parent_id,
        b.`name` AS office_name,
        true AS is_user
        FROM
        sys_user a
        LEFT JOIN sys_office b ON a.office_id = b.id
        WHERE
        a.del_flag = 0 and a.is_admin is null and a.tenant_id = '10003'
    </select>

    <select id="findDtoList" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.`name`,
        a.office_id AS parent_id,
        b.`name` AS office_name,
        true AS is_user
        FROM
        sys_user a
        LEFT JOIN sys_office b ON a.office_id = b.id
        WHERE
        a.del_flag = 0 and a.is_admin is null and a.tenant_id = #{tenantId}
    </select>

    <select id="finduserAllList" resultType="com.jeeplus.sys.service.dto.UserDTO">
        SELECT
        a.id,
        a.`name`,
        a.office_id AS parent_id,
        b.`name` AS office_name,
        true AS is_user
        FROM
        sys_user a
        LEFT JOIN sys_office b ON a.office_id = b.id
        WHERE
        a.del_flag = 0 and a.is_admin is null
    </select>

</mapper>
