<?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.TContractorAcceptanceDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.contractor.manage.model.domain.TContractorAcceptance">
                <id column="id" property="id"/>
                <id column="contractor_id" property="contractorId"/>
                <id column="proj_id" property="projId"/>
                <id column="biz_id" property="bizId"/>
                <id column="process_id" property="processId"/>
                <id column="process_status" property="processStatus"/>
                <id column="department_major" property="departmentMajor"/>
                <id column="department_belong" property="departmentBelong"/>
                <id column="finish_date" property="finishDate"/>
                <id column="org_id" property="orgId"/>
    </resultMap>
    <select id="listEntity"
            resultType="com.testor.module.contractor.manage.model.dto.TContractorAcceptanceDTO">
        SELECT
            tca.id,
            tci.name AS contractorName,
            tcp.name AS projectName,
            tsu.user_name,
            tca.create_date,
            tca.process_status
        FROM
            t_contractor_acceptance AS tca
                LEFT JOIN t_contractor_ledger AS tcl ON tcl.id = tca.contractor_id
                LEFT JOIN t_contractor_info AS tci ON tci.id = tcl.info_id
                LEFT JOIN t_contractor_project AS tcp ON tcp.id = tca.proj_id
                left join t_sys_user tsu on tsu.user_id = tca.create_by
        <!--<where>
            tca.status != '1'
            and tca.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})
            and tca.status = '0'
            <if test="param.contractorName != null and param.contractorName != ''">
                and tci.name like concat('%',#{param.contractorName},'%')
            </if>
            order by tca.update_date desc
        </where>-->
        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 tca.status != '1'
        and tca.status = '0'
        <if test="param.contractorName != null and param.contractorName != ''">
            and tci.name like concat('%',#{param.contractorName},'%')
        </if>
        order by tca.update_date desc
    </select>
</mapper>