<?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.contractRegistration.mapper.ContractInfoMapper">

    <update id="updateStatusById">
        UPDATE cw_work_contract_info SET `status` = #{status} WHERE del_flag = 0 AND id = #{id}
    </update>
    <update id="updatefiledTypeById">
        UPDATE cw_work_contract_info SET `filed_type` = #{status} WHERE del_flag = 0 AND id = #{id}
    </update>
    <update id="updateInfo">
        UPDATE cw_work_contract_info SET actual_contract_amount = #{actualContractAmount}
        ,contract_approval_type = #{contractApprovalType},filed_type = #{filedType} where del_flag = 0 AND id = #{id}
    </update>
    <update id="updatefiledPaperTypeById">
        UPDATE cw_work_contract_info SET `filed_paper_type` = #{status} WHERE del_flag = 0 AND id = #{id}
    </update>
    <update id="updateBorrowTypeById">
        UPDATE cw_work_contract_info SET `borrow_type` = #{status} WHERE del_flag = 0 AND id = #{id}
    </update>
    <update id="updatePaperInfo">
        UPDATE cw_work_contract_info SET filed_paper_type = #{filedPaperType},
        actual_contract_amount = #{actualContractAmount}
        ,contract_approval_type = #{contractApprovalType}
        where del_flag = 0 AND id = #{id}
    </update>

    <select id="findPageList" resultType="com.jeeplus.finance.contractRegistration.domain.ContractInfo">
        SELECT
        DISTINCT a.id,
			c.`name` AS create_by,
			c.`name` AS createName,
			c.`id` AS createId,
			a.create_time,
			a.update_by_id,
			a.update_time,
			a.del_flag,
			a.contract_no as contractNo,
			a.contract_name as contractName,
			a.payer_subject as payerSubject,
			a.payment_method as paymentMethod,
			DATE_FORMAT(a.signing_date,'%Y-%m-%d') as signingDate,
            a.contract_amount as contractAmount,
            a.actual_contract_amount as actualContractAmount,
            a.contract_num as contractNum,
            a.payment_agreement as paymentAgreement,
            a.change_num as changeNum,
            a.department,
            a.client_contacts as clientContacts,
            a.client_contacts_phone as clientContactsPhone,
            a.contract_approval_type as contractApprovalType,
            a.proc_ins_id as procInsId,
            a.process_definition_id as processDefinitionId,
            a.contract_amount_type,
            a.contract_opposite,
            a.contract_fee,
            a.payment_describe,
            a.contract_type,
            a.contract_serial_number,
            a.fees,
            a.status,
            b.filed_type as filedType,
            e.ID_ as taskFiledId,
            b.proc_ins_id as taskFiledProcInsId,
            d.name as departmentName,
            f.borrow_type,
            f.create_by_id as borrowUserId,
            g.ID_ as task_borrow_id,
            h.filed_paper_type as filedPaperType,
            h.filed_no as filedNo,
            h.confirm_filed_no as confirmFiledNo,
            h.proc_ins_id as taskFiledPaperProcInsId,
            cw_wcb.name as client_contacts_name,
            art.ID_ as task_id,
            art_fp.ID_ as file_paper_task_id
		FROM
			cw_work_contract_info a
            LEFT JOIN cw_work_contract_file b on a.id = b.contract_info_id
            LEFT JOIN sys_user c ON a.create_by_id = c.id
            left join sys_user_manage_office sumo on sumo.office_id = c.office_id
            LEFT JOIN act_ru_task e ON b.proc_ins_id = e.PROC_INST_ID_
            LEFT JOIN sys_office d on a.department = d.id
            LEFT JOIN cw_work_contract_borrow f ON a.id = f.contract_info_id
            LEFT JOIN act_ru_task g ON f.proc_ins_id = g.PROC_INST_ID_
            LEFT JOIN cw_work_contract_file_paper h on a.id = h.contract_info_id
            LEFT JOIN cw_work_client_base cw_wcb on a.client_contacts = cw_wcb.id and cw_wcb.del_flag = '0'
            LEFT JOIN act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
            LEFT JOIN act_ru_task art_fp ON h.proc_ins_id = art_fp.PROC_INST_ID_
		${ew.customSqlSegment}
		ORDER BY a.update_time DESC
    </select>
    <select id="findById" resultType="com.jeeplus.finance.contractRegistration.domain.ContractInfo">
        SELECT
			a.id,
			c.`name` AS createBy,
			c.`id` AS createId,
			a.create_time,
			a.update_by_id,
			a.update_time,
			a.del_flag,
			a.contract_no as contractNo,
			a.contract_name as contractName,
			a.payer_subject as payerSubject,
			a.payment_method as paymentMethod,
			DATE_FORMAT(a.signing_date,'%Y-%m-%d') as signingDate,
            a.contract_amount as contractAmount,
            a.actual_contract_amount as actualContractAmount,
            a.contract_num as contractNum,
            a.payment_agreement as paymentAgreement,
            a.change_num as changeNum,
            a.department,
            a.contract_amount_type,
            a.contract_opposite,
            a.contract_fee,
            a.payment_describe,
            a.contract_type,
            a.contract_serial_number,
            a.fees,
            cw.id as customerId,
            cw.name as clientContacts,
            cw.no as customerNo,
            cw.usc_code as uscCode,
            cw.mobile as clientContactsPhone,
            cw.address as address,
            a.contract_approval_type as contractApprovalType,
            a.proc_ins_id as procInsId,
            a.status,
            a.predict_amount as "predictAmount",
            b.filed_type as filedType,
            d.filed_paper_type as filedPaperType,
            d.filed_no as filedNo,
            d.confirm_filed_no as confirmFiledNo,
            e.name as departmentName
		FROM
			cw_work_contract_info a
            LEFT JOIN cw_work_contract_file b on a.id = b.contract_info_id
            LEFT JOIN sys_user c ON a.create_by_id = c.id
            left join cw_work_client_base cw on a.client_contacts = cw.id
            LEFT JOIN cw_work_contract_file_paper d on a.id = d.contract_info_id
            LEFT JOIN sys_office e on a.department = e.id
        WHERE
			a.id = #{id}
			AND a.del_flag = 0
    </select>
    <select id="findList" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT * FROM work_attachment WHERE del_flag = 0 AND attachment_id = #{id}
    </select>
    <select id="findIsExit" resultType="java.lang.Integer">
        SELECT
			COUNT( 0 )
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
			AND attachment_name = #{name}
    </select>
    <select id="findDtos" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
        SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
    </select>
    <select id="findByContractId" resultType="com.jeeplus.finance.contractRegistration.domain.ContractInfo">
        SELECT
			a.id,
			c.`name` AS createBy,
			c.`id` AS createId,
			a.create_time,
			a.update_by_id,
			a.update_time,
			a.del_flag,
			a.contract_no as contractNo,
			a.contract_name as contractName,
			a.payer_subject as payerSubject,
			a.payment_method as paymentMethod,
			DATE_FORMAT(a.signing_date,'%Y-%m-%d') as signingDate,
            a.contract_amount as contractAmount,
            a.actual_contract_amount as actualContractAmount,
            a.contract_num as contractNum,
            a.payment_agreement as paymentAgreement,
            a.change_num as changeNum,
            a.department,
            a.client_contacts as clientContacts,
            a.client_contacts_phone as clientContactsPhone,
            a.contract_approval_type as contractApprovalType,
            a.proc_ins_id as procInsId,
            a.status,
            b.filed_type as filedType,
            d.filed_paper_type as filedPaperType,
            d.filed_no as filedNo,
            d.confirm_filed_no as confirmFiledNo
		FROM
			cw_work_contract_info a
            LEFT JOIN cw_work_contract_file b on a.id = b.contract_info_id
            LEFT JOIN sys_user c ON a.create_by_id = c.id
            LEFT JOIN cw_work_contract_file_paper d on a.id = d.contract_info_id
        WHERE
			b.id = #{id}
			AND a.del_flag = 0
    </select>
    <select id="getById" resultType="com.jeeplus.finance.contractRegistration.domain.ContractFile">
        select * from cw_work_contract_file where id = #{id}
    </select>
    <select id="getInfoById" resultType="com.jeeplus.finance.contractRegistration.domain.ContractInfo">
        select * from cw_work_contract_info where id = #{id}
    </select>
    <select id="findChildIds" resultType="java.lang.String">
        select id from sys_office where parent_id = #{department}
    </select>
    <select id="findFileInfoById" resultType="com.jeeplus.finance.contractRegistration.domain.ContractInfo">
        select * from cw_work_contract_info where id = #{id}
    </select>
    <select id="getByClientId" resultType="com.jeeplus.finance.contractRegistration.domain.ContractInfo" parameterType="string">
        select
            cw_wci.contract_name,
            cw_wci.id,
            cw_wci.contract_no,
            cw_wci.signing_date,
            cw_wci.contract_amount,
            cw_wci.department,
            cw_wci.create_time,
            cw_wci.create_by_id as create_id,
            su.name as create_name,
            so.name as department_name
        from cw_work_contract_info cw_wci
        left join sys_user su on su.id = cw_wci.create_by_id and su.del_flag = '0'
        left join sys_office so on so.id = cw_wci.department and so.del_flag = '0'
        ${ew.customSqlSegment}
    </select>
    <select id="getClientListByClientNo" resultType="string">
        select
            cw_wcp.contract_info_id
        from cw_work_contract_participant cw_wcp
        where cw_wcp.del_flag = '0' and cw_wcp.customer_no = #{no}
    </select>

    <select id="isUseByProject" resultType="string">
        select
            cw_wci.contract_name
        from cw_project_records cw_pr
        left join cw_work_contract_info cw_wci on cw_wci.id = cw_pr.contract_id and cw_wci.del_flag = '0'
        where cw_pr.del_flag = '0' and cw_pr.contract_id = #{id}
    </select>

    <select id="isUseByReim" resultType="string">
        select
            cw_wci.contract_name
        from cw_reimbursement_detail_info_contract cw_rdic
        inner join cw_reimbursement_info cw_ri on cw_ri.id = cw_rdic.info_id and cw_ri.del_flag = '0'
        left join cw_work_contract_info cw_wci on cw_wci.id = cw_rdic.contract_id and cw_wci.del_flag = '0'
        where cw_rdic.del_flag = '0' and cw_rdic.contract_id = #{id}
    </select>

    <select id="isUseByInvoice" resultType="string">
        select
            cw_wci.contract_name
        from cw_finance_invoice_base cw_fib
        inner join cw_finance_invoice cw_fi on cw_fi.id = cw_fib.invoice_id and cw_fi.del_flag = '0'
        left join cw_work_contract_info cw_wci on cw_wci.id = cw_fib.contract_id and cw_wci.del_flag = '0'
        where cw_fib.del_flag = '0' and cw_fib.contract_id = #{id}
    </select>
</mapper>
