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

    <select id="getStationInfoById" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.HygfOnGrid">
        SELECT
            hph.*
        FROM
            hygf_on_grid_and_acceptance hogaa
        LEFT JOIN hygf_peasant_household hph ON hph.sequence_nbr = hogaa.peasant_household_id
        WHERE
            hogaa.peasant_household_id = #{id}
    </select>

    <select id="selectPageList" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.BasicGridAcceptanceDto">

        SELECT
        hogaa.sequence_nbr fonGridId,
        hbga.sequence_nbr sequenceNbr,
        hbga.work_order_id workOrderId,
        hbga.work_order_power_station_id workOrderPowerStationId,
        hbga.peasant_household_id peasantHouseholdId,
        hbga.acceptance_rec_date acceptanceRecDate,
        hbga.instance_id instanceId,
        hbga.next_executor_ids nextExecutorIds,
        hbga.next_node_key nextNodeKey,
        hph.developer_name as serviceAgent,
        hph.regional_companies_name AS regionalCompaniesName,
        hph.owners_name AS ownersName,
        hph.peasant_household_no peasantHouseholdNo,
        hph.project_address_name projectAddressName,
        DATE_FORMAT(hogaa.grid_connection_time, '%Y-%m-%d %H:%i:%S') AS gridConnectionTime,
        CASE WHEN hbga.grid_status = '1' THEN '待登记'
        WHEN hbga.grid_status = '2' THEN '待审核'
        WHEN hbga.grid_status = '3' THEN '已完成'
        ELSE '未通过' END AS gridStatus
        FROM
            hygf_basic_grid_acceptance 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.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.powerStationCode != null and map.powerStationCode !=''">
                AND hph.peasant_household_no LIKE concat(concat('%', #{map.powerStationCode}), '%')
            </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_connection_time, '%Y-%m-%d') = DATE_FORMAT( #{map.gridConnectionTime}, '%Y-%m-%d')
            </if>
        </where>
        ORDER BY
        FIELD(hbga.grid_status, '1', '2', '4', '3'),
         hogaa.grid_connection_time desc


    </select>
</mapper>
