<?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.PreparationMoneyMapper">
<select id="getPeasantHouseholdData" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PeasantHousehold">
SELECT hygf_peasant_household.* FROM  hygf_peasant_household
LEFT JOIN  hygf_document_station on hygf_document_station.station_id=hygf_peasant_household.sequence_nbr
 where hygf_document_station.preparation_money_id=#{sequenceNbr}

     <if test="ownersName!=null and ownersName!=''">
         and hygf_peasant_household.owners_name like concat(concat('%',#{ownersName}),'%')
     </if>

</select>

    <select id="selectPageList" resultType="com.yeejoin.amos.boot.module.hygf.api.entity.PreparationMoney">
        SELECT hygf_preparation_money.*,IFNULL(order_status,arrival_state) as orderStatus,IFNULL(order_status,delivery_state) as deliveryState,(select instance_id from hygf_preparation_money_auditing where preparation_money_id = hygf_preparation_money.sequence_nbr limit 1  ) as instanceId FROM hygf_preparation_money
        <where>
            <if test="dto.consigneeRegion != null and dto.consigneeRegion != ''">
                and hygf_preparation_money.consignee_region like concat ('%',#{dto.consigneeRegion},'%')
            </if>
            <if test="dto.amosUnitName != null and dto.amosUnitName != ''">
                and hygf_preparation_money.amos_unit_name like concat ('%',#{dto.amosUnitName},'%')
            </if>
            <if test="dto.oddNumbers != null and dto.oddNumbers != ''">
                and hygf_preparation_money.odd_numbers like concat ('%',#{dto.oddNumbers},'%')
            </if>
            <if test="dto.orderStatus != null and dto.orderStatus != ''">
                and hygf_preparation_money.order_status = #{dto.orderStatus}
            </if>
            <if test="dto.amosUnitId != null and dto.amosUnitId != ''">
                and hygf_preparation_money.amos_unit_id = #{dto.amosUnitId}
            </if>
            <if test="dto.flag != null and dto.flag != ''">
                and hygf_preparation_money.delivery_state != '已作废' and hygf_preparation_money.delivery_state != '待发货'
            </if>
            <if test="dto.deliveryTimeStart != null and dto.deliveryTimeEnd != ''">
                and hygf_preparation_money.delivery_time between #{dto.deliveryTimeStart} and #{dto.deliveryTimeEnd}
            </if>
        </where>
        ORDER BY hygf_preparation_money.sequence_nbr DESC
    </select>

    <select id="preparationStation" resultType="java.util.Map">
       select b.* from (   select DISTINCT
        hygf_peasant_household.peasant_household_no as peasantHouseholdNo,
        hygf_peasant_household.owners_name as ownersName,
        hygf_peasant_household.regional_companies_name as regionalCompaniesName,
        hygf_peasant_household.regional_companies_seq as regionalCompaniesSeq,
        hygf_peasant_household.regional_companies_code ,
        hygf_peasant_household.developer_code,
        hygf_peasant_household.developer_name as developerName,
        hygf_peasant_household.developer_id as developeId,
        hygf_peasant_household.sequence_nbr  as sequenceNbr,
        substring_index ( hygf_peasant_household.project_address_name, '/', 1 ) AS province,
        concat_ws('/',hygf_peasant_household.project_address_name,hygf_peasant_household.project_address_detail) as address,
        hygf_peasant_household.preparation_money_state preparationMoneyState,
        hygf_document_station.preparation_money_id as preparationMoneyId,
        hygf_preparation_money.delivery_time as deliveryTime,
        hygf_preparation_money.arrival_time as arrivalTime,
        (select instance_id from hygf_preparation_money_auditing where hygf_document_station.preparation_money_id = hygf_preparation_money_auditing.preparation_money_id limit 1) as instanceId        from
        hygf_peasant_household
        LEFT JOIN hygf_document_station on  hygf_peasant_household.sequence_nbr =   hygf_document_station.station_id
        LEFT JOIN hygf_power_station on hygf_peasant_household.sequence_nbr = hygf_power_station.peasant_household_id
        LEFT JOIN hygf_preparation_money on hygf_preparation_money.sequence_nbr = hygf_document_station.preparation_money_id
        <where>
        <if test=" preparationMoneyState == 0 ">
            and hygf_peasant_household.preparation_money_state in ('待发货','待补货','暂存发货')
        </if>
        <if test="regionalCompaniesSeq!=null and regionalCompaniesSeq !=''  ">
            and hygf_peasant_household.regional_companies_code  like concat('%',#{regionalCompaniesSeq},'%')
        </if>
        <if test="developerId!=null and developerId !=''">
            and hygf_peasant_household.developer_code like concat('%',#{developerId},'%')
        </if>
        <if test="state!=null and state !=''">
            and hygf_peasant_household.preparation_money_state  = #{state}
        </if>
        <if test="ownersName!=null and ownersName !=''">
            and hygf_peasant_household.owners_name like concat ('%',#{ownersName},'%')
        </if>
        <if test=" preparationMoneyState == 1 ">
            and hygf_peasant_household.preparation_money_state not in  ('待发货','待补货','暂存发货') and preparation_money_state is not null and preparation_money_state != ''
        </if>
        <if test="province != null and province != ''">
            and hygf_peasant_household.project_address like concat(concat('%',#{province}),'%')
        </if>
        </where>
<!--        GROUP BY  hygf_peasant_household.sequence_nbr-->
        <if test=" preparationMoneyState == 1 ">
            order by hygf_preparation_money.delivery_time  DESC
       </if>
        <if test=" preparationMoneyState == 0 ">
            order by hygf_peasant_household.sequence_nbr DESC
        </if>
        ) b

</select>
</mapper>
