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

    <select id="surveyStatusTotal" resultType="map">
        SELECT
        SUM(CASE WHEN ps.process_status = '不通过' THEN 1 ELSE 0 END)AS '未通过',
        SUM(CASE WHEN ps.process_status = '进行中' AND ps.business_status = '通过' AND next_node_name = '设计上传典型图库' THEN 1 ELSE 0 END)  AS '已完成(法务审核)',
        SUM(CASE WHEN ps.process_status = '完成' THEN 1 ELSE 0 END)  AS completeTotal,
        SUM(CASE WHEN ps.process_status = '进行中' AND (ps.business_status IS NULL OR ps.design_status IS NULL OR ps.technology_status IS NULL  or  ps.business_status = '' OR ps.design_status = '' OR ps.technology_status = '') THEN 1 ELSE 0 END)  AS '进行中(审核)',
        (		SELECT count(*)  FROM hygf_peasant_household WHERE construction_state = '待勘察' and is_delete = 0
        <if test="developerCode != null and developerCode != ''">
            AND hygf_peasant_household.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
        ) AS '未勘察'
        FROM
            hygf_power_station ps
                LEFT JOIN
            hygf_peasant_household ph ON ps.peasant_household_id = ph.sequence_nbr
        WHERE
        ps.is_delete = 0
        <if test="developerCode != null and developerCode != ''">
            AND ph.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND ph.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND ph.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND ph.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND ph.project_address LIKE CONCAT ('%',#{district},'%')
        </if>

    </select>

    <select id="contractStatusTotal" resultType="map">
        SELECT

        SUM(CASE WHEN status  = '未签署' and  initiate_status = '未发起' THEN 1 ELSE 0 END)  AS '未发起',
        SUM(CASE WHEN status  = '双方已签署' THEN 1 ELSE 0 END)  AS '双方已签署',
        SUM(CASE WHEN status  = '农户已签署' THEN 1 ELSE 0 END)  AS '农户已签署',
        SUM(CASE WHEN status  = '已作废' THEN 1 ELSE 0 END)  AS '已作废'
        FROM
        hygf_household_contract
        LEFT JOIN
        hygf_peasant_household ON hygf_household_contract.peasant_household_id = hygf_peasant_household.sequence_nbr   WHERE
        status != '已作废'
        <if test="developerCode != null and developerCode != ''">
            AND hygf_peasant_household.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
    </select>

    <select id="desionTotal" resultType="map">
        SELECT
        SUM( CASE WHEN next_node_name = '经销商设计人员上传设计图' and process_status = '进行中'THEN 1 ELSE 0 END ) AS '典设图已上传',
        SUM( CASE WHEN next_node_name = '设计图纸审核' and process_status = '进行中'THEN 1 ELSE 0 END ) AS '施工图已上传'
        FROM
        `hygf_power_station`
        LEFT JOIN
        hygf_peasant_household ON hygf_power_station.peasant_household_id = hygf_peasant_household.sequence_nbr
        <where>
            next_node_name IN ( '经销商设计人员上传设计图', '设计上传典型图库' )
            AND process_status = '进行中'
            <if test="developerCode != null and developerCode != ''">
                AND hygf_peasant_household.developer_code = #{developerCode}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="province != null and province != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
            </if>
            <if test="city != null and city != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
            </if>
            <if test="district != null and district != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
            </if>
        </where>

    </select>

    <select id="preparationMoneyTotal" resultType="map">
        SELECT
        SUM( CASE WHEN preparation_money_state = '待发货' or preparation_money_state = '暂存发货'or preparation_money_state = '待补货'     THEN 1 ELSE 0 END )  AS '待发货',
        SUM( CASE WHEN preparation_money_state = '备货完成'  THEN 1 ELSE 0 END )  AS '已收货',
        SUM( CASE WHEN preparation_money_state = '待收货'  THEN 1 ELSE 0 END )  AS '已发货'
        FROM
        hygf_peasant_household
        WHERE
        preparation_money_state IS NOT NULL
        <if test="developerCode != null and developerCode != ''">
            AND hygf_peasant_household.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
    </select>

    <select id="workOrderTotal" resultType="map">
        SELECT
        count(*) AS total,
        (CASE WHEN power_station_construction_status = '待施工' THEN '派工发料'
        WHEN power_station_construction_status = '未通过' THEN '未通过'
        WHEN power_station_construction_status = '施工中' or  power_station_construction_status = '待登记' THEN '施工资料已上传'
        WHEN power_station_construction_status = '待审核' and  work_order_power_station_node IS NULL THEN '完工登记'
        WHEN power_station_construction_status = '待审核' and  work_order_power_station_node ='area' THEN '完工自审'
        WHEN power_station_construction_status = '已完工' THEN '已完成'
        WHEN power_station_construction_status = '待审核' and  work_order_power_station_node in ('engineering','design')   THEN '审核中'
        ELSE power_station_construction_status END )
        AS statusText,
        ROUND(COALESCE(SUM(ei.real_scale), 0), 2) AS realScale        FROM
        hygf_work_order_power_station
        LEFT JOIN
        hygf_power_station_engineering_info ei on ei.work_order_power_station_id =hygf_work_order_power_station.sequence_nbr
        LEFT JOIN
        hygf_peasant_household ON hygf_work_order_power_station.peasant_household_id = hygf_peasant_household.sequence_nbr
        <where>
            <if test="developerCode != null and developerCode != ''">
                AND hygf_peasant_household.developer_code = #{developerCode}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="province != null and province != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
            </if>
            <if test="city != null and city != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
            </if>
            <if test="district != null and district != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
            </if>
        </where>
        GROUP BY
        power_station_construction_status,work_order_power_station_node
    </select>

    <select id="gridStatusTotal" resultType="map">
        SELECT
        COUNT( * ) AS total,
        CASE WHEN grid_status = '1' THEN '待登记'
        WHEN grid_status = '2' THEN '审核中'
        WHEN grid_status = '3' THEN '已完成'
        WHEN grid_status = '4' THEN '未通过'
        WHEN grid_status = '5' THEN '审核中'
        WHEN grid_status = '6' THEN '审核中'
        WHEN grid_status = '7' THEN '审核中'
        WHEN grid_status = '8' THEN '审核中'
        WHEN grid_status = '9' THEN '整改待审核'
        ELSE '待整改' END AS statusText,
        ROUND(COALESCE(SUM(ei.real_scale), 0), 2) AS realScale
        FROM
        hygf_basic_grid_record
        LEFT JOIN hygf_work_order_power_station ps ON ps.sequence_nbr = hygf_basic_grid_record.work_order_power_station_id
        LEFT JOIN hygf_power_station_engineering_info ei ON ei.work_order_power_station_id = ps.sequence_nbr
        LEFT JOIN hygf_peasant_household ON hygf_basic_grid_record.peasant_household_id = hygf_peasant_household.sequence_nbr
        <where>
            <if test="developerCode != null and developerCode != ''">
                AND hygf_peasant_household.developer_code = #{developerCode}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="province != null and province != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
            </if>
            <if test="city != null and city != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
            </if>
            <if test="district != null and district != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
            </if>
        </where>
        GROUP BY
        grid_status
    </select>

    <select id="acceptanceStatusTotal" resultType="map">
        SELECT
        COUNT(*) AS total,
        CASE
        WHEN acceptance_check_status = '15' THEN '待提交'
        WHEN acceptance_check_status = '2' THEN '审核中'
        WHEN acceptance_check_status = '3' THEN '已完成'
        WHEN acceptance_check_status = '4' THEN '未通过'
        WHEN acceptance_check_status = '7' THEN '审核中'
        WHEN acceptance_check_status = '9' THEN '整改待审核'
        WHEN acceptance_check_status = '11' THEN '审核中'
        WHEN acceptance_check_status = '12' THEN '审核中'
        WHEN acceptance_check_status = '13' THEN '审核中'
        WHEN acceptance_check_status = '14' THEN '审核中'
        ELSE '待整改'
        END AS statusText,
        ROUND(COALESCE(SUM(ei.real_scale), 0), 2) AS realScale
        FROM
        hygf_acceptance_check
        LEFT JOIN
        hygf_work_order_power_station ps ON ps.sequence_nbr = hygf_acceptance_check.work_order_power_station_id
        LEFT JOIN
        hygf_power_station_engineering_info ei ON ei.work_order_power_station_id = ps.sequence_nbr
        LEFT JOIN
        hygf_peasant_household ON hygf_acceptance_check.peasant_household_id = hygf_peasant_household.sequence_nbr
        <where>
            <if test="developerCode != null and developerCode != ''">
                AND hygf_peasant_household.developer_code = #{developerCode}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="province != null and province != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
            </if>
            <if test="city != null and city != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
            </if>
            <if test="district != null and district != ''">
                AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
            </if>
        </where>
        GROUP BY
        acceptance_check_status
    </select>

    <select id="financingStatusTotal" resultType="map">
        SELECT
        count(*) AS total,
        CASE
        WHEN info.`status` = '已放款' THEN
        '已放款'
        WHEN info.`status` = '审核通过' THEN
        '待放款'
        WHEN info.`status` = '待融资审核' THEN
        '待审核'
        WHEN info.`status` = '审核不通过' THEN
        '未通过'
        WHEN info.`status` = '放款完成' THEN
        '放款完成' ELSE '待推送'
        END AS statusText,
        SUM( info.disbursement_money ) AS disbursementMoney,
        ROUND(COALESCE(SUM( ei.real_scale ),0),2) AS realScale
        FROM
        `hygf_peasant_household` hph
        LEFT JOIN hygf_financing_info info ON info.peasant_household_id = hph.sequence_nbr
        LEFT JOIN hygf_work_order_power_station ps ON ps.peasant_household_id = hph.sequence_nbr
        LEFT JOIN hygf_power_station_engineering_info ei ON ei.work_order_power_station_id = ps.sequence_nbr
        WHERE
        hph.construction_state = '验收完成'
            <if test="developerCode != null and developerCode != ''">
                AND hph.developer_code = #{developerCode}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND hph.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="province != null and province != ''">
                AND hph.project_address LIKE CONCAT ('%',#{province},'%')
            </if>
            <if test="city != null and city != ''">
                AND hph.project_address LIKE CONCAT ('%',#{city},'%')
            </if>
            <if test="district != null and district != ''">
                AND hph.project_address LIKE CONCAT ('%',#{district},'%')
            </if>
        GROUP BY
        STATUS


    </select>

    <select id="inverterTotal" resultType="map">
        SELECT
        FLOOR(SUM((LENGTH(construction_inverter_info)-LENGTH(REPLACE(construction_inverter_info,'"id"',''))))/LENGTH('"id"')) AS total,
        '户用开发逆变器' AS statusText,
        0 AS  realScale
        FROM
            hygf_power_station_construction_data
                LEFT JOIN
            hygf_work_order_power_station   on hygf_power_station_construction_data.work_order_power_station_id =hygf_work_order_power_station.sequence_nbr
                LEFT JOIN
            hygf_peasant_household ON hygf_work_order_power_station.peasant_household_id = hygf_peasant_household.sequence_nbr
        <if test="developerCode != null and developerCode != ''">
            AND hygf_peasant_household.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
    </select>

    <select id="inverterTotalJp" resultType="map">
        SELECT
        COUNT(*) total,
        '接入逆变器' AS statusText,
        ROUND(COALESCE(SUM( ji.capacity),0),2) AS realScale
        FROM
        hygf_jp_inverter ji
        LEFT JOIN hygf_jp_station jp ON jp.third_station_id = ji.third_station_id
        LEFT JOIN privilege_company pc on jp.regional_companies_code = pc.ORG_CODE
        WHERE
        jp.regional_companies_code IS NOT NULL
        AND jp.amos_company_code IS NOT NULL
        <if test="developerCode != null and developerCode != ''">
            AND jp.amos_company_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND jp.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="regions != null and regions.size()>0 ">
            and pc.REGION_SEQ in
            <foreach collection="regions" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
    </select>


    <select id="stationNum" resultType="map">
        SELECT count(*)  as total  FROM hygf_peasant_household WHERE  is_delete = 0
        <if test="developerCode != null and developerCode != ''">
            AND hygf_peasant_household.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
        </if>

    </select>

    <select id="homePageExport" resultType="map">
        SELECT
        ph.regional_companies_name AS regionalCompaniesName,
        ph.developer_name AS developerName,
        ROUND(COALESCE(SUM(IFNULL(ei.real_scale, 0))/1000, 0), 2) AS realScale,
        SUM(CASE WHEN ps.process_status = '不通过' THEN 1 ELSE 0 END) AS kcwtg,
        '' AS kcwtgrl,
        SUM(CASE WHEN ps.process_status = '进行中' THEN 1 ELSE 0 END) AS kcjxz,
        '' AS kcjxzrl,
        SUM(CASE WHEN ps.process_status = '完成' THEN 1 ELSE 0 END) AS kcywc,
        '' AS kcywcrl,
        SUM(CASE WHEN ph.preparation_money_state = '备货完成' THEN 1 ELSE 0 END) AS bhywc,
        '' AS bhywcrl,
        SUM(CASE WHEN ph.preparation_money_state = '待收货' THEN 1 ELSE 0 END) AS bhjxz,
        '' AS bhjxzrl,
        SUM(CASE WHEN ph.preparation_money_state = '待补货' THEN 1 ELSE 0 END) AS bhwtg,
        '' AS bhwtgrl,
        SUM(CASE WHEN wops.power_station_construction_status = '未通过' THEN 1 ELSE 0 END) AS sgwtg,
        SUM(CASE WHEN wops.power_station_construction_status = '未通过' THEN ROUND(COALESCE(ei.real_scale/1000, 0), 2) ELSE 0 END) AS sgwtgrl,
        SUM(CASE WHEN wops.power_station_construction_status NOT IN ('未通过', '已完工', '待施工') THEN 1 ELSE 0 END) AS sgjxz,
        ROUND(COALESCE( SUM(CASE WHEN wops.power_station_construction_status NOT IN ('未通过', '已完工', '待施工') THEN ei.real_scale/1000 ELSE 0 END), 0), 2) AS sgjxzrl,
        SUM(CASE WHEN wops.power_station_construction_status = '已完工' THEN 1 ELSE 0 END) AS sgywc,
        ROUND(COALESCE(SUM(CASE WHEN wops.power_station_construction_status = '已完工' THEN ei.real_scale/1000 ELSE 0 END) , 0), 2)AS sgywcrl,
        SUM(CASE WHEN br.grid_status = '4' THEN 1 ELSE 0 END) AS bwwtg,
        SUM(CASE WHEN br.grid_status = '4' THEN ROUND(COALESCE(ei.real_scale/1000, 0), 2) ELSE 0 END) AS bwwtgrl,
        SUM(CASE WHEN br.grid_status NOT IN ('4', '3', '1') THEN 1 ELSE 0 END) AS bwjxz,
        ROUND(COALESCE(SUM(CASE WHEN br.grid_status NOT IN ('4', '3', '1') THEN ei.real_scale/1000 ELSE 0 END), 0), 2) AS bwjxzrl,
        SUM(CASE WHEN br.grid_status = '3' THEN 1 ELSE 0 END) AS bwywc,
        ROUND(COALESCE(SUM(CASE WHEN br.grid_status = '3' THEN ei.real_scale/1000 ELSE 0 END), 0), 2) AS bwywcrl,
        SUM(CASE WHEN ac.acceptance_check_status = '3' THEN 1 ELSE 0 END) AS ysywc,
        ROUND(COALESCE(SUM(CASE WHEN ac.acceptance_check_status = '3' THEN ei.real_scale/1000 ELSE 0 END), 0), 2) AS ysywcrl,
        SUM(CASE WHEN ac.acceptance_check_status = '4' THEN 1 ELSE 0 END) AS yswtg,
        ROUND(COALESCE(SUM(CASE WHEN ac.acceptance_check_status = '4' THEN ei.real_scale/1000 ELSE 0 END), 0), 2) AS yswtgrl,
        SUM(CASE WHEN ac.acceptance_check_status NOT IN ('3', '4', '15') THEN 1 ELSE 0 END) AS ysjxz,
        ROUND(COALESCE(SUM(CASE WHEN ac.acceptance_check_status NOT IN ('3', '4', '15') THEN ei.real_scale/1000 ELSE 0 END), 0), 2) AS ysjxzrl,
        SUM(CASE WHEN fi.status = '放款完成' THEN 1 ELSE 0 END) AS rzhs,
        MAX(CASE WHEN fi.status = '放款完成' THEN fi.unit_price ELSE 0 END) AS rzdj,
        MAX(CASE WHEN fi.status = '放款完成' THEN fi.financing_companies_name ELSE '' END) AS rzgs,
        ROUND(COALESCE(SUM(CASE WHEN fi.status = '放款完成' THEN fi.disbursement_money ELSE 0 END), 0), 2) AS fkje,
        ROUND(COALESCE(SUM(CASE WHEN fi.status = '放款完成' THEN ei.real_scale/1000 ELSE 0 END), 0), 2)AS rzrl
        FROM
        hygf_peasant_household ph
        LEFT JOIN hygf_power_station ps ON ps.peasant_household_id = ph.sequence_nbr
        LEFT JOIN hygf_work_order_power_station wops ON wops.peasant_household_id = ph.sequence_nbr
        LEFT JOIN hygf_power_station_engineering_info ei ON ei.work_order_power_station_id = wops.sequence_nbr
        LEFT JOIN hygf_acceptance_check ac ON ac.peasant_household_id = ph.sequence_nbr
        LEFT JOIN hygf_basic_grid_record br ON br.peasant_household_id = ph.sequence_nbr
        LEFT JOIN hygf_financing_info fi ON fi.peasant_household_id = ph.sequence_nbr
        WHERE
        ps.is_delete = 0
        <if test="developerCode != null and developerCode != ''">
            AND ph.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND ph.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND ph.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND ph.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND ph.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
        GROUP BY
        ph.regional_companies_name,
        ph.developer_name
        order by
        ph.regional_companies_name,ph.developer_name
    </select>


    <select id="merCount" resultType="map">
        SELECT
        count( regionalCompaniesName ) AS total,
        max( regionalCompaniesName ) regionalCompaniesName,
        max( regionSeq ) AS regionSeq,
        max( regionalCompaniesCode ) AS regionalCompaniesCode,
        max( developerCode ) AS developerCode,
        max( projectAddress ) AS projectAddress
        FROM
        (
        SELECT
        ph.regional_companies_name AS regionalCompaniesName,
        ph.developer_name developerName,
        max( pc.REGION_SEQ ) AS regionSeq,
        max( ph.regional_companies_code ) AS regionalCompaniesCode,
        max( ph.developer_code ) AS developerCode,
        max( ph.project_address ) AS projectAddress
        FROM
        hygf_peasant_household ph
        LEFT JOIN privilege_company pc ON pc.SEQUENCE_NBR = ph.regional_companies_seq
        <where>
            <if test="developerCode != null and developerCode != ''">
                AND ph.developer_code = #{developerCode}
            </if>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                AND ph.regional_companies_code = #{regionalCompaniesCode}
            </if>
            <if test="province != null and province != ''">
                AND ph.project_address LIKE CONCAT ('%',#{province},'%')
            </if>
            <if test="city != null and city != ''">
                AND ph.project_address LIKE CONCAT ('%',#{city},'%')
            </if>
            <if test="district != null and district != ''">
                AND ph.project_address LIKE CONCAT ('%',#{district},'%')
            </if>
        </where>
        GROUP BY
        ph.regional_companies_name,
        ph.developer_name
        ORDER BY
        ph.regional_companies_name,
        ph.developer_name
        ) b
        GROUP BY
        regionalCompaniesName
    </select>

    <select id="selectProvince" resultType="String">
        WITH RECURSIVE ParentTree AS (
            -- 初始查询：选择根节点
            SELECT
                SEQUENCE_NBR,
                PARENT_ID,
                REGION_NAME
            FROM
                systemctl_region sr
            WHERE
                sr.SEQUENCE_NBR = #{regionSeq}
            UNION ALL
            -- 递归部分：选择父节点
            SELECT
                p.SEQUENCE_NBR,
                p.PARENT_ID,
                p.REGION_NAME
            FROM
                systemctl_region p
                    JOIN
                ParentTree pt ON p.SEQUENCE_NBR = pt.PARENT_ID
        )

        SELECT
            pt.REGION_NAME AS topLevelNodeName
        FROM
            ParentTree pt
        WHERE
            pt.PARENT_ID = 0;
    </select>

    <select id="getRent" resultType="map">
        SELECT
            sum( CASE WHEN repay_state = '1' THEN rent ELSE 0 END ) yhk,
            sum( CASE WHEN repay_state = '0' THEN rent ELSE 0 END ) sywhk,
            sum( CASE WHEN repay_state = '0' AND ( YEAR ( repay_date ) = YEAR ( CURDATE())) THEN rent ELSE 0 END ) jnwhk,
            max(regional_companies_code) regionalCompaniesCode
        FROM
            `hygf_repayment`
        <where>
            <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
                regional_companies_code = #{regionalCompaniesCode}
            </if>
        </where>
        GROUP BY
            regional_companies_code
    </select>


    <select id="statisticsPowerStation" resultType="map">
        SELECT
            SUM( CASE WHEN ps.process_status = '不通过' THEN 1 ELSE 0 END ) AS kcwtg,
            SUM( CASE WHEN ps.process_status = '进行中' THEN 1 ELSE 0 END ) AS kcjxz,
            SUM( CASE WHEN ps.process_status = '完成' THEN 1 ELSE 0 END ) AS kcywc,
            SUM( CASE WHEN hc.STATUS = '未签署' THEN 1 ELSE 0 END ) AS wqs,
            SUM( CASE WHEN hc.STATUS = '农户已签署' THEN 1 ELSE 0 END ) AS nhyqs,
            SUM( CASE WHEN hc.STATUS = '双方已签署' THEN 1 ELSE 0 END ) AS sfyqs,
            SUM( CASE WHEN hc.STATUS = '已作废' THEN 1 ELSE 0 END ) AS yzf
        FROM
            hygf_peasant_household ph
                LEFT JOIN hygf_power_station ps ON ps.peasant_household_id = ph.sequence_nbr
                LEFT JOIN hygf_household_contract hc ON hc.peasant_household_id = ph.sequence_nbr
        WHERE
            ps.is_delete = 0 and ph.is_delete = 0
        <if test="developerCode != null and developerCode != ''">
            AND ph.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND ph.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND ph.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND ph.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND ph.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
    </select>


    <select id="statisticsPreparationMoney" resultType="map">
        SELECT
        SUM( CASE WHEN preparation_money_state = '待发货' or preparation_money_state = '暂存发货'     THEN 1 ELSE 0 END )  AS dfh,
        SUM( CASE WHEN  preparation_money_state = '待补货'    THEN 1 ELSE 0 END )  AS dbh,
        SUM( CASE WHEN preparation_money_state = '备货完成'  THEN 1 ELSE 0 END )  AS ysh,
        SUM( CASE WHEN preparation_money_state = '待收货'  THEN 1 ELSE 0 END )  AS yfh
        FROM
        hygf_peasant_household
        WHERE
        preparation_money_state IS NOT NULL
        <if test="developerCode != null and developerCode != ''">
            AND hygf_peasant_household.developer_code = #{developerCode}
        </if>
        <if test="regionalCompaniesCode != null and regionalCompaniesCode != ''">
            AND hygf_peasant_household.regional_companies_code = #{regionalCompaniesCode}
        </if>
        <if test="province != null and province != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{province},'%')
        </if>
        <if test="city != null and city != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{city},'%')
        </if>
        <if test="district != null and district != ''">
            AND hygf_peasant_household.project_address LIKE CONCAT ('%',#{district},'%')
        </if>
    </select>
</mapper>