<?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="org.jeecg.modules.projectManagement.mapper.FdProjectFileMapper">

    <select id="findByPage" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT
            a.id,
            a.contract_id,
            f.id AS customerId,
            a.customer_name AS customerName,
            d.code AS contractCode,
            a.contract_party,
            a.abbreviation,
            a.project_name,
            a.contract_type,
            a.sign_date,
            a.bd,
            a.clinical_pm AS pm,
            a.del_flag,
            a.create_time,
            a.status,
            a.project_file_no,
            CONCAT(d.code,'--',a.project_file_no) AS contractAndFileCode,
            a.personnel_with_authority_ids
        FROM
            fd_project_file a
            LEFT JOIN fd_contract d ON a.contract_id = d.id
            LEFT JOIN fd_customer f ON a.customer_id = f.id
             ${ew.customSqlSegment}
    </select>

    <select id="findInformationByPage" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT
        a.id,
        a.contract_id,
        f.id AS customerId,
        a.customer_name AS customerName,
        d.code AS contractCode,
        a.contract_party,
        a.abbreviation,
        a.project_name,
        a.contract_type,
        a.sign_date,
        a.bd,
        a.clinical_pm AS pm,
        a.del_flag,
        a.create_time,
        a.status,
        a.project_file_no,
        CONCAT(d.code,'--',a.project_file_no) AS contractAndFileCode,
        a.personnel_with_authority_ids,
        fp.pk_personnel
        FROM
        fd_project_file a
        LEFT JOIN fd_contract d ON a.contract_id = d.id
        LEFT JOIN fd_customer f ON a.customer_id = f.id
        left JOIN fd_personnel fp on a.project_manager_id=fp.id
        ${ew.customSqlSegment}
    </select>

    <select id="validationCodeFindByPage" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT
            a.id,
            a.contract_id,
            f.id AS customerId,
            a.customer_name AS customerName,
            d.code AS contractCode,
            a.contract_party,
            a.abbreviation,
            a.project_name,
            a.contract_type,
            a.sign_date,
            a.bd,
            a.clinical_pm AS pm,
            a.del_flag,
            a.create_time,
            a.status,
            a.project_file_no,
            CONCAT(d.code,'--',a.project_file_no) AS contractAndFileCode,
            fpfv.code validationCode
        FROM
            fd_project_file a
            LEFT JOIN fd_contract d ON a.contract_id = d.id
            LEFT JOIN fd_customer f ON a.customer_id = f.id
            LEFT JOIN fd_project_file_validation fpfv ON a.id=fpfv.project_id
             ${ew.customSqlSegment}
    </select>

    <!--查询成员数据-->
    <select id="getPersonnelList" resultType="org.jeecg.modules.basedata.entity.FdPersonnel">
        SELECT
        p.id,
        p.code,
        p.name,
        s.username,
	    r.role_name
        FROM
        fd_personnel p
        left join sys_user s on p.id=s.personnel_id
        left join sys_user_role ur on s.id=ur.user_id
        left join sys_role r on r.id=ur.role_id
        where s.personnel_id != ''
        and p.del_flag='0'
    </select>

    <update id="closeBatch" parameterType="java.util.List">
        update fd_project_file set status = '2' where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>


    <update id="auditBatch" parameterType="java.util.List">
        update fd_project_file set status = '0' where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>


    <update id="reviewBatch" parameterType="java.util.List">
        update fd_project_file set status = '1' where id in
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <select id="queryStatus0" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            fd_project_file
        where
            status = '0' AND del_flag='0'
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') >=DATE_FORMAT(date_sub(now(), interval 90 day),'%Y-%m-%d') ]]>
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') <=DATE_FORMAT(now(),'%Y-%m-%d') ]]>
    </select>

    <select id="queryStatus1" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            fd_project_file
        where
            status = '1' AND del_flag='0'
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') >=DATE_FORMAT(date_sub(now(), interval 90 day),'%Y-%m-%d') ]]>
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') <=DATE_FORMAT(now(),'%Y-%m-%d') ]]>
    </select>

    <select id="queryStatus2" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            fd_project_file
        where
            status = '2' AND del_flag='0'
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') >=DATE_FORMAT(date_sub(now(), interval 90 day),'%Y-%m-%d') ]]>
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') <=DATE_FORMAT(now(),'%Y-%m-%d') ]]>
    </select>

    <select id="queryStatus3" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            fd_project_file
        where
            status = '3' AND del_flag='0'
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') >=DATE_FORMAT(date_sub(now(), interval 90 day),'%Y-%m-%d') ]]>
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') <=DATE_FORMAT(now(),'%Y-%m-%d') ]]>
    </select>

    <select id="queryStatus4" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM
            fd_project_file
        where
            status = '4' AND del_flag='0'
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') >=DATE_FORMAT(date_sub(now(), interval 90 day),'%Y-%m-%d') ]]>
            <![CDATA[ and DATE_FORMAT(create_time,'%Y-%m-%d') <=DATE_FORMAT(now(),'%Y-%m-%d') ]]>
    </select>

    <select id="myGetById" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT * from  fd_project_file WHERE id=#{0}
    </select>

    <select id="supplementContractList" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT pf.contract_id,c.code as contractCode from fd_project_file pf
        LEFT JOIN fd_contract c on pf.contract_id=c.id
        where pf.status='1'
        and pf.del_flag='0'
        and c.code LIKE '%#%'
        ORDER BY pf.create_time DESC
    </select>

    <!--选择项目档案进行中的合同-->
    <select id="selectContractById" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT
            fpf.*,
            fp.NAME investigatorName,
	        fc.quotation_id quotationId,
	        fc.code contractCode
        FROM
            fd_project_file fpf
	        left join fd_contract fc on fpf.contract_id=fc.id
            LEFT JOIN fd_personnel fp ON fpf.investigator = fp.id
        where fpf.status='1' and fpf.del_flag='0' and fpf.id=#{contractId}
        ORDER BY fpf.create_time DESC
    </select>
    
    <select id="getPiFileList" resultType="org.jeecg.modules.projectManagement.entity.FdProjectFile">
        SELECT
            fpbio.NAME as investigatorName,
            fpccli.NAME as clinicalPiName,
            fpq.compound_name as compoundNameQuotation,
            fpq.tax_rate,
            fpf.*
        FROM
            fd_project_file fpf
            LEFT JOIN fd_personnel fpbio ON fpbio.id = fpf.investigator
            LEFT JOIN fd_personnel fpccli ON fpccli.id = fpf.clinical_pi
            left join fd_contract fc on fc.id=fpf.contract_id
            left join fd_project_quotation fpq on fpq.id=fc.quotation_id
        WHERE
            fpf.del_flag = '0'
            AND fpf.contract_id =#{fdProjectFile.contractId}
    </select>

</mapper>
