<?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.testor.module.contractor.manage.dao.TContractorPreparationDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.contractor.manage.model.domain.TContractorPreparation">
        <id column="id" property="id"/>
        <id column="contractor_id" property="contractorId"/>
        <id column="proj_id" property="projId"/>
        <id column="process_id" property="processId"/>
        <id column="process_status" property="processStatus"/>
        <id column="biz_id" property="bizId"/>
        <id column="plan_start_time" property="planStartTime"/>
        <id column="plan_end_time" property="planEndTime"/>
        <id column="construction_project_desc" property="constructionProjectDesc"/>
        <id column="construction_location" property="constructionLocation"/>
        <id column="person_id" property="personId"/>
        <id column="project_manager" property="projectManager"/>
        <id column="phone" property="phone"/>
        <id column="construction_scheme" property="constructionScheme"/>
        <id column="contingency_plan" property="contingencyPlan"/>
        <id column="safety_notify" property="safetyNotify"/>
        <id column="safety_pledge" property="safetyPledge"/>
        <id column="reject_opinion" property="rejectOpinion"/>
    </resultMap>
    <select id="pageList" resultType="com.testor.module.contractor.manage.model.dto.ContractorPreparationDto">
        SELECT
            t.id,
            t.contractor_id,
            tci.NAME as contractorName,
            t.proj_id,
            tcp.name as projectName,
            t.biz_id,
            t.process_id,
            t.process_status,
            t.plan_start_time,
            t.plan_end_time,
            t.construction_project_desc,
            t.construction_location,
            t.person_id,
            t.project_manager,
            t.phone,
            t.construction_scheme,
            t.contingency_plan,
            t.safety_notify,
            t.safety_pledge,
            t.reject_opinion,
            t.status,
            t.create_by,
            t.create_date,
            t.update_by,
            t.update_date,
            t.remarks,
            (select count(1) as analysisNum from t_contractor_preparation_analysis where preparation_id = t.id) as analysisNum
        FROM
            t_contractor_preparation t
                LEFT JOIN t_contractor_ledger tcl ON t.contractor_id = tcl.id
                LEFT JOIN t_contractor_info tci ON tcl.info_id = tci.id
                LEFT JOIN t_contractor_project tcp ON t.proj_id = tcp.id
        <!--WHERE
            t.org_id IN (select org.org_id from t_sys_org org where org.parent_ids like concat('%',#{param.orgId},'%') or org.org_id = #{param.orgId})-->
        WHERE EXISTS (
        SELECT 1
        FROM unnest(string_to_array(tcl.org_id, ',')) AS org(id)
        JOIN t_sys_org o ON org.id = o.org_id
        WHERE
        o.parent_ids LIKE concat('%',#{param.orgId},'%')
        OR o.org_id = #{param.orgId}
        )
            and t.status != '1'
        <if test="param.contractorName != null and param.contractorName != ''">
            AND tci.name like concat('%',#{param.contractorName},'%')
        </if>
        <if test="param.constructionLocation != null and param.constructionLocation != ''">
            AND t.construction_location like concat('%',#{param.constructionLocation},'%')
        </if>
        order by t.update_date desc
    </select>
</mapper>