<?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.ProjectRenewalFundDeclarationMapper">

    <select id="pageSql"
            parameterType="com.hz.pm.api.projectlib.model.req.ProjectRenewalListReq"
            resultType="com.hz.pm.api.projectlib.model.po.ProjectRenewalFundDeclarationPO">
        select
            prfd.id,prfd.create_on,prfd.update_on,prfd.project_id,
            prfd.project_year,prfd.annual_payment_amount,prfd.have_amount,
            prfd.gov_own_finance_amount,prfd.gov_superior_finance_amount,
            prfd.bank_lending_amount,prfd.other_amount,prfd.approval_status,prfd.deleted,
            (case when p.project_type is not null then p.project_type
            when b.base_proj_type is not null then b.base_proj_type
            else ob.base_proj_type end) project_type,
            (case when p.project_name is not null then p.project_name
            when b.base_proj_name is not null then b.base_proj_name
                else ob.base_proj_name end) project_name,
            (case when p.build_org_name is not null then p.build_org_name
            when b.base_build_deprt is not null then b.base_build_deprt
            else ob.base_build_deprt end) build_org_name,
            prfd.project_code
        from nd_project_renewal_fund_declaration prfd
        left join nd_project p on prfd.project_code = p.project_code and p.newest = 1
        left join gov_biz_project_baseinfo b on prfd.project_code = b.base_proj_id and b.deleted = 0
        left join gov_operation_project_baseinfo ob on prfd.project_code = ob.base_proj_id and ob.deleted = 0
        where prfd.deleted = 0
        <if test="param.projectName != null and param.projectName != ''">
            and (p.project_name like concat('%',#{param.projectName},'%') or
                 b.base_proj_name like concat('%',#{param.projectName},'%') or
                ob.base_proj_name like concat('%',#{param.projectName},'%'))
        </if>
        <if test="param.orgCode != null and param.orgCode != ''">
            and prfd.org_code = #{param.orgCode}
        </if>
        <if test="param.regionCode != null and param.regionCode != ''">
            and prfd.region_code = #{param.regionCode}
        </if>
        <if test="param.projectType != null">
            and (p.project_type = #{param.projectType} or
                 b.base_proj_type = #{param.projectType} or
                ob.base_proj_type = #{param.projectType})
        </if>
        <if test="param.year != null">
            and prfd.project_year = #{param.year}
        </if>
        <if test="param.stage != null">
            and p.stage = #{param.stage}
        </if>
        <if test="param.status != null">
            and (p.status = #{param.status} or
                 b.base_proj_set_prog = #{param.status} or
                ob.base_proj_set_prog = #{param.status})
        </if>
        <if test="param.startTime != null">
            and prfd.create_on &gt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            and prfd.create_on &lt;= #{param.endTime}
        </if>
        <if test="param.approvalStatus != null">
            and prfd.approval_status = #{param.approvalStatus}
        </if>
        <if test="param.userType != null and param.userType == 'normal'">
            and prfd.org_code = #{param.userValue}
        </if>
        <if test="param.userType != null and param.userType == 'org'">
            and prfd.org_code = #{param.userValue}
        </if>
        <if test="param.userType != null and param.userType == 'region'">
            <if test="param.regionCodes != null">
                and prfd.region_code in
                <foreach close=")" collection="param.regionCodes" item="code" open="(" separator=",">
                    #{code}
                </foreach>
            </if>
            <if test="param.userRegionCode != null">
                and prfd.region_code = #{param.userRegionCode}
            </if>
        </if>
        order by prfd.update_on desc
    </select>
</mapper>
