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

    <select id="getDzStationIdByProcessStatus" resultType="java.lang.String">
        select
        hps.peasant_household_id
        from hygf_power_station hps
        left join  hygf_peasant_household hph ON hph.sequence_nbr = hps.peasant_household_id
        where hps.is_delete=0
        <if test="map.powerStationCode!=null and map.powerStationCode!=''">
            and hps.power_station_code like concat(concat('%',#{map.powerStationCode}),'%')
        </if>
        <if test="map.ownersName!=null and map.ownersName!=''">
            and hps.owners_name like concat(concat('%',#{map.ownersName}),'%')
        </if>
        <if test="map.serviceAgent!=null and map.serviceAgent!=''">
            and hps.service_agent like concat(concat('%',#{map.serviceAgent}),'%')
        </if>
        <if test="map.regionalCompaniesName!=null and map.regionalCompaniesName!=''">
            and hph.regional_companies_name like concat(concat('%',#{map.regionalCompaniesName}),'%')
        </if>
        <if test="map.processStatus != null and map.processStatus != ''">
            and hps.process_status = #{map.processStatus}
        </if>
        <if test="map.province != null and map.province != ''">
            and hph.project_address like concat(concat('%',#{map.province}),'%')
        </if>
        GROUP BY hps.peasant_household_id
        ORDER BY hps.rec_date desc
    </select>

    <select id="getBasicInformation" resultType="java.util.Map">
        SELECT
        hsi.*,hph.peasant_household_no,hph.owners_name,hph.telephone,hph.project_address_name,hph.project_address_detail,hph.permanent_address_name,hph.permanent_address_detail,hph.sequence_nbr AS stationId
        FROM
        hygf_survey_information hsi
        LEFT JOIN hygf_peasant_household hph ON hph.survey_information_id=hsi.sequence_nbr
        <where>
            <if test="list != null and list.size()>0">
                AND hph.sequence_nbr  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getSurveyInformation" resultType="java.util.Map">
        SELECT
        hsd.*,hph.sequence_nbr AS stationId
        FROM
        hygf_survey_details hsd
        LEFT JOIN hygf_peasant_household hph ON hph.survey_information_id=hsd.survey_information_id
        <where>
            <if test="list != null and list.size()>0">
                AND hph.sequence_nbr  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getDesignInformation" resultType="java.util.Map">
        SELECT
        hdi.*,hdi.peasant_household_id AS stationId
        FROM
        hygf_design_information hdi
        <where>
            <if test="list != null and list.size()>0">
                AND hdi.peasant_household_id  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getBusinessInformation" resultType="java.util.Map">
        SELECT
        hc.*,hph.sequence_nbr AS stationId
        FROM
        hygf_commercial hc
        LEFT JOIN hygf_peasant_household hph ON hph.survey_information_id=hc.survey_information_id
        <where>
            <if test="list != null and list.size()>0">
                AND hph.sequence_nbr  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getExtendedInformation" resultType="java.util.Map">
        SELECT
        hei.*,hph.sequence_nbr AS stationId
        FROM
        hygf_extended_information hei
        LEFT JOIN hygf_peasant_household hph ON hph.survey_information_id=hei.survey_information_id
        <where>
            <if test="list != null and list.size()>0">
                AND hph.sequence_nbr  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getArchivingInformation" resultType="java.util.Map">
        SELECT
        hi.*,hph.sequence_nbr AS stationId
        FROM
        hygf_information hi
        LEFT JOIN hygf_peasant_household hph ON hph.survey_information_id=hi.survey_information_id
        <where>
            <if test="list != null and list.size()>0">
                AND hph.sequence_nbr  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getEngineeringInformation" resultType="java.util.Map">
        SELECT
        hpsei.*,hwops.peasant_household_id AS stationId,hpsc.construction_component_info,hpsc.construction_inverter_info,hpsc.construction_collector_box_info,hpsc.construction_grid_box_info
        FROM
        hygf_power_station_engineering_info hpsei
        LEFT JOIN hygf_work_order_power_station hwops ON hwops.sequence_nbr=hpsei.work_order_power_station_id
        LEFT JOIN hygf_power_station_construction_data hpsc on hpsc.work_order_power_station_id=hpsei.work_order_power_station_id
        <where>
            <if test="list != null and list.size()>0">
                AND hwops.peasant_household_id  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getGridInformation" resultType="java.util.Map">
        SELECT
        hogaa.*,hogaa.peasant_household_id AS stationId
        FROM
        hygf_on_grid_and_acceptance hogaa
        <where>
            <if test="list != null and list.size()>0">
                AND hogaa.peasant_household_id  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getDispatchOrderInformation" resultType="java.util.Map">
        SELECT
        hwo.*,hwops.peasant_household_id AS stationId
        FROM
        hygf_work_order hwo
        LEFT JOIN hygf_work_order_power_station hwops ON hwo.sequence_nbr=hwops.work_order_id
        <where>
            <if test="list != null and list.size()>0">
                AND hwops.peasant_household_id  IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getTrStationIdByStatus" resultType="java.lang.String">
        SELECT sta.sequenceNbr 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,
        hygf_unit_info.head_name responsibleUserName,
        hygf_unit_info.head_phone  responsibleUserPhone,
        CONCAT(info.disbursement_money,'') as disbursementMoney,
        hph.regional_companies_code,
        hph.developer_code,
        hph.is_history isHistory,
        info.financing_companies_name financingCompaniesName
        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.isHistory != null and params.isHistory !='' and params.type != 2 ">
                and hph.is_history is not null
            </if>
            <if test="(params.isHistory == null or params.isHistory == '')  and params.type != 2">
                and hph.is_history is null
            </if>
            <if test="params.regionalCompaniesCode != null and params.regionalCompaniesCode !=''">
                and hph.regional_companies_code = #{params.regionalCompaniesCode}
            </if>
            <if test="params.batchNo != null and params.batchNo !=''">
                and info.batch_no = #{batchNo}
            </if>
            <if test="params.type == 2 ">
                and info.status in ('待融资审核','审核通过','放款完成','已放款','审核不通过' ) and info.financing_companies_seq = #{params.financingCompaniesSeq}
            </if>
            <if test="params.region != null and params.region !='' ">
                and hph.project_address like concat ('%',#{params.region},'%')
            </if>
            <if test="params.peasantHouseholdNo != null and params.peasantHouseholdNo !='' ">
                and hph.peasant_household_no = #{params.peasantHouseholdNo}
            </if>
        </where>
        ORDER BY
        info.rec_date DESC ,hph.sequence_nbr DESC ) as sta
        <where>
            <if test="params.status != null and params.status.size() > 0">
                and sta.status in
                <foreach collection="params.status" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getSgysStationId" resultType="java.lang.String">
        select
        hygf_peasant_household.sequence_nbr sequenceNbr
        from   hygf_work_order_power_station LEFT join hygf_work_order
        on hygf_work_order.sequence_nbr=hygf_work_order_power_station.work_order_id
        LEFT JOIN hygf_peasant_household on  hygf_peasant_household.sequence_nbr=hygf_work_order_power_station.peasant_household_id
        LEFT JOIN hygf_power_station_engineering_info on hygf_power_station_engineering_info.work_order_power_station_id=hygf_work_order_power_station.sequence_nbr
        <where>
            <if test="dto.workOrderId!=null and dto.workOrderId!=''">
                and hygf_work_order.sequence_nbr = #{dto.workOrderId}
            </if>
            <if test=' dto.type!=null and dto.type == "1"'>
                and  hygf_work_order_power_station.power_station_construction_status in ('待登记','未通过','已完工','待审核')
            </if>
            <if test=' dto.type !=null and dto.type == "2"'>
                and  hygf_work_order_power_station.power_station_construction_status IN ( '未通过', '已完工', '待审核', '待整改', '整改中','整改待审核' )
            </if>
            <if test=' dto.type !=null and dto.type == "3" and dto.workOrderPowerStationNodes  !=null'>
                and ( hygf_work_order_power_station.work_order_power_station_node  is not null
                or hygf_work_order_power_station.work_order_power_station_node = 'all'
                or hygf_work_order_power_station.power_station_area_status  is not null
                or hygf_work_order_power_station.power_station_design_status  is not null
                or hygf_work_order_power_station.power_station_engineering_status  is not null
                )
            </if>
            <if test="dto.projectAddressName!=null and dto.projectAddressName!=''">
                and hygf_peasant_household.project_address_name like concat(concat('%',#{dto.projectAddressName}),'%')
            </if>
            <if test="dto.peasantHouseholdNo!=null and dto.peasantHouseholdNo!=''">
                and hygf_peasant_household.peasant_household_no like concat(concat('%',#{dto.peasantHouseholdNo}),'%')
            </if>
            <if test="dto.ownersName!=null and dto.ownersName!=''">
                and hygf_peasant_household.owners_name like concat(concat('%',#{dto.ownersName}),'%')
            </if>
            <if test="dto.workOrderNum!=null and dto.workOrderNum!=''">
                and hygf_work_order.work_order_num like concat(concat('%',#{dto.workOrderNum}),'%')
            </if>
            <if test="dto.projectRegionManager!=null and dto.projectRegionManager!=''">
                and hygf_work_order.project_region_manager like concat(concat('%',#{dto.projectRegionManager}),'%')
            </if>
            <if test="dto.powerStationConstructionStatus!=null and dto.powerStationConstructionStatus!=''">
                and hygf_work_order_power_station.power_station_construction_status=#{dto.powerStationConstructionStatus}
            </if>
            <if test="dto.amosDealerId!=null and dto.amosDealerId!=''">
                and hygf_work_order.amos_dealer_id = #{dto.amosDealerId}
            </if>
            <if test="dto.regionCompanyId!=null and dto.regionCompanyId!=''">
                and hygf_work_order.region_company_id =#{dto.regionCompanyId}
            </if>
            <if test="dto.province != null and dto.province != ''">
                and hygf_peasant_household.project_address like concat(concat('%',#{dto.province}),'%')
            </if>
        </where>
        ORDER BY
        hygf_work_order_power_station.sequence_nbr DESC
    </select>

    <select id="getBwysStationId" resultType="java.lang.String">

        SELECT
        hph.sequence_nbr sequenceNbr
        FROM
        hygf_basic_grid_record hbga
        LEFT JOIN hygf_peasant_household hph ON hph.sequence_nbr = hbga.peasant_household_id
        LEFT JOIN hygf_on_grid_and_acceptance hogaa ON hogaa.work_order_power_station_id = hbga.work_order_power_station_id
        LEFT JOIN hygf_work_order  ON hygf_work_order.sequence_nbr = hbga.work_order_id
        <where>
            hbga.is_delete = 0

            <if test="map.amosDealerId!=null and map.amosDealerId!=''">
                and hygf_work_order.amos_dealer_id = #{map.amosDealerId}
            </if>
            <if test="map.regionCompanyId!=null and map.regionCompanyId!=''">
                and hygf_work_order.region_company_id =#{map.regionCompanyId}
            </if>
            <if test="map.powerStationCode!=null and map.powerStationCode!=''">
                and hph.peasant_household_no like concat('%',#{map.powerStationCode},'%')
            </if>
            <if test="map.formType != null and map.formType!=''">
                AND hbga.grid_status IN ('2', '3', '4')
            </if>
            <if test="map.projectAddress != null and map.projectAddress!=''">
                AND hph.project_address_name LIKE concat(concat('%', #{map.projectAddress}), '%')
            </if>
            <if test="map.type != null">
                AND ( (hbga.basic_grid_node IS NOT NULL AND hbga.basic_grid_node != 'jxsAdmin' )
                OR hbga.power_station_area_status IS NOT NULL
                OR hbga.power_station_design_status IS NOT NULL
                OR hbga.power_station_engineering_status IS NOT NULL )
            </if>
            <if test="map.ownersName != null and map.ownersName !=''">
                AND hph.owners_name LIKE concat(concat('%', #{map.ownersName}), '%')
            </if>
            <if test="map.gridStatus != null and map.gridStatus !=''">
                AND hbga.grid_status = #{map.gridStatus}
            </if>
            <if test="map.gridConnectionTime != null">
                AND DATE_FORMAT(hogaa.grid_time, '%Y-%m-%d') = #{map.gridConnectionTime}
            </if>
            <if test="map.province != null and map.province != ''">
                AND hph.project_address like concat(concat('%',#{map.province}),'%')
            </if>
        </where>
        ORDER BY hbga.sequence_nbr desc
    </select>

    <select id="getYsshStationId" resultType="java.lang.String">

        SELECT
        hph.sequence_nbr sequenceNbr
        FROM
        hygf_acceptance_check hbga
        LEFT JOIN hygf_peasant_household hph ON hph.sequence_nbr = hbga.peasant_household_id
        LEFT JOIN hygf_on_grid_and_acceptance hogaa ON hogaa.work_order_power_station_id = hbga.work_order_power_station_id
        LEFT JOIN hygf_work_order  ON hygf_work_order.sequence_nbr = hbga.work_order_id
        <where>
            hbga.is_delete = 0

            <if test="map.amosDealerId!=null and map.amosDealerId!=''">
                and hygf_work_order.amos_dealer_id = #{map.amosDealerId}
            </if>
            <if test="map.regionCompanyId!=null and map.regionCompanyId!=''">
                and hygf_work_order.region_company_id =#{map.regionCompanyId}
            </if>
            <if test="map.powerStationCode!=null and map.powerStationCode!=''">
                and hph.peasant_household_no like concat('%',#{map.powerStationCode},'%')
            </if>
            <if test="map.formType != null and map.formType!=''">
                AND hbga.acceptance_check_status IN ('2', '3', '4')
            </if>
            <if test="map.projectAddress != null and map.projectAddress!=''">
                AND hph.project_address_name LIKE concat(concat('%', #{map.projectAddress}), '%')
            </if>
            <if test="map.type != null">
                and ( (hbga.basic_grid_node  is not null AND hbga.basic_grid_node != 'jxsAdmin')
                or hbga.basic_grid_node = 'all'
                or hbga.power_station_area_status  is not null
                or hbga.power_station_financing_status  is not null
                or hbga.power_station_assets_status  is not null
                or hbga.power_station_legal_status  is not null
                )
            </if>
            <if test="map.ownersName != null and map.ownersName !=''">
                AND hph.owners_name LIKE concat(concat('%', #{map.ownersName}), '%')
            </if>
            <if test="map.acceptanceCheckStatus != null and map.acceptanceCheckStatus !=''">
                AND hbga.acceptance_check_status = #{map.acceptanceCheckStatus}
            </if>
            <if test="map.acceptanceTime != null">
                AND DATE_FORMAT(hogaa.acceptance_time, '%Y-%m-%d') = #{map.acceptanceTime}
            </if>
            <if test="map.province != null and map.province != ''">
                AND hph.project_address like concat(concat('%',#{map.province}),'%')
            </if>
        </where>
        ORDER BY hbga.sequence_nbr desc
    </select>

    <select id="getFhStationId" resultType="java.lang.String">
        select b.sequenceNbr from (   select DISTINCT
        hygf_peasant_household.peasant_household_no as peasantHouseholdNo,
        hygf_peasant_household.owners_name as ownersName,
        hygf_peasant_household.regional_companies_name as regionalCompaniesName,
        hygf_peasant_household.regional_companies_seq as regionalCompaniesSeq,
        hygf_peasant_household.regional_companies_code ,
        hygf_peasant_household.developer_code,
        hygf_peasant_household.developer_name as developerName,
        hygf_peasant_household.developer_id as developeId,
        hygf_peasant_household.sequence_nbr  as sequenceNbr,
        substring_index ( hygf_peasant_household.project_address_name, '/', 1 ) AS province,
        concat_ws('/',hygf_peasant_household.project_address_name,hygf_peasant_household.project_address_detail) as address,
        hygf_peasant_household.preparation_money_state preparationMoneyState,
        hygf_document_station.preparation_money_id as preparationMoneyId,
        hygf_preparation_money.delivery_time as deliveryTime,
        hygf_preparation_money.arrival_time as arrivalTime,
        (select instance_id from hygf_preparation_money_auditing where hygf_document_station.preparation_money_id = hygf_preparation_money_auditing.preparation_money_id limit 1) as instanceId        from
        hygf_peasant_household
        LEFT JOIN hygf_document_station on  hygf_peasant_household.sequence_nbr =   hygf_document_station.station_id
        LEFT JOIN hygf_power_station on hygf_peasant_household.sequence_nbr = hygf_power_station.peasant_household_id
        LEFT JOIN hygf_preparation_money on hygf_preparation_money.sequence_nbr = hygf_document_station.preparation_money_id
        <where>
            <if test=" map.preparationMoneyState == 0 ">
                and hygf_peasant_household.preparation_money_state in ('待发货','待补货','暂存发货')
            </if>
            <if test="map.regionalCompaniesSeq!=null and map.regionalCompaniesSeq !=''  ">
                and hygf_peasant_household.regional_companies_code  like concat('%',#{map.regionalCompaniesSeq},'%')
            </if>
            <if test="map.developerId!=null and map.developerId !=''">
                and hygf_peasant_household.developer_code like concat('%',#{map.developerId},'%')
            </if>
            <if test="map.state!=null and map.state !=''">
                and hygf_peasant_household.preparation_money_state  = #{map.state}
            </if>
            <if test="map.ownersName!=null and map.ownersName !=''">
                and hygf_peasant_household.owners_name like concat ('%',#{map.ownersName},'%')
            </if>
            <if test=" map.preparationMoneyState == 1 ">
                and hygf_peasant_household.preparation_money_state not in  ('待发货','待补货','暂存发货') and preparation_money_state is not null and preparation_money_state != ''
            </if>
            <if test="map.province != null and map.province != ''">
                and hygf_peasant_household.project_address like concat(concat('%',#{map.province}),'%')
            </if>
        </where>
        <if test=" map.preparationMoneyState == 1 ">
            order by hygf_preparation_money.delivery_time  DESC
        </if>
        <if test=" map.preparationMoneyState == 0 ">
            order by hygf_peasant_household.sequence_nbr DESC
        </if>
        ) b
    </select>
</mapper>
