<?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.yeejoin.amos.boot.module.hygf.api.mapper.FinancingInfoMapper">


    <select id="getStationFinancingInfoList" resultType="java.util.Map">
        SELECT sta.* FROM  (SELECT
        hph.sequence_nbr AS sequenceNbr,
        hph.peasant_household_no peasantHouseholdNo,
        hph.owners_name AS ownersName,
        concat(hph.project_address_name,project_address_detail) AS projectAddress,
        hph.regional_companies_name AS regionalCompaniesName,
        IFNULL( info.`status`, '待推送' ) AS status,
        (select count( hfro.sequence_nbr ) from  hygf_financing_rectification_order hfro where  hfro.peasant_household_id = hph.sequence_nbr  )orderNum,
        (select CONCAT_WS(',',instance_id,node_routing) instanceId FROM  hygf_financing_auditing  WHERE hygf_financing_auditing.peasant_household_id = hph.sequence_nbr ORDER BY rec_date desc limit 1) instanceId,
        hygf_unit_info.head_name responsibleUserName,
        hygf_unit_info.head_phone  responsibleUserPhone,
        hph.regional_companies_code,
        hph.developer_code
        FROM
        `hygf_peasant_household` hph
        LEFT JOIN hygf_financing_info info ON info.peasant_household_id = hph.sequence_nbr
        LEFT JOIN hygf_unit_info  on hph.developer_code = hygf_unit_info.amos_company_code
        <where>
            hph.construction_state = '验收完成'
            <if test="params.ownersName != null and params.ownersName !=''">
                and hph.owners_name like concat('%',#{params.ownersName},'%')
            </if>
            <if test="params.regionalCompaniesCode != null and params.regionalCompaniesCode !=''">
                and hph.regional_companies_code = #{params.regionalCompaniesCode}
            </if>
            <if test="params.type == 2 ">
                and info.status in ('待融资审核','审核通过','放款完成' ) and info.financing_companies_seq = #{params.financingCompaniesSeq}
            </if>
            <if test="params.type == 3 ">
                and info.status in ('待整改','整改未完成')
            </if>
        </where>
        ORDER BY
        info.rec_date DESC ,hph.sequence_nbr DESC ) as sta
        <where>
            <if test="params.status != null and params.status != ''">
                and sta.status = #{params.status}
            </if>
        </where>
    </select>
    <select id="selectDataInfo" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.FinancingInfoDto">
        SELECT
            hfi.*,
            (select instance_id FROM hygf_financing_auditing WHERE peasant_household_id = hfi.peasant_household_id  ORDER BY rec_date desc LIMIT 1) as instanceId
        FROM
            `hygf_financing_info` hfi
        WHERE
            hfi.peasant_household_id = #{peasantHouseholdId}
    </select>
    <select id="selectOrgList" resultType="java.util.Map">
        select
        *
        from
         privilege_company
        where
            PARENT_ID = (select SEQUENCE_NBR from privilege_company where COMPANY_NAME = '融资机构')
    </select>


    <select id="selectRZOrgInfo" resultType="java.util.Map">
        select
            *
        from
            privilege_company
        where
            SEQUENCE_NBR=#{id}
    </select>
</mapper>
