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

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

        select
        *,
        (SELECT CASE WHEN SUM( CASE WHEN hhc.`status` = '已作废' THEN 1 ELSE 0 END ) = COUNT(*) THEN 'true' ELSE 'false'
        END FROM hygf_household_contract AS hhc
        WHERE
        hhc.peasant_household_id = hygf_household_contract.peasant_household_id
        ) AS isAllDisuse,
        SUBSTRING_INDEX(php.project_address_name, '/', 1) AS province
        from hygf_household_contract
        LEFT JOIN hygf_peasant_household php ON php.sequence_nbr = hygf_household_contract.peasant_household_id
        <where>
            <if test="dto.name != null and dto.name !='' ">
                and hygf_household_contract.name like concat('%',#{dto.name},'%')
            </if>
            <if test="dto.contractNumber != null  and dto.contractNumber !=''">
                and hygf_household_contract.contract_number like concat('%',#{dto.contractNumber},'%')
            </if>
            <if test="dto.initiateStatus != null and dto.initiateStatus !='' ">
                and hygf_household_contract.initiate_status = #{dto.initiateStatus}
            </if>
            <if test="dto.peasantHouseholdId != null and dto.peasantHouseholdId !=''">
                and hygf_household_contract.peasant_household_id = #{dto.peasantHouseholdId}
            </if>
            <if test="dto.dealerId != null and dto.dealerId !=''">
                and hygf_household_contract.dealer_id = #{dto.dealerId}
            </if>
            <if test="dto.surveyStatus != null and dto.surveyStatus !=''">
                and hygf_household_contract.survey_status = #{dto.surveyStatus}
            </if>
            <if test="dto.signStatus != null and dto.signStatus !=''">
                and hygf_household_contract.sign_status = #{dto.signStatus}
            </if>
            <if test="dto.userId != null and dto.userId !=''">
                and hygf_household_contract.project_user_id = #{dto.userId}
            </if>
            <if test="dto.partyA != null and dto.partyA !=''">
                and hygf_household_contract.party_a like concat('%',#{dto.partyA},'%')
            </if>
            <if test="dto.regionalCompaniesName != null and dto.regionalCompaniesName !=''">
                and hygf_household_contract.regional_companies_name like concat('%',#{dto.regionalCompaniesName},'%')
            </if>
            <if test="dto.province != null and dto.province !=''">
                and php.project_address like concat ('%',#{dto.province},'%')
            </if>
        </where>
        <if test="dto.orderBy  == null ">
            ORDER BY hygf_household_contract.rec_date DESC
        </if>
        <if test="dto.orderBy != null ">
            ORDER BY hygf_household_contract.rec_date DESC, hygf_household_contract.${orderBy} ${dto.isAsc}
        </if>
    </select>



    <select id="getHygfCommercialScale" resultType="String">
        select scale from hygf_commercial where survey_information_id =  (select survey_information_id from hygf_peasant_household where peasant_household_no = #{peasantHouseholdNumber} )
    </select>

    <select id="getHygfCommercialScaleList" resultType="java.util.Map">
        select hpg.peasant_household_no, hc.scale from hygf_commercial  hc
        left JOIN hygf_peasant_household hpg ON hpg.survey_information_id=hc.survey_information_id
        where hpg.peasant_household_no in
        <foreach item="peasantHouseholdNumber" collection="list" open="(" separator="," close=")">
        #{peasantHouseholdNumber}
        </foreach>
    </select>

    <select id="getDzhtqyEndTime" resultType="java.util.Map">
        SELECT sequence_nbr as sequenceNbr ,rec_date as time
        FROM hygf_household_contract
        WHERE status='双方已签署'
    </select>
</mapper>
