<?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.bonus.material.expectations.mapper.ExpectationsMapper">

    <select id="getExpectationsList" resultType="com.bonus.material.expectations.domain.ExpectationsEntity">
        SELECT
        CASE
        WHEN mm.ma_status = '2' THEN bu.unit_name
        ELSE NULL
        END AS unit,
        CASE
        WHEN mm.ma_status = '2' THEN bp.pro_name
        ELSE NULL
        END AS project,
        mm.type_id AS typeId,
        sai.agreement_id AS agreementId,
        mt2.type_name AS typeName,
        mt1.type_name AS specCode,
        COUNT(*) AS count,
        GROUP_CONCAT(mm.ma_id ORDER BY mm.ma_id SEPARATOR ',') AS maIds,
        CASE
        WHEN mm.next_check_time &lt; CURDATE() THEN 'expired'
        ELSE 'about_to_expire'
        END AS status
        FROM ma_machine mm
        LEFT JOIN ma_type mt1 ON mm.type_id = mt1.type_id
        LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
        LEFT JOIN slt_agreement_info sai ON sai.ma_id = mm.ma_id
        LEFT JOIN bm_agreement_info bai ON bai.agreement_id = sai.agreement_id
        LEFT JOIN bm_unit bu ON bai.unit_id = bu.unit_id
        LEFT JOIN bm_project bp ON bp.pro_id = bai.project_id
        WHERE
        mm.ma_status = #{useStatus}
        AND (
        mm.next_check_time &lt; CURDATE()
        OR mm.next_check_time &lt; DATE_ADD(CURDATE(), INTERVAL 30 DAY)
        )
        <if test="keyWord != null and keyWord != ''">
            AND (
            bu.unit_name LIKE CONCAT('%', #{keyWord}, '%') OR
            bp.pro_name LIKE CONCAT('%', #{keyWord}, '%') OR
            mt2.type_name LIKE CONCAT('%', #{keyWord}, '%') OR
            mt1.type_name LIKE CONCAT('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY
        unit,
        project,
        mt2.type_name,
        mt1.type_name,
        status
        ORDER BY
        status,
        unit,
        project
    </select>
    <select id="getCountExpectations" resultType="java.util.Map">
        SELECT
        CASE
        WHEN next_check_time &lt; CURDATE() THEN 'expired'
        WHEN next_check_time &lt; DATE_ADD(CURDATE(), INTERVAL 30 DAY) THEN 'aboutToExpire'
        END AS name,
        COUNT(*) AS count
        FROM ma_machine
        WHERE ma_status = #{useStatus}
        AND next_check_time &lt; DATE_ADD(CURDATE(), INTERVAL 30 DAY)
        GROUP BY name;
    </select>
    <select id="getListDetails" resultType="com.bonus.material.expectations.domain.ExpectationsEntity">
        SELECT
        mt2.type_name      AS typeName,
        mt1.type_name      AS specCode,
        mm.ma_code         AS maCode,
        mm.next_check_time AS nextCheckTime
        FROM
        ma_machine mm
        LEFT JOIN ma_type mt1 ON mm.type_id = mt1.type_id
        LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
        LEFT JOIN slt_agreement_info sai ON sai.ma_id = mm.ma_id
        WHERE
        <choose>
            <when test="useStatus == 2">
                sai.agreement_id = #{agreementId}
                AND sai.type_id = #{typeId}
            </when>
            <when test="useStatus == 1">
                mm.type_id = #{typeId}
            </when>
        </choose>
    </select>
</mapper>
