<?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.projectRecords.mapper.CwProjectRecordsMapper">

    <resultMap id="BaseResultMap" type="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        <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="projectNumber" column="project_number" jdbcType="VARCHAR"/>
        <result property="projectName" column="project_name" jdbcType="VARCHAR"/>
        <result property="officeId" column="office_id" jdbcType="VARCHAR"/>
        <result property="projectMoney" column="project_money" jdbcType="VARCHAR"/>
        <result property="auditYear" column="audit_year" jdbcType="VARCHAR"/>
        <result property="planStartDate" column="plan_start_date" jdbcType="TIMESTAMP"/>
        <result property="planEndDate" column="plan_end_date" jdbcType="TIMESTAMP"/>
        <result property="projectMasterId" column="project_master_id" jdbcType="VARCHAR"/>
        <result property="projectLeaderId" column="project_leader_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="createBy.name" column="user_name" jdbcType="VARCHAR"/>
        <result property="projectMasterName" column="project_master_name" jdbcType="VARCHAR"/>
        <result property="projectMasterName2" column="project_master_name" jdbcType="VARCHAR"/>
        <result property="contractId" column="contract_id" jdbcType="VARCHAR"/>
        <result property="contractName" column="contract_name" jdbcType="VARCHAR"/>
        <result property="contractAmount" column="contract_amount" jdbcType="VARCHAR"/>
        <result property="contractNum" column="contract_num" jdbcType="VARCHAR"/>
        <result property="payerSubject" column="payer_subject" jdbcType="VARCHAR"/>
        <result property="paymentMethod" column="payment_method" jdbcType="VARCHAR"/>
        <result property="createName" column="user_name" jdbcType="VARCHAR"/>
        <result property="createDateT" column="create_time" jdbcType="VARCHAR"/>
        <result property="businessType" column="business_type" jdbcType="VARCHAR"/>
        <result property="riskLevel" column="risk_level" jdbcType="VARCHAR"/>
        <result property="businessTypeName" column="business_type_name" jdbcType="VARCHAR"/>
        <result property="realHeader" column="real_header" jdbcType="VARCHAR"/>
        <result property="clientContactsName" column="client_contacts_name" jdbcType="VARCHAR"/>
        <result property="reportType" column="report_type" jdbcType="VARCHAR"/>
        <result property="memberCount" column="member_count" jdbcType="VARCHAR"/>
        <result property="realHeaderName" column="real_header_name" jdbcType="VARCHAR"/>
        <result property="reportReview" column="report_review" jdbcType="VARCHAR"/>
        <collection property="cwProjectClientInfoDTOList" ofType="com.jeeplus.finance.workClientInfo.service.dto.CwWorkClientBaseDTO" column="id" select="getProjectClient"></collection>
        <collection property="workAttachmentDtoList" ofType="com.jeeplus.sys.service.dto.WorkAttachmentInfoDTO" column="id" select="getFileList"></collection>
        <collection property="cwProjectClientContactDTOList" ofType="com.jeeplus.finance.projectRecords.service.dto.CwProjectClientContactDTO" column="id" select="getContact"></collection>
        <collection property="cwProjectMembersDTOList" ofType="com.jeeplus.finance.projectRecords.service.dto.CwProjectMembersDTO" column="id" select="getMembers"></collection>

    </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="Base_Column_List">
        a.id,
        a.create_by_id,
        a.create_time,
        a.update_by_id,
        a.update_time,
        a.del_flag,
        a.remarks,
        a.project_number,
        a.project_name,
        a.office_id,
        a.project_money,
        a.audit_year,
        a.plan_start_date,
        a.plan_end_date,
        a.project_master_id,
        a.project_leader_id,
        a.proc_ins_id,
        a.status,
        a.process_definition_id,
        a.contract_id,
        a.business_type,
        a.risk_level,
        a.real_header,
        a.report_type,
        a.report_review
    </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="Contact_Column_List">
        cw_pcc.id,
        cw_pcc.create_by_id,
        cw_pcc.create_time,
        cw_pcc.update_by_id,
        cw_pcc.update_time,
        cw_pcc.del_flag,
        cw_pcc.remarks,
        cw_pcc.project_id,
        cw_pcc.client_id,
        cw_pcc.name,
        cw_pcc.sex,
        cw_pcc.office_id,
        cw_pcc.position,
        cw_pcc.contact_first,
        cw_pcc.contact_second,
        cw_pcc.fax,
        cw_pcc.email,
        cw_pcc.client_contact_id
    </sql>
    <sql id="Project_Client_List">
        cw_pci.project_id,
        cw_pci.client_id,
        cw_wcb.no as clientNumber,
        cw_wcb.name as clientName,
        cw_wcb.company_level as companyLevel
    </sql>
    <sql id="Client_Column_List">
        cw_wcb.id,
        cw_wcb.create_by_id,
        cw_wcb.create_time,
        cw_wcb.update_by_id,
        cw_wcb.update_time,
        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
    </sql>
    <sql id="Members_Column_List">
        cw_pm.id,
        cw_pm.create_by_id,
        cw_pm.create_time,
        cw_pm.update_by_id,
        cw_pm.update_time,
        cw_pm.del_flag,
        cw_pm.remarks,
        cw_pm.project_id,
        cw_pm.user_id,
        cw_pm.status,
        cw_pm.is_important
    </sql>

    <select id="getProjectClient" resultType="com.jeeplus.finance.workClientInfo.service.dto.CwWorkClientBaseDTO">
        select
        <include refid="Client_Column_List"></include>
        from cw_project_client_info cw_pci
        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.project_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_id and su.del_flag = '0'
        where wa.del_flag = '0' and wa.attachment_id = #{id}
    </select>

    <select id="getContact" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectClientContactDTO">
        select
        <include refid="Contact_Column_List"></include>
        from cw_project_client_contact cw_pcc
        where cw_pcc.project_id = #{id} and cw_pcc.del_flag = '0'
    </select>
    <select id="getMembers" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectMembersDTO">
        select
        <include refid="Members_Column_List"></include>
        from cw_project_members cw_pm
        where cw_pm.project_id = #{id} and cw_pm.del_flag = '0'
    </select>


    <!--<select id="findList" resultMap="BaseResultMap">
        select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        su.name as user_name,
        su2.name as project_master_name,
        su3.name as real_header_name,
        cw_wci.contract_name,
        cw_wci.contract_amount,
        cw_wci.contract_num,
        cw_wci.payer_subject,
        cw_wci.payment_method,
        cw_wcb.name as client_contacts_name,
        cw_pbt.name as business_type_name,
        b.member_count
        from cw_project_records a
        left join sys_user su on su.id = a.create_by and su.del_flag = '0'
        left join sys_user_manage_office sumo on sumo.office_id = su.office_id
        left join sys_user su2 on su2.id = a.project_master_id and su2.del_flag = '0'
        left join sys_user su3 on su3.id = a.real_header and su3.del_flag = '0'
        left join cw_work_contract_info cw_wci on cw_wci.id = a.contract_id and cw_wci.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wci.client_contacts = cw_wcb.id and cw_wcb.del_flag = '0'
        left join cw_project_business_type cw_pbt on cw_pbt.id = a.business_type and cw_pbt.del_flag = '0'
        left join (select rec.id,count(cw_pm.id) as member_count
                    from cw_project_records rec
                    left join cw_project_members cw_pm on cw_pm.project_id = rec.id and cw_pm.del_flag = '0'
                    group by rec.id
                    ) b on b.id = a.id
        ${ew.customSqlSegment}
        <if test="currentUser.id != '1'.toString">
            <if test="isBmzr == '0'.toString">
                and (a.create_by = #{currentUser.id} or a.id in (
                select cw_pm.project_id
                from cw_project_members cw_pm
                where cw_pm.user_id = #{currentUser.id} and cw_pm.del_flag = '0' and cw_pm.status = '0'
                ))
            </if>
            <if test="isBmzr == '1'.toString">
                and (a.create_by in (
                select su.id from sys_user su where su.del_flag = '0' and su.office_id in
                (select so.id from sys_office so where so.id = #{currentUser.officeDTO.id} or so.parent_ids like concat('%',#{currentUser.officeDTO.id},'%') and so.del_flag = '0')
                ) or a.id in (
                select cw_pm.project_id
                from cw_project_members cw_pm
                where cw_pm.user_id = #{currentUser.id} and cw_pm.del_flag = '0' and cw_pm.status = '0'
                ))
            </if>
        </if>
        ORDER BY a.create_date DESC
    </select>-->


    <select id="findList" resultMap="BaseResultMap">
        select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        su.name as user_name,
        su2.name as project_master_name,
        su2.name as projectMasterName2,
        su3.name as real_header_name,
        cw_wci.contract_name,
        cw_wci.contract_amount,
        cw_wci.contract_num,
        cw_wci.payer_subject,
        cw_wci.payment_method,
        cw_wcb.name as client_contacts_name,
        cw_pbt.name as business_type_name,
        b.member_count
        from cw_project_records a
        left join sys_user su on su.id = a.create_by_id and su.del_flag = '0'
        left join sys_user su2 on su2.id = a.project_master_id and su2.del_flag = '0'
        left join sys_user su3 on su3.id = a.real_header and su3.del_flag = '0'
        left join cw_work_contract_info cw_wci on cw_wci.id = a.contract_id and cw_wci.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wci.client_contacts = cw_wcb.id and cw_wcb.del_flag = '0'
        left join cw_project_business_type cw_pbt on cw_pbt.id = a.business_type and cw_pbt.del_flag = '0'
        left join (select rec.id,count(cw_pm.id) as member_count
                    from cw_project_records rec
                    left join cw_project_members cw_pm on cw_pm.project_id = rec.id and cw_pm.del_flag = '0'
                    group by rec.id
                    ) b on b.id = a.id
        left join cw_project_members cpm on a.id = cpm.project_id
        left join sys_user sub on cpm.user_id = sub.id
        left join sys_user_manage_office sumo on sumo.office_id = sub.office_id
        ${ew.customSqlSegment}
        ORDER BY a.create_time DESC
    </select>
    <select id="findList2" resultMap="BaseResultMap">
        select
        DISTINCT
        <include refid="Base_Column_List"></include>,
        su.name as user_name,
        su2.name as project_master_name,
        su3.name as real_header_name,
        cw_wci.contract_name,
        cw_wci.contract_amount,
        cw_wci.contract_num,
        cw_wci.payer_subject,
        cw_wci.payment_method,
        cw_wcb.name as client_contacts_name,
        cw_pbt.name as business_type_name,
        b.member_count
        from cw_project_records a
        left join sys_user su on su.id = a.create_by_id and su.del_flag = '0'
        left join sys_user su2 on su2.id = a.project_master_id and su2.del_flag = '0'
        left join sys_user su3 on su3.id = a.real_header and su3.del_flag = '0'
        left join cw_work_contract_info cw_wci on cw_wci.id = a.contract_id and cw_wci.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wci.client_contacts = cw_wcb.id and cw_wcb.del_flag = '0'
        left join cw_project_business_type cw_pbt on cw_pbt.id = a.business_type and cw_pbt.del_flag = '0'
        left join (select rec.id,count(cw_pm.id) as member_count
        from cw_project_records rec
        left join cw_project_members cw_pm on cw_pm.project_id = rec.id and cw_pm.del_flag = '0'
        group by rec.id
        ) b on b.id = a.id
        left join cw_project_members cpm on a.id = cpm.project_id
        left join sys_user sub on cpm.user_id = sub.id
        left join sys_user_manage_office sumo on sumo.office_id = sub.office_id
        ${ew.customSqlSegment}
        ORDER BY a.create_time DESC
    </select>

    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"></include>,
        su.name as user_name,
        su2.name as project_master_name,
        su3.name as real_header_name,
        cw_wci.contract_name,
        cw_wci.contract_amount,
        cw_wci.contract_num,
        cw_wci.payer_subject,
        cw_wci.payment_method,
        cw_wcb.name as client_contacts_name,
        cw_pbt.name as business_type_name
        from cw_project_records a
        left join sys_user su on su.id = a.create_by_id and su.del_flag = '0'
        left join sys_user su2 on su2.id = a.project_master_id and su2.del_flag = '0'
        left join sys_user su3 on su3.id = a.real_header and su3.del_flag = '0'
        left join cw_work_contract_info cw_wci on cw_wci.id = a.contract_id and cw_wci.del_flag = '0'
        left join cw_work_client_base cw_wcb on cw_wci.client_contacts = cw_wcb.id and cw_wcb.del_flag = '0'
        left join cw_project_business_type cw_pbt on cw_pbt.id = a.business_type and cw_pbt.del_flag = '0'
        where a.del_flag = '0' and a.id = #{id}
    </select>

    <delete id="deleteProjectClientInfoByProjectId">
        delete from cw_project_client_info where project_id = #{projectId}
    </delete>

    <insert id="insertProjectClientInfoList">
        <foreach collection="cwProjectClientInfoList" item="item" separator=";">
            insert into cw_project_client_info(project_id,client_id) values(#{item.projectId},#{item.clientId})
        </foreach>
    </insert>

    <select id="getProjectIdByClientId" resultType="string">
        select
            cw_pci.project_id
        from cw_project_client_info cw_pci
        where cw_pci.client_id = #{id}
    </select>

    <select id="getProjectByIdList" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO" parameterType="string">
        select
        <include refid="Base_Column_List"></include>,
        su.name as create_name,
        su2.name as project_master_name
        from cw_project_records a
        left join sys_user su on su.id = a.create_by_id and su.del_flag = '0'
        left join sys_user su2 on su2.id = a.project_master_id and su2.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="getByContractId" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        select
            <include refid="Base_Column_List"></include>,
            su.name as create_name,
            su2.name as project_master_name
        from cw_project_records a
        left join sys_user su on su.id = a.create_by_id and su.del_flag = '0'
        left join sys_user su2 on su2.id = a.project_master_id and su2.del_flag = '0'
        ${ew.customSqlSegment}
    </select>

    <select id="getHaveProjectIds" resultType="string">
        select
            distinct id
        from
        (select cw_pm.project_id as id
        from cw_project_members cw_pm
        where cw_pm.user_id = #{currentUser.id} and cw_pm.del_flag = '0' and cw_pm.status = '0'
        union
        select cw_pr.id as id
        from cw_project_records cw_pr
        where cw_pr.create_by_id = #{currentUser.id} and cw_pr.del_flag = '0') a
    </select>

    <select id="isUseByReport" resultType="string">
        select distinct cw_pro.project_name
        from cw_project_report cw_pr
        left join cw_project_records cw_pro on cw_pro.id = cw_pr.project_id and cw_pro.del_flag = '0'
        where cw_pr.del_flag = '0' and cw_pr.project_id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <select id="isUseByReim" resultType="string">
        select distinct cw_pro.project_name
        from cw_reimbursement_detail_info cw_reim
        inner join cw_reimbursement_info cw_info on cw_info.id = cw_reim.info_id and cw_info.del_flag = '0'
        left join cw_project_records cw_pro on cw_pro.id = cw_reim.project_id and cw_pro.del_flag = '0'
        where cw_reim.del_flag = '0' and cw_reim.project_id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <select id="isUseByFinance" resultType="string">
        select distinct cw_pro.project_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_project_records cw_pro on cw_pro.id = cw_fib.program_id and cw_pro.del_flag = '0'
        where cw_fib.del_flag = '0' and cw_fib.program_id in
        <foreach collection="idArray" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </select>

    <update id="updatereportReviewById">
        UPDATE cw_project_records
        SET
        <if test="reportReview != null and reportReview != ''">
            report_review = #{reportReview},
        </if>
        update_by_id = #{updateById},
        update_time = #{updateTime}
        WHERE
            id = #{id}
            AND del_flag = 0
    </update>

    <select id="getListByIds" resultType="com.jeeplus.finance.projectRecords.domain.CwProjectRecords">
        SELECT  id,remarks,project_number,project_name,office_id,project_money,audit_year,plan_start_date,plan_end_date,project_master_id,project_leader_id,proc_ins_id,process_definition_id,status,contract_id,business_type,risk_level,real_header,report_type,report_review,create_time,create_by_id,update_time,update_by_id,del_flag,tenant_id  FROM cw_project_records
         <where>
             del_flag=0
             and id in
             <foreach collection="ids" open="(" close=")" separator="," item="id">
                 #{id}
             </foreach>
         </where>

    </select>

    <select id="selectLabel" resultType="String">
        SELECT id FROM sys_dict_type WHERE type='cw_work_client_report_type'
    </select>

    <select id="getReportById" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        SELECT
        distinct
        cpr.project_name,
        prnl.report_no,
        fi.no,
        fid.number,
        fid.account,
        fi.billing_people,
        fi.billing_date
        FROM cw_project_report_archive a
        LEFT JOIN cw_project_records cpr on a.project_id=cpr.id
        LEFT JOIN cw_project_report pr on pr.project_id=cpr.id
        LEFT JOIN cw_finance_invoice_base fib on cpr.id=fib.program_id
        LEFT JOIN cw_finance_invoice fi on fi.id=fib.invoice_id
        LEFT JOIN cw_project_report_new_line prnl on pr.id=prnl.report_id
        LEFT JOIN cw_finance_invoice_detail fid on fi.id=fid.invoice_id
        <where>
        <if test="null!=cwProjectRecordsDTO.number and cwProjectRecordsDTO.number!=''">
           and fid.number=#{cwProjectRecordsDTO.number}
        </if>
        <if test="cwProjectRecordsDTO.no!=null and cwProjectRecordsDTO.no!=''">
           and fi.no=#{cwProjectRecordsDTO.no}
        </if>
        <if test="cwProjectRecordsDTO.reportNo!=null and cwProjectRecordsDTO.reportNo!=''">
           and prnl.report_no=#{cwProjectRecordsDTO.reportNo}
        </if>
            and (a.id=#{id} or fib.program_id=(SELECT project_id FROM cw_project_report_archive where id=#{id}))
            and (fib.account is not null or fib.account != '')
        </where>
    </select>

    <select id="getReportByProjectId" resultType="com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO">
        SELECT
			  cpr.project_name,
				prnl.report_no,
				pr.audit_fees,
				su.login_name,
				pr.create_time
        from cw_project_report pr
        LEFT JOIN cw_project_report_new_line prnl on pr.id=prnl.report_id
        LEFT JOIN cw_project_records cpr on pr.project_id=cpr.id
        LEFT JOIN sys_user su on pr.create_by_id = su.id
        WHERE pr.audit_fees >'0' and pr.project_id=#{id}
    </select>


</mapper>
