<?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.casic.missiles.mapper.equipment.EquipmentWorkbenchMapper">
    <!-- 上次检定日期+检定周期在未来一年时间内,按照月份分组 (上次检定日期+检定周期=该设备下次检定的时间) -->
    <select id="measureTrend" resultType="com.casic.missiles.dto.equipment.EquipmentWorkbenchResponse">
        SELECT
            DATE_FORMAT( DATE_ADD( mesure_date, INTERVAL mesure_cycle MONTH ), '%Y-%m' ) AS `date`,
            count( 0 ) AS `count`
        FROM
            equipment_fixed_assets
        WHERE
            is_del = 0
	    AND manager_state != 4
	    AND mesure_date IS NOT NULL
        AND DATE_FORMAT( DATE_ADD( mesure_date, INTERVAL mesure_cycle MONTH ), '%Y-%m' ) &gt;= #{startTime}
          AND DATE_FORMAT( DATE_ADD( mesure_date, INTERVAL mesure_cycle MONTH ), '%Y-%m' ) &lt;= #{endTime}
        GROUP BY
            DATE_FORMAT(
            DATE_ADD( mesure_date, INTERVAL mesure_cycle MONTH ),
            '%Y-%m')
    </select>

    <select id="standardTrend" resultType="com.casic.missiles.dto.equipment.EquipmentWorkbenchResponse">
        SELECT
            DATE_FORMAT( remind_time, '%Y-%m' ) AS `date`,
            count( 0 ) AS `count`
        FROM
            equipment_standard_equipment
        WHERE
            is_del = 0
        AND DATE_FORMAT( remind_time, '%Y-%m' ) &gt;= #{startTime}
        AND DATE_FORMAT( remind_time, '%Y-%m' ) &lt;= #{endTime}
	    AND remind_time IS NOT NULL
        GROUP BY
            DATE_FORMAT( remind_time, '%Y-%m' )
    </select>

    <select id="selectMeasureStateList" resultType="hashmap">
        SELECT
            manager_state AS state,
            COUNT( 0 ) AS count
        FROM
            equipment_standard_equipment
        GROUP BY
            manager_state
    </select>
    <!-- 查询管理状态不是报废，上次检定日期距离和当前时间相差的月份>=检定周期-1(提前30天)-->
    <select id="remindListPage" resultType="com.casic.missiles.model.equipment.EquipmentFixedAssets">
        SELECT
            efa.*, su.NAME AS usePersonName
        FROM
            equipment_fixed_assets efa
        LEFT JOIN sys_user su ON efa.use_person = su.ID
        WHERE
            efa.is_del = 0
	    AND efa.manager_state != 4
	    AND IFNULL( TIMESTAMPDIFF( MONTH, efa.mesure_date, curdate()), 0 ) >= efa.mesure_cycle - 1
    </select>

    <select id="queryVerifiedNum" resultType="int">
        SELECT
            count( 0 ) AS `count`
        FROM
            equipment_apply_equipment eae
                LEFT JOIN equipment_fixed_assets efa ON eae.equipment_id = efa.id
        WHERE
          eae.equipment_id IS NOT NULL
          AND efa.use_person = #{uid}
        <if test="businessKeys != null and businessKeys.size() > 0">
            and eae.apply_id in
            <foreach collection='businessKeys' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
    </select>

    <select id="queryOvertimeNum" resultType="int">
        SELECT
        count( 0 ) AS `count`
        FROM
        equipment_apply ea
        LEFT JOIN equipment_apply_equipment eae on ea.id = eae.apply_id
        LEFT JOIN equipment_fixed_assets efa ON eae.equipment_id = efa.id
        WHERE
        eae.equipment_id IS NOT NULL
        AND ea.time > DATE_ADD( mesure_date, INTERVAL mesure_cycle MONTH )
        AND efa.use_person = #{uid}
        <if test="businessKeys != null and businessKeys.size() > 0">
            and eae.apply_id in
            <foreach collection='businessKeys' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
    </select>

</mapper>
