<?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.PeasantHouseholdMapper">
<select id="selectPeasantHouseholdList" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">

SELECT 	DISTINCT	hygf_peasant_household.sequence_nbr stationId,
    hygf_peasant_household.*
 FROM  hygf_peasant_household LEFT JOIN hygf_household_contract ON hygf_household_contract.peasant_household_id=hygf_peasant_household.sequence_nbr where
hygf_household_contract.`status`='双方已签署'
and hygf_peasant_household.developer_id=#{developerId}
and hygf_peasant_household.regional_companies_seq=#{regionalCompaniesSeq}
and hygf_peasant_household.survey_or_not=3 and  hygf_peasant_household.sequence_nbr not in
(select hygf_document_station.station_id  from hygf_preparation_money RIGHT JOIN hygf_document_station
on hygf_document_station.preparation_money_id=hygf_preparation_money.sequence_nbr  where
  hygf_document_station.station_id is not null
    <if test="ids!=null and ids !=''">
        and hygf_document_station.preparation_money_id !=#{ids}
    </if>)
    <if test="peasantHouseholdNo!=null and peasantHouseholdNo !=''">
        and hygf_peasant_household.peasant_household_no like concat(concat('%',#{peasantHouseholdNo}),'%')
    </if>
    <if test="ownersName!=null and ownersName !='' ">
        and hygf_peasant_household.owners_name like concat(concat('%',#{ownersName}),'%')
    </if>
    <if test="orderBy !=null and orderBy !='' ">
        ORDER BY ${orderBy}
    </if>
</select>

    <select id="selectPeasantHouseholdListsg" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">

        SELECT *,
        hygf_peasant_household.sequence_nbr stationId
        FROM  hygf_peasant_household
        where
        hygf_peasant_household.construction_state='备货完成'
        and hygf_peasant_household.developer_code like concat('%',#{unitInfoCode},'%')
        and hygf_peasant_household.regional_companies_seq=#{regionalCompaniesSeq}
        and hygf_peasant_household.sequence_nbr not in (
        select hygf_work_order_power_station.peasant_household_id   from hygf_work_order_power_station
        where 1=1
        <if test="ids!=null and ids !=''">
           and hygf_work_order_power_station.work_order_id !=#{ids}
        </if>
        )
        <if test="peasantHouseholdNo!=null and peasantHouseholdNo !=''">
            and hygf_peasant_household.peasant_household_no like concat(concat('%',#{peasantHouseholdNo}),'%')
        </if>
        <if test="ownersName!=null and ownersName !='' ">
            and hygf_peasant_household.owners_name like concat(concat('%',#{ownersName}),'%')
        </if>

    </select>
    <select id="queryForPage" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">
        SELECT *,
        substring_index ( hygf_peasant_household.project_address_name, '/', 1 ) AS province
        FROM hygf_peasant_household
        WHERE IS_CERTIFIED = #{isCertified}
        <if test="ownersName!=null and ownersName !=''">
            and owners_name like concat ('%',#{ownersName},'%')
        </if>
        <if test="preparationMoneyState!=null and preparationMoneyState !='' and preparationMoneyState == '0' ">
            and preparation_money_state in ('待发货','待补货','暂存发货')
        </if>
        <if test="preparationMoneyState!=null and preparationMoneyState !='' and preparationMoneyState == '1' ">
            and preparation_money_state not in  ('待发货','待补货','暂存发货')
        </if>
        <if test="developerId!=null and developerId !=''">
            and developer_id = #{developerId}
        </if>
        <if test="developerCode!=null and developerCode !=''">
            and developer_code = #{developerCode}
        </if>
        <if test="peasantHouseholdNo!=null and peasantHouseholdNo !=''">
            and peasant_household_no like concat ('%',#{peasantHouseholdNo},'%')
        </if>
        <if test="province != null and province != ''">
            and project_address like concat(concat('%',#{province}),'%')
        </if>
        <if test="isHistory != null and isHistory != ''">
            and is_history = '1'
        </if>
        <if test="regionalCompaniesSeq != null and regionalCompaniesSeq != ''">
            and regional_companies_seq = #{regionalCompaniesSeq}
        </if>
        <if test="isHistory == null or isHistory == ''">
            and is_history is null
        </if>
        ORDER BY creation_time DESC
    </select>
    <select id="selectUserIsHouse" resultType="java.util.Map">
        SELECT
            role.*
        FROM
            privilege_user_org_role role
                INNER JOIN privilege_agency_user `user` ON `user`.USER_ID = role.USER_ID
                AND role.ROLE_SEQ NOT IN ( 1702551322030534658 )
        WHERE
            `user`.MOBILE = #{telephone}
    </select>

    <select id="selectReview" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">
        select review, amos_user_id from hygf_peasant_household where sequence_nbr = #{sequenceNbr}
    </select>



    <select id="selectIsAllRevoke" resultType="Integer">
<!--        SELECT-->
<!--            count(*) = ( SELECT count(*) FROM hygf_household_contract WHERE peasant_household_number = #{peasantHouseholdNo} and initiate_status = '已发起' AND STATUS = '已作废' )-->
<!--        FROM-->
<!--            hygf_household_contract-->
<!--        WHERE-->
<!--            peasant_household_number = #{peasantHouseholdNo} and initiate_status = '已发起'-->
        SELECT count(1) FROM hygf_household_contract WHERE peasant_household_number = #{peasantHouseholdNo} and initiate_status = '已发起' AND STATUS = '已作废'
    </select>

    <select id="selectIsAllRevokePassCount" resultType="java.lang.Integer">
        SELECT count(1) FROM hygf_household_contract WHERE peasant_household_number = #{peasantHouseholdNo} and initiate_status = '已发起'
    </select>
    <select id="deleteHphByUserId">
        DELETE FROM hygf_peasant_household WHERE amos_user_id=#{userId}
    </select>

    <select id="deletePauByUserId">
        DELETE FROM privilege_agency_user WHERE USER_ID=#{userId}
    </select>

    <select id="deleteAliByUserId">
        DELETE FROM auth_login_info WHERE USER_ID=#{userId}
    </select>

    <select id="deleteHhcByUserId">
        DELETE FROM hygf_household_contract WHERE project_user_id=#{userId}
    </select>


    <select id="selectPowerStationNumber" resultType="Integer">
        select count(*) from hygf_peasant_household where amos_user_id = #{amosUserId}
    </select>

    <select id="deleteHphByNo">
        DELETE FROM hygf_peasant_household WHERE peasant_household_no=#{peasantHouseholdNo}
    </select>

    <select id="deleteHhcByNo">
        DELETE FROM hygf_household_contract WHERE peasant_household_number=#{peasantHouseholdNo}
    </select>
    <select id="getInfoByIds" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">
        SELECT
               ph.sequence_nbr,
               ph.owners_name,
               concat( ph.project_address_name, ph.project_address_detail ) AS project_address_name,
               (ei.component_quantity * ei.component_specification / 1000) as realScale
        FROM
             hygf_peasant_household ph
        LEFT JOIN hygf_work_order_power_station ps ON ps.peasant_household_id = ph.sequence_nbr
        LEFT JOIN hygf_power_station_engineering_info ei ON ei.work_order_power_station_id= ps.sequence_nbr
        <where>
            ph.is_delete = 0
            <if test="ids != null and ids.size()>0">
                AND ph.sequence_nbr  IN
                <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


    <select id="getInfoByIdsHistory" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">
        SELECT
        ph.sequence_nbr,
        ph.owners_name,
        concat( ph.project_address_name, ph.project_address_detail ) AS project_address_name,
        (ei.component_quantity * ei.component_specification / 1000) as realScale
        FROM
        hygf_peasant_household ph
        LEFT JOIN hygf_power_station_engineering_info ei ON ei.work_order_power_station_id= ph.sequence_nbr
        <where>
            ph.is_delete = 0
            <if test="ids != null and ids.size()>0">
                AND ph.sequence_nbr  IN
                <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryForPageByPowerStationStatistics"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT
        hph.sequence_nbr,
        hph.peasant_household_no,
        hph.owners_name,
        hc.scale,
        hpsei.real_scale,
        substring_index ( hph.project_address_name, '/', 1 ) AS province,
        hph.regional_companies_name,
        hph.developer_name AS serviceAgent
        FROM
        hygf_peasant_household hph
        LEFT JOIN hygf_commercial hc ON hc.survey_information_id = hph.survey_information_id
        LEFT JOIN hygf_work_order_power_station hwops ON hph.sequence_nbr = hwops.peasant_household_id
        LEFT JOIN hygf_power_station_engineering_info hpsei ON hwops.sequence_nbr = hpsei.work_order_power_station_id
        <where>
            <if test="statisticsDto.ownersName!=null and statisticsDto.ownersName !=''">
                and hph.owners_name like concat ('%',#{statisticsDto.ownersName},'%')
            </if>
            <if test="statisticsDto.province!=null and statisticsDto.province !=''">
                and hph.project_address_name like concat ('%',#{statisticsDto.province},'%')
            </if>
            <if test="statisticsDto.regionalCompaniesName!=null and statisticsDto.regionalCompaniesName !=''">
                and hph.regional_companies_name like concat ('%',#{statisticsDto.regionalCompaniesName},'%')
            </if>
            <if test="statisticsDto.serviceAgent!=null and statisticsDto.serviceAgent !=''">
                and hph.developer_name like concat ('%',#{statisticsDto.serviceAgent},'%')
            </if>
        </where>
        ORDER BY hph.rec_date DESC ,hph.sequence_nbr DESC
    </select>

    <select id="getPowerStationTimeDetail"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT
        hph.sequence_nbr,
        hph.peasant_household_no,
        hph.owners_name,
        hc.scale,
        hpsei.real_scale,
        substring_index ( hph.project_address_name, '/', 1 ) AS province,
        hph.regional_companies_name,
        hph.developer_name AS serviceAgent
        FROM
        hygf_peasant_household hph
        LEFT JOIN hygf_commercial hc ON hc.survey_information_id = hph.survey_information_id
        LEFT JOIN hygf_work_order_power_station hwops ON hph.sequence_nbr = hwops.peasant_household_id
        LEFT JOIN hygf_power_station_engineering_info hpsei ON hwops.sequence_nbr = hpsei.work_order_power_station_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>
        ORDER BY hph.rec_date DESC ,hph.sequence_nbr DESC
    </select>

    <select id="getAllPowerStationTimeDetail"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT
        hph.sequence_nbr,
        hph.peasant_household_no,
        hph.owners_name,
        hc.scale,
        hpsei.real_scale,
        substring_index ( hph.project_address_name, '/', 1 ) AS province,
        hph.regional_companies_name,
        hph.developer_name AS serviceAgent
        FROM
        hygf_peasant_household hph
        LEFT JOIN hygf_commercial hc ON hc.survey_information_id = hph.survey_information_id
        LEFT JOIN hygf_work_order_power_station hwops ON hph.sequence_nbr = hwops.peasant_household_id
        LEFT JOIN hygf_power_station_engineering_info hpsei ON hwops.sequence_nbr = hpsei.work_order_power_station_id
        ORDER BY hph.rec_date DESC ,hph.sequence_nbr DESC
    </select>

    <select id="getKcTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,kc_create_time,kc_end_time
        FROM
        hygf_power_station
        <where>
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getDzhtqyTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,dzhtqy_create_time,signing_time AS dzhtqy_end_time
        FROM
        hygf_household_contract
        <where>
            status!='已作废'
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getSjTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,sj_create_time,sj_end_time
        FROM
        hygf_power_station
        <where>
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getFhbhTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT hds.station_id as sequenceNbr,hpm.delivery_time as fhbhCreateTime,hpm.arrival_time as fhbhEndTime
        FROM
        hygf_preparation_money hpm
        LEFT JOIN hygf_document_station hds ON hds.preparation_money_id=hpm.sequence_nbr
        <where>
            <if test="list != null and list.size()>0">
                AND hds.station_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getSgTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,sg_create_time,sg_end_time
        FROM
        hygf_work_order_power_station
        <where>
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getBwTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,bw_create_time,bw_end_time
        FROM
        hygf_basic_grid_record
        <where>
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getYsTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,ys_create_time,ys_end_time
        FROM
        hygf_acceptance_check
        <where>
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getTrTime"
            resultType="com.yeejoin.amos.boot.module.hygf.api.dto.PowerStationTimeStatisticsDto">
        SELECT peasant_household_id as sequenceNbr,tr_create_time,tr_end_time
        FROM
        hygf_financing_info
        <where>
            <if test="list != null and list.size()>0">
                AND peasant_household_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <!--    <select id="getHygfCommercialScale" resultType="String">-->
<!--        select scale from hygf_commercial where survey_information_id=${surveyInformationId}-->
<!--    </select>-->
</mapper>
