<?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.workClientInfo.mapper.CwWorkClientBaseMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBaseDTO">
            <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="no" column="no" jdbcType="VARCHAR"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="abbreviation" column="abbreviation" jdbcType="VARCHAR"/>
            <result property="englishName" column="english_name" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="uscCode" column="usc_code" jdbcType="VARCHAR"/>
            <result property="scType" column="sc_type" jdbcType="VARCHAR"/>
            <result property="manageLevelType" column="manage_level_type" jdbcType="VARCHAR"/>
            <result property="oneUpCompany" column="one_up_company" jdbcType="VARCHAR"/>
            <result property="companyLevel" column="company_level" jdbcType="VARCHAR"/>
            <result property="businessScope" column="business_scope" jdbcType="VARCHAR"/>
            <result property="keyResponsibilities" column="key_responsibilities" jdbcType="VARCHAR"/>
            <result property="industry" column="industry" jdbcType="VARCHAR"/>
            <result property="affiliation" column="affiliation" jdbcType="VARCHAR"/>
            <result property="organizationType" column="organization_type" jdbcType="VARCHAR"/>
            <result property="reportType" column="report_type" jdbcType="VARCHAR"/>
            <result property="oneUpOffice" column="one_up_office" jdbcType="VARCHAR"/>
            <result property="registrationCert" column="registration_cert" jdbcType="VARCHAR"/>
            <result property="approvingBodies" column="approving_bodies" jdbcType="VARCHAR"/>
            <result property="officeId" column="office_id" jdbcType="VARCHAR"/>
            <result property="procInsId" column="proc_ins_id" jdbcType="VARCHAR"/>
            <result property="processDefinitionId" column="process_definition_id" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="VARCHAR"/>
            <result property="agreeDate" column="agree_date" jdbcType="TIMESTAMP"/>
            <result property="agreeUserId" column="agree_user_id" jdbcType="VARCHAR"/>
            <result property="createBy.name" column="user_name" jdbcType="VARCHAR"/>
            <result property="createName" column="user_name" jdbcType="VARCHAR"/>
            <result property="createDateT" column="create_date" jdbcType="TIMESTAMP"/>
            <result property="isListed" column="is_listed" jdbcType="VARCHAR"/>
            <result property="ownershipType" column="ownership_type" jdbcType="VARCHAR"/>
            <result property="finalStatusType" column="final_status_type" jdbcType="VARCHAR"/>
            <result property="address" column="address" jdbcType="VARCHAR"/>
            <result property="mobile" column="mobile" jdbcType="VARCHAR"/>
            <result property="isUscCode" column="is_usc_code" jdbcType="VARCHAR"/>
            <result property="manageLevelTypeName" column="manage_level_type_name" jdbcType="VARCHAR"/>
            <result property="organizationTypeName" column="organization_type_name" jdbcType="VARCHAR"/>
            <result property="updateRequestId" column="update_request_id" jdbcType="VARCHAR"/>
            <result property="updateUser" column="update_user" jdbcType="VARCHAR"/>
            <result property="updateUserName" column="update_user_name" jdbcType="VARCHAR"/>
            <result property="requestUserId" column="request_user_id" jdbcType="VARCHAR"/>
            <result property="taskId" column="task_id" jdbcType="VARCHAR"/>
            <association property="cwWorkClientTypeDTO" column="id" select="getType" javaType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientTypeDTO"></association>
            <collection property="workAttachmentDtoList" ofType="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto" column="id" select="getFileList"></collection>
            <collection property="cwWorkClientBillingDTOList" ofType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBillingDTO" column="id" select="getBilling"></collection>
            <collection property="cwWorkClientContactDTOList" ofType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientContactDTO" column="id" select="getContact"></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="Base_Column_List">
        cw_wcb.id,
        cw_wcb.create_by,
        cw_wcb.create_date,
        cw_wcb.update_by,
        cw_wcb.update_date,
        cw_wcb.del_flag,
        cw_wcb.remarks,
        cw_wcb.no,
        cw_wcb.name,
        cw_wcb.abbreviation,
        cw_wcb.english_name,
        cw_wcb.type,
        cw_wcb.usc_code,
        cw_wcb.sc_type,
        cw_wcb.manage_level_type,
        cw_wcb.one_up_company,
        cw_wcb.company_level,
        cw_wcb.business_scope,
        cw_wcb.key_responsibilities,
        cw_wcb.industry,
        cw_wcb.affiliation,
        cw_wcb.organization_type,
        cw_wcb.report_type,
        cw_wcb.one_up_office,
        cw_wcb.registration_cert,
        cw_wcb.approving_bodies,
        cw_wcb.office_id,
        cw_wcb.proc_ins_id,
        cw_wcb.process_definition_id,
        cw_wcb.status,
        cw_wcb.agree_date,
        cw_wcb.agree_user_id,
        cw_wcb.address,
        cw_wcb.mobile,
        cw_wcb.is_usc_code,
        cw_wcb.update_request_id,
        cw_wcb.update_user,
        cw_wcb.request_user_id
    </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="Billing_Column_List">
        cw_wcbi.id,
        cw_wcbi.create_by,
        cw_wcbi.create_date,
        cw_wcbi.update_by,
        cw_wcbi.update_date,
        cw_wcbi.del_flag,
        cw_wcbi.remarks,
        cw_wcbi.client_id,
        cw_wcbi.is_taxpayer_identification_no,
        cw_wcbi.taxpayer_identification_no,
        cw_wcbi.account_holder,
        cw_wcbi.account,
        cw_wcbi.phone,
        cw_wcbi.address,
        cw_wcbi.company_name
    </sql>
    <sql id="Contact_Column_List">
        cw_wcc.id,
        cw_wcc.create_by,
        cw_wcc.create_date,
        cw_wcc.update_by,
        cw_wcc.update_date,
        cw_wcc.del_flag,
        cw_wcc.remarks,
        cw_wcc.client_id,
        cw_wcc.name,
        cw_wcc.sex,
        cw_wcc.office_id,
        cw_wcc.position,
        cw_wcc.contact_first,
        cw_wcc.contact_second,
        cw_wcc.fax,
        cw_wcc.email,
        cw_wcc.project_id
    </sql>
    <sql id="Type_Column_List">
        cw_wct.id,
        cw_wct.create_by,
        cw_wct.create_date,
        cw_wct.update_by,
        cw_wct.update_date,
        cw_wct.del_flag,
        cw_wct.remarks,
        cw_wct.client_id,
        cw_wct.is_listed,
        cw_wct.listed_type,
        cw_wct.holding_comp_num,
        cw_wct.holding_listed_comp_num,
        cw_wct.stock_symbol,
        cw_wct.stock_abbreviation,
        cw_wct.release_date,
        cw_wct.listed_date,
        cw_wct.securities_market,
        cw_wct.control_natural_person,
        cw_wct.signatory1,
        cw_wct.signatory2,
        cw_wct.undertake_business_date,
        cw_wct.is_change_firm,
        cw_wct.former_firm,
        cw_wct.firm_change_cause,
        cw_wct.ownership_type,
        cw_wct.supervision_request_type,
        cw_wct.final_status_type,
        cw_wct.tax_authorities
    </sql>

    <select id="getType" resultType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientTypeDTO">
        select
        <include refid="Type_Column_List"></include>
        from cw_work_client_type cw_wct
        where cw_wct.del_flag = '0' and cw_wct.client_id = #{id}
    </select>

    <select id="getContact" resultType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientContactDTO">
        select
        <include refid="Contact_Column_List"></include>
        from cw_work_client_contact cw_wcc
        where cw_wcc.del_flag = '0' and cw_wcc.client_id = #{id}
    </select>

    <select id="getBilling" resultType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientBillingDTO">
        select
        <include refid="Billing_Column_List"></include>
        from cw_work_client_billing cw_wcbi
        where cw_wcbi.del_flag = '0' and cw_wcbi.client_id = #{id}
    </select>

    <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="findList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        su.name as user_name,
        cw_wct.is_listed as is_listed,
        cw_wct.ownership_type as ownership_type,
        cw_wct.final_status_type as final_status_type,
        cw_mlt.name as manage_level_type_name,
        cw_ot.name as organization_type_name,
        su1.name as update_user_name,
        art.ID_ as task_id
        from cw_work_client_base cw_wcb
        left join sys_user su on su.id = cw_wcb.create_by and su.del_flag = '0'
        left join cw_work_client_type cw_wct on cw_wct.client_id = cw_wcb.id and cw_wct.del_flag = '0'
        left join cw_manage_level_type cw_mlt on cw_mlt.id = cw_wcb.manage_level_type and cw_mlt.del_flag = '0'
        left join cw_organization_type cw_ot on cw_ot.id = cw_wcb.organization_type and cw_ot.del_flag = '0'
        left join sys_user su1 on su1.id = cw_wcb.update_user and su1.del_flag = '0'
        left join act_ru_task art on cw_wcb.proc_ins_id = art.PROC_INST_ID_
        ${ew.customSqlSegment}
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        wcb.name as one_up_company_name,
        su.name as user_name,
        cw_wct.is_listed as is_listed,
        cw_wct.ownership_type as ownership_type,
        cw_wct.final_status_type as final_status_type,
        cw_mlt.name as manage_level_type_name,
        cw_ot.name as organization_type_name
        from cw_work_client_base cw_wcb
        left join cw_work_client_base wcb on cw_wcb.one_up_company = wcb.id and wcb.del_flag = '0'
        left join sys_user su on su.id = cw_wcb.create_by
        left join cw_work_client_type cw_wct on cw_wct.client_id = cw_wcb.id and cw_wct.del_flag = '0'
        left join cw_manage_level_type cw_mlt on cw_mlt.id = cw_wcb.manage_level_type and cw_mlt.del_flag = '0'
        left join cw_organization_type cw_ot on cw_ot.id = cw_wcb.organization_type and cw_ot.del_flag = '0'
        where cw_wcb.del_flag = '0' and cw_wcb.id = ${id}
    </select>

    <select id="queryUpdateLogList" resultType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientTempDTO">
        select
            <include refid="Temp_Column_List"></include>,
            su.name as create_name,
            cw_wcb.no as client_no
        from cw_work_client_temp temp
        left join sys_user su on temp.create_by = su.id and su.del_flag = '0'
        inner join cw_work_client_base cw_wcb on temp.client_id = cw_wcb.id and cw_wcb.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="getContractClientList" resultType="com.jeeplus.test.cw.workClientInfo.service.dto.CwWorkClientContactDTO">
        SELECT
            *
        FROM
            cw_work_client_contact
        <where>
            client_id = (
            SELECT
            id
            FROM
            cw_work_client_base
            WHERE
            id = ( SELECT client_contacts FROM cw_work_contract_info WHERE id = #{cwWorkClientContactDTO.contractId} )
            ) and del_flag = 0
            <if test="cwWorkClientContactDTO.name != null and cwWorkClientContactDTO.name != ''">
                and `name` LIKE CONCAT ('%', #{cwWorkClientContactDTO.name}, '%')
            </if>
            <if test="cwWorkClientContactDTO.contactFirst != null and cwWorkClientContactDTO.contactFirst != ''">
                and contact_first LIKE CONCAT ('%', #{cwWorkClientContactDTO.contactFirst}, '%')
            </if>
            <if test="cwWorkClientContactDTO.contactSecond != null and cwWorkClientContactDTO.contactSecond != ''">
                and contact_second LIKE CONCAT ('%', #{cwWorkClientContactDTO.contactSecond}, '%')
            </if>
        </where>
        order by update_date desc
    </select>
    <sql id="Temp_Column_List">
        temp.id,
        temp.create_by,
        temp.create_date,
        temp.update_by,
        temp.update_date,
        temp.del_flag,
        temp.remarks,
        temp.client_id,
        temp.source_data,
        temp.status
    </sql>

    <select id="getNoByIdArray" resultType="string">
        select distinct cw_wcb.no
        from cw_work_client_base cw_wcb
        where cw_wcb.del_flag = '0' and cw_wcb.id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <select id="isUseByClientContacts" resultType="string">
        select
            distinct cw_wcb.name
        from cw_work_contract_info cw_wci
        left join cw_work_client_base cw_wcb on cw_wcb.id = cw_wci.client_contacts and cw_wcb.del_flag = '0'
        where cw_wci.del_flag = '0' and cw_wci.client_contacts in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <select id="isUseByContactParty" resultType="string">
        select
            distinct cw_wcb.name
        from cw_work_contract_participant cw_wcp
        inner join cw_work_contract_info cw_wci on cw_wci.id = cw_wcp.contract_info_id and cw_wci.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wcb.no = cw_wcp.customer_no and cw_wcb.del_flag = '0'
        where cw_wcp.del_flag = '0' and cw_wcp.customer_no in
        <foreach collection="noList" open="(" close=")" separator="," item="no">
            #{no}
        </foreach>
    </select>

    <select id="isUseByProject" resultType="string">
        select
            distinct cw_wcb.name
        from cw_project_client_info cw_pci
        inner join cw_project_records cw_pr on cw_pr.id = cw_pci.project_id and cw_pr.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wcb.id = cw_pci.client_id and cw_wcb.del_flag = '0'
        where cw_pci.client_id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <select id="isUseByReport" resultType="string">
        select
            distinct cw_wcb.name
        from cw_project_report_new_line cw_prnl
        inner join cw_project_report cw_pr on cw_pr.id = cw_prnl.report_id and cw_pr.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wcb.id = cw_prnl.served_unit_id and cw_wcb.del_flag = '0'
        where cw_prnl.del_flag = '0' and cw_prnl.served_unit_id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <select id="isUseByInvoice" resultType="string">
        select
            distinct cw_wcb.name
        from cw_finance_invoice cw_fi
        left join cw_work_client_base cw_wcb on cw_wcb.id = cw_fi.billing_workplace_real_id and cw_wcb.del_flag = '0'
        where cw_fi.del_flag = '0' and cw_fi.billing_workplace_real_id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>
</mapper>
