<?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.program.configuration.projectList.mapper.ProjectListMapper">

   <select id="findContractPageList" resultType="com.jeeplus.test.workContract.domain.WorkContractInfo">
       SELECT
           a.id,
           a.create_date,
           a.update_by,
           a.update_date,
           a.del_flag,
           a.client_id,
           a.client_name,
           a.no,
           a.name,
           a.contract_date,
           a.effective_date,
           a.closing_date,
           a.contract_type,
           a.contract_amount_type,
           a.contract_amount,
           a.contract_opposite,
           a.contract_fee,
           a.fees,
           a.describes,
           a.contract_special,
           a.remarks,
           a.filed_by,
           a.filed_date,
           a.filed_no,
           a.confirm_filed_no,
           a.filed_remarks,
           a.proc_ins_id,
           a.process_definition_id,
           a.status,
           a.filed_type,
           a.borrow_type,
           b.NAME AS create_by
        FROM
            work_contract_info a
            LEFT JOIN sys_user b ON a.create_by = b.id
        WHERE
            a.del_flag = 0
            and a.status != 1
            <if test="dto.createBy != null and dto.createBy != ''">
                AND b.id = #{dto.createBy}
            </if>
           <if test="dto.name != null and dto.name != ''">
               AND a.name LIKE CONCAT ('%', #{dto.name}, '%')
           </if>
           <if test="dto.clientName != null and dto.clientName != ''">
               AND a.client_name LIKE CONCAT ('%', #{dto.clientName}, '%')
           </if>
            ORDER BY a.create_by DESC
   </select>

    <update id="updateStatusById">
        UPDATE program_project_list_info SET `status` = #{status} WHERE id = #{id}
    </update>

    <select id="findPageList" resultType="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
        SELECT
        distinct
           a.id,
           b.name AS create_by,
           a.create_date,
           a.update_by,
           a.update_date,
           a.del_flag,
           a.contract_id,
           a.contract_name,
           a.client,
           a.client_name,
           a.amount,
           a.contract_type,
           a.name,
           a.no,
           a.project_type,
           a.approval_no,
           a.company,
           a.project_mould,
           a.property_holder,
           a.property_holder_name,
           a.location,
           a.is_first,
           a.year_num,
           a.check_year,
           a.project_manager,
           c.name AS project_manager_name,
           a.report_type,
           a.industry,
           a.enterprise_type,
           a.risk_level,
           a.project_source,
           a.estimate,
           a.plan_end_time,
           a.use_num,
           a.appointment,
           a.work_hours,
           a.remarks,
           a.assessment_enterprise,
           a.assessment_enterprise_name,
           a.link_num,
           a.relationship,
           a.assessment_way,
           a.assessment_objective,
           a.assessment_date,
           a.num,
           a.project_development,
           a.delegate_project_type,
           a.assessment_object,
           a.work_begin_date,
           a.work_end_date,
           a.reporting_date,
           a.is_have,
           a.is_influence,
           a.mode,
           a.status,
           a.proc_ins_id,
           a.process_definition_id,
           prn.report_no,
           pa1.status as status1,
           pa1.id as auditId1,
           pa1.proc_ins_id as procInsId1,
           pa2.status as status2,
           pa2.id as auditId2,
           pa2.proc_ins_id as procInsId2,
           pa3.status as status3,
           pa3.id as auditId3,
           pa3.proc_ins_id as procInsId3,
           pa.status as archive_status,
           pa.id as archive_id,
           pa.proc_ins_id as procInsIdArchive,
           d.id AS issued_id,
           d.proc_ins_id AS procInsId4,
           d.status AS issued_status,
           wci.no AS contract_no,
           art.ID_ as task_id,
           art1.ID_ as task_id_audit1,
           art2.ID_ as task_id_audit2,
           art3.ID_ as task_id_audit3,
           artd.ID_ as task_id_issued,
           art_archive.ID_ as task_id_archive
        FROM
            program_project_list_info a
            LEFT JOIN sys_user b ON a.create_by = b.id
            LEFT JOIN sys_user c ON a.project_manager = c.id
            LEFT JOIN work_contract_info wci ON wci.id = a.contract_id and wci.del_flag = '0'
            LEFT JOIN program_report_no prn ON prn.program_id = a.id and prn.del_flag = '0'
            LEFT JOIN program_audit pa1 ON pa1.program_id = a.id and pa1.audit_level = '1' and pa1.del_flag = '0'
            LEFT JOIN program_audit pa2 ON pa2.program_id = a.id and pa2.audit_level = '2' and pa2.del_flag = '0'
            LEFT JOIN program_audit pa3 ON pa3.program_id = a.id and pa3.audit_level = '3' and pa3.del_flag = '0'
            LEFT JOIN program_archive pa ON pa.program_id = a.id and pa.del_flag = '0'
            left join program_members pm on a.id = pm.project_id
            LEFT JOIN proofread_issued d ON a.id = d.project_id
            LEFT JOIN act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
            LEFT JOIN act_ru_task art1 ON pa1.proc_ins_id = art1.PROC_INST_ID_
            LEFT JOIN act_ru_task art2 ON pa2.proc_ins_id = art2.PROC_INST_ID_
            LEFT JOIN act_ru_task art3 ON pa3.proc_ins_id = art3.PROC_INST_ID_
            LEFT JOIN act_ru_task artd ON d.proc_ins_id = artd.PROC_INST_ID_
            LEFT JOIN act_ru_task art_archive ON pa.proc_ins_id = art_archive.PROC_INST_ID_
            ${ew.customSqlSegment}
            ORDER BY a.update_date DESC
    </select>
    <select id="findPageList2" resultType="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
        SELECT
        distinct
           a.id,
           b.name AS create_by,
           a.create_date,
           a.update_by,
           a.update_date,
           a.del_flag,
           a.contract_id,
           a.contract_name,
           a.client,
           a.client_name,
           a.amount,
           a.contract_type,
           a.name,
           a.no,
           a.project_type,
           a.approval_no,
           a.company,
           a.project_mould,
           a.property_holder,
           a.property_holder_name,
           a.location,
           a.is_first,
           a.year_num,
           a.check_year,
           a.project_manager,
           c.name AS project_manager_name,
           a.report_type,
           a.industry,
           a.enterprise_type,
           a.risk_level,
           a.project_source,
           a.estimate,
           a.plan_end_time,
           a.use_num,
           a.appointment,
           a.work_hours,
           a.remarks,
           a.assessment_enterprise,
           a.assessment_enterprise_name,
           a.link_num,
           a.relationship,
           a.assessment_way,
           a.assessment_objective,
           a.assessment_date,
           a.num,
           a.project_development,
           a.delegate_project_type,
           a.assessment_object,
           a.work_begin_date,
           a.work_end_date,
           a.reporting_date,
           a.is_have,
           a.is_influence,
           a.mode,
           a.status,
           a.proc_ins_id,
           a.process_definition_id,
           prn.report_no,
           pa1.status as status1,
           pa1.id as auditId1,
           pa1.proc_ins_id as procInsId1,
           pa2.status as status2,
           pa2.id as auditId2,
           pa2.proc_ins_id as procInsId2,
           pa3.status as status3,
           pa3.id as auditId3,
           pa3.proc_ins_id as procInsId3,
           pa.status as archive_status,
           pa.id as archive_id,
           pa.proc_ins_id as procInsIdArchive,
           d.id AS issued_id,
           d.proc_ins_id AS procInsId4,
           d.status AS issued_status,
           wci.no AS contract_no
        FROM
            program_project_list_info a
            LEFT JOIN sys_user b ON a.create_by = b.id
            LEFT JOIN sys_user c ON a.project_manager = c.id
            LEFT JOIN work_contract_info wci ON wci.id = a.contract_id and wci.del_flag = '0'
            LEFT JOIN program_report_no prn ON prn.program_id = a.id and prn.del_flag = '0'
            LEFT JOIN program_audit pa1 ON pa1.program_id = a.id and pa1.audit_level = '1' and pa1.del_flag = '0'
            LEFT JOIN program_audit pa2 ON pa2.program_id = a.id and pa2.audit_level = '2' and pa2.del_flag = '0'
            LEFT JOIN program_audit pa3 ON pa3.program_id = a.id and pa3.audit_level = '3' and pa3.del_flag = '0'
            LEFT JOIN program_archive pa ON pa.program_id = a.id and pa.del_flag = '0'
            left join program_members pm on a.id = pm.project_id
            LEFT JOIN proofread_issued d ON a.id = d.project_id
            ${ew.customSqlSegment}
            ORDER BY a.update_date DESC
    </select>

    <select id="findInfoList" resultType="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
        SELECT
           a.id,
           b.name AS create_by,
           a.create_date,
           a.update_by,
           a.update_date,
           a.del_flag,
           a.contract_id,
           a.contract_name,
           a.client,
           a.client_name,
           a.amount,
           a.contract_type,
           a.name,
           a.no,
           a.project_type,
           a.approval_no,
           a.company,
           a.project_mould,
           a.property_holder,
           a.property_holder_name,
           a.location,
           a.is_first,
           a.year_num,
           a.check_year,
           a.project_manager,
           c.name AS project_manager_name,
           a.report_type,
           a.industry,
           a.enterprise_type,
           a.risk_level,
           a.project_source,
           a.estimate,
           a.plan_end_time,
           a.use_num,
           a.appointment,
           a.work_hours,
           a.remarks,
           a.assessment_enterprise,
           a.assessment_enterprise_name,
           a.link_num,
           a.relationship,
           a.assessment_way,
           a.assessment_objective,
           a.assessment_date,
           a.num,
           a.project_development,
           a.delegate_project_type,
           a.assessment_object,
           a.assessment_range,
           a.work_begin_date,
           a.work_end_date,
           a.reporting_date,
           a.is_have,
           a.is_influence,
           a.mode,
           a.status,
           a.proc_ins_id,
           a.process_definition_id,
           prn.report_no,
           pa1.status as status1,
           pa1.id as auditId1,
           pa1.proc_ins_id as procInsId1,
           pa2.status as status2,
           pa2.id as auditId2,
           pa2.proc_ins_id as procInsId2,
           pa3.status as status3,
           pa3.id as auditId3,
           pa3.proc_ins_id as procInsId3,
           pa.status as archive_status,
           pa.id as archive_id,
           pa.proc_ins_id as procInsIdArchive,
           d.id AS issued_id,
           d.proc_ins_id AS procInsId4,
           d.status AS issued_status
        FROM
            program_project_list_info a
            LEFT JOIN sys_user b ON a.create_by = b.id
            LEFT JOIN sys_user c ON a.project_manager = c.id
            LEFT JOIN program_report_no prn ON prn.program_id = a.id
            LEFT JOIN program_audit pa1 ON pa1.program_id = a.id and pa1.audit_level = '1' and pa1.del_flag = '0'
            LEFT JOIN program_audit pa2 ON pa2.program_id = a.id and pa2.audit_level = '2' and pa2.del_flag = '0'
            LEFT JOIN program_audit pa3 ON pa3.program_id = a.id and pa3.audit_level = '3' and pa3.del_flag = '0'
            LEFT JOIN program_archive pa ON pa.program_id = a.id and pa.del_flag = '0'
            LEFT JOIN proofread_issued d ON a.id = d.project_id
            ${ew.customSqlSegment}
            ORDER BY a.update_date DESC
    </select>

    <select id="getById" resultType="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
        SELECT
           a.id,
           b.name AS create_by,
           a.create_date,
           a.update_by,
           a.update_date,
           a.del_flag,
           a.contract_id,
           a.contract_name,
           a.client,
           a.client_name,
           a.amount,
           a.contract_type,
           a.name,
           a.no,
           a.project_type,
           a.approval_no,
           a.company,
           a.project_mould,
           a.property_holder,
           a.property_holder_name,
           a.location,
           a.is_first,
           a.year_num,
           a.check_year,
           a.project_manager,
           c.name AS project_manager_name,
           a.report_type,
           a.industry,
           a.enterprise_type,
           a.risk_level,
           a.project_source,
           a.estimate,
           a.plan_end_time,
           a.use_num,
           a.appointment,
           a.work_hours,
           a.remarks,
           a.assessment_enterprise,
           a.assessment_enterprise_name,
           a.link_num,
           a.relationship,
           a.assessment_way,
           a.assessment_objective,
           a.assessment_date,
           a.num,
           a.project_development,
           a.delegate_project_type,
           a.assessment_object,
           a.work_begin_date,
           a.work_end_date,
           a.reporting_date,
           a.is_have,
           a.is_influence,
           a.mode,
           a.status,
           a.proc_ins_id,
           a.process_definition_id,
           a.generate_report_number,
           prn.report_no,
           pa1.status as status1,
           pa1.id as auditId1,
           pa1.proc_ins_id as procInsId1,
           pa2.status as status2,
           pa2.id as auditId2,
           pa2.proc_ins_id as procInsId2,
           pa3.status as status3,
           pa3.id as auditId3,
           pa3.proc_ins_id as procInsId3,
           pa.status as archive_status,
           pa.id as archive_id,
           pa.proc_ins_id as procInsIdArchive,
           d.id AS issued_id,
           d.proc_ins_id AS procInsId4,
           d.status AS issued_status
        FROM
            program_project_list_info a
            LEFT JOIN sys_user b ON a.create_by = b.id
            LEFT JOIN sys_user c ON a.project_manager = c.id
            LEFT JOIN program_report_no prn ON prn.program_id = a.id
            LEFT JOIN program_audit pa1 ON pa1.program_id = a.id and pa1.audit_level = '1' and pa1.del_flag = '0'
            LEFT JOIN program_audit pa2 ON pa2.program_id = a.id and pa2.audit_level = '2' and pa2.del_flag = '0'
            LEFT JOIN program_audit pa3 ON pa3.program_id = a.id and pa3.audit_level = '3' and pa3.del_flag = '0'
            LEFT JOIN program_archive pa ON pa.program_id = a.id and pa.del_flag = '0'
            LEFT JOIN proofread_issued d ON a.id = d.project_id
        where a.id = #{id}
    </select>

    <select id="findList" resultType="com.jeeplus.test.program.configuration.projectList.service.dto.ExportFileDto">
        SELECT
            (@i:=@i+1) AS `no`,
            tab.*
        FROM
            (SELECT
                b.`year`,
                d.`name` AS client_name,
                a.`name`,
                a.assessment_objective,
                a.assessment_object,
                a.assessment_range,
                a.project_mould as program_type,
                DATE_FORMAT(a.assessment_date,'%Y-%m-%d') AS assessment_date,
                DATE_FORMAT(b.evaluation_report_date, '%Y-%m-%d') as "evaluation_report_date",
                b.protocol_num,
                c.no as contract_num,
                e.report_no,
                f.`name` AS project_manager_name,
                g.`name` AS signature_evaluator_first,
                h.`name` AS signature_evaluator_second,
                b.wayst_evaluation,
                b.fixed_assets_evaluation,
                b.net_assets_evaluation,
                b.rent_evaluation,
                b.forensics,
                b.report_charges,
                i.name as current_dispose_person,
                (CASE b.is_invoice WHEN 0 THEN "否" WHEN 1 THEN "是" ELSE "" END) AS is_invoice,
                DATE_FORMAT(b.invoice_date,'%Y-%m-%d') AS invoice_date,
                (CASE b.is_contract_archive WHEN 0 THEN "否" WHEN 1 THEN "是" ELSE "" END) AS is_contract_archive,
                (CASE b.is_papers_intact WHEN 0 THEN "否" WHEN 1 THEN "是" ELSE "" END) AS is_papers_intact,
                (CASE b.is_papers_archive WHEN 0 THEN "否" WHEN 1 THEN "是" ELSE "" END) AS is_papers_archive
            FROM
                program_project_list_info a
                LEFT JOIN program_archive b ON a.id = b.program_id
                LEFT JOIN work_contract_info c ON a.contract_id = c.id
                LEFT JOIN work_client_info d ON c.client_id = d.id
                LEFT JOIN program_report_no e ON a.id = e.program_id
                LEFT JOIN sys_user f ON a.project_manager = f.id
                LEFT JOIN sys_user i ON b.current_dispose_person = i.id
                LEFT JOIN sys_user g ON b.signature_evaluator_first = g.id
                LEFT JOIN sys_user h ON b.signature_evaluator_second = h.id
            WHERE a.del_flag = 0
            <if test="infoList != null">
                AND a.id IN
                <foreach collection="infoList" item="item" index="index" open="(" close=")" separator=",">
                    #{item.id}
                </foreach>
            </if>
            ORDER BY a.update_date DESC) AS tab
            ,(SELECT @i:=0) AS itable
    </select>

    <resultMap id="BaseResultMap" type="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="delFlag" column="del_flag" jdbcType="VARCHAR"/>
        <result property="contractId" column="contract_id" jdbcType="VARCHAR"/>
        <result property="contractName" column="contract_name" jdbcType="VARCHAR"/>
        <result property="client" column="client" jdbcType="VARCHAR"/>
        <result property="clientName" column="client_name" jdbcType="VARCHAR"/>
        <result property="amount" column="amount" jdbcType="VARCHAR"/>
        <result property="contractType" column="contract_type" jdbcType="VARCHAR"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="no" column="no" jdbcType="VARCHAR"/>
        <result property="projectType" column="project_type" jdbcType="VARCHAR"/>
        <result property="approvalNo" column="approval_no" jdbcType="VARCHAR"/>
        <result property="company" column="company" jdbcType="VARCHAR"/>
        <result property="projectMould" column="project_mould" jdbcType="VARCHAR"/>
        <result property="propertyHolder" column="property_holder" jdbcType="VARCHAR"/>
        <result property="propertyHolderName" column="property_holder_name" jdbcType="VARCHAR"/>
        <result property="location" column="location" jdbcType="VARCHAR"/>
        <result property="isFirst" column="is_first" jdbcType="VARCHAR"/>
        <result property="yearNum" column="year_num" jdbcType="VARCHAR"/>
        <result property="checkYear" column="check_year" jdbcType="VARCHAR"/>
        <result property="projectManager" column="project_manager" jdbcType="VARCHAR"/>
        <result property="projectManagerName" column="project_manager_name" jdbcType="VARCHAR"/>
        <result property="reportType" column="report_type" jdbcType="VARCHAR"/>
        <result property="industry" column="industry" jdbcType="VARCHAR"/>
        <result property="enterpriseType" column="enterprise_type" jdbcType="VARCHAR"/>
        <result property="riskLevel" column="risk_level" jdbcType="VARCHAR"/>
        <result property="projectSource" column="project_source" jdbcType="VARCHAR"/>
        <result property="estimate" column="estimate" jdbcType="DECIMAL"/>
        <result property="planEndTime" column="plan_end_time" jdbcType="TIMESTAMP"/>
        <result property="useNum" column="use_num" jdbcType="INTEGER"/>
        <result property="appointment" column="appointment" jdbcType="INTEGER"/>
        <result property="workHours" column="work_hours" jdbcType="DOUBLE"/>
        <result property="remarks" column="remarks" jdbcType="VARCHAR"/>
        <result property="assessmentEnterprise" column="assessment_enterprise" jdbcType="VARCHAR"/>
        <result property="assessmentEnterpriseName" column="assessment_enterprise_name" jdbcType="VARCHAR"/>
        <result property="linkNum" column="link_num" jdbcType="VARCHAR"/>
        <result property="relationship" column="relationship" jdbcType="VARCHAR"/>
        <result property="assessmentWay" column="assessment_way" jdbcType="VARCHAR"/>
        <result property="assessmentObjective" column="assessment_objective" jdbcType="VARCHAR"/>
        <result property="assessmentDate" column="assessment_date" jdbcType="TIMESTAMP"/>
        <result property="num" column="num" jdbcType="DECIMAL"/>
        <result property="projectDevelopment" column="project_development" jdbcType="VARCHAR"/>
        <result property="delegateProjectType" column="delegate_project_type" jdbcType="VARCHAR"/>
        <result property="assessmentObject" column="assessment_object" jdbcType="VARCHAR"/>
        <result property="workBeginDate" column="work_begin_date" jdbcType="TIMESTAMP"/>
        <result property="workEndDate" column="work_end_date" jdbcType="TIMESTAMP"/>
        <result property="reportingDate" column="reporting_date" jdbcType="TIMESTAMP"/>
        <result property="isHave" column="is_have" jdbcType="VARCHAR"/>
        <result property="isInfluence" column="is_influence" jdbcType="VARCHAR"/>
        <result property="mode" column="mode" 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"/>
    </resultMap>

    <sql id="Base_Column_List">
        ppli.id,
        ppli.create_by,
        ppli.create_date,
        ppli.update_by,
        ppli.update_date,
        ppli.del_flag,
        ppli.contract_id,
        ppli.contract_name,
        ppli.client,
        ppli.client_name,
        ppli.amount,
        ppli.contract_type,
        ppli.name,
        ppli.no,
        ppli.project_type,
        ppli.approval_no,
        ppli.company,
        ppli.project_mould,
        ppli.property_holder,
        ppli.property_holder_name,
        ppli.location,
        ppli.is_first,
        ppli.year_num,
        ppli.check_year,
        ppli.project_manager,
        ppli.project_manager_name,
        ppli.report_type,
        ppli.industry,
        ppli.enterprise_type,
        ppli.risk_level,
        ppli.project_source,
        ppli.estimate,
        ppli.plan_end_time,
        ppli.use_num,
        ppli.appointment,
        ppli.work_hours,
        ppli.remarks,
        ppli.assessment_enterprise,
        ppli.assessment_enterprise_name,
        ppli.link_num,
        ppli.relationship,
        ppli.assessment_way,
        ppli.assessment_objective,
        ppli.assessment_date,
        ppli.num,
        ppli.project_development,
        ppli.delegate_project_type,
        ppli.assessment_object,
        ppli.work_begin_date,
        ppli.work_end_date,
        ppli.reporting_date,
        ppli.is_have,
        ppli.is_influence,
        ppli.mode,
        ppli.status,
        ppli.proc_ins_id,
        ppli.process_definition_id
    </sql>
    <select id="findPage" resultType="com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo">
        SELECT
        <include refid="Base_Column_List"></include>,
        su.name AS create_by,
        sup.name as project_manager_name
        FROM
            program_project_list_info ppli
            LEFT JOIN sys_user su ON ppli.create_by = su.id
            LEFT JOIN sys_user sup ON ppli.project_manager = su.id
            LEFT JOIN
            ${ew.customSqlSegment}
            ORDER BY ppli.update_date DESC
    </select>

    <select id="selectInvoiceFlag" resultType="string">
        select
            case
                 when fi.status = '1' then '开票中'
                 when fi.status = '2' then '开票中'
                 when fi.status = '3' then '开票中'
                 when fi.status = '4' then '开票中'
                 when fi.status = '5' then '已开票'
                 when fi.status = '6' then '已开票'
                 when fi.status = '7' then '已开票'
                 when fi.status = '8' then '已开票'
            else '' end
        from finance_invoice_base fib
        inner join finance_invoice fi on fi.id = fib.invoice_id and fi.del_flag = '0'
        where fib.del_flag = '0' and fi.status != '9' and fib.program_id = #{id}
        order by fi.create_date desc
        limit 1
    </select>

    <select id="getHaveProjectIds" resultType="string">
        select
            pm.project_id
        from program_members pm
        where pm.user_id = #{currentUser.id} and pm.del_flag = '0'
    </select>
</mapper>
