<?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.tzs.flc.api.mapper.EquipmentInformMapper">

    <select id="queryDtoList" resultType="java.util.Map">
        SELECT
        a.sequence_nbr AS sequenceNbr,
        a.product_code AS productCode,
        a.product_unit AS productUnit,
        CONCAT(a.province,a.city,a.district) AS productArea,
        CONCAT(a.stree,a.community,a.address) AS address,
        a.plan_product_date AS planProductDate,
        a.product_inform_date AS productInformDate,
        a.accept_unit AS acceptUnit,
        CASE
        a.inform_status
        WHEN 0 THEN
        '暂存'
        WHEN 1 THEN
        '未接收'
        WHEN 2 THEN
        '已接收'
        WHEN 9 THEN
        '已驳回'
        ELSE
        ''
        END AS informStatus,
        IFNULL(t.equipmentNum, 0) as equipmentNum
        FROM
        tz_equipment_inform a
        LEFT JOIN (select count(e.sequence_nbr) as equipmentNum, e.inform_id from tcb_inform_equipment e GROUP BY e.inform_id ) t on t.inform_id = a.sequence_nbr
        WHERE a.is_delete = 0
        AND a.product_unit_id =  #{companyId}
        <if test="productCode != null and productCode != ''">
            AND a.product_code like CONCAT('%',#{productCode},'%')
        </if>
        <if test="productUnitId != null ">
            AND a.product_unit_id = #{productUnitId}
        </if>
        <if test="regionCode != null and regionCode != ''">
            AND a.region_code like CONCAT('%',#{regionCode},'%')
        </if>
        <if test="address != null and address != ''">
            AND ( a.stree like CONCAT('%',#{address},'%')
            or a.community like CONCAT('%',#{address},'%')
            or a.address like CONCAT('%',#{address},'%') )
        </if>
        <if test="productInformDateStart != null and productInformDateStart != ''">
            and #{productInformDateStart} <![CDATA[ <= ]]> a.product_inform_date
        </if>
        <if test="productInformDateEnd != null and productInformDateEnd != ''">
            and a.product_inform_date <![CDATA[ <= ]]> #{productInformDateEnd}
        </if>
        <if test="planProductDateStart != null and planProductDateStart != ''">
            and #{planProductDateStart} <![CDATA[ <= ]]> a.plan_product_date
        </if>
        <if test="planProductDateEnd != null and planProductDateEnd != ''">
            and a.plan_product_date <![CDATA[ <= ]]> #{planProductDateEnd}
        </if>
        <if test="acceptUnitId != null ">
            AND a.accept_unit_id = #{acceptUnitId}
        </if>
        <if test="informStatus != null and informStatus != ''">
            AND a.inform_status = #{informStatus}
        </if>
        <if test="sortParam != null and sortParam != '' and sortRule != null and sortRule != ''    ">
            ORDER BY  ${sortParam}  ${sortRule}
        </if>

    </select>


    <select id="queryDtoListSub" resultType="java.util.Map">
        SELECT
        a.sequence_nbr AS sequenceNbr,
        a.product_code AS productCode,
        a.product_unit AS productUnit,
        CONCAT(a.province,a.city,a.district) AS productArea,
        CONCAT(a.stree,a.community,a.address) AS address,
        a.plan_product_date AS planProductDate,
        a.product_inform_date AS productInformDate,
        a.accept_unit AS acceptUnit,
        CASE
        a.inform_status
        WHEN 0 THEN
        '暂存'
        WHEN 1 THEN
        '未接收'
        WHEN 2 THEN
        '已接收'
        WHEN 9 THEN
        '已驳回'
        ELSE
        ''
        END AS informStatus,
        IFNULL(t.equipmentNum, 0) as equipmentNum
        FROM
        tz_equipment_inform a
        LEFT JOIN (select count(e.sequence_nbr) as equipmentNum, e.inform_id from tcb_inform_equipment e GROUP BY e.inform_id ) t on t.inform_id = a.sequence_nbr
        WHERE a.is_delete = 0
        AND a.accept_unit_id =  #{companyId}
        AND a.inform_status != 0
        <if test="productCode != null and productCode != ''">
            AND a.product_code like CONCAT('%',#{productCode},'%')
        </if>
        <if test="productUnitId != null ">
            AND a.product_unit_id = #{productUnitId}
        </if>
        <if test="regionCode != null and regionCode != ''">
            AND a.region_code like CONCAT('%',#{regionCode},'%')
        </if>
        <if test="address != null and address != ''">
            AND ( a.stree like CONCAT('%',#{address},'%')
            or a.community like CONCAT('%',#{address},'%')
            or a.address like CONCAT('%',#{address},'%') )
        </if>
        <if test="productInformDateStart != null and productInformDateStart != ''">
            and #{productInformDateStart} <![CDATA[ <= ]]> a.product_inform_date
        </if>
        <if test="productInformDateEnd != null and productInformDateEnd != ''">
            and a.product_inform_date <![CDATA[ <= ]]> #{productInformDateEnd}
        </if>
        <if test="planProductDateStart != null and planProductDateStart != ''">
            and #{planProductDateStart} <![CDATA[ <= ]]> a.plan_product_date
        </if>
        <if test="planProductDateEnd != null and planProductDateEnd != ''">
            and a.plan_product_date <![CDATA[ <= ]]> #{planProductDateEnd}
        </if>
        <if test="acceptUnitId != null ">
            AND a.accept_unit_id = #{acceptUnitId}
        </if>
        <if test="informStatus != null and informStatus != ''">
            AND a.inform_status = #{informStatus}
        </if>
        <if test="sortParam != null and sortParam != '' and sortRule != null and sortRule != ''    ">
            ORDER BY  ${sortParam}  ${sortRule}
        </if>

    </select>

</mapper>
