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

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

        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.basic_grid_node basicGridNode,
        hbga.acceptance_time acceptanceTime,
        hbga.power_station_area_status powerStationAreaStatus,
        hbga.power_station_financing_status powerStationFinancingStatus,
        hbga.power_station_assets_status powerStationAssetsStatus,
        hbga.rectification_status rectificationStatus,
        hbga.power_station_legal_status powerStationLegalStatus,
        hbga.instance_id instanceId,
        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,
        substring_index ( hph.project_address_name, '/', 1 ) AS province,
        DATE_FORMAT(hogaa.grid_connection_time, '%Y-%m-%d %H:%i:%S') AS gridConnectionTime,
        CASE WHEN hbga.acceptance_check_status = '15' THEN '待提交验收'
        WHEN hbga.acceptance_check_status = '2' THEN '经销商管理员待审核'
        WHEN hbga.acceptance_check_status = '3' THEN '已完成'
        WHEN hbga.acceptance_check_status = '4' THEN '未通过'
        WHEN hbga.acceptance_check_status = '7' THEN '片区运营待审核'
        WHEN hbga.acceptance_check_status = '9' THEN '整改待审核'
        WHEN hbga.acceptance_check_status = '11' THEN '投融待审核/法务待审核'
        WHEN hbga.acceptance_check_status = '12' THEN '投融待审核'
        WHEN hbga.acceptance_check_status = '13' THEN '资产待审核'
        WHEN hbga.acceptance_check_status = '14' THEN '法务待审核'
        ELSE '待整改' END AS acceptanceCheckStatus
        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="getYsEndTime" resultType="java.util.Map">
        SELECT SEQUENCE_NBR as sequenceNbr,rec_date AS time
        FROM hygf_acceptance_check
        WHERE acceptance_check_status='3'
    </select>

    <select id="getYsCreateTime" resultType="java.util.Map">
        SELECT
        PROC_INST_ID_ AS processInstanceId,
        MIN( START_TIME_ ) AS time
        FROM
        act_hi_taskinst aht
        WHERE
        EXISTS ( SELECT 1 FROM hygf_acceptance_check hac WHERE hac.instance_Id = aht.PROC_INST_ID_  and aht.NAME_='验收提交' )
        GROUP BY
        PROC_INST_ID_
    </select>

</mapper>
