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

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        mfa.*
    </sql>
    <select id="selectMaxNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(asset_no, 12)), 0) from equipment_fixed_assets
    </select>

    <select id="listPage" resultType="com.casic.missiles.model.equipment.EquipmentFixedAssets">
        SELECT <include refid="Base_Column_List"/>,
        su.name as usePersonName, sd.full_name as useDeptName
        FROM equipment_fixed_assets mfa
        LEFT JOIN sys_user su ON su.id = mfa.use_person
        LEFT JOIN sys_dept sd on sd.id = mfa.use_dept
<!--        LEFT JOIN sys_dept sd1 on sd1.id = mfa.mesure_dept-->
        WHERE mfa.is_del = 0
        <if test="fixedAssetRequest.id != null">
            and mfa.id = #{fixedAssetRequest.id}
        </if>
        <if test="fixedAssetRequest.assetNo != null and fixedAssetRequest.assetNo !=''">
            and mfa.asset_no like concat('%', #{fixedAssetRequest.assetNo}, '%')
        </if>
        <if test="fixedAssetRequest.assetType != null">
            and mfa.asset_type = #{fixedAssetRequest.assetType}
        </if>
        <if test="fixedAssetRequest.equipmentNo != null and fixedAssetRequest.equipmentNo !=''">
            and mfa.equipment_no like concat('%', #{fixedAssetRequest.equipmentNo}, '%')
        </if>
        <if test="fixedAssetRequest.equipmentName != null and fixedAssetRequest.equipmentName !=''">
            and mfa.equipment_name like concat('%', #{fixedAssetRequest.equipmentName}, '%')
        </if>
        <if test="fixedAssetRequest.mesureType != null">
            and mfa.mesure_type = #{fixedAssetRequest.mesureType}
        </if>
        <if test="fixedAssetRequest.managerState != null">
            and mfa.manager_state = #{fixedAssetRequest.managerState}
        </if>
        <if test="fixedAssetRequest.useDept != null">
            and mfa.use_dept = #{fixedAssetRequest.useDept}
        </if>
<!--        <if test="fixedAssetRequest.validDate != null">-->
<!--            and DATE_FORMAT(valid_date,'%Y-%m-%d') = #{fixedAssetRequest.validDate}-->
<!--        </if>-->
        <if test="fixedAssetRequest.validDateStart != null and fixedAssetRequest.validDateStart != ''">
            and mfa.valid_date <![CDATA[ >= ]]> #{fixedAssetRequest.validDateStart}
        </if>
        <if test="fixedAssetRequest.validDateEnd != null and fixedAssetRequest.validDateEnd != ''">
            and mfa.valid_date <![CDATA[ <= ]]> #{fixedAssetRequest.validDateEnd}
        </if>
        <if test="fixedAssetRequest.ABC != null and fixedAssetRequest.ABC != ''">
            and mfa.ABC = #{fixedAssetRequest.abc}
        </if>
        <if test="fixedAssetRequest.modelNo != null and fixedAssetRequest.modelNo !=''">
            and mfa.model_no like concat('%', #{fixedAssetRequest.modelNo}, '%')
        </if>
        <if test="fixedAssetRequest.equipmentSpecifications != null and fixedAssetRequest.equipmentSpecifications !=''">
            and mfa.equipment_specifications like concat('%', #{fixedAssetRequest.equipmentSpecifications}, '%')
        </if>
        <if test="fixedAssetRequest.equipmentCategory != null and fixedAssetRequest.equipmentCategory !=''">
            and mfa.equipment_category = #{fixedAssetRequest.equipmentCategory}
        </if>
        <if test="fixedAssetRequest.manufacturingNo != null and fixedAssetRequest.manufacturingNo !=''">
            and mfa.manufacturing_no like concat('%', #{fixedAssetRequest.manufacturingNo}, '%')
        </if>
        <if test="fixedAssetRequest.isCalibrationTestEquipment != null">
            and mfa.is_calibration_test_equipment = #{fixedAssetRequest.isCalibrationTestEquipment}
        </if>
        <if test="fixedAssetRequest.isMeasureAccount != null">
            and mfa.is_measure_account = #{fixedAssetRequest.isMeasureAccount}
        </if>
        <if test="fixedAssetRequest.isStandardSupportEquipment != null">
            and mfa.is_standard_support_equipment = #{fixedAssetRequest.isStandardSupportEquipment}
        </if>
        <if test="fixedAssetRequest.isFixedAssets != null">
            and mfa.is_fixed_assets = #{fixedAssetRequest.isFixedAssets}
        </if>
        <if test="fixedAssetRequest.labelId != null and fixedAssetRequest.labelId != ''">
            and mfa.label_bind = #{fixedAssetRequest.labelId}
        </if>
        <if test="fixedAssetRequest.ids != null and fixedAssetRequest.ids.size() > 0 ">
            and mfa.id in
            <foreach collection='fixedAssetRequest.ids' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
        ORDER BY mfa.create_time DESC
    </select>
    <select id="queryEquipmentInfo" resultType="com.casic.missiles.model.equipment.EquipmentStandardSupportEquipment">
        SELECT <include refid="Base_Column_List"/>,
        su.name as usePersonName, sd.full_name as useDeptName
        FROM equipment_fixed_assets mfa
        LEFT JOIN sys_user su ON su.id = mfa.use_person
        LEFT JOIN sys_dept sd on sd.id = mfa.use_dept
        WHERE mfa.is_del = 0
        <if test="oldEquipmentIds != null and oldEquipmentIds.size() > 0 ">
            and mfa.id in
            <foreach collection='oldEquipmentIds' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
        ORDER BY mfa.create_time DESC
    </select>

    <select id="queryEquipment" resultType="com.casic.missiles.dto.equipment.EquipmentOrSampleDto">
        SELECT
            *
        FROM
            (
                SELECT
                    id,
                    equipment_no AS asset_no,
                    equipment_name AS asset_name,
                    1 AS asset_type
                FROM
                    equipment_fixed_assets
                WHERE
                    asset_type = '1'
                  AND is_del = 0 UNION
                SELECT
                    id,
                    sample_no AS asset_no,
                    sample_name AS asset_name,
                    2 AS asset_type
                FROM
                    customer_sample_info
                WHERE
                    is_del = 0
            ) t1
        WHERE 1=1
        <if test="request.assetNo != null and request.assetNo !=''">
            and t1.asset_no like concat('%', #{request.assetNo}, '%')
        </if>
        <if test="request.assetName != null and request.assetName !=''">
            and t1.asset_name like concat('%', #{request.assetName}, '%')
        </if>
    </select>
    <select id="remindListPage" resultType="com.casic.missiles.model.equipment.EquipmentFixedAssets">
        SELECT
            <include refid="Base_Column_List"/>,
            su.NAME AS usePersonName,
            sd.full_name AS useDeptName
        FROM
            equipment_fixed_assets mfa
        LEFT JOIN sys_user su ON su.id = mfa.use_person
        LEFT JOIN sys_dept sd ON sd.id = mfa.use_dept
<!--        LEFT JOIN sys_dept sd1 ON sd1.id = mfa.mesure_dept-->
        WHERE
            mfa.is_del = 0
        AND manager_state != 4
        AND IFNULL( TIMESTAMPDIFF( MONTH, mesure_date, curdate()), 0 ) >= mesure_cycle - 1
        <if test="fixedAssetRequest.equipmentNo != null and fixedAssetRequest.equipmentNo !=''">
            and mfa.equipment_no like concat('%', #{fixedAssetRequest.equipmentNo}, '%')
        </if>
        <if test="fixedAssetRequest.equipmentName != null and fixedAssetRequest.equipmentName !=''">
            and mfa.equipment_name like concat('%', #{fixedAssetRequest.equipmentName}, '%')
        </if>
        <if test="fixedAssetRequest.mesureType != null and fixedAssetRequest.mesureType != ''">
            and mfa.mesure_type = #{fixedAssetRequest.mesureType}
        </if>
        <if test="fixedAssetRequest.useDept != null and fixedAssetRequest.useDept != ''">
            and mfa.use_dept = #{fixedAssetRequest.useDept}
        </if>
        <if test="fixedAssetRequest.usePerson != null and fixedAssetRequest.usePerson != ''">
            and mfa.use_person = #{fixedAssetRequest.usePerson}
        </if>
        ORDER BY mfa.create_time DESC
    </select>
    <select id="remindList" resultType="com.casic.missiles.model.equipment.EquipmentRemindExport">
        SELECT
        <include refid="Base_Column_List"/>,
        su.NAME AS usePersonName,
        sd.full_name AS useDeptName
        FROM
        equipment_fixed_assets mfa
        LEFT JOIN sys_user su ON su.id = mfa.use_person
        LEFT JOIN sys_dept sd ON sd.id = mfa.use_dept
<!--        LEFT JOIN sys_dept sd1 ON sd1.id = mfa.mesure_dept-->
        WHERE
        mfa.is_del = 0
        AND manager_state != 4
        AND IFNULL( TIMESTAMPDIFF( MONTH, mesure_date, curdate()), 0 ) >= mesure_cycle - 1
        <if test="fixedAssetRequest.equipmentNo != null and fixedAssetRequest.equipmentNo !=''">
            and mfa.equipment_no like concat('%', #{fixedAssetRequest.equipmentNo}, '%')
        </if>
        <if test="fixedAssetRequest.equipmentName != null and fixedAssetRequest.equipmentName !=''">
            and mfa.equipment_name like concat('%', #{fixedAssetRequest.equipmentName}, '%')
        </if>
        <if test="fixedAssetRequest.mesureType != null and fixedAssetRequest.mesureType != ''">
            and mfa.mesure_type = #{fixedAssetRequest.mesureType}
        </if>
        <if test="fixedAssetRequest.useDept != null and fixedAssetRequest.useDept != ''">
            and mfa.use_dept = #{fixedAssetRequest.useDept}
        </if>
        <if test="fixedAssetRequest.usePerson != null and fixedAssetRequest.usePerson != ''">
            and mfa.use_person = #{fixedAssetRequest.usePerson}
        </if>
        <if test="fixedAssetRequest.ids != null and fixedAssetRequest.ids.size() > 0 ">
            and mfa.id in
            <foreach collection='fixedAssetRequest.ids' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
        ORDER BY mfa.create_time DESC
    </select>

    <select id="selectRemindQuantity" resultType="java.lang.Integer">
        SELECT IFNULL(COUNT(mfa.id),0)
        FROM equipment_fixed_assets mfa
        LEFT JOIN sys_user su ON su.id = mfa.use_person
        LEFT JOIN sys_dept sd ON sd.id = mfa.use_dept
<!--        LEFT JOIN sys_dept sd1 ON sd1.id = mfa.mesure_dept-->
        WHERE mfa.is_del = 0
        AND manager_state != 4
        AND IFNULL( TIMESTAMPDIFF( MONTH, mesure_date, curdate()), 0 ) >= mesure_cycle - 1
    </select>

    <select id="selectExpireDeviceStatistics" resultType="com.casic.missiles.dto.cockpit.ExpireDeviceStatisticsResponse">
        SELECT DATE_FORMAT(valid_date,'%Y-%m') AS month, COUNT(id) AS expiredQuantity
        FROM equipment_fixed_assets
        WHERE is_del = 0
        AND DATE_FORMAT(valid_date,'%Y') = DATE_FORMAT(CURDATE(),'%Y')
        GROUP BY DATE_FORMAT(valid_date,'%Y-%m')
    </select>
</mapper>
