<?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.PowerStationMapper">
<select id="queryPage" resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationDto">
    select DISTINCT
    hygf_power_station.*,
    b.initiate_status,
    b.contract_lock_id,
    hygf_peasant_household.regional_companies_code,
    hygf_peasant_household.regional_companies_name,
    hygf_peasant_household.developer_code,
    hygf_peasant_household.developer_user_id,
    hygf_peasant_household.project_address_detail,
    substring_index ( hygf_peasant_household.project_address_name, '/', 1 ) AS province,
    b.stamp_status,
    b.status
    from hygf_power_station LEFT JOIN ( SELECT  bb.peasant_household_id, initiate_status, contract_lock_id, stamp_status, status FROM (SELECT peasant_household_id,MAX(sequence_nbr) AS sequence_nbr from hygf_household_contract GROUP BY peasant_household_id ) aa INNER  JOIN  hygf_household_contract bb ON aa.sequence_nbr=bb.sequence_nbr) b    on b.peasant_household_id=hygf_power_station.peasant_household_id
    LEFT JOIN hygf_peasant_household on hygf_peasant_household.sequence_nbr=hygf_power_station.peasant_household_id
    where hygf_power_station.is_delete=0

    <if test="powerStationCode!=null and powerStationCode!=''">
        and hygf_power_station.power_station_code like concat(concat('%',#{powerStationCode}),'%')
    </if>
    <if test="ownersName!=null and ownersName!=''">
        and hygf_power_station.owners_name like concat(concat('%',#{ownersName}),'%')
    </if>
    <if test="serviceAgent!=null and serviceAgent!=''">
        and hygf_power_station.service_agent like concat(concat('%',#{serviceAgent}),'%')
    </if>
    <if test="regionalCompaniesName!=null and regionalCompaniesName!=''">
        and hygf_peasant_household.regional_companies_name like concat(concat('%',#{regionalCompaniesName}),'%')
    </if>
    <if test="processStatus != null and processStatus != ''">
        and hygf_power_station.process_status = #{processStatus}
    </if>
    <if test="province != null and province != ''">
        and hygf_peasant_household.project_address like concat(concat('%',#{province}),'%')
    </if>
<!--    GROUP BY hygf_peasant_household.sequence_nbr-->
    ORDER BY hygf_power_station.rec_date desc
</select>
    <select id="getInstanceIdByhouseId" resultType="java.lang.String">
        select process_instance_id From hygf_power_station where peasant_household_id  = #{peasantHouseholdId}
    </select>

    <select id="getKcCreateTime" resultType="java.util.Map">
        SELECT
        PROC_INST_ID_ AS processInstanceId,
        MIN( TIME_ ) AS time
        FROM
        act_hi_detail ahd
        WHERE
        EXISTS ( SELECT 1 FROM hygf_power_station hps WHERE hps.process_instance_id = ahd.PROC_INST_ID_ )
        GROUP BY
        PROC_INST_ID_
    </select>

    <select id="getKcEndTime" resultType="java.util.Map">
        SELECT
        PROC_INST_ID_ AS processInstanceId,
        MAX( END_TIME_ ) AS time
        FROM
        act_hi_taskinst aht
        WHERE
        EXISTS ( SELECT 1 FROM hygf_power_station hps WHERE hps.process_instance_id = aht.PROC_INST_ID_ and hps.business_status='通过' and aht.NAME_='法务审核' )
        GROUP BY
        PROC_INST_ID_
    </select>

    <select id="getSjCreateTime" 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_power_station hps WHERE hps.process_instance_id = aht.PROC_INST_ID_  and aht.NAME_='设计上传典型图库' )
        GROUP BY
        PROC_INST_ID_
    </select>

    <select id="getSjEndTime" resultType="java.util.Map">
        SELECT
        PROC_INST_ID_ AS processInstanceId,
        MAX( END_TIME_ ) AS time
        FROM
        act_hi_taskinst aht
        WHERE
        EXISTS ( SELECT 1 FROM hygf_power_station hps WHERE hps.process_instance_id = aht.PROC_INST_ID_  and hps.process_status='完成' and aht.NAME_='设计图纸审核' )
        GROUP BY
        PROC_INST_ID_
    </select>
</mapper>
