<?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.hz.pm.api.projectlib.mapper.ProjectMapper">
    <select id="pageAllWithPermission" resultType="com.hz.pm.api.projectlib.model.po.ProjectPO"
            parameterType="com.hz.pm.api.projectlib.model.req.ProjectListReq">
        SELECT
            from_type,
            id,
            project_name,
            project_year,
            build_org_code,
            build_org_name,
            project_code,
            project_type,
            area,
            area_code,
            declare_amount,
            approval_amount,
            stage,
            status,
            annual_plan_amount,
            biz_domain,
            inst_code,
            is_higher_super_org,
            pre_plan_project_id,
            create_on,
            update_on
        from
            (SELECT
                 1 as from_type,
                 id,
                 project_name,
                 project_year,
                 build_org_code,
                 build_org_name,
                 project_code,
                 project_type,
                 area,
                 area_code,
                 declare_amount,
                 approval_amount,
                 stage,
                 status,
                 annual_plan_amount,
                 biz_domain,
                 inst_code,
                 is_higher_super_org,
                 pre_plan_project_id,
                 create_on,
                 update_on
             FROM nd_project p
             WHERE p.newest = 1
            <if test="req.id != null">
                and p.id = #{req.id}
            </if>
            <if test="req.projectName != null and req.projectName != ''">
                and p.project_name like concat('%',#{req.projectName},'%')
            </if>
            <if test="req.projectType != null and req.projectType != ''">
                and p.project_type = #{req.projectType}
            </if>
            <if test="req.buildOrgName != null and req.buildOrgName != ''">
                and p.build_org_name like concat('%',#{req.buildOrgName},'%')
            </if>
            <if test="req.buildOrg != null and req.buildOrg != ''">
                and p.build_org_name like concat('%',#{req.buildOrg},'%')
            </if>
            <if test="req.status != null">
                and p.status = #{req.status}
            </if>
            <if test="req.projectYear != null">
                and p.project_year = #{req.projectYear}
            </if>
            <if test="req.createOnMin != null">
                and p.create_on &gt;= #{req.createOnMin}
            </if>
            <if test="req.createOnMax != null">
                and p.create_on &lt;= #{req.createOnMax}
            </if>
            <if test="req.declareAmountMin != null">
                and p.declare_amount &gt;= #{req.declareAmountMin}
            </if>
            <if test="req.declareAmountMax != null">
                and p.declare_amount &lt;= #{req.declareAmountMax}
            </if>
            <if test="req.approveAmountMin != null">
                and p.approval_amount &gt;= #{req.approveAmountMin}
            </if>
            <if test="req.approveAmountMax != null">
                and p.approval_amount &lt;= #{req.approveAmountMax}
            </if>
            <if test="req.userType != null and req.userType == 'normal'">
                and p.build_org_code = #{req.userValue}
            </if>
            <if test="req.userType != null and req.userType == 'org'">
                and (p.build_org_code = #{req.userValue} or
                 (p.stage = 10000 and p.status = 10003 and p.super_org_code = #{req.userValue}))
            </if>
            <if test="req.userType != null and req.userType == 'region'">
                <if test="req.regionCodes != null">
                    and p.area_code in
                    <foreach close=")" collection="req.regionCodes" item="code" open="(" separator=",">
                        #{code}
                    </foreach>
                </if>
                <if test="req.userRegionCode != null">
                    and p.area_code = #{req.userRegionCode}
                </if>
            </if>
             UNION ALL
             (
                 SELECT
                     2 AS from_type,
                     b.id id,
                     b.base_proj_name project_name,
                     CAST(apply.base_proj_set_year AS INTEGER) project_year,
                     b.base_build_deprt_ding build_org_code,
                     b.base_build_deprt build_org_name,
                     b.base_proj_id project_code,
                     b.base_proj_type project_type,
                     b.base_area_name area,
                     (CASE WHEN b.base_area_code IS NOT NULL then substring(b.base_area_code,0,6) end) area_code,
                     apply.base_proj_decl_amount declare_amount,
                     approve.base_initial_review_total_money approval_amount,
                     NULL stage,
                     CAST(b.base_proj_set_prog AS INTEGER) status,
                     NULL annual_plan_amount,
                     NULL biz_domain,
                     NULL inst_code,
                     NULL is_higher_super_org,
                     NULL pre_plan_project_id,
                     b.tong_time create_on,
                     b.biz_time update_on
                 FROM gov_biz_project_baseinfo b
                          LEFT JOIN gov_biz_project_apply apply ON b.base_proj_id = apply.base_proj_id
                          LEFT JOIN gov_biz_project_approve approve ON b.base_proj_id = approve.base_proj_id
                 WHERE b.deleted = 0
                <if test="req.id != null">
                    and b.id = #{req.id}
                </if>
                <if test="req.projectName != null and req.projectName != ''">
                    and b.base_proj_name like concat('%',#{req.projectName},'%')
                </if>
                <if test="req.projectType != null and req.projectType != ''">
                    and b.base_proj_type = #{req.projectType}
                </if>
                <if test="req.buildOrgName != null and req.buildOrgName != ''">
                    and b.base_build_deprt like concat('%',#{req.buildOrgName},'%')
                </if>
                <if test="req.buildOrg != null and req.buildOrg != ''">
                    and b.base_build_deprt like concat('%',#{req.buildOrg},'%')
                </if>
                <if test="req.status != null">
                    and b.base_proj_set_prog = #{req.status}
                </if>
                <if test="req.projectYear != null">
                    and apply.base_proj_set_year = #{req.projectYear}
                </if>
                <if test="req.createOnMin != null">
                    and b.tong_time &gt;= #{req.createOnMin}
                </if>
                <if test="req.createOnMax != null">
                    and b.tong_time &lt;= #{req.createOnMax}
                </if>
                <if test="req.declareAmountMin != null">
                    and apply.base_proj_decl_amount &gt;= #{req.declareAmountMin}
                </if>
                <if test="req.declareAmountMax != null">
                    and apply.base_proj_decl_amount &lt;= #{req.declareAmountMax}
                </if>
                <if test="req.approveAmountMin != null">
                    and approve.base_initial_review_total_money &gt;= #{req.approveAmountMin}
                </if>
                <if test="req.approveAmountMax != null">
                    and approve.base_initial_review_total_money &lt;= #{req.approveAmountMax}
                </if>
                <if test="req.userType != null and req.userType == 'normal'">
                    and b.base_build_deprt_ding = #{req.userValue}
                </if>
                <if test="req.userType != null and req.userType == 'org'">
                    and b.base_build_deprt_ding = #{req.userValue}
                </if>
                <if test="req.userType != null and req.userType == 'region'">
                    <if test="req.regionCodes != null">
                        and b.base_area_code in
                        <foreach close=")" collection="req.regionCodes" item="code" open="(" separator=",">
                            concat(#{code},'000')
                        </foreach>
                    </if>
                    <if test="req.userRegionCode != null">
                        and b.base_area_code = concat(#{req.userRegionCode},'000')
                    </if>
                </if>
             )
                UNION ALL
            (
                SELECT
                3 AS from_type,
                b.id id,
                b.base_proj_name project_name,
                CAST(apply.base_proj_set_year AS INTEGER) project_year,
                b.base_build_deprt_ding build_org_code,
                b.base_build_deprt build_org_name,
                b.base_proj_id project_code,
                b.base_proj_type project_type,
                b.base_area_name area,
                (CASE WHEN b.base_area_code IS NOT NULL then substring(b.base_area_code,0,6) end) area_code,
                apply.base_proj_decl_amount declare_amount,
                approve.base_initial_review_total_money approval_amount,
                NULL stage,
                CAST(b.base_proj_set_prog AS INTEGER) status,
                NULL annual_plan_amount,
                NULL biz_domain,
                NULL inst_code,
                NULL is_higher_super_org,
                NULL pre_plan_project_id,
                b.tong_time create_on,
                b.biz_time update_on
                FROM gov_operation_project_baseinfo b
                LEFT JOIN gov_biz_project_apply apply ON b.base_proj_id = apply.base_proj_id
                LEFT JOIN gov_biz_project_approve approve ON b.base_proj_id = approve.base_proj_id
                WHERE b.deleted = 0
                <if test="req.id != null">
                    and b.id = #{req.id}
                </if>
                <if test="req.projectName != null and req.projectName != ''">
                    and b.base_proj_name like concat('%',#{req.projectName},'%')
                </if>
                <if test="req.projectType != null and req.projectType != ''">
                    and b.base_proj_type = #{req.projectType}
                </if>
                <if test="req.buildOrgName != null and req.buildOrgName != ''">
                    and b.base_build_deprt like concat('%',#{req.buildOrgName},'%')
                </if>
                <if test="req.buildOrg != null and req.buildOrg != ''">
                    and b.base_build_deprt like concat('%',#{req.buildOrg},'%')
                </if>
                <if test="req.status != null">
                    and b.base_proj_set_prog = #{req.status}
                </if>
                <if test="req.projectYear != null">
                    and apply.base_proj_set_year = #{req.projectYear}
                </if>
                <if test="req.createOnMin != null">
                    and b.tong_time &gt;= #{req.createOnMin}
                </if>
                <if test="req.createOnMax != null">
                    and b.tong_time &lt;= #{req.createOnMax}
                </if>
                <if test="req.declareAmountMin != null">
                    and apply.base_proj_decl_amount &gt;= #{req.declareAmountMin}
                </if>
                <if test="req.declareAmountMax != null">
                    and apply.base_proj_decl_amount &lt;= #{req.declareAmountMax}
                </if>
                <if test="req.approveAmountMin != null">
                    and approve.base_initial_review_total_money &gt;= #{req.approveAmountMin}
                </if>
                <if test="req.approveAmountMax != null">
                    and approve.base_initial_review_total_money &lt;= #{req.approveAmountMax}
                </if>
                <if test="req.userType != null and req.userType == 'normal'">
                    and b.base_build_deprt_ding = #{req.userValue}
                </if>
                <if test="req.userType != null and req.userType == 'org'">
                    and b.base_build_deprt_ding = #{req.userValue}
                </if>
                <if test="req.userType != null and req.userType == 'region'">
                    <if test="req.regionCodes != null">
                        and b.base_area_code in
                        <foreach close=")" collection="req.regionCodes" item="code" open="(" separator=",">
                            concat(#{code},'000')
                        </foreach>
                    </if>
                    <if test="req.userRegionCode != null">
                        and b.base_area_code = concat(#{req.userRegionCode},'000')
                    </if>
                </if>
                )
            ) ORDER BY update_on DESC
    </select>

    <select id="pageLibWithPermission" resultType="com.hz.pm.api.projectlib.model.po.ProjectPO"
            parameterType="com.hz.pm.api.projectlib.model.req.ProjectListReq">
        SELECT
        from_type,
        id,
        project_name,
        project_year,
        build_org_code,
        build_org_name,
        project_code,
        project_type,
        area,
        area_code,
        declare_amount,
        approval_amount,
        stage,
        status,
        annual_plan_amount,
        biz_domain,
        inst_code,
        is_higher_super_org,
        pre_plan_project_id,
        create_on,
        update_on
        from
        (SELECT
        1 as from_type,
        id,
        project_name,
        project_year,
        build_org_code,
        build_org_name,
        project_code,
        project_type,
        area,
        area_code,
        declare_amount,
        approval_amount,
        stage,
        status,
        annual_plan_amount,
        biz_domain,
        inst_code,
        is_higher_super_org,
        pre_plan_project_id,
        create_on,
        update_on
        FROM nd_project p
        WHERE p.newest = 1
        <if test="req.id != null">
            and p.id = #{req.id}
        </if>
        <if test="req.projectName != null and req.projectName != ''">
            and p.project_name like concat('%',#{req.projectName},'%')
        </if>
        <if test="req.projectType != null and req.projectType != ''">
            and p.project_type = #{req.projectType}
        </if>
        <if test="req.buildOrgName != null and req.buildOrgName != ''">
            and p.build_org_name like concat('%',#{req.buildOrgName},'%')
        </if>
        <if test="req.buildOrg != null and req.buildOrg != ''">
            and p.build_org_name like concat('%',#{req.buildOrg},'%')
        </if>
        <if test="req.status != null">
            and p.status = #{req.status}
        </if>
        <if test="req.projectYear != null">
            and p.project_year = #{req.projectYear}
        </if>
        <if test="req.createOnMin != null">
            and p.create_on &gt;= #{req.createOnMin}
        </if>
        <if test="req.createOnMax != null">
            and p.create_on &lt;= #{req.createOnMax}
        </if>
        <if test="req.declareAmountMin != null">
            and p.declare_amount &gt;= #{req.declareAmountMin}
        </if>
        <if test="req.declareAmountMax != null">
            and p.declare_amount &lt;= #{req.declareAmountMax}
        </if>
        <if test="req.approveAmountMin != null">
            and p.approval_amount &gt;= #{req.approveAmountMin}
        </if>
        <if test="req.approveAmountMax != null">
            and p.approval_amount &lt;= #{req.approveAmountMax}
        </if>
        <if test="req.userType != null and req.userType == 'normal'">
            and p.build_org_code = #{req.userValue}
        </if>
        <if test="req.userType != null and req.userType == 'org'">
            and (p.build_org_code = #{req.userValue} or
            (p.stage = 10000 and p.status = 10003 and p.super_org_code = #{req.userValue}))
        </if>
        UNION ALL
        (
        SELECT
        3 AS from_type,
        b.id id,
        b.base_proj_name project_name,
        CAST(apply.base_proj_set_year AS INTEGER) project_year,
        b.base_build_deprt_ding build_org_code,
        b.base_build_deprt build_org_name,
        b.base_proj_id project_code,
        b.base_proj_type project_type,
        b.base_area_name area,
        (CASE WHEN b.base_area_code IS NOT NULL then substring(b.base_area_code,0,6) end) area_code,
        apply.base_proj_decl_amount declare_amount,
        approve.base_initial_review_total_money approval_amount,
        NULL stage,
        CAST(b.base_proj_set_prog AS INTEGER) status,
        apply.base_proj_decl_amount annual_plan_amount,
        NULL biz_domain,
        NULL inst_code,
        NULL is_higher_super_org,
        NULL pre_plan_project_id,
        b.tong_time create_on,
        b.biz_time update_on
        FROM gov_operation_project_baseinfo b
        LEFT JOIN gov_biz_project_apply apply ON b.base_proj_id = apply.base_proj_id
        LEFT JOIN gov_biz_project_approve approve ON b.base_proj_id = approve.base_proj_id
        WHERE b.deleted = 0 and b.base_proj_type in ('04','05')
        <if test="req.id != null">
            and b.id = #{req.id}
        </if>
        <if test="req.projectName != null and req.projectName != ''">
            and b.base_proj_name like concat('%',#{req.projectName},'%')
        </if>
        <if test="req.projectType != null and req.projectType != ''">
            and b.base_proj_type = #{req.projectType}
        </if>
        <if test="req.buildOrgName != null and req.buildOrgName != ''">
            and b.base_build_deprt like concat('%',#{req.buildOrgName},'%')
        </if>
        <if test="req.buildOrg != null and req.buildOrg != ''">
            and b.base_build_deprt like concat('%',#{req.buildOrg},'%')
        </if>
        <if test="req.status != null">
            and b.base_proj_set_prog = #{req.status}
        </if>
        <if test="req.projectYear != null">
            and apply.base_proj_set_year = #{req.projectYear}
        </if>
        <if test="req.createOnMin != null">
            and b.tong_time &gt;= #{req.createOnMin}
        </if>
        <if test="req.createOnMax != null">
            and b.tong_time &lt;= #{req.createOnMax}
        </if>
        <if test="req.declareAmountMin != null">
            and apply.base_proj_decl_amount &gt;= #{req.declareAmountMin}
        </if>
        <if test="req.declareAmountMax != null">
            and apply.base_proj_decl_amount &lt;= #{req.declareAmountMax}
        </if>
        <if test="req.approveAmountMin != null">
            and approve.base_initial_review_total_money &gt;= #{req.approveAmountMin}
        </if>
        <if test="req.approveAmountMax != null">
            and approve.base_initial_review_total_money &lt;= #{req.approveAmountMax}
        </if>
        <if test="req.userType != null and req.userType == 'normal'">
            and b.base_build_deprt_ding = #{req.userValue}
        </if>
        <if test="req.userType != null and req.userType == 'org'">
            and b.base_build_deprt_ding = #{req.userValue}
        </if>)
        ) ORDER BY update_on DESC
    </select>
</mapper>
